blob: 81c3988fd449f8869c4e9379458746262c523919 [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
Zack Williams5c2ea232019-01-30 15:23:01 -070020from __future__ import absolute_import
21
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070022import functools
23
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070024ContentTypeMap = {}
25
Zack Williams045b63d2019-01-22 16:30:57 -070026
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070027class FakeObj(object):
Zack Williams045b63d2019-01-22 16:30:57 -070028 BASES = []
Scott Bakerff104cc2017-08-14 15:24:41 -070029
Scott Baker03a163f2017-05-17 09:21:47 -070030 def __init__(self, fields=[], **kwargs):
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070031 super(FakeObj, self).__setattr__("is_set", {})
32 super(FakeObj, self).__setattr__("fields", [])
33
Scott Baker03a163f2017-05-17 09:21:47 -070034 for f in fields:
35 name = f["name"]
36 self.fields.append(name)
37 setattr(self, name, f["default"])
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070038
39 super(FakeObj, self).__setattr__("is_set", {})
Zack Williams045b63d2019-01-22 16:30:57 -070040 for (k, v) in kwargs.items():
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070041 setattr(self, k, v)
42
43 def __repr__(self):
44 lines = []
45 for k in self.fields:
46 if self.is_set.get(k, False):
47 lines.append('%s: "%s"' % (k, getattr(self, k)))
48 if lines:
49 return "\n".join(lines) + "\n"
50 else:
51 return ""
52
53 def __setattr__(self, name, value):
54 self.is_set[name] = True
55 super(FakeObj, self).__setattr__(name, value)
56
Scott Baker37cf9e22018-08-20 14:39:33 -070057 def HasField(self, name):
58 """ Return True if the field is set in the protobuf. """
59
60 # gRPC throws a valueerror if the field doesn't exist in the schema
61 if name not in self.fields:
62 raise ValueError("Field %s does not exist in schema" % name)
63
64 # Fields that are always set
65 if name in ["leaf_model_name"]:
66 return True
67
68 field = self.DESCRIPTOR.fields_by_name[name].field_decl
69
70 # Reverse foreign keys lists are never None, they are an empty list
71 if field.get("fk_reverse", None):
72 return True
73
74 return self.is_set.get(name, False)
75
Scott Baker5b7fba02018-10-17 08:46:46 -070076 def ListFields(self):
77 fbn = self.DESCRIPTOR.fields_by_name
Zack Williams045b63d2019-01-22 16:30:57 -070078 fieldlist = []
79 for (k, v) in fbn.items():
Scott Baker5b7fba02018-10-17 08:46:46 -070080 if self.is_set.get(k, False):
Zack Williams045b63d2019-01-22 16:30:57 -070081 fieldlist.append((v, getattr(self, k)))
82 return fieldlist
Scott Baker5b7fba02018-10-17 08:46:46 -070083
Scott Baker500f8c72017-05-19 09:41:50 -070084 @property
85 def self_content_type_id(self):
86 return "xos.%s" % self.__class__.__name__.lower()
87
Zack Williams045b63d2019-01-22 16:30:57 -070088
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070089class FakeExtensionManager(object):
90 def __init__(self, obj, extensions):
91 self.obj = obj
92 self.extensions = extensions
93
94 def _FindExtensionByName(self, name):
95 return name
96
97 def __getitem__(self, name, default=None):
98 if name in self.extensions:
99 return self.extensions[name]
100 return default
101
Zack Williams045b63d2019-01-22 16:30:57 -0700102
Scott Baker03a163f2017-05-17 09:21:47 -0700103class FakeFieldOption(object):
Scott Bakerc4156c32017-12-08 10:58:21 -0800104 def __init__(self, modelName=None, reverseFieldName=None):
Scott Baker03a163f2017-05-17 09:21:47 -0700105 self.modelName = modelName
Scott Bakerc4156c32017-12-08 10:58:21 -0800106 self.reverseFieldName = reverseFieldName
Scott Baker03a163f2017-05-17 09:21:47 -0700107
Zack Williams045b63d2019-01-22 16:30:57 -0700108
Scott Baker03a163f2017-05-17 09:21:47 -0700109class FakeField(object):
110 def __init__(self, field):
111 extensions = {}
112
Scott Baker37cf9e22018-08-20 14:39:33 -0700113 self.field_decl = field
Scott Baker5b7fba02018-10-17 08:46:46 -0700114 self.name = field["name"]
Scott Baker37cf9e22018-08-20 14:39:33 -0700115
Scott Baker03a163f2017-05-17 09:21:47 -0700116 fk_model = field.get("fk_model", None)
117 if fk_model:
Scott Bakerc4156c32017-12-08 10:58:21 -0800118 reverseFieldName = field.get("fk_reverseFieldName", None)
Zack Williams045b63d2019-01-22 16:30:57 -0700119 extensions["xos.foreignKey"] = FakeFieldOption(
120 modelName=fk_model, reverseFieldName=reverseFieldName
121 )
Scott Baker03a163f2017-05-17 09:21:47 -0700122
123 fk_reverse = field.get("fk_reverse", None)
124 if fk_reverse:
125 extensions["xos.reverseForeignKey"] = FakeFieldOption(modelName=fk_reverse)
126
127 self.Extensions = FakeExtensionManager(self, extensions)
128
129 def GetOptions(self):
130 return self
131
Zack Williams045b63d2019-01-22 16:30:57 -0700132
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700133class FakeDescriptor(object):
134 def __init__(self, objName):
135 global ContentTypeIdCounter
136 global ContentTypeMap
Scott Baker03a163f2017-05-17 09:21:47 -0700137 self.objName = objName
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700138 if objName in ContentTypeMap:
139 ct = ContentTypeMap[objName]
140 else:
Scott Baker500f8c72017-05-19 09:41:50 -0700141 ct = "xos.%s" % objName.lower()
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700142 ContentTypeMap[objName] = ct
143 self.Extensions = FakeExtensionManager(self, {"xos.contentTypeId": ct})
144
145 def GetOptions(self):
146 return self
147
148 @property
149 def fields_by_name(self):
Scott Baker03a163f2017-05-17 09:21:47 -0700150 cls = globals()[self.objName]
151 fbn = {}
152 for field in cls.FIELDS:
153 fake_field = FakeField(field)
Zack Williams045b63d2019-01-22 16:30:57 -0700154 fbn[field["name"]] = fake_field
Scott Baker03a163f2017-05-17 09:21:47 -0700155
156 return fbn
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700157
Zack Williams045b63d2019-01-22 16:30:57 -0700158
Scott Baker5f38c7b2017-08-18 10:12:49 -0700159class Controller(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700160 FIELDS = (
161 {"name": "id", "default": 0},
162 {"name": "name", "default": ""},
163 {"name": "deployment_id", "default": 0, "fk_model": "Deployment"},
164 {"name": "class_names", "default": "Controller"},
165 )
Scott Baker5f38c7b2017-08-18 10:12:49 -0700166
167 def __init__(self, **kwargs):
168 return super(Controller, self).__init__(self.FIELDS, **kwargs)
169
170 DESCRIPTOR = FakeDescriptor("Controller")
171
Zack Williams045b63d2019-01-22 16:30:57 -0700172
Scott Baker5f38c7b2017-08-18 10:12:49 -0700173class Deployment(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700174 FIELDS = (
175 {"name": "id", "default": 0},
176 {"name": "name", "default": ""},
177 {"name": "class_names", "default": "Deployment"},
178 )
Scott Baker5f38c7b2017-08-18 10:12:49 -0700179
180 def __init__(self, **kwargs):
181 return super(Deployment, self).__init__(self.FIELDS, **kwargs)
182
183 DESCRIPTOR = FakeDescriptor("Controller")
184
Zack Williams045b63d2019-01-22 16:30:57 -0700185
Scott Bakerff104cc2017-08-14 15:24:41 -0700186class User(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700187 FIELDS = (
188 {"name": "id", "default": 0},
189 {"name": "email", "default": ""},
190 {"name": "site_id", "default": 0, "fk_model": "Site"},
191 {"name": "class_names", "default": "User"},
192 )
Scott Bakerff104cc2017-08-14 15:24:41 -0700193
194 def __init__(self, **kwargs):
195 return super(User, self).__init__(self.FIELDS, **kwargs)
196
197 DESCRIPTOR = FakeDescriptor("User")
198
Zack Williams045b63d2019-01-22 16:30:57 -0700199
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700200class Slice(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700201 FIELDS = (
202 {"name": "id", "default": 0},
203 {"name": "name", "default": ""},
204 {
205 "name": "site_id",
206 "default": 0,
207 "fk_model": "Site",
208 "fk_reverseFieldName": "slices",
209 },
210 {"name": "service_id", "default": 0, "fk_model": "Service"},
211 {"name": "creator_id", "default": 0, "fk_model": "User"},
212 {"name": "networks_ids", "default": [], "fk_reverse": "Network"},
213 {"name": "network", "default": ""},
214 {"name": "leaf_model_name", "default": "Slice"},
215 {"name": "class_names", "default": "Slice"},
216 )
Scott Baker03a163f2017-05-17 09:21:47 -0700217
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700218 def __init__(self, **kwargs):
Scott Baker03a163f2017-05-17 09:21:47 -0700219 return super(Slice, self).__init__(self.FIELDS, **kwargs)
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700220
221 DESCRIPTOR = FakeDescriptor("Slice")
222
Zack Williams045b63d2019-01-22 16:30:57 -0700223
Scott Baker03a163f2017-05-17 09:21:47 -0700224class Site(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700225 FIELDS = (
226 {"name": "id", "default": 0},
227 {"name": "name", "default": ""},
228 {"name": "login_base", "default": ""},
229 {"name": "slices_ids", "default": [], "fk_reverse": "Slice"},
230 {"name": "leaf_model_name", "default": "Site"},
231 {"name": "class_names", "default": "Site"},
232 )
Scott Baker03a163f2017-05-17 09:21:47 -0700233
234 def __init__(self, **kwargs):
235 return super(Site, self).__init__(self.FIELDS, **kwargs)
236
237 DESCRIPTOR = FakeDescriptor("Site")
238
Zack Williams045b63d2019-01-22 16:30:57 -0700239
Scott Baker500f8c72017-05-19 09:41:50 -0700240class Service(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700241 FIELDS = (
242 {"name": "id", "default": 0},
243 {"name": "name", "default": ""},
244 {"name": "slices_ids", "default": [], "fk_reverse": "Slice"},
245 {"name": "leaf_model_name", "default": "Service"},
246 {"name": "class_names", "default": "Service"},
247 )
Scott Baker500f8c72017-05-19 09:41:50 -0700248
249 def __init__(self, **kwargs):
250 return super(Service, self).__init__(self.FIELDS, **kwargs)
251
252 DESCRIPTOR = FakeDescriptor("Service")
253
Zack Williams045b63d2019-01-22 16:30:57 -0700254
Scott Baker8f36c3f2017-09-26 16:44:36 -0700255class ServiceInstance(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700256 FIELDS = (
257 {"name": "id", "default": 0},
258 {"name": "owher", "default": 0, "fk_model": "Service"},
259 {"name": "leaf_model_name", "default": "ServiceInstance"},
260 {"name": "class_names", "default": "ServiceInstance"},
261 )
Scott Baker8f36c3f2017-09-26 16:44:36 -0700262
263 def __init__(self, **kwargs):
264 return super(ServiceInstance, self).__init__(self.FIELDS, **kwargs)
265
266 DESCRIPTOR = FakeDescriptor("ServiceInstance")
267
Zack Williams045b63d2019-01-22 16:30:57 -0700268
Scott Bakerff104cc2017-08-14 15:24:41 -0700269class ONOSService(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700270 FIELDS = (
271 {"name": "id", "default": 0},
272 {"name": "name", "default": ""},
273 {"name": "leaf_model_name", "default": "ONOSService"},
274 {"name": "class_names", "default": "ONOSService,Service"},
275 )
Scott Bakerff104cc2017-08-14 15:24:41 -0700276
277 BASES = ["Service"]
278
279 def __init__(self, **kwargs):
280 return super(ONOSService, self).__init__(self.FIELDS, **kwargs)
281
282 DESCRIPTOR = FakeDescriptor("ONOSService")
283
Zack Williams045b63d2019-01-22 16:30:57 -0700284
Scott Baker5f38c7b2017-08-18 10:12:49 -0700285class Network(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700286 FIELDS = (
287 {"name": "id", "default": 0},
288 {"name": "name", "default": ""},
289 {"name": "owner_id", "default": 0, "fk_model": "Slice"},
290 {"name": "template_id", "default": 0, "fk_model": "NetworkTemplate"},
291 {
292 "name": "controllernetworks_ids",
293 "default": [],
294 "fk_reverse": "ControllerNetwork",
295 },
296 {"name": "leaf_model_name", "default": "Network"},
297 {"name": "class_names", "default": "Network"},
298 )
Scott Baker5f38c7b2017-08-18 10:12:49 -0700299
300 def __init__(self, **kwargs):
301 return super(Network, self).__init__(self.FIELDS, **kwargs)
302
303 DESCRIPTOR = FakeDescriptor("Network")
304
Zack Williams045b63d2019-01-22 16:30:57 -0700305
Scott Baker5f38c7b2017-08-18 10:12:49 -0700306class NetworkTemplate(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700307 FIELDS = (
308 {"name": "id", "default": 0},
309 {"name": "name", "default": ""},
310 {"name": "vtn_kind", "default": ""},
311 {"name": "leaf_model_name", "default": "NetworkTemplate"},
312 {"name": "class_names", "default": "NetworkTemplate"},
313 )
Scott Baker5f38c7b2017-08-18 10:12:49 -0700314
315 def __init__(self, **kwargs):
316 return super(NetworkTemplate, self).__init__(self.FIELDS, **kwargs)
317
318 DESCRIPTOR = FakeDescriptor("NetworkTemplate")
319
Zack Williams045b63d2019-01-22 16:30:57 -0700320
Scott Baker5f38c7b2017-08-18 10:12:49 -0700321class ControllerNetwork(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700322 FIELDS = (
323 {"name": "id", "default": 0},
324 {"name": "network_id", "default": 0, "fk_model": "Network"},
325 {"name": "controller_id", "default": 0, "fk_model": "Controller"},
326 {"name": "leaf_model_name", "default": "ControllerNetwork"},
327 {"name": "class_names", "default": "ControllerNetwork"},
328 )
Scott Baker5f38c7b2017-08-18 10:12:49 -0700329
330 def __init__(self, **kwargs):
331 return super(ControllerNetwork, self).__init__(self.FIELDS, **kwargs)
332
333 DESCRIPTOR = FakeDescriptor("ControllerNetwork")
334
Zack Williams045b63d2019-01-22 16:30:57 -0700335
Scott Baker5f38c7b2017-08-18 10:12:49 -0700336class NetworkSlice(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700337 FIELDS = (
338 {"name": "id", "default": 0},
339 {"name": "network_id", "default": 0, "fk_model": "Network"},
340 {"name": "slice_id", "default": 0, "fk_model": "Slice"},
341 {"name": "leaf_model_name", "default": "NetworkSlice"},
342 {"name": "class_names", "default": "NetworkSlice"},
343 )
Scott Baker5f38c7b2017-08-18 10:12:49 -0700344
345 def __init__(self, **kwargs):
346 return super(NetworkSlice, self).__init__(self.FIELDS, **kwargs)
347
348 DESCRIPTOR = FakeDescriptor("NetworkSlice")
349
Zack Williams045b63d2019-01-22 16:30:57 -0700350
Scott Baker500f8c72017-05-19 09:41:50 -0700351class Tag(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700352 FIELDS = (
353 {"name": "id", "default": 0},
354 {"name": "service_id", "default": None},
355 {"name": "name", "default": ""},
356 {"name": "value", "default": ""},
357 {"name": "content_type", "default": None},
358 {"name": "object_id", "default": None},
359 {"name": "leaf_model_name", "default": "Tag"},
360 {"name": "class_names", "default": "Tag"},
361 )
Scott Baker500f8c72017-05-19 09:41:50 -0700362
363 def __init__(self, **kwargs):
364 return super(Tag, self).__init__(self.FIELDS, **kwargs)
365
366 DESCRIPTOR = FakeDescriptor("Tag")
367
Zack Williams045b63d2019-01-22 16:30:57 -0700368
Scott Baker37cf9e22018-08-20 14:39:33 -0700369class TestModel(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700370 FIELDS = (
371 {"name": "id", "default": 0},
372 {"name": "intfield", "default": 0},
373 {"name": "stringfield", "default": "somestring"},
374 {"name": "testmodeltwos_ids", "default": [], "fk_reverse": "TestModelTwo"},
375 {"name": "class_names", "default": "TestModel"},
376 )
Scott Baker37cf9e22018-08-20 14:39:33 -0700377
378 def __init__(self, **kwargs):
379 return super(TestModel, self).__init__(self.FIELDS, **kwargs)
380
381 DESCRIPTOR = FakeDescriptor("TestModel")
382
Zack Williams045b63d2019-01-22 16:30:57 -0700383
Scott Baker9e477252019-01-07 11:49:45 -0800384class TestModelTwo(FakeObj):
Zack Williams045b63d2019-01-22 16:30:57 -0700385 FIELDS = (
386 {"name": "id", "default": 0},
387 {"name": "intfieldtwo", "default": 0},
388 {"name": "stringfieldtwo", "default": "somestringtwo"},
389 {
390 "name": "testmodel_id",
391 "default": 0,
392 "fk_model": "TestModel",
393 "fk_reverseFieldName": "testmodeltwos",
394 },
395 {"name": "class_names", "default": "TestModel"},
396 )
Scott Baker9e477252019-01-07 11:49:45 -0800397
398 def __init__(self, **kwargs):
399 return super(TestModelTwo, self).__init__(self.FIELDS, **kwargs)
400
401 DESCRIPTOR = FakeDescriptor("TestModelTwo")
402
Scott Baker37cf9e22018-08-20 14:39:33 -0700403
Scott Baker500f8c72017-05-19 09:41:50 -0700404class ID(FakeObj):
405 pass
406
Zack Williams045b63d2019-01-22 16:30:57 -0700407
Scott Baker500f8c72017-05-19 09:41:50 -0700408class FakeItemList(object):
409 def __init__(self, items):
410 self.items = items
411
Zack Williams045b63d2019-01-22 16:30:57 -0700412
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700413class FakeElement(object):
Zack Williams045b63d2019-01-22 16:30:57 -0700414 EQUAL = "equal"
415 IEXACT = "iexact"
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700416
417 def __init__(self):
418 pass
419
Zack Williams045b63d2019-01-22 16:30:57 -0700420
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700421class FakeElements(object):
422 def __init__(self):
423 self.items = []
424
425 def add(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700426 el = FakeElement()
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700427 self.items.append(el)
428 return el
429
Scott Bakere1607b82018-09-20 14:10:59 -0700430 def __getitem__(self, index):
431 return self.items[index]
432
433 def __len__(self):
434 return len(self.items)
435
Zack Williams045b63d2019-01-22 16:30:57 -0700436
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700437class FakeQuery(object):
Zack Williams045b63d2019-01-22 16:30:57 -0700438 DEFAULT = 0
Scott Bakerea1f4d02018-12-17 10:21:50 -0800439 ALL = 1
440 SYNCHRONIZER_DIRTY_OBJECTS = 2
441 SYNCHRONIZER_DELETED_OBJECTS = 3
442 SYNCHRONIZER_DIRTY_POLICIES = 4
443 SYNCHRONIZER_DELETED_POLICIES = 5
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700444
445 def __init__(self):
446 self.elements = FakeElements()
447
Zack Williams045b63d2019-01-22 16:30:57 -0700448
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700449class FakeStub(object):
450 def __init__(self):
Scott Baker500f8c72017-05-19 09:41:50 -0700451 self.id_counter = 1
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700452 self.objs = {}
Scott Bakerea1f4d02018-12-17 10:21:50 -0800453 self.deleted_objs = {}
Zack Williams045b63d2019-01-22 16:30:57 -0700454 for name in [
455 "Controller",
456 "Deployment",
457 "Slice",
458 "Site",
459 "Tag",
460 "Service",
461 "ServiceInstance",
462 "ONOSService",
463 "User",
464 "Network",
465 "NetworkTemplate",
466 "ControllerNetwork",
467 "NetworkSlice",
468 "TestModel",
469 "TestModelTwo",
470 ]:
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700471 setattr(self, "Get%s" % name, functools.partial(self.get, name))
472 setattr(self, "List%s" % name, functools.partial(self.list, name))
473 setattr(self, "Create%s" % name, functools.partial(self.create, name))
474 setattr(self, "Delete%s" % name, functools.partial(self.delete, name))
475 setattr(self, "Update%s" % name, functools.partial(self.update, name))
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700476 setattr(self, "Filter%s" % name, functools.partial(self.filter, name))
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700477
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700478 def make_key(self, name, id):
479 return "%s:%d" % (name, id.id)
480
Scott Baker500f8c72017-05-19 09:41:50 -0700481 def get(self, classname, id, metadata=None):
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700482 obj = self.objs.get(self.make_key(classname, id), None)
483 return obj
484
Scott Baker500f8c72017-05-19 09:41:50 -0700485 def list(self, classname, empty, metadata=None):
486 items = []
Zack Williams045b63d2019-01-22 16:30:57 -0700487 for (k, v) in self.objs.items():
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700488 (this_classname, id) = k.split(":")
489 if this_classname == classname:
Zack Williams045b63d2019-01-22 16:30:57 -0700490 items.append(v)
Scott Baker500f8c72017-05-19 09:41:50 -0700491 return FakeItemList(items)
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700492
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700493 def filter(self, classname, query, metadata=None):
494 items = []
Scott Bakerea1f4d02018-12-17 10:21:50 -0800495
496 if query.kind == FakeQuery.SYNCHRONIZER_DELETED_OBJECTS:
Zack Williams5c2ea232019-01-30 15:23:01 -0700497 objs = list(self.deleted_objs.items())
Scott Bakerea1f4d02018-12-17 10:21:50 -0800498 else:
Zack Williams5c2ea232019-01-30 15:23:01 -0700499 objs = list(self.objs.items())
Scott Bakerea1f4d02018-12-17 10:21:50 -0800500
Zack Williams045b63d2019-01-22 16:30:57 -0700501 for (k, v) in objs:
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700502 (this_classname, id) = k.split(":")
503 if this_classname != classname:
504 continue
Scott Bakerea1f4d02018-12-17 10:21:50 -0800505 match = True
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700506 for q in query.elements.items:
507 iValue = getattr(q, "iValue", None)
Zack Williams045b63d2019-01-22 16:30:57 -0700508 if (iValue is not None) and getattr(v, q.name) != iValue:
Scott Bakerea1f4d02018-12-17 10:21:50 -0800509 match = False
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700510 sValue = getattr(q, "sValue", None)
511 if (sValue is not None) and getattr(v, q.name) != sValue:
Scott Bakerea1f4d02018-12-17 10:21:50 -0800512 match = False
513 if match:
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700514 items.append(v)
515 return FakeItemList(items)
516
Scott Baker500f8c72017-05-19 09:41:50 -0700517 def create(self, classname, obj, metadata=None):
518 obj.id = self.id_counter
519 self.id_counter = self.id_counter + 1
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700520 k = self.make_key(classname, FakeObj(id=obj.id))
521 self.objs[k] = obj
Scott Bakerff104cc2017-08-14 15:24:41 -0700522
523 for base_classname in obj.BASES:
524 base_class = globals()[base_classname]
Zack Williams045b63d2019-01-22 16:30:57 -0700525 base_obj = base_class(id=obj.id, leaf_model_name=classname)
Scott Bakerff104cc2017-08-14 15:24:41 -0700526 k = self.make_key(base_classname, base_obj)
527 self.objs[k] = base_obj
528
Scott Baker500f8c72017-05-19 09:41:50 -0700529 return obj
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700530
Scott Baker500f8c72017-05-19 09:41:50 -0700531 def update(self, classname, obj, metadata=None):
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700532 # TODO: partial update support?
533 k = self.make_key(classname, FakeObj(id=obj.id))
534 self.objs[k] = obj
Scott Baker500f8c72017-05-19 09:41:50 -0700535 return obj
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700536
Scott Baker500f8c72017-05-19 09:41:50 -0700537 def delete(self, classname, id, metadata=None):
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700538 k = self.make_key(classname, id)
Scott Bakerea1f4d02018-12-17 10:21:50 -0800539 obj = self.objs[k]
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700540 del self.objs[k]
Scott Bakerea1f4d02018-12-17 10:21:50 -0800541 self.deleted_objs[k] = obj
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700542
Zack Williams045b63d2019-01-22 16:30:57 -0700543
Scott Bakerb96ba432018-02-26 09:53:48 -0800544class FakeCommonProtos(object):
545 def __init__(self):
546 self.ID = ID
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700547 self.Query = FakeQuery
Scott Bakerb96ba432018-02-26 09:53:48 -0800548
Zack Williams045b63d2019-01-22 16:30:57 -0700549
Scott Bakerb96ba432018-02-26 09:53:48 -0800550class FakeProtos(object):
551 def __init__(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700552 for name in [
553 "Controller",
554 "Deployment",
555 "Slice",
556 "Site",
557 "ID",
558 "Tag",
559 "Service",
560 "ServiceInstance",
561 "ONOSService",
562 "User",
563 "Network",
564 "NetworkTemplate",
565 "ControllerNetwork",
566 "NetworkSlice",
567 "TestModel",
568 "TestModelTwo",
569 ]:
Scott Bakerb96ba432018-02-26 09:53:48 -0800570 setattr(self, name, globals()[name])
571 self.common__pb2 = FakeCommonProtos()
572
Zack Williams045b63d2019-01-22 16:30:57 -0700573
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700574class FakeSymDb(object):
575 def __init__(self):
576 self._classes = {}
Zack Williams045b63d2019-01-22 16:30:57 -0700577 for name in [
578 "Controller",
579 "Deployment",
580 "Slice",
581 "Site",
582 "ID",
583 "Tag",
584 "Service",
585 "ServiceInstance",
586 "ONOSService",
587 "User",
588 "Network",
589 "NetworkTemplate",
590 "ControllerNetwork",
591 "NetworkSlice",
592 "TestModel",
593 "TestModelTwo",
594 ]:
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700595 self._classes["xos.%s" % name] = globals()[name]