blob: abfce004cb2c2d249baf952959670143e9d748b9 [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070015""" fake_stub.py
16
17 Implements a simple fake grpc stub to use for unit testing.
18"""
19
20import functools
21
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070022ContentTypeMap = {}
23
Zack Williams045b63d2019-01-22 16:30:57 -070024
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070025class FakeObj(object):
Zack Williams045b63d2019-01-22 16:30:57 -070026 BASES = []
Scott Bakerff104cc2017-08-14 15:24:41 -070027
Scott Baker03a163f2017-05-17 09:21:47 -070028 def __init__(self, fields=[], **kwargs):
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070029 super(FakeObj, self).__setattr__("is_set", {})
30 super(FakeObj, self).__setattr__("fields", [])
31
Scott Baker03a163f2017-05-17 09:21:47 -070032 for f in fields:
33 name = f["name"]
34 self.fields.append(name)
35 setattr(self, name, f["default"])
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070036
37 super(FakeObj, self).__setattr__("is_set", {})
Zack Williams045b63d2019-01-22 16:30:57 -070038 for (k, v) in kwargs.items():
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070039 setattr(self, k, v)
40
41 def __repr__(self):
42 lines = []
43 for k in self.fields:
44 if self.is_set.get(k, False):
45 lines.append('%s: "%s"' % (k, getattr(self, k)))
46 if lines:
47 return "\n".join(lines) + "\n"
48 else:
49 return ""
50
51 def __setattr__(self, name, value):
52 self.is_set[name] = True
53 super(FakeObj, self).__setattr__(name, value)
54
Scott Baker37cf9e22018-08-20 14:39:33 -070055 def HasField(self, name):
56 """ Return True if the field is set in the protobuf. """
57
58 # gRPC throws a valueerror if the field doesn't exist in the schema
59 if name not in self.fields:
60 raise ValueError("Field %s does not exist in schema" % name)
61
62 # Fields that are always set
63 if name in ["leaf_model_name"]:
64 return True
65
66 field = self.DESCRIPTOR.fields_by_name[name].field_decl
67
68 # Reverse foreign keys lists are never None, they are an empty list
69 if field.get("fk_reverse", None):
70 return True
71
72 return self.is_set.get(name, False)
73
Scott Baker5b7fba02018-10-17 08:46:46 -070074 def ListFields(self):
75 fbn = self.DESCRIPTOR.fields_by_name
Zack Williams045b63d2019-01-22 16:30:57 -070076 fieldlist = []
77 for (k, v) in fbn.items():
Scott Baker5b7fba02018-10-17 08:46:46 -070078 if self.is_set.get(k, False):
Zack Williams045b63d2019-01-22 16:30:57 -070079 fieldlist.append((v, getattr(self, k)))
80 return fieldlist
Scott Baker5b7fba02018-10-17 08:46:46 -070081
Scott Baker500f8c72017-05-19 09:41:50 -070082 @property
83 def self_content_type_id(self):
84 return "xos.%s" % self.__class__.__name__.lower()
85
Zack Williams045b63d2019-01-22 16:30:57 -070086
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070087class FakeExtensionManager(object):
88 def __init__(self, obj, extensions):
89 self.obj = obj
90 self.extensions = extensions
91
92 def _FindExtensionByName(self, name):
93 return name
94
95 def __getitem__(self, name, default=None):
96 if name in self.extensions:
97 return self.extensions[name]
98 return default
99
Zack Williams045b63d2019-01-22 16:30:57 -0700100
Scott Baker03a163f2017-05-17 09:21:47 -0700101class FakeFieldOption(object):
Scott Bakerc4156c32017-12-08 10:58:21 -0800102 def __init__(self, modelName=None, reverseFieldName=None):
Scott Baker03a163f2017-05-17 09:21:47 -0700103 self.modelName = modelName
Scott Bakerc4156c32017-12-08 10:58:21 -0800104 self.reverseFieldName = reverseFieldName
Scott Baker03a163f2017-05-17 09:21:47 -0700105
Zack Williams045b63d2019-01-22 16:30:57 -0700106
Scott Baker03a163f2017-05-17 09:21:47 -0700107class FakeField(object):
108 def __init__(self, field):
109 extensions = {}
110
Scott Baker37cf9e22018-08-20 14:39:33 -0700111 self.field_decl = field
Scott Baker5b7fba02018-10-17 08:46:46 -0700112 self.name = field["name"]
Scott Baker37cf9e22018-08-20 14:39:33 -0700113
Scott Baker03a163f2017-05-17 09:21:47 -0700114 fk_model = field.get("fk_model", None)
115 if fk_model:
Scott Bakerc4156c32017-12-08 10:58:21 -0800116 reverseFieldName = field.get("fk_reverseFieldName", None)
Zack Williams045b63d2019-01-22 16:30:57 -0700117 extensions["xos.foreignKey"] = FakeFieldOption(
118 modelName=fk_model, reverseFieldName=reverseFieldName
119 )
Scott Baker03a163f2017-05-17 09:21:47 -0700120
121 fk_reverse = field.get("fk_reverse", None)
122 if fk_reverse:
123 extensions["xos.reverseForeignKey"] = FakeFieldOption(modelName=fk_reverse)
124
125 self.Extensions = FakeExtensionManager(self, extensions)
126
127 def GetOptions(self):
128 return self
129
Zack Williams045b63d2019-01-22 16:30:57 -0700130
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700131class FakeDescriptor(object):
132 def __init__(self, objName):
133 global ContentTypeIdCounter
134 global ContentTypeMap
Scott Baker03a163f2017-05-17 09:21:47 -0700135 self.objName = objName
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700136 if objName in ContentTypeMap:
137 ct = ContentTypeMap[objName]
138 else:
Scott Baker500f8c72017-05-19 09:41:50 -0700139 ct = "xos.%s" % objName.lower()
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700140 ContentTypeMap[objName] = ct
141 self.Extensions = FakeExtensionManager(self, {"xos.contentTypeId": ct})
142
143 def GetOptions(self):
144 return self
145
146 @property
147 def fields_by_name(self):
Scott Baker03a163f2017-05-17 09:21:47 -0700148 cls = globals()[self.objName]
149 fbn = {}
150 for field in cls.FIELDS:
151 fake_field = FakeField(field)
Zack Williams045b63d2019-01-22 16:30:57 -0700152 fbn[field["name"]] = fake_field
Scott Baker03a163f2017-05-17 09:21:47 -0700153
154 return fbn
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700155
Zack Williams045b63d2019-01-22 16:30:57 -0700156
Scott Baker5f38c7b2017-08-18 10:12:49 -0700157class Controller(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700158 FIELDS = (
159 {"name": "id", "default": 0},
160 {"name": "name", "default": ""},
161 {"name": "deployment_id", "default": 0, "fk_model": "Deployment"},
162 {"name": "class_names", "default": "Controller"},
163 )
Scott Baker5f38c7b2017-08-18 10:12:49 -0700164
165 def __init__(self, **kwargs):
166 return super(Controller, self).__init__(self.FIELDS, **kwargs)
167
168 DESCRIPTOR = FakeDescriptor("Controller")
169
Zack Williams045b63d2019-01-22 16:30:57 -0700170
Scott Baker5f38c7b2017-08-18 10:12:49 -0700171class Deployment(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700172 FIELDS = (
173 {"name": "id", "default": 0},
174 {"name": "name", "default": ""},
175 {"name": "class_names", "default": "Deployment"},
176 )
Scott Baker5f38c7b2017-08-18 10:12:49 -0700177
178 def __init__(self, **kwargs):
179 return super(Deployment, self).__init__(self.FIELDS, **kwargs)
180
181 DESCRIPTOR = FakeDescriptor("Controller")
182
Zack Williams045b63d2019-01-22 16:30:57 -0700183
Scott Bakerff104cc2017-08-14 15:24:41 -0700184class User(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700185 FIELDS = (
186 {"name": "id", "default": 0},
187 {"name": "email", "default": ""},
188 {"name": "site_id", "default": 0, "fk_model": "Site"},
189 {"name": "class_names", "default": "User"},
190 )
Scott Bakerff104cc2017-08-14 15:24:41 -0700191
192 def __init__(self, **kwargs):
193 return super(User, self).__init__(self.FIELDS, **kwargs)
194
195 DESCRIPTOR = FakeDescriptor("User")
196
Zack Williams045b63d2019-01-22 16:30:57 -0700197
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700198class Slice(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700199 FIELDS = (
200 {"name": "id", "default": 0},
201 {"name": "name", "default": ""},
202 {
203 "name": "site_id",
204 "default": 0,
205 "fk_model": "Site",
206 "fk_reverseFieldName": "slices",
207 },
208 {"name": "service_id", "default": 0, "fk_model": "Service"},
209 {"name": "creator_id", "default": 0, "fk_model": "User"},
210 {"name": "networks_ids", "default": [], "fk_reverse": "Network"},
211 {"name": "network", "default": ""},
212 {"name": "leaf_model_name", "default": "Slice"},
213 {"name": "class_names", "default": "Slice"},
214 )
Scott Baker03a163f2017-05-17 09:21:47 -0700215
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700216 def __init__(self, **kwargs):
Scott Baker03a163f2017-05-17 09:21:47 -0700217 return super(Slice, self).__init__(self.FIELDS, **kwargs)
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700218
219 DESCRIPTOR = FakeDescriptor("Slice")
220
Zack Williams045b63d2019-01-22 16:30:57 -0700221
Scott Baker03a163f2017-05-17 09:21:47 -0700222class Site(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700223 FIELDS = (
224 {"name": "id", "default": 0},
225 {"name": "name", "default": ""},
226 {"name": "login_base", "default": ""},
227 {"name": "slices_ids", "default": [], "fk_reverse": "Slice"},
228 {"name": "leaf_model_name", "default": "Site"},
229 {"name": "class_names", "default": "Site"},
230 )
Scott Baker03a163f2017-05-17 09:21:47 -0700231
232 def __init__(self, **kwargs):
233 return super(Site, self).__init__(self.FIELDS, **kwargs)
234
235 DESCRIPTOR = FakeDescriptor("Site")
236
Zack Williams045b63d2019-01-22 16:30:57 -0700237
Scott Baker500f8c72017-05-19 09:41:50 -0700238class Service(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700239 FIELDS = (
240 {"name": "id", "default": 0},
241 {"name": "name", "default": ""},
242 {"name": "slices_ids", "default": [], "fk_reverse": "Slice"},
243 {"name": "leaf_model_name", "default": "Service"},
244 {"name": "class_names", "default": "Service"},
245 )
Scott Baker500f8c72017-05-19 09:41:50 -0700246
247 def __init__(self, **kwargs):
248 return super(Service, self).__init__(self.FIELDS, **kwargs)
249
250 DESCRIPTOR = FakeDescriptor("Service")
251
Zack Williams045b63d2019-01-22 16:30:57 -0700252
Scott Baker8f36c3f2017-09-26 16:44:36 -0700253class ServiceInstance(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700254 FIELDS = (
255 {"name": "id", "default": 0},
256 {"name": "owher", "default": 0, "fk_model": "Service"},
257 {"name": "leaf_model_name", "default": "ServiceInstance"},
258 {"name": "class_names", "default": "ServiceInstance"},
259 )
Scott Baker8f36c3f2017-09-26 16:44:36 -0700260
261 def __init__(self, **kwargs):
262 return super(ServiceInstance, self).__init__(self.FIELDS, **kwargs)
263
264 DESCRIPTOR = FakeDescriptor("ServiceInstance")
265
Zack Williams045b63d2019-01-22 16:30:57 -0700266
Scott Bakerff104cc2017-08-14 15:24:41 -0700267class ONOSService(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700268 FIELDS = (
269 {"name": "id", "default": 0},
270 {"name": "name", "default": ""},
271 {"name": "leaf_model_name", "default": "ONOSService"},
272 {"name": "class_names", "default": "ONOSService,Service"},
273 )
Scott Bakerff104cc2017-08-14 15:24:41 -0700274
275 BASES = ["Service"]
276
277 def __init__(self, **kwargs):
278 return super(ONOSService, self).__init__(self.FIELDS, **kwargs)
279
280 DESCRIPTOR = FakeDescriptor("ONOSService")
281
Zack Williams045b63d2019-01-22 16:30:57 -0700282
Scott Baker5f38c7b2017-08-18 10:12:49 -0700283class Network(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700284 FIELDS = (
285 {"name": "id", "default": 0},
286 {"name": "name", "default": ""},
287 {"name": "owner_id", "default": 0, "fk_model": "Slice"},
288 {"name": "template_id", "default": 0, "fk_model": "NetworkTemplate"},
289 {
290 "name": "controllernetworks_ids",
291 "default": [],
292 "fk_reverse": "ControllerNetwork",
293 },
294 {"name": "leaf_model_name", "default": "Network"},
295 {"name": "class_names", "default": "Network"},
296 )
Scott Baker5f38c7b2017-08-18 10:12:49 -0700297
298 def __init__(self, **kwargs):
299 return super(Network, self).__init__(self.FIELDS, **kwargs)
300
301 DESCRIPTOR = FakeDescriptor("Network")
302
Zack Williams045b63d2019-01-22 16:30:57 -0700303
Scott Baker5f38c7b2017-08-18 10:12:49 -0700304class NetworkTemplate(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700305 FIELDS = (
306 {"name": "id", "default": 0},
307 {"name": "name", "default": ""},
308 {"name": "vtn_kind", "default": ""},
309 {"name": "leaf_model_name", "default": "NetworkTemplate"},
310 {"name": "class_names", "default": "NetworkTemplate"},
311 )
Scott Baker5f38c7b2017-08-18 10:12:49 -0700312
313 def __init__(self, **kwargs):
314 return super(NetworkTemplate, self).__init__(self.FIELDS, **kwargs)
315
316 DESCRIPTOR = FakeDescriptor("NetworkTemplate")
317
Zack Williams045b63d2019-01-22 16:30:57 -0700318
Scott Baker5f38c7b2017-08-18 10:12:49 -0700319class ControllerNetwork(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700320 FIELDS = (
321 {"name": "id", "default": 0},
322 {"name": "network_id", "default": 0, "fk_model": "Network"},
323 {"name": "controller_id", "default": 0, "fk_model": "Controller"},
324 {"name": "leaf_model_name", "default": "ControllerNetwork"},
325 {"name": "class_names", "default": "ControllerNetwork"},
326 )
Scott Baker5f38c7b2017-08-18 10:12:49 -0700327
328 def __init__(self, **kwargs):
329 return super(ControllerNetwork, self).__init__(self.FIELDS, **kwargs)
330
331 DESCRIPTOR = FakeDescriptor("ControllerNetwork")
332
Zack Williams045b63d2019-01-22 16:30:57 -0700333
Scott Baker5f38c7b2017-08-18 10:12:49 -0700334class NetworkSlice(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700335 FIELDS = (
336 {"name": "id", "default": 0},
337 {"name": "network_id", "default": 0, "fk_model": "Network"},
338 {"name": "slice_id", "default": 0, "fk_model": "Slice"},
339 {"name": "leaf_model_name", "default": "NetworkSlice"},
340 {"name": "class_names", "default": "NetworkSlice"},
341 )
Scott Baker5f38c7b2017-08-18 10:12:49 -0700342
343 def __init__(self, **kwargs):
344 return super(NetworkSlice, self).__init__(self.FIELDS, **kwargs)
345
346 DESCRIPTOR = FakeDescriptor("NetworkSlice")
347
Zack Williams045b63d2019-01-22 16:30:57 -0700348
Scott Baker500f8c72017-05-19 09:41:50 -0700349class Tag(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700350 FIELDS = (
351 {"name": "id", "default": 0},
352 {"name": "service_id", "default": None},
353 {"name": "name", "default": ""},
354 {"name": "value", "default": ""},
355 {"name": "content_type", "default": None},
356 {"name": "object_id", "default": None},
357 {"name": "leaf_model_name", "default": "Tag"},
358 {"name": "class_names", "default": "Tag"},
359 )
Scott Baker500f8c72017-05-19 09:41:50 -0700360
361 def __init__(self, **kwargs):
362 return super(Tag, self).__init__(self.FIELDS, **kwargs)
363
364 DESCRIPTOR = FakeDescriptor("Tag")
365
Zack Williams045b63d2019-01-22 16:30:57 -0700366
Scott Baker37cf9e22018-08-20 14:39:33 -0700367class TestModel(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700368 FIELDS = (
369 {"name": "id", "default": 0},
370 {"name": "intfield", "default": 0},
371 {"name": "stringfield", "default": "somestring"},
372 {"name": "testmodeltwos_ids", "default": [], "fk_reverse": "TestModelTwo"},
373 {"name": "class_names", "default": "TestModel"},
374 )
Scott Baker37cf9e22018-08-20 14:39:33 -0700375
376 def __init__(self, **kwargs):
377 return super(TestModel, self).__init__(self.FIELDS, **kwargs)
378
379 DESCRIPTOR = FakeDescriptor("TestModel")
380
Zack Williams045b63d2019-01-22 16:30:57 -0700381
Scott Baker9e477252019-01-07 11:49:45 -0800382class TestModelTwo(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700383 FIELDS = (
384 {"name": "id", "default": 0},
385 {"name": "intfieldtwo", "default": 0},
386 {"name": "stringfieldtwo", "default": "somestringtwo"},
387 {
388 "name": "testmodel_id",
389 "default": 0,
390 "fk_model": "TestModel",
391 "fk_reverseFieldName": "testmodeltwos",
392 },
393 {"name": "class_names", "default": "TestModel"},
394 )
Scott Baker9e477252019-01-07 11:49:45 -0800395
396 def __init__(self, **kwargs):
397 return super(TestModelTwo, self).__init__(self.FIELDS, **kwargs)
398
399 DESCRIPTOR = FakeDescriptor("TestModelTwo")
400
Scott Baker37cf9e22018-08-20 14:39:33 -0700401
Scott Baker500f8c72017-05-19 09:41:50 -0700402class ID(FakeObj):
403 pass
404
Zack Williams045b63d2019-01-22 16:30:57 -0700405
Scott Baker500f8c72017-05-19 09:41:50 -0700406class FakeItemList(object):
407 def __init__(self, items):
408 self.items = items
409
Zack Williams045b63d2019-01-22 16:30:57 -0700410
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700411class FakeElement(object):
Zack Williams045b63d2019-01-22 16:30:57 -0700412 EQUAL = "equal"
413 IEXACT = "iexact"
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700414
415 def __init__(self):
416 pass
417
Zack Williams045b63d2019-01-22 16:30:57 -0700418
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700419class FakeElements(object):
420 def __init__(self):
421 self.items = []
422
423 def add(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700424 el = FakeElement()
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700425 self.items.append(el)
426 return el
427
Scott Bakere1607b82018-09-20 14:10:59 -0700428 def __getitem__(self, index):
429 return self.items[index]
430
431 def __len__(self):
432 return len(self.items)
433
Zack Williams045b63d2019-01-22 16:30:57 -0700434
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700435class FakeQuery(object):
Zack Williams045b63d2019-01-22 16:30:57 -0700436 DEFAULT = 0
Scott Bakerea1f4d02018-12-17 10:21:50 -0800437 ALL = 1
438 SYNCHRONIZER_DIRTY_OBJECTS = 2
439 SYNCHRONIZER_DELETED_OBJECTS = 3
440 SYNCHRONIZER_DIRTY_POLICIES = 4
441 SYNCHRONIZER_DELETED_POLICIES = 5
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700442
443 def __init__(self):
444 self.elements = FakeElements()
445
Zack Williams045b63d2019-01-22 16:30:57 -0700446
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700447class FakeStub(object):
448 def __init__(self):
Scott Baker500f8c72017-05-19 09:41:50 -0700449 self.id_counter = 1
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700450 self.objs = {}
Scott Bakerea1f4d02018-12-17 10:21:50 -0800451 self.deleted_objs = {}
Zack Williams045b63d2019-01-22 16:30:57 -0700452 for name in [
453 "Controller",
454 "Deployment",
455 "Slice",
456 "Site",
457 "Tag",
458 "Service",
459 "ServiceInstance",
460 "ONOSService",
461 "User",
462 "Network",
463 "NetworkTemplate",
464 "ControllerNetwork",
465 "NetworkSlice",
466 "TestModel",
467 "TestModelTwo",
468 ]:
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700469 setattr(self, "Get%s" % name, functools.partial(self.get, name))
470 setattr(self, "List%s" % name, functools.partial(self.list, name))
471 setattr(self, "Create%s" % name, functools.partial(self.create, name))
472 setattr(self, "Delete%s" % name, functools.partial(self.delete, name))
473 setattr(self, "Update%s" % name, functools.partial(self.update, name))
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700474 setattr(self, "Filter%s" % name, functools.partial(self.filter, name))
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700475
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700476 def make_key(self, name, id):
477 return "%s:%d" % (name, id.id)
478
Scott Baker500f8c72017-05-19 09:41:50 -0700479 def get(self, classname, id, metadata=None):
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700480 obj = self.objs.get(self.make_key(classname, id), None)
481 return obj
482
Scott Baker500f8c72017-05-19 09:41:50 -0700483 def list(self, classname, empty, metadata=None):
484 items = []
Zack Williams045b63d2019-01-22 16:30:57 -0700485 for (k, v) in self.objs.items():
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700486 (this_classname, id) = k.split(":")
487 if this_classname == classname:
Zack Williams045b63d2019-01-22 16:30:57 -0700488 items.append(v)
Scott Baker500f8c72017-05-19 09:41:50 -0700489 return FakeItemList(items)
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700490
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700491 def filter(self, classname, query, metadata=None):
492 items = []
Scott Bakerea1f4d02018-12-17 10:21:50 -0800493
494 if query.kind == FakeQuery.SYNCHRONIZER_DELETED_OBJECTS:
495 objs = self.deleted_objs.items()
496 else:
497 objs = self.objs.items()
498
Zack Williams045b63d2019-01-22 16:30:57 -0700499 for (k, v) in objs:
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700500 (this_classname, id) = k.split(":")
501 if this_classname != classname:
502 continue
Scott Bakerea1f4d02018-12-17 10:21:50 -0800503 match = True
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700504 for q in query.elements.items:
505 iValue = getattr(q, "iValue", None)
Zack Williams045b63d2019-01-22 16:30:57 -0700506 if (iValue is not None) and getattr(v, q.name) != iValue:
Scott Bakerea1f4d02018-12-17 10:21:50 -0800507 match = False
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700508 sValue = getattr(q, "sValue", None)
509 if (sValue is not None) and getattr(v, q.name) != sValue:
Scott Bakerea1f4d02018-12-17 10:21:50 -0800510 match = False
511 if match:
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700512 items.append(v)
513 return FakeItemList(items)
514
Scott Baker500f8c72017-05-19 09:41:50 -0700515 def create(self, classname, obj, metadata=None):
516 obj.id = self.id_counter
517 self.id_counter = self.id_counter + 1
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700518 k = self.make_key(classname, FakeObj(id=obj.id))
519 self.objs[k] = obj
Scott Bakerff104cc2017-08-14 15:24:41 -0700520
521 for base_classname in obj.BASES:
522 base_class = globals()[base_classname]
Zack Williams045b63d2019-01-22 16:30:57 -0700523 base_obj = base_class(id=obj.id, leaf_model_name=classname)
Scott Bakerff104cc2017-08-14 15:24:41 -0700524 k = self.make_key(base_classname, base_obj)
525 self.objs[k] = base_obj
526
Scott Baker500f8c72017-05-19 09:41:50 -0700527 return obj
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700528
Scott Baker500f8c72017-05-19 09:41:50 -0700529 def update(self, classname, obj, metadata=None):
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700530 # TODO: partial update support?
531 k = self.make_key(classname, FakeObj(id=obj.id))
532 self.objs[k] = obj
Scott Baker500f8c72017-05-19 09:41:50 -0700533 return obj
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700534
Scott Baker500f8c72017-05-19 09:41:50 -0700535 def delete(self, classname, id, metadata=None):
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700536 k = self.make_key(classname, id)
Scott Bakerea1f4d02018-12-17 10:21:50 -0800537 obj = self.objs[k]
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700538 del self.objs[k]
Scott Bakerea1f4d02018-12-17 10:21:50 -0800539 self.deleted_objs[k] = obj
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700540
Zack Williams045b63d2019-01-22 16:30:57 -0700541
Scott Bakerb96ba432018-02-26 09:53:48 -0800542class FakeCommonProtos(object):
543 def __init__(self):
544 self.ID = ID
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700545 self.Query = FakeQuery
Scott Bakerb96ba432018-02-26 09:53:48 -0800546
Zack Williams045b63d2019-01-22 16:30:57 -0700547
Scott Bakerb96ba432018-02-26 09:53:48 -0800548class FakeProtos(object):
549 def __init__(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700550 for name in [
551 "Controller",
552 "Deployment",
553 "Slice",
554 "Site",
555 "ID",
556 "Tag",
557 "Service",
558 "ServiceInstance",
559 "ONOSService",
560 "User",
561 "Network",
562 "NetworkTemplate",
563 "ControllerNetwork",
564 "NetworkSlice",
565 "TestModel",
566 "TestModelTwo",
567 ]:
Scott Bakerb96ba432018-02-26 09:53:48 -0800568 setattr(self, name, globals()[name])
569 self.common__pb2 = FakeCommonProtos()
570
Zack Williams045b63d2019-01-22 16:30:57 -0700571
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700572class FakeSymDb(object):
573 def __init__(self):
574 self._classes = {}
Zack Williams045b63d2019-01-22 16:30:57 -0700575 for name in [
576 "Controller",
577 "Deployment",
578 "Slice",
579 "Site",
580 "ID",
581 "Tag",
582 "Service",
583 "ServiceInstance",
584 "ONOSService",
585 "User",
586 "Network",
587 "NetworkTemplate",
588 "ControllerNetwork",
589 "NetworkSlice",
590 "TestModel",
591 "TestModelTwo",
592 ]:
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700593 self._classes["xos.%s" % name] = globals()[name]