Gabe Black | 4c040b7 | 2016-11-17 22:35:04 +0000 | [diff] [blame] | 1 | # models.py - ExampleService Models |
| 2 | |
| 3 | from core.models import Service, TenantWithContainer |
| 4 | from django.db import models, transaction |
| 5 | |
| 6 | ACTIVETEST_KIND = 'ActiveTest' |
| 7 | ACTIVETEST_SERVICE_NAME = 'activetest' |
| 8 | ACTIVETEST_SERVICE_NAME_VERBOSE = 'ACTIVETEST Service' |
| 9 | ACTIVETEST_SERVICE_NAME_VERBOSE_PLURAL = 'ACTIVETEST Services' |
| 10 | ACTIVETEST_TENANT_NAME_VERBOSE = 'ACTIVETEST Tenant' |
| 11 | ACTIVETEST_TENANT_NAME_VERBOSE_PLURAL = 'ACTIVETEST Tenants' |
| 12 | |
| 13 | QT600_SERVICE_NAME = 'qt600' |
| 14 | QT600_SERVICE_NAME_VERBOSE = 'QT600 Service' |
| 15 | QT600_SERVICE_NAME_VERBOSE_PLURAL = 'QT600 Services' |
| 16 | QT600_TENANT_NAME_VERBOSE = 'QT600 Tenant' |
| 17 | QT600_TENANT_NAME_VERBOSE_PLURAL = 'QT600 Tenants' |
| 18 | |
| 19 | VPMA_SERVICE_NAME = 'vpma' |
| 20 | VPMA_SERVICE_NAME_VERBOSE = 'VPMA Service' |
| 21 | VPMA_SERVICE_NAME_VERBOSE_PLURAL = 'VPMA Services' |
| 22 | VPMA_TENANT_NAME_VERBOSE = 'VPMA Tenant' |
| 23 | VPMA_TENANT_NAME_VERBOSE_PLURAL = 'VPMA Tenants' |
| 24 | |
| 25 | VTA_SERVICE_NAME = 'vta' |
| 26 | VTA_SERVICE_NAME_VERBOSE = 'VTA Service' |
| 27 | VTA_SERVICE_NAME_VERBOSE_PLURAL = 'VTA Services' |
| 28 | VTA_TENANT_NAME_VERBOSE = 'VTA Tenant' |
| 29 | VTA_TENANT_NAME_VERBOSE_PLURAL = 'VTA Tenants' |
| 30 | |
| 31 | class ActiveTestService(Service): |
| 32 | |
| 33 | KIND = ACTIVETEST_KIND |
| 34 | |
| 35 | class Meta: |
| 36 | app_label = ACTIVETEST_SERVICE_NAME |
| 37 | verbose_name = ACTIVETEST_SERVICE_NAME_VERBOSE |
| 38 | proxy = True |
| 39 | |
| 40 | class Qt600Service(Service): |
| 41 | |
| 42 | KIND = ACTIVETEST_KIND |
| 43 | class Meta: |
| 44 | app_label = QT600_SERVICE_NAME |
| 45 | verbose_name = QT600_SERVICE_NAME_VERBOSE |
| 46 | proxy = True |
| 47 | |
| 48 | class VpmaService(Service): |
| 49 | |
| 50 | KIND = ACTIVETEST_KIND |
| 51 | class Meta: |
| 52 | app_label = VPMA_SERVICE_NAME |
| 53 | verbose_name = VPMA_SERVICE_NAME_VERBOSE |
| 54 | proxy = True |
| 55 | |
| 56 | class VtaService(Service): |
| 57 | |
| 58 | KIND = ACTIVETEST_KIND |
| 59 | class Meta: |
| 60 | app_label = VTA_SERVICE_NAME |
| 61 | verbose_name = VTA_SERVICE_NAME_VERBOSE |
| 62 | proxy = True |
| 63 | |
| 64 | class ActiveTestTenant(TenantWithContainer): |
| 65 | |
| 66 | KIND = ACTIVETEST_KIND |
| 67 | |
| 68 | class Meta: |
| 69 | verbose_name = ACTIVETEST_TENANT_NAME_VERBOSE |
| 70 | |
| 71 | def __init__(self, *args, **kwargs): |
| 72 | activetestservice = ActiveTestService.get_service_objects().all() |
| 73 | if activetestservice: |
| 74 | self._meta.get_field('provider_service').default = activetestservice[0].id |
| 75 | super(ActiveTestTenant, self).__init__(*args, **kwargs) |
| 76 | |
| 77 | def save(self, *args, **kwargs): |
| 78 | super(ActiveTestTenant, self).save(*args, **kwargs) |
| 79 | model_policy_activetesttenant(self.pk) |
| 80 | |
| 81 | def delete(self, *args, **kwargs): |
| 82 | self.cleanup_container() |
| 83 | super(ActiveTestTenant, self).delete(*args, **kwargs) |
| 84 | |
| 85 | @property |
| 86 | def public_ip(self): |
| 87 | for port in self.instance.ports.all(): |
| 88 | if "public" in port.network.name.lower(): |
| 89 | return port.ip |
| 90 | return None |
| 91 | |
| 92 | def model_policy_activetesttenant(pk): |
| 93 | with transaction.atomic(): |
| 94 | tenant = ActiveTestTenant.objects.select_for_update().filter(pk=pk) |
| 95 | if not tenant: |
| 96 | return |
| 97 | tenant = tenant[0] |
| 98 | tenant.manage_container() |
| 99 | |
| 100 | class Qt600Tenant(TenantWithContainer): |
| 101 | |
| 102 | KIND = ACTIVETEST_KIND |
| 103 | sync_attributes = ["is_reflector","is_initiator","reflector_ip","reflector_public_ip","private_ip","public_ip"] |
| 104 | |
| 105 | is_reflector = models.BooleanField(default=False,help_text="True if this tenant should be a TWAMP reflector") |
| 106 | is_initiator = models.BooleanField(default=False,help_text="True if this tenant should be a TWAMP initiator") |
| 107 | reflector = models.CharField(max_length=254,default="", blank=True, help_text="The reflector node to use for the test") |
| 108 | |
| 109 | class Meta: |
| 110 | verbose_name = QT600_TENANT_NAME_VERBOSE |
| 111 | |
| 112 | def __init__(self, *args, **kwargs): |
| 113 | service = Qt600Service.get_service_objects().all() |
| 114 | if service: |
| 115 | self._meta.get_field('provider_service').default = service[0].id |
| 116 | super(Qt600Tenant, self).__init__(*args, **kwargs) |
| 117 | |
| 118 | def save(self, *args, **kwargs): |
| 119 | super(Qt600Tenant, self).save(*args, **kwargs) |
| 120 | model_policy_qt600tenant(self.pk) |
| 121 | |
| 122 | def delete(self, *args, **kwargs): |
| 123 | self.cleanup_container() |
| 124 | super(Qt600TenantTenant, self).delete(*args, **kwargs) |
| 125 | |
| 126 | @property |
| 127 | def private_ip(self): |
| 128 | for port in self.instance.ports.all(): |
| 129 | if "test_network" in port.network.name.lower(): |
| 130 | return port.ip |
| 131 | return None |
| 132 | |
| 133 | @property |
| 134 | def public_ip(self): |
| 135 | for port in self.instance.ports.all(): |
| 136 | if "public" in port.network.name.lower(): |
| 137 | return port.ip |
| 138 | return None |
| 139 | |
| 140 | def get_reflector_vm(self): |
| 141 | service = [s for s in ActiveTestService.get_service_objects().all() if "qt600" in s.name.lower()] |
| 142 | if service and self.reflector: |
| 143 | slices = service[0].slices.all() |
| 144 | for slice in slices: |
| 145 | instance = [i for i in slice.instances.all() if self.reflector in i.instance_name.lower()] |
| 146 | if len(instance) > 0: |
| 147 | return instance[0] |
| 148 | return None |
| 149 | |
| 150 | @property |
| 151 | def reflector_ip(self): |
| 152 | instance = self.get_reflector_vm() |
| 153 | if instance: |
| 154 | for port in instance.ports.all(): |
| 155 | if "test_network" in port.network.name.lower(): |
| 156 | return port.ip |
| 157 | return None |
| 158 | |
| 159 | @property |
| 160 | def reflector_public_ip(self): |
| 161 | instance = self.get_reflector_vm() |
| 162 | if instance: |
| 163 | for port in instance.ports.all(): |
| 164 | if "public" in port.network.name.lower(): |
| 165 | return port.ip |
| 166 | return None |
| 167 | |
| 168 | @property |
| 169 | def controller_ip(self): |
| 170 | service = [s for s in ActiveTestService.get_service_objects().all() if "activetest" in s.name.lower()] |
| 171 | if service: |
| 172 | slices = service[0].slices.all() |
| 173 | for slice in slices: |
| 174 | for instance in slice.instances.all(): |
| 175 | for port in instance.ports.all(): |
| 176 | if "public" in port.network.name.lower(): |
| 177 | return port.ip |
| 178 | return None |
| 179 | |
| 180 | |
| 181 | def model_policy_qt600tenant(pk): |
| 182 | with transaction.atomic(): |
| 183 | tenant = Qt600Tenant.objects.select_for_update().filter(pk=pk) |
| 184 | if not tenant: |
| 185 | return |
| 186 | tenant = tenant[0] |
| 187 | tenant.manage_container() |
| 188 | |
| 189 | class VpmaTenant(TenantWithContainer): |
| 190 | |
| 191 | KIND = ACTIVETEST_KIND |
| 192 | sync_attributes = ["is_reflector","is_initiator","reflector_ip","reflector_public_ip","private_ip","public_ip"] |
| 193 | |
| 194 | is_reflector = models.BooleanField(default=False,help_text="True if this tenant should be a TWAMP reflector") |
| 195 | is_initiator = models.BooleanField(default=False,help_text="True if this tenant should be a TWAMP initiator") |
| 196 | reflector = models.CharField(max_length=254,default="", blank=True, help_text="The reflector node to use for the test") |
| 197 | |
| 198 | class Meta: |
| 199 | verbose_name = VPMA_TENANT_NAME_VERBOSE |
| 200 | |
| 201 | def __init__(self, *args, **kwargs): |
| 202 | service = VpmaService.get_service_objects().all() |
| 203 | if service: |
| 204 | self._meta.get_field('provider_service').default = service[0].id |
| 205 | super(VpmaTenant, self).__init__(*args, **kwargs) |
| 206 | |
| 207 | def save(self, *args, **kwargs): |
| 208 | super(VpmaTenant, self).save(*args, **kwargs) |
| 209 | model_policy_vpmatenant(self.pk) |
| 210 | |
| 211 | def delete(self, *args, **kwargs): |
| 212 | self.cleanup_container() |
| 213 | super(VpmaTenantTenant, self).delete(*args, **kwargs) |
| 214 | |
| 215 | @property |
| 216 | def private_ip(self): |
| 217 | for port in self.instance.ports.all(): |
| 218 | if "test_network" in port.network.name.lower(): |
| 219 | return port.ip |
| 220 | return None |
| 221 | |
| 222 | @property |
| 223 | def public_ip(self): |
| 224 | for port in self.instance.ports.all(): |
| 225 | if "public" in port.network.name.lower(): |
| 226 | return port.ip |
| 227 | return None |
| 228 | |
| 229 | def get_reflector_vm(self): |
| 230 | service = [s for s in ActiveTestService.get_service_objects().all() if "vpma" in s.name.lower()] |
| 231 | if service and self.reflector: |
| 232 | slices = service[0].slices.all() |
| 233 | for slice in slices: |
| 234 | instance = [i for i in slice.instances.all() if self.reflector in i.instance_name.lower()] |
| 235 | if len(instance) > 0: |
| 236 | return instance[0] |
| 237 | return None |
| 238 | |
| 239 | @property |
| 240 | def reflector_ip(self): |
| 241 | instance = self.get_reflector_vm() |
| 242 | if instance: |
| 243 | for port in instance.ports.all(): |
| 244 | if "test_network" in port.network.name.lower(): |
| 245 | return port.ip |
| 246 | return None |
| 247 | |
| 248 | @property |
| 249 | def reflector_public_ip(self): |
| 250 | instance = self.get_reflector_vm() |
| 251 | if instance: |
| 252 | for port in instance.ports.all(): |
| 253 | if "public" in port.network.name.lower(): |
| 254 | return port.ip |
| 255 | return None |
| 256 | |
| 257 | @property |
| 258 | def controller_ip(self): |
| 259 | service = [s for s in ActiveTestService.get_service_objects().all() if "activetest" in s.name.lower()] |
| 260 | if service: |
| 261 | slices = service[0].slices.all() |
| 262 | for slice in slices: |
| 263 | for instance in slice.instances.all(): |
| 264 | for port in instance.ports.all(): |
| 265 | if "public" in port.network.name.lower(): |
| 266 | return port.ip |
| 267 | return None |
| 268 | |
| 269 | |
| 270 | def model_policy_vpmatenant(pk): |
| 271 | with transaction.atomic(): |
| 272 | tenant = VpmaTenant.objects.select_for_update().filter(pk=pk) |
| 273 | if not tenant: |
| 274 | return |
| 275 | tenant = tenant[0] |
| 276 | tenant.manage_container() |
| 277 | |
| 278 | class VtaTenant(TenantWithContainer): |
| 279 | |
| 280 | KIND = ACTIVETEST_KIND |
| 281 | sync_attributes = ["private_ip"] |
| 282 | |
| 283 | class Meta: |
| 284 | verbose_name = VTA_TENANT_NAME_VERBOSE |
| 285 | |
| 286 | def __init__(self, *args, **kwargs): |
| 287 | service = VtaService.get_service_objects().all() |
| 288 | if service: |
| 289 | self._meta.get_field('provider_service').default = service[0].id |
| 290 | super(VtaTenant, self).__init__(*args, **kwargs) |
| 291 | |
| 292 | def save(self, *args, **kwargs): |
| 293 | super(VtaTenant, self).save(*args, **kwargs) |
| 294 | model_policy_vtatenant(self.pk) |
| 295 | |
| 296 | def delete(self, *args, **kwargs): |
| 297 | self.cleanup_container() |
| 298 | super(VtaTenantTenant, self).delete(*args, **kwargs) |
| 299 | |
| 300 | @property |
| 301 | def private_ip(self): |
| 302 | for port in self.instance.ports.all(): |
| 303 | if "test_network" in port.network.name.lower(): |
| 304 | return port.ip |
| 305 | return None |
| 306 | |
| 307 | @property |
| 308 | def public_ip(self): |
| 309 | for port in self.instance.ports.all(): |
| 310 | if "public" in port.network.name.lower(): |
| 311 | return port.ip |
| 312 | return None |
| 313 | |
| 314 | @property |
| 315 | def controller_ip(self): |
| 316 | service = [s for s in ActiveTestService.get_service_objects().all() if "activetest" in s.name.lower()] |
| 317 | if service: |
| 318 | slices = service[0].slices.all() |
| 319 | for slice in slices: |
| 320 | for instance in slice.instances.all(): |
| 321 | for port in instance.ports.all(): |
| 322 | if "public" in port.network.name.lower(): |
| 323 | return port.ip |
| 324 | return None |
| 325 | |
| 326 | def model_policy_vtatenant(pk): |
| 327 | with transaction.atomic(): |
| 328 | tenant = VtaTenant.objects.select_for_update().filter(pk=pk) |
| 329 | if not tenant: |
| 330 | return |
| 331 | tenant = tenant[0] |
| 332 | tenant.manage_container() |
| 333 | |
| 334 | |
| 335 | |