e8d4b63e92ac56fcb6e4f2fb2b0f73a26fe11e13
[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, location_reference):
107 super(UndefinedWimConnector, self).__init__(
108 ('{}: `{}`. Could not find module `{}` '
109 '(check if it is necessary to install a plugin)'
110 .format(self.__class__.__doc__, wim_type, module_name)),
111 http_code=Bad_Request)
112
113
114 class WimAccountOverwrite(DbBaseException):
115 """An attempt to overwrite an existing WIM account was identified"""
116
117 def __init__(self, wim_account, diff=None, tip=None):
118 message = self.__class__.__doc__
119 account_info = (
120 'Account -- name: {name}, uuid: {uuid}'.format(**wim_account)
121 if wim_account else '')
122 diff_info = (
123 'Differing fields: ' + ', '.join(diff.keys()) if diff else '')
124
125 super(WimAccountOverwrite, self).__init__(
126 '\n'.join(m for m in (message, account_info, diff_info, tip) if m),
127 http_code=Conflict)
128
129
130 class UnexpectedDatabaseError(DbBaseException):
131 """The database didn't raised an exception but also the query was not
132 executed (maybe the connection had some problems?)
133 """
134
135
136 class UndefinedUuidOrName(DbBaseException):
137 """Trying to query for a record using an empty uuid or name"""
138
139 def __init__(self, table=None):
140 table_info = '{} - '.format(table.split()[0]) if table else ''
141 super(UndefinedUuidOrName, self).__init__(
142 table_info + self.__class__.__doc__, http_status=Bad_Request)
143
144
145 class UndefinedWanMappingType(InvalidParameters):
146 """The dict wan_service_mapping_info MUST contain a `type` field"""
147
148 def __init__(self, given):
149 super(UndefinedWanMappingType, self).__init__(
150 '{}. Given: `{}`'.format(self.__class__.__doc__, given))
151
152
153 class QueueFull(HttpMappedError, queue.Full):
154 """Thread queue is full"""
155
156 def __init__(self, thread_name, http_code=Internal_Server_Error):
157 message = ('Thread {} queue is full'.format(thread_name))
158 super(QueueFull, self).__init__(message, http_code)
159
160
161 class InconsistentState(HttpMappedError):
162 """An unexpected inconsistency was find in the state of the program"""
163
164 def __init__(self, arg, http_code=Internal_Server_Error):
165 if isinstance(arg, HttpMappedError):
166 http_code = arg.http_code
167 message = str(arg)
168 else:
169 message = arg
170
171 super(InconsistentState, self).__init__(message, http_code)
172
173
174 class WimAccountNotActive(HttpMappedError, KeyError):
175 """WIM Account is not active yet (no thread is running)"""
176
177 def __init__(self, message, http_code=Internal_Server_Error):
178 message += ('\nThe thread responsible for processing the actions have '
179 'suddenly stopped, or have never being spawned')
180 super(WimAccountNotActive, self).__init__(message, http_code)
181
182
183 class NoExternalPortFound(HttpMappedError):
184 """No external port associated to the instance_net"""
185
186 def __init__(self, instance_net):
187 super(NoExternalPortFound, self).__init__(
188 '{} uuid({})'.format(self.__class__.__doc__, instance_net['uuid']),
189 http_code=Not_Found)