Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 1 | from django.db import models |
| 2 | from core.models import Service, PlCoreBase, Slice, Instance, Tenant, TenantWithContainer, Node, Image, User, Flavor, Subscriber |
| 3 | from core.models.plcorebase import StrippedCharField |
| 4 | import os |
| 5 | from django.db import models, transaction |
| 6 | from django.forms.models import model_to_dict |
| 7 | from django.db.models import Q |
| 8 | from operator import itemgetter, attrgetter, methodcaller |
| 9 | import traceback |
| 10 | from xos.exceptions import * |
| 11 | from core.models import SlicePrivilege, SitePrivilege |
| 12 | from sets import Set |
| 13 | |
| 14 | ONOS_KIND = "onos" |
| 15 | |
| 16 | class ONOSService(Service): |
| 17 | KIND = ONOS_KIND |
| 18 | |
| 19 | class Meta: |
| 20 | app_label = "onos" |
| 21 | verbose_name = "ONOS Service" |
| 22 | proxy = True |
| 23 | |
| 24 | default_attributes = {"rest_hostname": "", |
| 25 | "rest_port": "8181", |
| 26 | "no_container": False, |
| 27 | "node_key": ""} |
| 28 | |
| 29 | @property |
| 30 | def rest_hostname(self): |
| 31 | return self.get_attribute("rest_hostname", self.default_attributes["rest_hostname"]) |
| 32 | |
| 33 | @rest_hostname.setter |
| 34 | def rest_hostname(self, value): |
| 35 | self.set_attribute("rest_hostname", value) |
| 36 | |
| 37 | @property |
| 38 | def rest_port(self): |
| 39 | return self.get_attribute("rest_port", self.default_attributes["rest_port"]) |
| 40 | |
| 41 | @rest_port.setter |
| 42 | def rest_port(self, value): |
| 43 | self.set_attribute("rest_port", value) |
| 44 | |
| 45 | @property |
| 46 | def no_container(self): |
| 47 | return self.get_attribute("no_container", self.default_attributes["no_container"]) |
| 48 | |
| 49 | @no_container.setter |
| 50 | def no_container(self, value): |
| 51 | self.set_attribute("no_container", value) |
| 52 | |
| 53 | @property |
| 54 | def node_key(self): |
| 55 | return self.get_attribute("node_key", self.default_attributes["node_key"]) |
| 56 | |
| 57 | @node_key.setter |
| 58 | def node_key(self, value): |
| 59 | self.set_attribute("node_key", value) |
| 60 | |
| 61 | |
| 62 | class ONOSApp(Tenant): # aka 'ONOSTenant' |
| 63 | class Meta: |
| 64 | proxy = True |
| 65 | |
| 66 | KIND = ONOS_KIND |
| 67 | |
| 68 | default_attributes = {"name": "", |
| 69 | "install_dependencies": "", |
| 70 | "dependencies": ""} |
| 71 | def __init__(self, *args, **kwargs): |
| 72 | onos_services = ONOSService.get_service_objects().all() |
| 73 | if onos_services: |
| 74 | self._meta.get_field("provider_service").default = onos_services[0].id |
| 75 | super(ONOSApp, self).__init__(*args, **kwargs) |
| 76 | |
| 77 | @property |
| 78 | def creator(self): |
| 79 | from core.models import User |
| 80 | if getattr(self, "cached_creator", None): |
| 81 | return self.cached_creator |
| 82 | creator_id=self.get_attribute("creator_id") |
| 83 | if not creator_id: |
| 84 | return None |
| 85 | users=User.objects.filter(id=creator_id) |
| 86 | if not users: |
| 87 | return None |
| 88 | user=users[0] |
| 89 | self.cached_creator = users[0] |
| 90 | return user |
| 91 | |
| 92 | @creator.setter |
| 93 | def creator(self, value): |
| 94 | if value: |
| 95 | value = value.id |
| 96 | if (value != self.get_attribute("creator_id", None)): |
| 97 | self.cached_creator=None |
| 98 | self.set_attribute("creator_id", value) |
| 99 | |
| 100 | @property |
| 101 | def name(self): |
| 102 | return self.get_attribute("name", self.default_attributes["name"]) |
| 103 | |
| 104 | @name.setter |
| 105 | def name(self, value): |
| 106 | self.set_attribute("name", value) |
| 107 | |
| 108 | @property |
| 109 | def dependencies(self): |
| 110 | return self.get_attribute("dependencies", self.default_attributes["dependencies"]) |
| 111 | |
| 112 | @dependencies.setter |
| 113 | def dependencies(self, value): |
| 114 | self.set_attribute("dependencies", value) |
| 115 | |
| 116 | @property |
| 117 | def install_dependencies(self): |
| 118 | return self.get_attribute("install_dependencies", self.default_attributes["install_dependencies"]) |
| 119 | |
| 120 | @install_dependencies.setter |
| 121 | def install_dependencies(self, value): |
| 122 | self.set_attribute("install_dependencies", value) |
| 123 | |
| 124 | def save(self, *args, **kwargs): |
| 125 | if not self.creator: |
| 126 | if not getattr(self, "caller", None): |
| 127 | # caller must be set when creating a vCPE since it creates a slice |
| 128 | raise XOSProgrammingError("ONOSApp's self.caller was not set") |
| 129 | self.creator = self.caller |
| 130 | if not self.creator: |
| 131 | raise XOSProgrammingError("ONOSApp's self.creator was not set") |
| 132 | |
| 133 | super(ONOSApp, self).save(*args, **kwargs) |
| 134 | model_policy_onos_app(self.pk) |
| 135 | |
| 136 | # TODO: Probably don't need this... |
| 137 | def model_policy_onos_app(pk): |
| 138 | # TODO: this should be made in to a real model_policy |
| 139 | with transaction.atomic(): |
| 140 | oa = ONOSApp.objects.select_for_update().filter(pk=pk) |
| 141 | if not oa: |
| 142 | return |
| 143 | oa = oa[0] |
| 144 | #oa.manage_container() |
| 145 | |
| 146 | |