feature8030 move WIM connector to plugins
[osm/RO.git] / RO / osm_ro / wim / errors.py
1 # -*- coding: utf-8 -*-
2 ##
3 # Copyright 2018 University of Bristol - High Performance Networks Research
4 # Group
5 # All Rights Reserved.
6 #
7 # Contributors: Anderson Bravalheri, Dimitrios Gkounis, Abubakar Siddique
8 # Muqaddas, Navdeep Uniyal, Reza Nejabati and Dimitra Simeonidou
9 #
10 # Licensed under the Apache License, Version 2.0 (the "License"); you may
11 # not use this file except in compliance with the License. You may obtain
12 # a copy of the License at
13 #
14 # http://www.apache.org/licenses/LICENSE-2.0
15 #
16 # Unless required by applicable law or agreed to in writing, software
17 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
19 # License for the specific language governing permissions and limitations
20 # under the License.
21 #
22 # For those usages not covered by the Apache License, Version 2.0 please
23 # contact with: <highperformance-networks@bristol.ac.uk>
24 #
25 # Neither the name of the University of Bristol nor the names of its
26 # contributors may be used to endorse or promote products derived from
27 # this software without specific prior written permission.
28 #
29 # This work has been performed in the context of DCMS UK 5G Testbeds
30 # & Trials Programme and in the framework of the Metro-Haul project -
31 # funded by the European Commission under Grant number 761727 through the
32 # Horizon 2020 and 5G-PPP programmes.
33 ##
34 import queue
35
36 from ..db_base import db_base_Exception as DbBaseException
37 from ..http_tools.errors import (
38 Bad_Request,
39 Conflict,
40 HttpMappedError,
41 Internal_Server_Error,
42 Not_Found
43 )
44
45
46 class NoRecordFound(DbBaseException):
47 """No record was found in the database"""
48
49 def __init__(self, criteria, table=None):
50 table_info = '{} - '.format(table) if table else ''
51 super(NoRecordFound, self).__init__(
52 '{}: {}`{}`'.format(self.__class__.__doc__, table_info, criteria),
53 http_code=Not_Found)
54
55
56 class MultipleRecordsFound(DbBaseException):
57 """More than one record was found in the database"""
58
59 def __init__(self, criteria, table=None):
60 table_info = '{} - '.format(table) if table else ''
61 super(MultipleRecordsFound, self).__init__(
62 '{}: {}`{}`'.format(self.__class__.__doc__, table_info, criteria),
63 http_code=Conflict)
64
65
66 class WimAndTenantNotAttached(DbBaseException):
67 """Wim and Tenant are not attached"""
68
69 def __init__(self, wim, tenant):
70 super(WimAndTenantNotAttached, self).__init__(
71 '{}: `{}` <> `{}`'.format(self.__class__.__doc__, wim, tenant),
72 http_code=Conflict)
73
74
75 class WimAndTenantAlreadyAttached(DbBaseException):
76 """There is already a wim account attaching the given wim and tenant"""
77
78 def __init__(self, wim, tenant):
79 super(WimAndTenantAlreadyAttached, self).__init__(
80 '{}: `{}` <> `{}`'.format(self.__class__.__doc__, wim, tenant),
81 http_code=Conflict)
82
83
84 class NoWimConnectedToDatacenters(NoRecordFound):
85 """No WIM that is able to connect the given datacenters was found"""
86
87
88 class InvalidParameters(DbBaseException):
89 """The given parameters are invalid"""
90
91 def __init__(self, message, http_code=Bad_Request):
92 super(InvalidParameters, self).__init__(message, http_code)
93
94
95 class UndefinedAction(HttpMappedError):
96 """No action found"""
97
98 def __init__(self, item_type, action, http_code=Internal_Server_Error):
99 message = ('The action {} {} is not defined'.format(action, item_type))
100 super(UndefinedAction, self).__init__(message, http_code)
101
102
103 class UndefinedWimConnector(DbBaseException):
104 """The connector class for the specified wim type is not implemented"""
105
106 def __init__(self, wim_type, module_name):
107 super(UndefinedWimConnector, self).__init__("Cannot load a module for {t} type '{n}'. The plugin 'osm_{n}' has"
108 " not been registered".format(t=wim_type, n=module_name),
109 http_code=Bad_Request)
110
111
112 class WimAccountOverwrite(DbBaseException):
113 """An attempt to overwrite an existing WIM account was identified"""
114
115 def __init__(self, wim_account, diff=None, tip=None):
116 message = self.__class__.__doc__
117 account_info = (
118 'Account -- name: {name}, uuid: {uuid}'.format(**wim_account)
119 if wim_account else '')
120 diff_info = (
121 'Differing fields: ' + ', '.join(diff.keys()) if diff else '')
122
123 super(WimAccountOverwrite, self).__init__(
124 '\n'.join(m for m in (message, account_info, diff_info, tip) if m),
125 http_code=Conflict)
126
127
128 class UnexpectedDatabaseError(DbBaseException):
129 """The database didn't raised an exception but also the query was not
130 executed (maybe the connection had some problems?)
131 """
132
133
134 class UndefinedUuidOrName(DbBaseException):
135 """Trying to query for a record using an empty uuid or name"""
136
137 def __init__(self, table=None):
138 table_info = '{} - '.format(table.split()[0]) if table else ''
139 super(UndefinedUuidOrName, self).__init__(
140 table_info + self.__class__.__doc__, http_status=Bad_Request)
141
142
143 class UndefinedWanMappingType(InvalidParameters):
144 """The dict wan_service_mapping_info MUST contain a `type` field"""
145
146 def __init__(self, given):
147 super(UndefinedWanMappingType, self).__init__(
148 '{}. Given: `{}`'.format(self.__class__.__doc__, given))
149
150
151 class QueueFull(HttpMappedError, queue.Full):
152 """Thread queue is full"""
153
154 def __init__(self, thread_name, http_code=Internal_Server_Error):
155 message = ('Thread {} queue is full'.format(thread_name))
156 super(QueueFull, self).__init__(message, http_code)
157
158
159 class InconsistentState(HttpMappedError):
160 """An unexpected inconsistency was find in the state of the program"""
161
162 def __init__(self, arg, http_code=Internal_Server_Error):
163 if isinstance(arg, HttpMappedError):
164 http_code = arg.http_code
165 message = str(arg)
166 else:
167 message = arg
168
169 super(InconsistentState, self).__init__(message, http_code)
170
171
172 class WimAccountNotActive(HttpMappedError, KeyError):
173 """WIM Account is not active yet (no thread is running)"""
174
175 def __init__(self, message, http_code=Internal_Server_Error):
176 message += ('\nThe thread responsible for processing the actions have '
177 'suddenly stopped, or have never being spawned')
178 super(WimAccountNotActive, self).__init__(message, http_code)
179
180
181 class NoExternalPortFound(HttpMappedError):
182 """No external port associated to the instance_net"""
183
184 def __init__(self, instance_net):
185 super(NoExternalPortFound, self).__init__(
186 '{} uuid({})'.format(self.__class__.__doc__, instance_net['uuid']),
187 http_code=Not_Found)