Scott Baker | e9ff7ce | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 1 | from django.db import models |
Scott Baker | 839daa8 | 2015-11-16 22:53:49 -0800 | [diff] [blame] | 2 | from core.models import Service, PlCoreBase, Slice, Instance, Tenant, TenantWithContainer, Node, Image, User, Flavor, Subscriber, NetworkParameter, NetworkParameterType, Port |
Scott Baker | e9ff7ce | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 3 | from core.models.plcorebase import StrippedCharField |
| 4 | import os |
Scott Baker | 192da22 | 2015-07-08 19:01:56 -0700 | [diff] [blame] | 5 | from django.db import models, transaction |
Scott Baker | e9ff7ce | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 6 | from django.forms.models import model_to_dict |
| 7 | from django.db.models import Q |
| 8 | from operator import itemgetter, attrgetter, methodcaller |
Scott Baker | fa5cdd6 | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 9 | import traceback |
Scott Baker | 7f8ef8f | 2015-04-20 14:24:29 -0700 | [diff] [blame] | 10 | from xos.exceptions import * |
Scott Baker | e9ff7ce | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 11 | |
| 12 | """ |
| 13 | import os |
| 14 | import sys |
| 15 | sys.path.append("/opt/xos") |
| 16 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings") |
| 17 | import django |
Scott Baker | c749d62 | 2015-12-29 23:47:06 -0800 | [diff] [blame] | 18 | from core.models import * |
| 19 | from services.hpc.models import * |
Scott Baker | 98f5155 | 2015-12-31 10:18:28 -0800 | [diff] [blame] | 20 | from services.cord.models import * |
Scott Baker | e9ff7ce | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 21 | django.setup() |
Scott Baker | e9ff7ce | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 22 | |
Scott Baker | 323eca9 | 2015-04-20 09:48:34 -0700 | [diff] [blame] | 23 | t = VOLTTenant() |
Scott Baker | e9ff7ce | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 24 | t.caller = User.objects.all()[0] |
| 25 | t.save() |
| 26 | |
Scott Baker | dfc0bef | 2015-05-11 08:31:24 -0700 | [diff] [blame] | 27 | for v in VOLTTenant.get_tenant_objects().all(): |
Scott Baker | d4b48c0 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 28 | v.caller = User.objects.all()[0] |
| 29 | v.delete() |
| 30 | |
Scott Baker | dfc0bef | 2015-05-11 08:31:24 -0700 | [diff] [blame] | 31 | for v in VCPETenant.get_tenant_objects().all(): |
Scott Baker | e9ff7ce | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 32 | v.caller = User.objects.all()[0] |
| 33 | v.delete() |
Scott Baker | fa5cdd6 | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 34 | |
Scott Baker | dfc0bef | 2015-05-11 08:31:24 -0700 | [diff] [blame] | 35 | for v in VOLTTenant.get_tenant_objects().all(): |
Scott Baker | fa5cdd6 | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 36 | v.caller = User.objects.all()[0] |
| 37 | v.delete() |
Scott Baker | c633dc9 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 38 | |
Scott Baker | dfc0bef | 2015-05-11 08:31:24 -0700 | [diff] [blame] | 39 | for v in VOLTTenant.get_tenant_objects().all(): |
Scott Baker | c633dc9 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 40 | if not v.creator: |
| 41 | v.creator= User.objects.all()[0] |
| 42 | v.save() |
| 43 | |
Scott Baker | dfc0bef | 2015-05-11 08:31:24 -0700 | [diff] [blame] | 44 | for v in VCPETenant.get_tenant_objects().all(): |
Scott Baker | c633dc9 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 45 | if not v.creator: |
| 46 | v.creator= User.objects.all()[0] |
| 47 | v.save() |
Scott Baker | e9ff7ce | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 48 | """ |
| 49 | |
| 50 | class ConfigurationError(Exception): |
| 51 | pass |
| 52 | |
Scott Baker | d913334 | 2015-07-06 14:38:02 -0700 | [diff] [blame] | 53 | VOLT_KIND = "vOLT" |
| 54 | VCPE_KIND = "vCPE" |
| 55 | VBNG_KIND = "vBNG" |
Scott Baker | 81de640 | 2015-07-06 16:50:30 -0700 | [diff] [blame] | 56 | CORD_SUBSCRIBER_KIND = "CordSubscriberRoot" |
| 57 | |
| 58 | # ------------------------------------------- |
| 59 | # CordSubscriberRoot |
| 60 | # ------------------------------------------- |
| 61 | |
| 62 | class CordSubscriberRoot(Subscriber): |
Scott Baker | e4364d1 | 2015-07-06 17:21:21 -0700 | [diff] [blame] | 63 | class Meta: |
| 64 | proxy = True |
| 65 | |
Scott Baker | 81de640 | 2015-07-06 16:50:30 -0700 | [diff] [blame] | 66 | KIND = CORD_SUBSCRIBER_KIND |
| 67 | |
| 68 | default_attributes = {"firewall_enable": False, |
| 69 | "firewall_rules": "accept all anywhere anywhere", |
| 70 | "url_filter_enable": False, |
| 71 | "url_filter_rules": "allow all", |
| 72 | "url_filter_level": "PG", |
| 73 | "cdn_enable": False, |
Scott Baker | 126ad47 | 2015-07-07 17:59:44 -0700 | [diff] [blame] | 74 | "users": [], |
| 75 | "is_demo_user": False } |
Scott Baker | 81de640 | 2015-07-06 16:50:30 -0700 | [diff] [blame] | 76 | |
Scott Baker | 050f8b3 | 2015-07-07 12:15:03 -0700 | [diff] [blame] | 77 | sync_attributes = ("firewall_enable", |
| 78 | "firewall_rules", |
| 79 | "url_filter_enable", |
| 80 | "url_filter_rules", |
| 81 | "cdn_enable",) |
| 82 | |
Scott Baker | 81de640 | 2015-07-06 16:50:30 -0700 | [diff] [blame] | 83 | def __init__(self, *args, **kwargs): |
| 84 | super(CordSubscriberRoot, self).__init__(*args, **kwargs) |
| 85 | self.cached_volt = None |
Scott Baker | 050f8b3 | 2015-07-07 12:15:03 -0700 | [diff] [blame] | 86 | self._initial_url_filter_enable = self.url_filter_enable |
Scott Baker | 81de640 | 2015-07-06 16:50:30 -0700 | [diff] [blame] | 87 | |
| 88 | @property |
| 89 | def volt(self): |
| 90 | volt = self.get_newest_subscribed_tenant(VOLTTenant) |
| 91 | if not volt: |
| 92 | return None |
| 93 | |
| 94 | # always return the same object when possible |
| 95 | if (self.cached_volt) and (self.cached_volt.id == volt.id): |
Scott Baker | 126ad47 | 2015-07-07 17:59:44 -0700 | [diff] [blame] | 96 | return self.cached_volt |
Scott Baker | 81de640 | 2015-07-06 16:50:30 -0700 | [diff] [blame] | 97 | |
Scott Baker | e4364d1 | 2015-07-06 17:21:21 -0700 | [diff] [blame] | 98 | #volt.caller = self.creator |
Scott Baker | 81de640 | 2015-07-06 16:50:30 -0700 | [diff] [blame] | 99 | self.cached_volt = volt |
| 100 | return volt |
| 101 | |
| 102 | @property |
| 103 | def firewall_enable(self): |
| 104 | return self.get_attribute("firewall_enable", self.default_attributes["firewall_enable"]) |
| 105 | |
| 106 | @firewall_enable.setter |
| 107 | def firewall_enable(self, value): |
| 108 | self.set_attribute("firewall_enable", value) |
| 109 | |
| 110 | @property |
| 111 | def firewall_rules(self): |
| 112 | return self.get_attribute("firewall_rules", self.default_attributes["firewall_rules"]) |
| 113 | |
| 114 | @firewall_rules.setter |
| 115 | def firewall_rules(self, value): |
| 116 | self.set_attribute("firewall_rules", value) |
| 117 | |
| 118 | @property |
| 119 | def url_filter_enable(self): |
| 120 | return self.get_attribute("url_filter_enable", self.default_attributes["url_filter_enable"]) |
| 121 | |
| 122 | @url_filter_enable.setter |
| 123 | def url_filter_enable(self, value): |
| 124 | self.set_attribute("url_filter_enable", value) |
| 125 | |
| 126 | @property |
| 127 | def url_filter_level(self): |
| 128 | return self.get_attribute("url_filter_level", self.default_attributes["url_filter_level"]) |
| 129 | |
| 130 | @url_filter_level.setter |
| 131 | def url_filter_level(self, value): |
| 132 | self.set_attribute("url_filter_level", value) |
| 133 | |
| 134 | @property |
| 135 | def url_filter_rules(self): |
| 136 | return self.get_attribute("url_filter_rules", self.default_attributes["url_filter_rules"]) |
| 137 | |
| 138 | @url_filter_rules.setter |
| 139 | def url_filter_rules(self, value): |
| 140 | self.set_attribute("url_filter_rules", value) |
| 141 | |
| 142 | @property |
| 143 | def cdn_enable(self): |
| 144 | return self.get_attribute("cdn_enable", self.default_attributes["cdn_enable"]) |
| 145 | |
| 146 | @cdn_enable.setter |
| 147 | def cdn_enable(self, value): |
| 148 | self.set_attribute("cdn_enable", value) |
| 149 | |
| 150 | @property |
| 151 | def users(self): |
| 152 | return self.get_attribute("users", self.default_attributes["users"]) |
| 153 | |
| 154 | @users.setter |
| 155 | def users(self, value): |
| 156 | self.set_attribute("users", value) |
| 157 | |
| 158 | def find_user(self, uid): |
| 159 | uid = int(uid) |
| 160 | for user in self.users: |
| 161 | if user["id"] == uid: |
| 162 | return user |
| 163 | return None |
| 164 | |
| 165 | def update_user(self, uid, **kwargs): |
| 166 | # kwargs may be "level" or "mac" |
| 167 | # Setting one of these to None will cause None to be stored in the db |
| 168 | uid = int(uid) |
| 169 | users = self.users |
| 170 | for user in users: |
| 171 | if user["id"] == uid: |
| 172 | for arg in kwargs.keys(): |
| 173 | user[arg] = kwargs[arg] |
| 174 | self.users = users |
| 175 | return user |
| 176 | raise ValueError("User %d not found" % uid) |
| 177 | |
| 178 | def create_user(self, **kwargs): |
| 179 | if "name" not in kwargs: |
| 180 | raise XOSMissingField("The name field is required") |
| 181 | |
| 182 | for user in self.users: |
| 183 | if kwargs["name"] == user["name"]: |
| 184 | raise XOSDuplicateKey("User %s already exists" % kwargs["name"]) |
| 185 | |
| 186 | uids = [x["id"] for x in self.users] |
| 187 | if uids: |
| 188 | uid = max(uids)+1 |
| 189 | else: |
| 190 | uid = 0 |
| 191 | newuser = kwargs.copy() |
| 192 | newuser["id"] = uid |
| 193 | |
| 194 | users = self.users |
| 195 | users.append(newuser) |
| 196 | self.users = users |
| 197 | |
| 198 | return newuser |
| 199 | |
| 200 | def delete_user(self, uid): |
| 201 | uid = int(uid) |
| 202 | users = self.users |
| 203 | for user in users: |
| 204 | if user["id"]==uid: |
| 205 | users.remove(user) |
| 206 | self.users = users |
| 207 | return |
| 208 | |
| 209 | raise ValueError("Users %d not found" % uid) |
| 210 | |
| 211 | @property |
| 212 | def services(self): |
| 213 | return {"cdn": self.cdn_enable, |
| 214 | "url_filter": self.url_filter_enable, |
| 215 | "firewall": self.firewall_enable} |
| 216 | |
| 217 | @services.setter |
| 218 | def services(self, value): |
| 219 | pass |
Scott Baker | d913334 | 2015-07-06 14:38:02 -0700 | [diff] [blame] | 220 | |
Scott Baker | 050f8b3 | 2015-07-07 12:15:03 -0700 | [diff] [blame] | 221 | def save(self, *args, **kwargs): |
Scott Baker | fe38c2a | 2015-07-24 18:56:57 -0700 | [diff] [blame] | 222 | if (not hasattr(self, 'caller') or not self.caller.is_admin): |
| 223 | if (self.has_field_changed("service_specific_id")): |
| 224 | raise XOSPermissionDenied("You do not have permission to change service_specific_id") |
Scott Baker | 050f8b3 | 2015-07-07 12:15:03 -0700 | [diff] [blame] | 225 | super(CordSubscriberRoot, self).save(*args, **kwargs) |
| 226 | if (self.volt) and (self.volt.vcpe): # and (self._initial_url_filter_enabled != self.url_filter_enable): |
| 227 | # 1) trigger manage_bbs_account to run |
| 228 | # 2) trigger vcpe observer to wake up |
| 229 | self.volt.vcpe.save() |
| 230 | |
Scott Baker | 126ad47 | 2015-07-07 17:59:44 -0700 | [diff] [blame] | 231 | @property |
| 232 | def is_demo_user(self): |
| 233 | return self.get_attribute("is_demo_user", self.default_attributes["is_demo_user"]) |
| 234 | |
| 235 | @is_demo_user.setter |
| 236 | def is_demo_user(self, value): |
| 237 | self.set_attribute("is_demo_user", value) |
| 238 | |
Scott Baker | d4b48c0 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 239 | # ------------------------------------------- |
| 240 | # VOLT |
| 241 | # ------------------------------------------- |
| 242 | |
| 243 | class VOLTService(Service): |
Scott Baker | d913334 | 2015-07-06 14:38:02 -0700 | [diff] [blame] | 244 | KIND = VOLT_KIND |
Scott Baker | d4b48c0 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 245 | |
Scott Baker | e9ff7ce | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 246 | class Meta: |
Scott Baker | d4b48c0 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 247 | app_label = "cord" |
| 248 | verbose_name = "vOLT Service" |
| 249 | proxy = True |
| 250 | |
| 251 | class VOLTTenant(Tenant): |
| 252 | class Meta: |
| 253 | proxy = True |
| 254 | |
Scott Baker | d913334 | 2015-07-06 14:38:02 -0700 | [diff] [blame] | 255 | KIND = VOLT_KIND |
Scott Baker | d4b48c0 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 256 | |
Scott Baker | f1527cd | 2015-11-09 20:25:21 -0800 | [diff] [blame] | 257 | default_attributes = {"vlan_id": None, "s_tag": None, "c_tag": None} |
Scott Baker | 323eca9 | 2015-04-20 09:48:34 -0700 | [diff] [blame] | 258 | def __init__(self, *args, **kwargs): |
| 259 | volt_services = VOLTService.get_service_objects().all() |
| 260 | if volt_services: |
| 261 | self._meta.get_field("provider_service").default = volt_services[0].id |
| 262 | super(VOLTTenant, self).__init__(*args, **kwargs) |
Scott Baker | d913334 | 2015-07-06 14:38:02 -0700 | [diff] [blame] | 263 | self.cached_vcpe = None |
Scott Baker | 868dabf | 2015-06-24 12:54:24 -0700 | [diff] [blame] | 264 | |
Scott Baker | d4b48c0 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 265 | @property |
Scott Baker | f1527cd | 2015-11-09 20:25:21 -0800 | [diff] [blame] | 266 | def s_tag(self): |
| 267 | return self.get_attribute("s_tag", self.default_attributes["s_tag"]) |
| 268 | |
| 269 | @s_tag.setter |
| 270 | def s_tag(self, value): |
| 271 | self.set_attribute("s_tag", value) |
| 272 | |
| 273 | @property |
| 274 | def c_tag(self): |
| 275 | return self.get_attribute("c_tag", self.default_attributes["c_tag"]) |
| 276 | |
| 277 | @c_tag.setter |
| 278 | def c_tag(self, value): |
| 279 | self.set_attribute("c_tag", value) |
| 280 | |
| 281 | # for now, vlan_id is a synonym for c_tag |
| 282 | |
| 283 | @property |
Scott Baker | 679f702 | 2015-04-20 11:50:09 -0700 | [diff] [blame] | 284 | def vlan_id(self): |
Scott Baker | f1527cd | 2015-11-09 20:25:21 -0800 | [diff] [blame] | 285 | return self.c_tag |
Scott Baker | 679f702 | 2015-04-20 11:50:09 -0700 | [diff] [blame] | 286 | |
| 287 | @vlan_id.setter |
| 288 | def vlan_id(self, value): |
Scott Baker | f1527cd | 2015-11-09 20:25:21 -0800 | [diff] [blame] | 289 | self.c_tag = value |
Scott Baker | 679f702 | 2015-04-20 11:50:09 -0700 | [diff] [blame] | 290 | |
| 291 | @property |
Scott Baker | d4b48c0 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 292 | def vcpe(self): |
Scott Baker | d913334 | 2015-07-06 14:38:02 -0700 | [diff] [blame] | 293 | vcpe = self.get_newest_subscribed_tenant(VCPETenant) |
| 294 | if not vcpe: |
| 295 | return None |
| 296 | |
| 297 | # always return the same object when possible |
| 298 | if (self.cached_vcpe) and (self.cached_vcpe.id == vcpe.id): |
Scott Baker | fa5cdd6 | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 299 | return self.cached_vcpe |
Scott Baker | d913334 | 2015-07-06 14:38:02 -0700 | [diff] [blame] | 300 | |
Scott Baker | c633dc9 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 301 | vcpe.caller = self.creator |
Scott Baker | fa5cdd6 | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 302 | self.cached_vcpe = vcpe |
| 303 | return vcpe |
Scott Baker | d4b48c0 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 304 | |
| 305 | @vcpe.setter |
| 306 | def vcpe(self, value): |
Scott Baker | e4364d1 | 2015-07-06 17:21:21 -0700 | [diff] [blame] | 307 | raise XOSConfigurationError("vOLT.vCPE cannot be set this way -- create a new vCPE object and set its subscriber_tenant instead") |
| 308 | |
| 309 | @property |
| 310 | def subscriber(self): |
| 311 | if not self.subscriber_root: |
| 312 | return None |
| 313 | subs = CordSubscriberRoot.objects.filter(id=self.subscriber_root.id) |
| 314 | if not subs: |
| 315 | return None |
| 316 | return subs[0] |
Scott Baker | d4b48c0 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 317 | |
Scott Baker | c633dc9 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 318 | @property |
| 319 | def creator(self): |
| 320 | if getattr(self, "cached_creator", None): |
| 321 | return self.cached_creator |
| 322 | creator_id=self.get_attribute("creator_id") |
| 323 | if not creator_id: |
| 324 | return None |
| 325 | users=User.objects.filter(id=creator_id) |
| 326 | if not users: |
| 327 | return None |
| 328 | user=users[0] |
| 329 | self.cached_creator = users[0] |
| 330 | return user |
| 331 | |
| 332 | @creator.setter |
| 333 | def creator(self, value): |
| 334 | if value: |
| 335 | value = value.id |
| 336 | if (value != self.get_attribute("creator_id", None)): |
| 337 | self.cached_creator=None |
| 338 | self.set_attribute("creator_id", value) |
| 339 | |
Scott Baker | d4b48c0 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 340 | def manage_vcpe(self): |
| 341 | # Each VOLT object owns exactly one VCPE object |
| 342 | |
| 343 | if self.deleted: |
| 344 | return |
| 345 | |
| 346 | if self.vcpe is None: |
| 347 | vcpeServices = VCPEService.get_service_objects().all() |
| 348 | if not vcpeServices: |
Scott Baker | 7f8ef8f | 2015-04-20 14:24:29 -0700 | [diff] [blame] | 349 | raise XOSConfigurationError("No VCPE Services available") |
Scott Baker | d4b48c0 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 350 | |
| 351 | vcpe = VCPETenant(provider_service = vcpeServices[0], |
| 352 | subscriber_tenant = self) |
Scott Baker | c633dc9 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 353 | vcpe.caller = self.creator |
Scott Baker | d4b48c0 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 354 | vcpe.save() |
| 355 | |
Scott Baker | 050f8b3 | 2015-07-07 12:15:03 -0700 | [diff] [blame] | 356 | def manage_subscriber(self): |
| 357 | if (self.subscriber_root is None): |
| 358 | # The vOLT is not connected to a Subscriber, so either find an |
| 359 | # existing subscriber with the same SSID, or autogenerate a new |
| 360 | # subscriber. |
| 361 | # |
| 362 | # TODO: This probably goes away when we rethink the ONOS-to-XOS |
| 363 | # vOLT API. |
| 364 | |
| 365 | subs = CordSubscriberRoot.get_tenant_objects().filter(service_specific_id = self.service_specific_id) |
| 366 | if subs: |
| 367 | sub = subs[0] |
| 368 | else: |
| 369 | sub = CordSubscriberRoot(service_specific_id = self.service_specific_id, |
| 370 | name = "autogenerated-for-vOLT-%s" % self.id) |
| 371 | sub.save() |
| 372 | self.subscriber_root = sub |
| 373 | self.save() |
| 374 | |
Scott Baker | d4b48c0 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 375 | def cleanup_vcpe(self): |
| 376 | if self.vcpe: |
Scott Baker | d913334 | 2015-07-06 14:38:02 -0700 | [diff] [blame] | 377 | # print "XXX cleanup vcpe", self.vcpe |
Scott Baker | d4b48c0 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 378 | self.vcpe.delete() |
Scott Baker | d4b48c0 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 379 | |
Scott Baker | 868dabf | 2015-06-24 12:54:24 -0700 | [diff] [blame] | 380 | def cleanup_orphans(self): |
Scott Baker | d913334 | 2015-07-06 14:38:02 -0700 | [diff] [blame] | 381 | # ensure vOLT only has one vCPE |
| 382 | cur_vcpe = self.vcpe |
| 383 | for vcpe in list(self.get_subscribed_tenants(VCPETenant)): |
| 384 | if (not cur_vcpe) or (vcpe.id != cur_vcpe.id): |
| 385 | # print "XXX clean up orphaned vcpe", vcpe |
| 386 | vcpe.delete() |
Scott Baker | 868dabf | 2015-06-24 12:54:24 -0700 | [diff] [blame] | 387 | |
Scott Baker | d4b48c0 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 388 | def save(self, *args, **kwargs): |
Scott Baker | 7f8ef8f | 2015-04-20 14:24:29 -0700 | [diff] [blame] | 389 | self.validate_unique_service_specific_id() |
| 390 | |
Scott Baker | 050f8b3 | 2015-07-07 12:15:03 -0700 | [diff] [blame] | 391 | if (self.subscriber_root is not None): |
| 392 | subs = self.subscriber_root.get_subscribed_tenants(VOLTTenant) |
| 393 | if (subs) and (self not in subs): |
| 394 | raise XOSDuplicateKey("Subscriber should only be linked to one vOLT") |
| 395 | |
Scott Baker | c633dc9 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 396 | if not self.creator: |
| 397 | if not getattr(self, "caller", None): |
| 398 | # caller must be set when creating a vCPE since it creates a slice |
| 399 | raise XOSProgrammingError("VOLTTenant's self.caller was not set") |
| 400 | self.creator = self.caller |
| 401 | if not self.creator: |
| 402 | raise XOSProgrammingError("VOLTTenant's self.creator was not set") |
| 403 | |
Scott Baker | d4b48c0 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 404 | super(VOLTTenant, self).save(*args, **kwargs) |
Scott Baker | 192da22 | 2015-07-08 19:01:56 -0700 | [diff] [blame] | 405 | model_policy_volt(self.pk) |
| 406 | #self.manage_vcpe() |
| 407 | #self.manage_subscriber() |
| 408 | #self.cleanup_orphans() |
Scott Baker | d4b48c0 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 409 | |
| 410 | def delete(self, *args, **kwargs): |
| 411 | self.cleanup_vcpe() |
| 412 | super(VOLTTenant, self).delete(*args, **kwargs) |
| 413 | |
Scott Baker | 192da22 | 2015-07-08 19:01:56 -0700 | [diff] [blame] | 414 | def model_policy_volt(pk): |
| 415 | # TODO: this should be made in to a real model_policy |
| 416 | with transaction.atomic(): |
| 417 | volt = VOLTTenant.objects.select_for_update().filter(pk=pk) |
| 418 | if not volt: |
| 419 | return |
| 420 | volt = volt[0] |
| 421 | volt.manage_vcpe() |
| 422 | volt.manage_subscriber() |
| 423 | volt.cleanup_orphans() |
| 424 | |
Scott Baker | d4b48c0 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 425 | # ------------------------------------------- |
| 426 | # VCPE |
| 427 | # ------------------------------------------- |
| 428 | |
| 429 | class VCPEService(Service): |
Scott Baker | d913334 | 2015-07-06 14:38:02 -0700 | [diff] [blame] | 430 | KIND = VCPE_KIND |
Scott Baker | d4b48c0 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 431 | |
Scott Baker | 2b56cf4 | 2015-07-15 18:08:06 -0700 | [diff] [blame] | 432 | simple_attributes = ( ("bbs_api_hostname", None), |
| 433 | ("bbs_api_port", None), |
Scott Baker | 80157da | 2015-07-15 17:42:43 -0700 | [diff] [blame] | 434 | ("bbs_server", None), |
Scott Baker | d4829ea | 2015-07-15 18:04:22 -0700 | [diff] [blame] | 435 | ("backend_network_label", "hpc_client"), ) |
Scott Baker | 6231297 | 2015-07-13 14:30:25 -0700 | [diff] [blame] | 436 | |
| 437 | def __init__(self, *args, **kwargs): |
| 438 | super(VCPEService, self).__init__(*args, **kwargs) |
| 439 | |
Scott Baker | d4b48c0 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 440 | class Meta: |
| 441 | app_label = "cord" |
Scott Baker | e9ff7ce | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 442 | verbose_name = "vCPE Service" |
Scott Baker | d4b48c0 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 443 | proxy = True |
Scott Baker | e9ff7ce | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 444 | |
Scott Baker | 4e6d60d | 2015-05-20 20:45:11 -0700 | [diff] [blame] | 445 | def allocate_bbs_account(self): |
| 446 | vcpes = VCPETenant.get_tenant_objects().all() |
| 447 | bbs_accounts = [vcpe.bbs_account for vcpe in vcpes] |
| 448 | |
| 449 | # There's a bit of a race here; some other user could be trying to |
| 450 | # allocate a bbs_account at the same time we are. |
| 451 | |
Scott Baker | 2f0b346 | 2015-06-09 12:03:56 -0700 | [diff] [blame] | 452 | for i in range(2,21): |
Scott Baker | 4e6d60d | 2015-05-20 20:45:11 -0700 | [diff] [blame] | 453 | account_name = "bbs%02d@onlab.us" % i |
| 454 | if (account_name not in bbs_accounts): |
| 455 | return account_name |
| 456 | |
| 457 | raise XOSConfigurationError("We've run out of available broadbandshield accounts. Delete some vcpe and try again.") |
| 458 | |
Scott Baker | 80157da | 2015-07-15 17:42:43 -0700 | [diff] [blame] | 459 | @property |
| 460 | def bbs_slice(self): |
| 461 | bbs_slice_id=self.get_attribute("bbs_slice_id") |
| 462 | if not bbs_slice_id: |
| 463 | return None |
| 464 | bbs_slices=Slice.objects.filter(id=bbs_slice_id) |
| 465 | if not bbs_slices: |
| 466 | return None |
| 467 | return bbs_slices[0] |
| 468 | |
| 469 | @bbs_slice.setter |
| 470 | def bbs_slice(self, value): |
| 471 | if value: |
| 472 | value = value.id |
| 473 | self.set_attribute("bbs_slice_id", value) |
| 474 | |
Scott Baker | 6231297 | 2015-07-13 14:30:25 -0700 | [diff] [blame] | 475 | VCPEService.setup_simple_attributes() |
| 476 | |
| 477 | |
Scott Baker | c1584b8 | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 478 | class VCPETenant(TenantWithContainer): |
Scott Baker | e9ff7ce | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 479 | class Meta: |
| 480 | proxy = True |
| 481 | |
Scott Baker | d913334 | 2015-07-06 14:38:02 -0700 | [diff] [blame] | 482 | KIND = VCPE_KIND |
Scott Baker | d4b48c0 | 2015-04-15 20:59:15 -0700 | [diff] [blame] | 483 | |
Scott Baker | 92a81d4 | 2015-09-09 10:57:16 -0700 | [diff] [blame] | 484 | sync_attributes = ("nat_ip", "nat_mac", |
| 485 | "lan_ip", "lan_mac", |
Andy Bavier | 733733d | 2015-10-29 14:01:47 -0400 | [diff] [blame] | 486 | "wan_ip", "wan_mac", "wan_container_mac", |
Scott Baker | 92a81d4 | 2015-09-09 10:57:16 -0700 | [diff] [blame] | 487 | "private_ip", "private_mac", |
| 488 | "hpc_client_ip", "hpc_client_mac") |
Scott Baker | c633dc9 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 489 | |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 490 | default_attributes = {"instance_id": None, |
Scott Baker | 4b20697 | 2015-11-02 20:55:07 -0800 | [diff] [blame] | 491 | "container_id": None, |
Scott Baker | 4e6d60d | 2015-05-20 20:45:11 -0700 | [diff] [blame] | 492 | "users": [], |
Scott Baker | 4aa660e | 2015-06-09 12:22:29 -0700 | [diff] [blame] | 493 | "bbs_account": None, |
| 494 | "last_ansible_hash": None} |
Scott Baker | e9ff7ce | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 495 | |
Scott Baker | fa5cdd6 | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 496 | def __init__(self, *args, **kwargs): |
| 497 | super(VCPETenant, self).__init__(*args, **kwargs) |
| 498 | self.cached_vbng=None |
Scott Baker | c633dc9 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 499 | |
| 500 | @property |
Scott Baker | fa5cdd6 | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 501 | def vbng(self): |
Scott Baker | d913334 | 2015-07-06 14:38:02 -0700 | [diff] [blame] | 502 | vbng = self.get_newest_subscribed_tenant(VBNGTenant) |
| 503 | if not vbng: |
| 504 | return None |
| 505 | |
| 506 | # always return the same object when possible |
| 507 | if (self.cached_vbng) and (self.cached_vbng.id == vbng.id): |
Scott Baker | fa5cdd6 | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 508 | return self.cached_vbng |
Scott Baker | d913334 | 2015-07-06 14:38:02 -0700 | [diff] [blame] | 509 | |
Scott Baker | c633dc9 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 510 | vbng.caller = self.creator |
Scott Baker | fa5cdd6 | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 511 | self.cached_vbng = vbng |
| 512 | return vbng |
| 513 | |
| 514 | @vbng.setter |
| 515 | def vbng(self, value): |
Scott Baker | d913334 | 2015-07-06 14:38:02 -0700 | [diff] [blame] | 516 | raise XOSConfigurationError("vCPE.vBNG cannot be set this way -- create a new vBNG object and set it's subscriber_tenant instead") |
Scott Baker | e9ff7ce | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 517 | |
Scott Baker | e4364d1 | 2015-07-06 17:21:21 -0700 | [diff] [blame] | 518 | @property |
| 519 | def volt(self): |
| 520 | if not self.subscriber_tenant: |
| 521 | return None |
| 522 | volts = VOLTTenant.objects.filter(id=self.subscriber_tenant.id) |
| 523 | if not volts: |
| 524 | return None |
| 525 | return volts[0] |
| 526 | |
Scott Baker | 81de640 | 2015-07-06 16:50:30 -0700 | [diff] [blame] | 527 | @property |
| 528 | def bbs_account(self): |
| 529 | return self.get_attribute("bbs_account", self.default_attributes["bbs_account"]) |
| 530 | |
| 531 | @bbs_account.setter |
| 532 | def bbs_account(self, value): |
| 533 | return self.set_attribute("bbs_account", value) |
| 534 | |
| 535 | @property |
| 536 | def last_ansible_hash(self): |
| 537 | return self.get_attribute("last_ansible_hash", self.default_attributes["last_ansible_hash"]) |
| 538 | |
| 539 | @last_ansible_hash.setter |
| 540 | def last_ansible_hash(self, value): |
| 541 | return self.set_attribute("last_ansible_hash", value) |
| 542 | |
| 543 | @property |
| 544 | def ssh_command(self): |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 545 | if self.instance: |
| 546 | return self.instance.get_ssh_command() |
Scott Baker | 81de640 | 2015-07-06 16:50:30 -0700 | [diff] [blame] | 547 | else: |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 548 | return "no-instance" |
Scott Baker | 81de640 | 2015-07-06 16:50:30 -0700 | [diff] [blame] | 549 | |
| 550 | @ssh_command.setter |
| 551 | def ssh_command(self, value): |
| 552 | pass |
| 553 | |
Scott Baker | 5571c69 | 2015-05-20 08:19:25 -0700 | [diff] [blame] | 554 | @property |
| 555 | def addresses(self): |
Scott Baker | 4b20697 | 2015-11-02 20:55:07 -0800 | [diff] [blame] | 556 | if self.instance: |
| 557 | ports = self.instance.ports.all() |
| 558 | elif self.container: |
| 559 | ports = self.container.ports.all() |
| 560 | else: |
Scott Baker | 5571c69 | 2015-05-20 08:19:25 -0700 | [diff] [blame] | 561 | return {} |
| 562 | |
| 563 | addresses = {} |
Scott Baker | 4b20697 | 2015-11-02 20:55:07 -0800 | [diff] [blame] | 564 | for ns in ports: |
Scott Baker | 5571c69 | 2015-05-20 08:19:25 -0700 | [diff] [blame] | 565 | if "lan" in ns.network.name.lower(): |
Scott Baker | 92a81d4 | 2015-09-09 10:57:16 -0700 | [diff] [blame] | 566 | addresses["lan"] = (ns.ip, ns.mac) |
Scott Baker | 5571c69 | 2015-05-20 08:19:25 -0700 | [diff] [blame] | 567 | elif "wan" in ns.network.name.lower(): |
Scott Baker | 92a81d4 | 2015-09-09 10:57:16 -0700 | [diff] [blame] | 568 | addresses["wan"] = (ns.ip, ns.mac) |
Scott Baker | 5571c69 | 2015-05-20 08:19:25 -0700 | [diff] [blame] | 569 | elif "private" in ns.network.name.lower(): |
Scott Baker | 92a81d4 | 2015-09-09 10:57:16 -0700 | [diff] [blame] | 570 | addresses["private"] = (ns.ip, ns.mac) |
Scott Baker | 5571c69 | 2015-05-20 08:19:25 -0700 | [diff] [blame] | 571 | elif "nat" in ns.network.name.lower(): |
Scott Baker | 92a81d4 | 2015-09-09 10:57:16 -0700 | [diff] [blame] | 572 | addresses["nat"] = (ns.ip, ns.mac) |
Scott Baker | 281b1af | 2015-06-04 10:26:44 -0700 | [diff] [blame] | 573 | elif "hpc_client" in ns.network.name.lower(): |
Scott Baker | 92a81d4 | 2015-09-09 10:57:16 -0700 | [diff] [blame] | 574 | addresses["hpc_client"] = (ns.ip, ns.mac) |
Scott Baker | 5571c69 | 2015-05-20 08:19:25 -0700 | [diff] [blame] | 575 | return addresses |
| 576 | |
Scott Baker | 432d140 | 2015-05-20 08:25:29 -0700 | [diff] [blame] | 577 | @property |
| 578 | def nat_ip(self): |
Scott Baker | 92a81d4 | 2015-09-09 10:57:16 -0700 | [diff] [blame] | 579 | return self.addresses.get("nat", (None,None) )[0] |
| 580 | |
| 581 | @property |
| 582 | def nat_mac(self): |
| 583 | return self.addresses.get("nat", (None,None) )[1] |
Scott Baker | 432d140 | 2015-05-20 08:25:29 -0700 | [diff] [blame] | 584 | |
| 585 | @property |
| 586 | def lan_ip(self): |
Scott Baker | 92a81d4 | 2015-09-09 10:57:16 -0700 | [diff] [blame] | 587 | return self.addresses.get("lan", (None, None) )[0] |
| 588 | |
| 589 | @property |
| 590 | def lan_mac(self): |
| 591 | return self.addresses.get("lan", (None, None) )[1] |
Scott Baker | 432d140 | 2015-05-20 08:25:29 -0700 | [diff] [blame] | 592 | |
| 593 | @property |
| 594 | def wan_ip(self): |
Scott Baker | 92a81d4 | 2015-09-09 10:57:16 -0700 | [diff] [blame] | 595 | return self.addresses.get("wan", (None, None) )[0] |
Scott Baker | 432d140 | 2015-05-20 08:25:29 -0700 | [diff] [blame] | 596 | |
| 597 | @property |
Scott Baker | 4f97259 | 2015-06-05 12:08:34 -0700 | [diff] [blame] | 598 | def wan_mac(self): |
Andy Bavier | 4c7e76d | 2015-09-09 18:06:30 -0400 | [diff] [blame] | 599 | return self.addresses.get("wan", (None, None) )[1] |
Scott Baker | 4f97259 | 2015-06-05 12:08:34 -0700 | [diff] [blame] | 600 | |
Andy Bavier | 733733d | 2015-10-29 14:01:47 -0400 | [diff] [blame] | 601 | # Generate the MAC for the container interface connected to WAN |
| 602 | @property |
| 603 | def wan_container_mac(self): |
| 604 | (a, b, c, d) = self.wan_ip.split('.') |
| 605 | return "02:42:%02x:%02x:%02x:%02x" % (int(a), int(b), int(c), int(d)) |
| 606 | |
Scott Baker | 4f97259 | 2015-06-05 12:08:34 -0700 | [diff] [blame] | 607 | @property |
Scott Baker | 432d140 | 2015-05-20 08:25:29 -0700 | [diff] [blame] | 608 | def private_ip(self): |
Scott Baker | 92a81d4 | 2015-09-09 10:57:16 -0700 | [diff] [blame] | 609 | return self.addresses.get("private", (None, None) )[0] |
| 610 | |
| 611 | @property |
| 612 | def private_mac(self): |
| 613 | return self.addresses.get("private", (None, None) )[1] |
Scott Baker | 432d140 | 2015-05-20 08:25:29 -0700 | [diff] [blame] | 614 | |
Scott Baker | 281b1af | 2015-06-04 10:26:44 -0700 | [diff] [blame] | 615 | @property |
| 616 | def hpc_client_ip(self): |
Scott Baker | 92a81d4 | 2015-09-09 10:57:16 -0700 | [diff] [blame] | 617 | return self.addresses.get("hpc_client", (None, None) )[0] |
| 618 | |
| 619 | @property |
| 620 | def hpc_client_mac(self): |
| 621 | return self.addresses.get("hpc_client", (None, None) )[1] |
Scott Baker | 281b1af | 2015-06-04 10:26:44 -0700 | [diff] [blame] | 622 | |
Scott Baker | d517a10 | 2015-06-09 12:30:30 -0700 | [diff] [blame] | 623 | @property |
| 624 | def is_synced(self): |
| 625 | return (self.enacted is not None) and (self.enacted >= self.updated) |
| 626 | |
| 627 | @is_synced.setter |
| 628 | def is_synced(self, value): |
| 629 | pass |
| 630 | |
Scott Baker | fa5cdd6 | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 631 | def manage_vbng(self): |
| 632 | # Each vCPE object owns exactly one vBNG object |
| 633 | |
| 634 | if self.deleted: |
| 635 | return |
| 636 | |
| 637 | if self.vbng is None: |
| 638 | vbngServices = VBNGService.get_service_objects().all() |
| 639 | if not vbngServices: |
Scott Baker | 7f8ef8f | 2015-04-20 14:24:29 -0700 | [diff] [blame] | 640 | raise XOSConfigurationError("No VBNG Services available") |
Scott Baker | fa5cdd6 | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 641 | |
| 642 | vbng = VBNGTenant(provider_service = vbngServices[0], |
| 643 | subscriber_tenant = self) |
Scott Baker | c633dc9 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 644 | vbng.caller = self.creator |
Scott Baker | fa5cdd6 | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 645 | vbng.save() |
| 646 | |
Scott Baker | fa5cdd6 | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 647 | def cleanup_vbng(self): |
| 648 | if self.vbng: |
Scott Baker | d913334 | 2015-07-06 14:38:02 -0700 | [diff] [blame] | 649 | # print "XXX cleanup vnbg", self.vbng |
Scott Baker | fa5cdd6 | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 650 | self.vbng.delete() |
Scott Baker | fa5cdd6 | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 651 | |
Scott Baker | 868dabf | 2015-06-24 12:54:24 -0700 | [diff] [blame] | 652 | def cleanup_orphans(self): |
Scott Baker | d913334 | 2015-07-06 14:38:02 -0700 | [diff] [blame] | 653 | # ensure vCPE only has one vBNG |
| 654 | cur_vbng = self.vbng |
| 655 | for vbng in list(self.get_subscribed_tenants(VBNGTenant)): |
| 656 | if (not cur_vbng) or (vbng.id != cur_vbng.id): |
| 657 | # print "XXX clean up orphaned vbng", vbng |
| 658 | vbng.delete() |
Scott Baker | 868dabf | 2015-06-24 12:54:24 -0700 | [diff] [blame] | 659 | |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 660 | if self.orig_instance_id and (self.orig_instance_id != self.get_attribute("instance_id")): |
| 661 | instances=Instance.objects.filter(id=self.orig_instance_id) |
| 662 | if instances: |
| 663 | # print "XXX clean up orphaned instance", instances[0] |
| 664 | instances[0].delete() |
Scott Baker | 868dabf | 2015-06-24 12:54:24 -0700 | [diff] [blame] | 665 | |
Scott Baker | 4e6d60d | 2015-05-20 20:45:11 -0700 | [diff] [blame] | 666 | def manage_bbs_account(self): |
| 667 | if self.deleted: |
| 668 | return |
| 669 | |
Scott Baker | 126ad47 | 2015-07-07 17:59:44 -0700 | [diff] [blame] | 670 | if self.volt and self.volt.subscriber and self.volt.subscriber.url_filter_enable: |
Scott Baker | 642126f | 2015-05-20 20:57:28 -0700 | [diff] [blame] | 671 | if not self.bbs_account: |
| 672 | # make sure we use the proxied VCPEService object, not the generic Service object |
| 673 | vcpe_service = VCPEService.objects.get(id=self.provider_service.id) |
Scott Baker | 050f8b3 | 2015-07-07 12:15:03 -0700 | [diff] [blame] | 674 | self.bbs_account = vcpe_service.allocate_bbs_account() |
Scott Baker | 642126f | 2015-05-20 20:57:28 -0700 | [diff] [blame] | 675 | super(VCPETenant, self).save() |
| 676 | else: |
| 677 | if self.bbs_account: |
| 678 | self.bbs_account = None |
| 679 | super(VCPETenant, self).save() |
Scott Baker | 4e6d60d | 2015-05-20 20:45:11 -0700 | [diff] [blame] | 680 | |
Scott Baker | 839daa8 | 2015-11-16 22:53:49 -0800 | [diff] [blame] | 681 | def find_or_make_port(self, instance, network, **kwargs): |
| 682 | port = Port.objects.filter(instance=instance, network=network) |
| 683 | if port: |
| 684 | port = port[0] |
| 685 | else: |
| 686 | port = Port(instance=instance, network=network, **kwargs) |
| 687 | port.save() |
| 688 | return port |
| 689 | |
| 690 | def save_instance(self, instance): |
| 691 | with transaction.atomic(): |
Andy Bavier | 1aa49af | 2015-12-16 14:10:01 -0500 | [diff] [blame] | 692 | instance.volumes = "/etc/dnsmasq.d,/etc/ufw" |
Scott Baker | 839daa8 | 2015-11-16 22:53:49 -0800 | [diff] [blame] | 693 | super(VCPETenant, self).save_instance(instance) |
| 694 | |
| 695 | if instance.isolation in ["container", "container_vm"]: |
| 696 | lan_networks = [x for x in instance.slice.networks.all() if "lan" in x.name] |
| 697 | if not lan_networks: |
| 698 | raise XOSProgrammingError("No lan_network") |
| 699 | port = self.find_or_make_port(instance, lan_networks[0], ip="192.168.0.1", port_id="unmanaged") |
| 700 | port.set_parameter("c_tag", self.volt.c_tag) |
| 701 | port.set_parameter("s_tag", self.volt.s_tag) |
| 702 | port.set_parameter("device", "eth1") |
Scott Baker | e2920b2 | 2015-11-19 16:51:27 -0800 | [diff] [blame] | 703 | port.set_parameter("bridge", "br-lan") |
Scott Baker | 839daa8 | 2015-11-16 22:53:49 -0800 | [diff] [blame] | 704 | |
| 705 | wan_networks = [x for x in instance.slice.networks.all() if "wan" in x.name] |
| 706 | if not wan_networks: |
| 707 | raise XOSProgrammingError("No wan_network") |
| 708 | port = self.find_or_make_port(instance, wan_networks[0]) |
| 709 | port.set_parameter("next_hop", value="10.0.1.253") # FIX ME |
| 710 | port.set_parameter("device", "eth0") |
| 711 | |
Scott Baker | e9ff7ce | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 712 | def save(self, *args, **kwargs): |
Scott Baker | c633dc9 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 713 | if not self.creator: |
| 714 | if not getattr(self, "caller", None): |
| 715 | # caller must be set when creating a vCPE since it creates a slice |
Scott Baker | f6790db | 2015-05-06 15:46:34 -0700 | [diff] [blame] | 716 | raise XOSProgrammingError("VCPETenant's self.caller was not set") |
Scott Baker | c633dc9 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 717 | self.creator = self.caller |
| 718 | if not self.creator: |
Scott Baker | f6790db | 2015-05-06 15:46:34 -0700 | [diff] [blame] | 719 | raise XOSProgrammingError("VCPETenant's self.creator was not set") |
Scott Baker | c633dc9 | 2015-05-05 17:49:46 -0700 | [diff] [blame] | 720 | |
Scott Baker | e9ff7ce | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 721 | super(VCPETenant, self).save(*args, **kwargs) |
Scott Baker | 192da22 | 2015-07-08 19:01:56 -0700 | [diff] [blame] | 722 | model_policy_vcpe(self.pk) |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 723 | #self.manage_instance() |
Scott Baker | 192da22 | 2015-07-08 19:01:56 -0700 | [diff] [blame] | 724 | #self.manage_vbng() |
| 725 | #self.manage_bbs_account() |
| 726 | #self.cleanup_orphans() |
Scott Baker | e9ff7ce | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 727 | |
| 728 | def delete(self, *args, **kwargs): |
Scott Baker | fa5cdd6 | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 729 | self.cleanup_vbng() |
Scott Baker | c1584b8 | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 730 | self.cleanup_container() |
Scott Baker | e9ff7ce | 2015-04-14 17:19:16 -0700 | [diff] [blame] | 731 | super(VCPETenant, self).delete(*args, **kwargs) |
| 732 | |
Scott Baker | 192da22 | 2015-07-08 19:01:56 -0700 | [diff] [blame] | 733 | def model_policy_vcpe(pk): |
| 734 | # TODO: this should be made in to a real model_policy |
| 735 | with transaction.atomic(): |
| 736 | vcpe = VCPETenant.objects.select_for_update().filter(pk=pk) |
| 737 | if not vcpe: |
| 738 | return |
| 739 | vcpe = vcpe[0] |
Scott Baker | c1584b8 | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 740 | vcpe.manage_container() |
Scott Baker | 192da22 | 2015-07-08 19:01:56 -0700 | [diff] [blame] | 741 | vcpe.manage_vbng() |
| 742 | vcpe.manage_bbs_account() |
| 743 | vcpe.cleanup_orphans() |
| 744 | |
Scott Baker | fa5cdd6 | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 745 | #---------------------------------------------------------------------------- |
| 746 | # vBNG |
| 747 | #---------------------------------------------------------------------------- |
| 748 | |
| 749 | class VBNGService(Service): |
Scott Baker | d913334 | 2015-07-06 14:38:02 -0700 | [diff] [blame] | 750 | KIND = VBNG_KIND |
Scott Baker | fa5cdd6 | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 751 | |
Scott Baker | 6667ddc | 2015-10-20 22:12:51 -0700 | [diff] [blame] | 752 | simple_attributes = ( ("vbng_url", ""), ) # "http://10.0.3.136:8181/onos/virtualbng/" |
Scott Baker | 21d1893 | 2015-07-21 18:24:21 -0700 | [diff] [blame] | 753 | |
Scott Baker | fa5cdd6 | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 754 | class Meta: |
| 755 | app_label = "cord" |
| 756 | verbose_name = "vBNG Service" |
| 757 | proxy = True |
| 758 | |
Scott Baker | 21d1893 | 2015-07-21 18:24:21 -0700 | [diff] [blame] | 759 | VBNGService.setup_simple_attributes() |
| 760 | |
Scott Baker | fa5cdd6 | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 761 | class VBNGTenant(Tenant): |
| 762 | class Meta: |
| 763 | proxy = True |
| 764 | |
Scott Baker | d913334 | 2015-07-06 14:38:02 -0700 | [diff] [blame] | 765 | KIND = VBNG_KIND |
Scott Baker | fa5cdd6 | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 766 | |
Scott Baker | 61c8e8d | 2015-06-02 14:34:04 -0700 | [diff] [blame] | 767 | default_attributes = {"routeable_subnet": "", |
Scott Baker | cf155f4 | 2015-06-08 19:09:53 -0700 | [diff] [blame] | 768 | "mapped_ip": "", |
| 769 | "mapped_mac": "", |
| 770 | "mapped_hostname": ""} |
Scott Baker | fa5cdd6 | 2015-04-20 09:16:17 -0700 | [diff] [blame] | 771 | |
| 772 | @property |
| 773 | def routeable_subnet(self): |
| 774 | return self.get_attribute("routeable_subnet", self.default_attributes["routeable_subnet"]) |
| 775 | |
| 776 | @routeable_subnet.setter |
| 777 | def routeable_subnet(self, value): |
| 778 | self.set_attribute("routeable_subnet", value) |
Scott Baker | 61c8e8d | 2015-06-02 14:34:04 -0700 | [diff] [blame] | 779 | |
| 780 | @property |
| 781 | def mapped_ip(self): |
| 782 | return self.get_attribute("mapped_ip", self.default_attributes["mapped_ip"]) |
| 783 | |
| 784 | @mapped_ip.setter |
| 785 | def mapped_ip(self, value): |
| 786 | self.set_attribute("mapped_ip", value) |
Scott Baker | cf155f4 | 2015-06-08 19:09:53 -0700 | [diff] [blame] | 787 | |
| 788 | @property |
| 789 | def mapped_mac(self): |
| 790 | return self.get_attribute("mapped_mac", self.default_attributes["mapped_mac"]) |
| 791 | |
| 792 | @mapped_mac.setter |
| 793 | def mapped_mac(self, value): |
| 794 | self.set_attribute("mapped_mac", value) |
| 795 | |
| 796 | @property |
| 797 | def mapped_hostname(self): |
| 798 | return self.get_attribute("mapped_hostname", self.default_attributes["mapped_hostname"]) |
| 799 | |
| 800 | @mapped_hostname.setter |
| 801 | def mapped_hostname(self, value): |
| 802 | self.set_attribute("mapped_hostname", value) |