Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
from typing import Dict
import grpc, logging
from prometheus_client import Counter, Histogram
from common.database.api.Database import Database
from common.exceptions.ServiceException import ServiceException
from service.proto.context_pb2 import Empty
from service.proto.service_pb2 import ConnectionList, Service, ServiceId, ServiceList
from service.proto.service_pb2_grpc import ServiceServiceServicer
from service.service.Tools import check_service_id_request, check_service_request
LOGGER = logging.getLogger(__name__)
GETSERVICELIST_COUNTER_STARTED = Counter ('service_getservicelist_counter_started',
'Service:GetServiceList counter of requests started' )
GETSERVICELIST_COUNTER_COMPLETED = Counter ('service_getservicelist_counter_completed',
'Service:GetServiceList counter of requests completed')
GETSERVICELIST_COUNTER_FAILED = Counter ('service_getservicelist_counter_failed',
'Service:GetServiceList counter of requests failed' )
GETSERVICELIST_HISTOGRAM_DURATION = Histogram('service_getservicelist_histogram_duration',
'Service:GetServiceList histogram of request duration')
CREATESERVICE_COUNTER_STARTED = Counter ('service_createservice_counter_started',
'Service:CreateService counter of requests started' )
CREATESERVICE_COUNTER_COMPLETED = Counter ('service_createservice_counter_completed',
'Service:CreateService counter of requests completed')
CREATESERVICE_COUNTER_FAILED = Counter ('service_createservice_counter_failed',
'Service:CreateService counter of requests failed' )
CREATESERVICE_HISTOGRAM_DURATION = Histogram('service_createservice_histogram_duration',
'Service:CreateService histogram of request duration')
UPDATESERVICE_COUNTER_STARTED = Counter ('service_updateservice_counter_started',
'Service:UpdateService counter of requests started' )
UPDATESERVICE_COUNTER_COMPLETED = Counter ('service_updateservice_counter_completed',
'Service:UpdateService counter of requests completed')
UPDATESERVICE_COUNTER_FAILED = Counter ('service_updateservice_counter_failed',
'Service:UpdateService counter of requests failed' )
UPDATESERVICE_HISTOGRAM_DURATION = Histogram('service_updateservice_histogram_duration',
'Service:UpdateService histogram of request duration')
DELETESERVICE_COUNTER_STARTED = Counter ('service_deleteservice_counter_started',
'Service:DeleteService counter of requests started' )
DELETESERVICE_COUNTER_COMPLETED = Counter ('service_deleteservice_counter_completed',
'Service:DeleteService counter of requests completed')
DELETESERVICE_COUNTER_FAILED = Counter ('service_deleteservice_counter_failed',
'Service:DeleteService counter of requests failed' )
DELETESERVICE_HISTOGRAM_DURATION = Histogram('service_deleteservice_histogram_duration',
'Service:DeleteService histogram of request duration')
GETSERVICEBYID_COUNTER_STARTED = Counter ('service_getservicebyid_counter_started',
'Service:GetServiceById counter of requests started' )
GETSERVICEBYID_COUNTER_COMPLETED = Counter ('service_getservicebyid_counter_completed',
'Service:GetServiceById counter of requests completed')
GETSERVICEBYID_COUNTER_FAILED = Counter ('service_getservicebyid_counter_failed',
'Service:GetServiceById counter of requests failed' )
GETSERVICEBYID_HISTOGRAM_DURATION = Histogram('service_getservicebyid_histogram_duration',
'Service:GetServiceById histogram of request duration')
GETCONNECTIONLIST_COUNTER_STARTED = Counter ('service_getconnectionlist_counter_started',
'Service:GetConnectionList counter of requests started' )
GETCONNECTIONLIST_COUNTER_COMPLETED = Counter ('service_getconnectionlist_counter_completed',
'Service:GetConnectionList counter of requests completed')
GETCONNECTIONLIST_COUNTER_FAILED = Counter ('service_getconnectionlist_counter_failed',
'Service:GetConnectionList counter of requests failed' )
GETCONNECTIONLIST_HISTOGRAM_DURATION = Histogram('service_getconnectionlist_histogram_duration',
'Service:GetConnectionList histogram of request duration')
class ServiceServiceServicerImpl(ServiceServiceServicer):
def __init__(self, database : Database):
LOGGER.debug('Creating Servicer...')
self.database = database
LOGGER.debug('Servicer Created')
@GETSERVICELIST_HISTOGRAM_DURATION.time()
def GetServiceList(self, request : Empty, grpc_context : grpc.ServicerContext) -> ServiceList:
GETSERVICELIST_COUNTER_STARTED.inc()
try:
LOGGER.debug('GetServiceList request: {}'.format(str(request)))
# ----- Validate request data and pre-conditions -----------------------------------------------------------
# ----- Retrieve data from the database --------------------------------------------------------------------
db_context_uuids = self.database.contexts.get()
json_services = []
for db_context_uuid in db_context_uuids:
db_context = self.database.context(db_context_uuid)
json_services.extend(db_context.dump_services())
# ----- Compose reply --------------------------------------------------------------------------------------
reply = ServiceList(cs=json_services)
LOGGER.debug('GetServiceList reply: {}'.format(str(reply)))
GETSERVICELIST_COUNTER_COMPLETED.inc()
return reply
except ServiceException as e: # pragma: no cover (ServiceException not thrown)
LOGGER.exception('GetServiceList exception')
GETSERVICELIST_COUNTER_FAILED.inc()
grpc_context.abort(e.code, e.details)
except Exception as e: # pragma: no cover
LOGGER.exception('GetServiceList exception')
GETSERVICELIST_COUNTER_FAILED.inc()
grpc_context.abort(grpc.StatusCode.INTERNAL, str(e))
@CREATESERVICE_HISTOGRAM_DURATION.time()
def CreateService(self, request : Service, grpc_context : grpc.ServicerContext) -> ServiceId:
CREATESERVICE_COUNTER_STARTED.inc()
try:
LOGGER.debug('CreateService request: {}'.format(str(request)))
# ----- Validate request data and pre-conditions -----------------------------------------------------------
context_id, service_id, service_type, service_config, service_state, db_endpoints, constraint_tuples = \
check_service_request('CreateService', request, self.database, LOGGER)
# ----- Implement changes in the database ------------------------------------------------------------------
db_context = self.database.context(context_id)
db_service = db_context.service(service_id)
db_service.create(service_type, service_config, service_state)
for db_endpoint in db_endpoints:
service_endpoint_id = '{}:{}/{}'.format(
db_endpoint.topology_uuid, db_endpoint.device_uuid, db_endpoint.endpoint_uuid)
db_service.endpoint(service_endpoint_id).create(db_endpoint)
for cons_type,cons_value in constraint_tuples: db_service.constraint(cons_type).create(cons_value)
# ----- Compose reply --------------------------------------------------------------------------------------
reply = ServiceId(**db_service.dump_id())
LOGGER.debug('CreateService reply: {}'.format(str(reply)))
CREATESERVICE_COUNTER_COMPLETED.inc()
return reply
except ServiceException as e:
LOGGER.exception('CreateService exception')
CREATESERVICE_COUNTER_FAILED.inc()
grpc_context.abort(e.code, e.details)
except Exception as e: # pragma: no cover
LOGGER.exception('CreateService exception')
CREATESERVICE_COUNTER_FAILED.inc()
grpc_context.abort(grpc.StatusCode.INTERNAL, str(e))
@UPDATESERVICE_HISTOGRAM_DURATION.time()
def UpdateService(self, request : Service, grpc_context : grpc.ServicerContext) -> ServiceId:
UPDATESERVICE_COUNTER_STARTED.inc()
try:
LOGGER.debug('UpdateService request: {}'.format(str(request)))
# ----- Validate request data and pre-conditions -----------------------------------------------------------
context_id, service_id, service_type, service_config, service_state, db_endpoints, constraint_tuples = \
check_service_request('UpdateService', request, self.database, LOGGER)
# ----- Implement changes in the database ------------------------------------------------------------------
db_context = self.database.context(context_id)
db_service = db_context.service(service_id)
# Update service attributes
db_service.update(update_attributes={
'service_type' : service_type,
'service_config': service_config,
'service_state' : service_state,
})
# Update service constraints; first add missing, then remove existing, but not added to Service
db_service_constraint_types = set(db_service.constraints.get())
for constraint_type,constraint_value in constraint_tuples:
if constraint_type in db_service_constraint_types:
db_service.constraint(constraint_type).update(update_attributes={
'constraint_value': constraint_value
})
else:
db_service.constraint(constraint_type).create(constraint_value)
db_service_constraint_types.discard(constraint_type)
for constraint_type in db_service_constraint_types:
db_service.constraint(constraint_type).delete()
# Update service endpoints; first add missing, then remove existing, but not added to Service
db_service_endpoint_uuids = set(db_service.endpoints.get())
for db_endpoint in db_endpoints:
service_endpoint_id = '{}:{}/{}'.format(
db_endpoint.topology_uuid, db_endpoint.device_uuid, db_endpoint.endpoint_uuid)
if service_endpoint_id not in db_service_endpoint_uuids:
db_service.endpoint(service_endpoint_id).create(db_endpoint)
db_service_endpoint_uuids.discard(service_endpoint_id)
for db_service_endpoint_uuid in db_service_endpoint_uuids:
db_service.endpoint(db_service_endpoint_uuid).delete()
# ----- Compose reply --------------------------------------------------------------------------------------
reply = ServiceId(**db_service.dump_id())
LOGGER.debug('UpdateService reply: {}'.format(str(reply)))
UPDATESERVICE_COUNTER_COMPLETED.inc()
return reply
except ServiceException as e:
LOGGER.exception('UpdateService exception')
UPDATESERVICE_COUNTER_FAILED.inc()
grpc_context.abort(e.code, e.details)
except Exception as e: # pragma: no cover
LOGGER.exception('UpdateService exception')
UPDATESERVICE_COUNTER_FAILED.inc()
grpc_context.abort(grpc.StatusCode.INTERNAL, str(e))
@DELETESERVICE_HISTOGRAM_DURATION.time()
def DeleteService(self, request : ServiceId, grpc_context : grpc.ServicerContext) -> Empty:
DELETESERVICE_COUNTER_STARTED.inc()
try:
LOGGER.debug('DeleteService request: {}'.format(str(request)))
# ----- Validate request data and pre-conditions -----------------------------------------------------------
context_id, service_id = check_service_id_request('DeleteService', request, self.database, LOGGER)
# ----- Implement changes in the database ------------------------------------------------------------------
db_context = self.database.context(context_id)
db_service = db_context.service(service_id)
db_service.delete()
# ----- Compose reply --------------------------------------------------------------------------------------
reply = Empty()
LOGGER.debug('DeleteService reply: {}'.format(str(reply)))
DELETESERVICE_COUNTER_COMPLETED.inc()
return reply
except ServiceException as e:
LOGGER.exception('DeleteService exception')
DELETESERVICE_COUNTER_FAILED.inc()
grpc_context.abort(e.code, e.details)
except Exception as e: # pragma: no cover
LOGGER.exception('DeleteService exception')
DELETESERVICE_COUNTER_FAILED.inc()
grpc_context.abort(grpc.StatusCode.INTERNAL, str(e))
@GETSERVICEBYID_HISTOGRAM_DURATION.time()
def GetServiceById(self, request : ServiceId, grpc_context : grpc.ServicerContext) -> Service:
GETSERVICEBYID_COUNTER_STARTED.inc()
try:
LOGGER.debug('GetServiceById request: {}'.format(str(request)))
# ----- Validate request data and pre-conditions -----------------------------------------------------------
context_id, service_id = check_service_id_request('GetServiceById', request, self.database, LOGGER)
# ----- Retrieve data from the database --------------------------------------------------------------------
db_context = self.database.context(context_id)
db_service = db_context.service(service_id)
# ----- Compose reply --------------------------------------------------------------------------------------
reply = Service(**db_service.dump())
LOGGER.debug('GetServiceById reply: {}'.format(str(reply)))
GETSERVICEBYID_COUNTER_COMPLETED.inc()
return reply
except ServiceException as e:
LOGGER.exception('GetServiceById exception')
GETSERVICEBYID_COUNTER_FAILED.inc()
grpc_context.abort(e.code, e.details)
except Exception as e: # pragma: no cover
LOGGER.exception('GetServiceById exception')
GETSERVICEBYID_COUNTER_FAILED.inc()
grpc_context.abort(grpc.StatusCode.INTERNAL, str(e))
@GETCONNECTIONLIST_HISTOGRAM_DURATION.time()
def GetConnectionList(self, request : Empty, grpc_context : grpc.ServicerContext) -> ConnectionList:
GETCONNECTIONLIST_COUNTER_STARTED.inc()
try:
LOGGER.debug('GetConnectionList request: {}'.format(str(request)))
# ----- Validate request data and pre-conditions -----------------------------------------------------------
# ----- Retrieve data from the database --------------------------------------------------------------------
raise ServiceException(grpc.StatusCode.UNIMPLEMENTED, 'RPC GetConnectionList() not implemented')
# ----- Compose reply --------------------------------------------------------------------------------------
#reply = ConnectionList()
#LOGGER.debug('GetConnectionList reply: {}'.format(str(reply)))
#GETCONNECTIONLIST_COUNTER_COMPLETED.inc()
#return reply
except ServiceException as e:
LOGGER.exception('GetConnectionList exception')
GETCONNECTIONLIST_COUNTER_FAILED.inc()
grpc_context.abort(e.code, e.details)
except Exception as e: # pragma: no cover
LOGGER.exception('GetConnectionList exception')
GETCONNECTIONLIST_COUNTER_FAILED.inc()
grpc_context.abort(grpc.StatusCode.INTERNAL, str(e))