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