Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | # 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 Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 15 | """ fake_stub.py |
| 16 | |
| 17 | Implements a simple fake grpc stub to use for unit testing. |
| 18 | """ |
| 19 | |
| 20 | import functools |
| 21 | |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 22 | ContentTypeMap = {} |
| 23 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 24 | |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 25 | class FakeObj(object): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 26 | BASES = [] |
Scott Baker | ff104cc | 2017-08-14 15:24:41 -0700 | [diff] [blame] | 27 | |
Scott Baker | 03a163f | 2017-05-17 09:21:47 -0700 | [diff] [blame] | 28 | def __init__(self, fields=[], **kwargs): |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 29 | super(FakeObj, self).__setattr__("is_set", {}) |
| 30 | super(FakeObj, self).__setattr__("fields", []) |
| 31 | |
Scott Baker | 03a163f | 2017-05-17 09:21:47 -0700 | [diff] [blame] | 32 | for f in fields: |
| 33 | name = f["name"] |
| 34 | self.fields.append(name) |
| 35 | setattr(self, name, f["default"]) |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 36 | |
| 37 | super(FakeObj, self).__setattr__("is_set", {}) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 38 | for (k, v) in kwargs.items(): |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 39 | 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 Baker | 37cf9e2 | 2018-08-20 14:39:33 -0700 | [diff] [blame] | 55 | 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 Baker | 5b7fba0 | 2018-10-17 08:46:46 -0700 | [diff] [blame] | 74 | def ListFields(self): |
| 75 | fbn = self.DESCRIPTOR.fields_by_name |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 76 | fieldlist = [] |
| 77 | for (k, v) in fbn.items(): |
Scott Baker | 5b7fba0 | 2018-10-17 08:46:46 -0700 | [diff] [blame] | 78 | if self.is_set.get(k, False): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 79 | fieldlist.append((v, getattr(self, k))) |
| 80 | return fieldlist |
Scott Baker | 5b7fba0 | 2018-10-17 08:46:46 -0700 | [diff] [blame] | 81 | |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 82 | @property |
| 83 | def self_content_type_id(self): |
| 84 | return "xos.%s" % self.__class__.__name__.lower() |
| 85 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 86 | |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 87 | class 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 Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 100 | |
Scott Baker | 03a163f | 2017-05-17 09:21:47 -0700 | [diff] [blame] | 101 | class FakeFieldOption(object): |
Scott Baker | c4156c3 | 2017-12-08 10:58:21 -0800 | [diff] [blame] | 102 | def __init__(self, modelName=None, reverseFieldName=None): |
Scott Baker | 03a163f | 2017-05-17 09:21:47 -0700 | [diff] [blame] | 103 | self.modelName = modelName |
Scott Baker | c4156c3 | 2017-12-08 10:58:21 -0800 | [diff] [blame] | 104 | self.reverseFieldName = reverseFieldName |
Scott Baker | 03a163f | 2017-05-17 09:21:47 -0700 | [diff] [blame] | 105 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 106 | |
Scott Baker | 03a163f | 2017-05-17 09:21:47 -0700 | [diff] [blame] | 107 | class FakeField(object): |
| 108 | def __init__(self, field): |
| 109 | extensions = {} |
| 110 | |
Scott Baker | 37cf9e2 | 2018-08-20 14:39:33 -0700 | [diff] [blame] | 111 | self.field_decl = field |
Scott Baker | 5b7fba0 | 2018-10-17 08:46:46 -0700 | [diff] [blame] | 112 | self.name = field["name"] |
Scott Baker | 37cf9e2 | 2018-08-20 14:39:33 -0700 | [diff] [blame] | 113 | |
Scott Baker | 03a163f | 2017-05-17 09:21:47 -0700 | [diff] [blame] | 114 | fk_model = field.get("fk_model", None) |
| 115 | if fk_model: |
Scott Baker | c4156c3 | 2017-12-08 10:58:21 -0800 | [diff] [blame] | 116 | reverseFieldName = field.get("fk_reverseFieldName", None) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 117 | extensions["xos.foreignKey"] = FakeFieldOption( |
| 118 | modelName=fk_model, reverseFieldName=reverseFieldName |
| 119 | ) |
Scott Baker | 03a163f | 2017-05-17 09:21:47 -0700 | [diff] [blame] | 120 | |
| 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 Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 130 | |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 131 | class FakeDescriptor(object): |
| 132 | def __init__(self, objName): |
| 133 | global ContentTypeIdCounter |
| 134 | global ContentTypeMap |
Scott Baker | 03a163f | 2017-05-17 09:21:47 -0700 | [diff] [blame] | 135 | self.objName = objName |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 136 | if objName in ContentTypeMap: |
| 137 | ct = ContentTypeMap[objName] |
| 138 | else: |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 139 | ct = "xos.%s" % objName.lower() |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 140 | 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 Baker | 03a163f | 2017-05-17 09:21:47 -0700 | [diff] [blame] | 148 | cls = globals()[self.objName] |
| 149 | fbn = {} |
| 150 | for field in cls.FIELDS: |
| 151 | fake_field = FakeField(field) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 152 | fbn[field["name"]] = fake_field |
Scott Baker | 03a163f | 2017-05-17 09:21:47 -0700 | [diff] [blame] | 153 | |
| 154 | return fbn |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 155 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 156 | |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 157 | class Controller(FakeObj): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 158 | 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 Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 164 | |
| 165 | def __init__(self, **kwargs): |
| 166 | return super(Controller, self).__init__(self.FIELDS, **kwargs) |
| 167 | |
| 168 | DESCRIPTOR = FakeDescriptor("Controller") |
| 169 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 170 | |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 171 | class Deployment(FakeObj): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 172 | FIELDS = ( |
| 173 | {"name": "id", "default": 0}, |
| 174 | {"name": "name", "default": ""}, |
| 175 | {"name": "class_names", "default": "Deployment"}, |
| 176 | ) |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 177 | |
| 178 | def __init__(self, **kwargs): |
| 179 | return super(Deployment, self).__init__(self.FIELDS, **kwargs) |
| 180 | |
| 181 | DESCRIPTOR = FakeDescriptor("Controller") |
| 182 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 183 | |
Scott Baker | ff104cc | 2017-08-14 15:24:41 -0700 | [diff] [blame] | 184 | class User(FakeObj): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 185 | 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 Baker | ff104cc | 2017-08-14 15:24:41 -0700 | [diff] [blame] | 191 | |
| 192 | def __init__(self, **kwargs): |
| 193 | return super(User, self).__init__(self.FIELDS, **kwargs) |
| 194 | |
| 195 | DESCRIPTOR = FakeDescriptor("User") |
| 196 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 197 | |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 198 | class Slice(FakeObj): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 199 | 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 Baker | 03a163f | 2017-05-17 09:21:47 -0700 | [diff] [blame] | 215 | |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 216 | def __init__(self, **kwargs): |
Scott Baker | 03a163f | 2017-05-17 09:21:47 -0700 | [diff] [blame] | 217 | return super(Slice, self).__init__(self.FIELDS, **kwargs) |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 218 | |
| 219 | DESCRIPTOR = FakeDescriptor("Slice") |
| 220 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 221 | |
Scott Baker | 03a163f | 2017-05-17 09:21:47 -0700 | [diff] [blame] | 222 | class Site(FakeObj): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 223 | 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 Baker | 03a163f | 2017-05-17 09:21:47 -0700 | [diff] [blame] | 231 | |
| 232 | def __init__(self, **kwargs): |
| 233 | return super(Site, self).__init__(self.FIELDS, **kwargs) |
| 234 | |
| 235 | DESCRIPTOR = FakeDescriptor("Site") |
| 236 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 237 | |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 238 | class Service(FakeObj): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 239 | 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 Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 246 | |
| 247 | def __init__(self, **kwargs): |
| 248 | return super(Service, self).__init__(self.FIELDS, **kwargs) |
| 249 | |
| 250 | DESCRIPTOR = FakeDescriptor("Service") |
| 251 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 252 | |
Scott Baker | 8f36c3f | 2017-09-26 16:44:36 -0700 | [diff] [blame] | 253 | class ServiceInstance(FakeObj): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 254 | 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 Baker | 8f36c3f | 2017-09-26 16:44:36 -0700 | [diff] [blame] | 260 | |
| 261 | def __init__(self, **kwargs): |
| 262 | return super(ServiceInstance, self).__init__(self.FIELDS, **kwargs) |
| 263 | |
| 264 | DESCRIPTOR = FakeDescriptor("ServiceInstance") |
| 265 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 266 | |
Scott Baker | ff104cc | 2017-08-14 15:24:41 -0700 | [diff] [blame] | 267 | class ONOSService(FakeObj): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 268 | 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 Baker | ff104cc | 2017-08-14 15:24:41 -0700 | [diff] [blame] | 274 | |
| 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 Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 282 | |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 283 | class Network(FakeObj): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 284 | 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 Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 297 | |
| 298 | def __init__(self, **kwargs): |
| 299 | return super(Network, self).__init__(self.FIELDS, **kwargs) |
| 300 | |
| 301 | DESCRIPTOR = FakeDescriptor("Network") |
| 302 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 303 | |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 304 | class NetworkTemplate(FakeObj): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 305 | 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 Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 312 | |
| 313 | def __init__(self, **kwargs): |
| 314 | return super(NetworkTemplate, self).__init__(self.FIELDS, **kwargs) |
| 315 | |
| 316 | DESCRIPTOR = FakeDescriptor("NetworkTemplate") |
| 317 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 318 | |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 319 | class ControllerNetwork(FakeObj): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 320 | 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 Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 327 | |
| 328 | def __init__(self, **kwargs): |
| 329 | return super(ControllerNetwork, self).__init__(self.FIELDS, **kwargs) |
| 330 | |
| 331 | DESCRIPTOR = FakeDescriptor("ControllerNetwork") |
| 332 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 333 | |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 334 | class NetworkSlice(FakeObj): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 335 | 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 Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 342 | |
| 343 | def __init__(self, **kwargs): |
| 344 | return super(NetworkSlice, self).__init__(self.FIELDS, **kwargs) |
| 345 | |
| 346 | DESCRIPTOR = FakeDescriptor("NetworkSlice") |
| 347 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 348 | |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 349 | class Tag(FakeObj): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 350 | 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 Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 360 | |
| 361 | def __init__(self, **kwargs): |
| 362 | return super(Tag, self).__init__(self.FIELDS, **kwargs) |
| 363 | |
| 364 | DESCRIPTOR = FakeDescriptor("Tag") |
| 365 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 366 | |
Scott Baker | 37cf9e2 | 2018-08-20 14:39:33 -0700 | [diff] [blame] | 367 | class TestModel(FakeObj): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 368 | 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 Baker | 37cf9e2 | 2018-08-20 14:39:33 -0700 | [diff] [blame] | 375 | |
| 376 | def __init__(self, **kwargs): |
| 377 | return super(TestModel, self).__init__(self.FIELDS, **kwargs) |
| 378 | |
| 379 | DESCRIPTOR = FakeDescriptor("TestModel") |
| 380 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 381 | |
Scott Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 382 | class TestModelTwo(FakeObj): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 383 | 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 Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 395 | |
| 396 | def __init__(self, **kwargs): |
| 397 | return super(TestModelTwo, self).__init__(self.FIELDS, **kwargs) |
| 398 | |
| 399 | DESCRIPTOR = FakeDescriptor("TestModelTwo") |
| 400 | |
Scott Baker | 37cf9e2 | 2018-08-20 14:39:33 -0700 | [diff] [blame] | 401 | |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 402 | class ID(FakeObj): |
| 403 | pass |
| 404 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 405 | |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 406 | class FakeItemList(object): |
| 407 | def __init__(self, items): |
| 408 | self.items = items |
| 409 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 410 | |
Scott Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 411 | class FakeElement(object): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 412 | EQUAL = "equal" |
| 413 | IEXACT = "iexact" |
Scott Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 414 | |
| 415 | def __init__(self): |
| 416 | pass |
| 417 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 418 | |
Scott Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 419 | class FakeElements(object): |
| 420 | def __init__(self): |
| 421 | self.items = [] |
| 422 | |
| 423 | def add(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 424 | el = FakeElement() |
Scott Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 425 | self.items.append(el) |
| 426 | return el |
| 427 | |
Scott Baker | e1607b8 | 2018-09-20 14:10:59 -0700 | [diff] [blame] | 428 | def __getitem__(self, index): |
| 429 | return self.items[index] |
| 430 | |
| 431 | def __len__(self): |
| 432 | return len(self.items) |
| 433 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 434 | |
Scott Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 435 | class FakeQuery(object): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 436 | DEFAULT = 0 |
Scott Baker | ea1f4d0 | 2018-12-17 10:21:50 -0800 | [diff] [blame] | 437 | ALL = 1 |
| 438 | SYNCHRONIZER_DIRTY_OBJECTS = 2 |
| 439 | SYNCHRONIZER_DELETED_OBJECTS = 3 |
| 440 | SYNCHRONIZER_DIRTY_POLICIES = 4 |
| 441 | SYNCHRONIZER_DELETED_POLICIES = 5 |
Scott Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 442 | |
| 443 | def __init__(self): |
| 444 | self.elements = FakeElements() |
| 445 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 446 | |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 447 | class FakeStub(object): |
| 448 | def __init__(self): |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 449 | self.id_counter = 1 |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 450 | self.objs = {} |
Scott Baker | ea1f4d0 | 2018-12-17 10:21:50 -0800 | [diff] [blame] | 451 | self.deleted_objs = {} |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 452 | 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 Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 469 | 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 Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 474 | setattr(self, "Filter%s" % name, functools.partial(self.filter, name)) |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 475 | |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 476 | def make_key(self, name, id): |
| 477 | return "%s:%d" % (name, id.id) |
| 478 | |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 479 | def get(self, classname, id, metadata=None): |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 480 | obj = self.objs.get(self.make_key(classname, id), None) |
| 481 | return obj |
| 482 | |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 483 | def list(self, classname, empty, metadata=None): |
| 484 | items = [] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 485 | for (k, v) in self.objs.items(): |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 486 | (this_classname, id) = k.split(":") |
| 487 | if this_classname == classname: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 488 | items.append(v) |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 489 | return FakeItemList(items) |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 490 | |
Scott Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 491 | def filter(self, classname, query, metadata=None): |
| 492 | items = [] |
Scott Baker | ea1f4d0 | 2018-12-17 10:21:50 -0800 | [diff] [blame] | 493 | |
| 494 | if query.kind == FakeQuery.SYNCHRONIZER_DELETED_OBJECTS: |
| 495 | objs = self.deleted_objs.items() |
| 496 | else: |
| 497 | objs = self.objs.items() |
| 498 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 499 | for (k, v) in objs: |
Scott Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 500 | (this_classname, id) = k.split(":") |
| 501 | if this_classname != classname: |
| 502 | continue |
Scott Baker | ea1f4d0 | 2018-12-17 10:21:50 -0800 | [diff] [blame] | 503 | match = True |
Scott Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 504 | for q in query.elements.items: |
| 505 | iValue = getattr(q, "iValue", None) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 506 | if (iValue is not None) and getattr(v, q.name) != iValue: |
Scott Baker | ea1f4d0 | 2018-12-17 10:21:50 -0800 | [diff] [blame] | 507 | match = False |
Scott Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 508 | sValue = getattr(q, "sValue", None) |
| 509 | if (sValue is not None) and getattr(v, q.name) != sValue: |
Scott Baker | ea1f4d0 | 2018-12-17 10:21:50 -0800 | [diff] [blame] | 510 | match = False |
| 511 | if match: |
Scott Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 512 | items.append(v) |
| 513 | return FakeItemList(items) |
| 514 | |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 515 | def create(self, classname, obj, metadata=None): |
| 516 | obj.id = self.id_counter |
| 517 | self.id_counter = self.id_counter + 1 |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 518 | k = self.make_key(classname, FakeObj(id=obj.id)) |
| 519 | self.objs[k] = obj |
Scott Baker | ff104cc | 2017-08-14 15:24:41 -0700 | [diff] [blame] | 520 | |
| 521 | for base_classname in obj.BASES: |
| 522 | base_class = globals()[base_classname] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 523 | base_obj = base_class(id=obj.id, leaf_model_name=classname) |
Scott Baker | ff104cc | 2017-08-14 15:24:41 -0700 | [diff] [blame] | 524 | k = self.make_key(base_classname, base_obj) |
| 525 | self.objs[k] = base_obj |
| 526 | |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 527 | return obj |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 528 | |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 529 | def update(self, classname, obj, metadata=None): |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 530 | # TODO: partial update support? |
| 531 | k = self.make_key(classname, FakeObj(id=obj.id)) |
| 532 | self.objs[k] = obj |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 533 | return obj |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 534 | |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 535 | def delete(self, classname, id, metadata=None): |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 536 | k = self.make_key(classname, id) |
Scott Baker | ea1f4d0 | 2018-12-17 10:21:50 -0800 | [diff] [blame] | 537 | obj = self.objs[k] |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 538 | del self.objs[k] |
Scott Baker | ea1f4d0 | 2018-12-17 10:21:50 -0800 | [diff] [blame] | 539 | self.deleted_objs[k] = obj |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 540 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 541 | |
Scott Baker | b96ba43 | 2018-02-26 09:53:48 -0800 | [diff] [blame] | 542 | class FakeCommonProtos(object): |
| 543 | def __init__(self): |
| 544 | self.ID = ID |
Scott Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 545 | self.Query = FakeQuery |
Scott Baker | b96ba43 | 2018-02-26 09:53:48 -0800 | [diff] [blame] | 546 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 547 | |
Scott Baker | b96ba43 | 2018-02-26 09:53:48 -0800 | [diff] [blame] | 548 | class FakeProtos(object): |
| 549 | def __init__(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 550 | 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 Baker | b96ba43 | 2018-02-26 09:53:48 -0800 | [diff] [blame] | 568 | setattr(self, name, globals()[name]) |
| 569 | self.common__pb2 = FakeCommonProtos() |
| 570 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 571 | |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 572 | class FakeSymDb(object): |
| 573 | def __init__(self): |
| 574 | self._classes = {} |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 575 | 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 Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 593 | self._classes["xos.%s" % name] = globals()[name] |