Scott Baker | 2342903 | 2016-09-06 12:02:39 -0700 | [diff] [blame] | 1 | from core.models import * |
| 2 | from services.vtn.models import VTNService |
| 3 | |
| 4 | VTN_SERVCOMP_KINDS=["PRIVATE","VSG"] |
| 5 | |
| 6 | class VTNNetwork(object): |
| 7 | def __init__(self, xos_network=None): |
| 8 | self.xos_network = xos_network |
| 9 | |
| 10 | def get_controller_network(self): |
| 11 | for cn in self.xos_network.controllernetworks.all(): |
| 12 | # TODO: find the right one |
| 13 | return cn |
| 14 | return None |
| 15 | |
| 16 | def get_cn_field(self, fieldname): |
| 17 | cn=self.get_controller_network() |
| 18 | if not cn: |
| 19 | return None |
| 20 | return getattr(cn, fieldname) |
| 21 | |
| 22 | @property |
| 23 | def id(self): |
| 24 | return self.get_cn_field("net_id") |
| 25 | |
| 26 | @property |
| 27 | def name(self): |
| 28 | return self.xos_network.name |
| 29 | |
| 30 | @property |
| 31 | def subnet(self): |
| 32 | return self.get_cn_field("subnet") |
| 33 | |
| 34 | @property |
| 35 | def gateway(self): |
| 36 | return self.get_cn_field("gateway") |
| 37 | |
| 38 | @property |
| 39 | def segmentation_id(self): |
| 40 | return self.get_cn_field("segmentation_id") |
| 41 | |
| 42 | @property |
| 43 | def type(self): |
| 44 | return self.xos_network.template.vtn_kind |
| 45 | |
| 46 | @property |
| 47 | def providerNetworks(self): |
| 48 | slice = self.xos_network.owner |
| 49 | service = slice.service |
| 50 | if not service: |
| 51 | return [] |
| 52 | |
| 53 | nets=[] |
| 54 | for tenant in service.subscribed_tenants.all(): |
| 55 | if tenant.provider_service: |
| 56 | bidirectional = tenant.connect_method!="private-unidirectional" |
| 57 | for slice in tenant.provider_service.slices.all(): |
| 58 | for net in slice.networks.all(): |
| 59 | if net.template.vtn_kind not in VTN_SERVCOMP_KINDS: |
| 60 | continue |
| 61 | |
| 62 | if not net.controllernetworks.exists(): |
| 63 | continue |
| 64 | |
| 65 | cn = net.controllernetworks.all()[0] |
| 66 | nets.append({"id": cn.net_id, |
| 67 | "name": net.name, |
| 68 | "bidirectional": bidirectional}) |
| 69 | return nets |
| 70 | |
| 71 | @property |
| 72 | def subscriberNetworks(self): |
| 73 | slice = self.xos_network.owner |
| 74 | service = slice.service |
| 75 | if not service: |
| 76 | return [] |
| 77 | |
| 78 | nets=[] |
| 79 | for tenant in service.provided_tenants.all(): |
| 80 | if tenant.subscriber_service: |
| 81 | bidirectional = tenant.connect_method!="private-unidirectional" |
| 82 | for slice in tenant.subscriber_service.slices.all(): |
| 83 | for net in slice.networks.all(): |
| 84 | if net.template.vtn_kind not in VTN_SERVCOMP_KINDS: |
| 85 | continue |
| 86 | |
| 87 | if not net.controllernetworks.exists(): |
| 88 | continue |
| 89 | |
| 90 | cn = net.controllernetworks.all()[0] |
| 91 | nets.append({"id": cn.net_id, |
| 92 | "name": net.name, |
| 93 | "bidirectional": bidirectional}) |
| 94 | return nets |
| 95 | |
| 96 | @property |
| 97 | def ownerSliceName(self): |
| 98 | if self.xos_network.owner: |
| 99 | return self.xos_network.owner.name |
| 100 | return None |
| 101 | |
| 102 | @property |
| 103 | def ownerServiceName(self): |
| 104 | if self.xos_network.owner and self.xos_network.owner.service: |
| 105 | return self.xos_network.owner.service.name |
| 106 | return None |
| 107 | |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 108 | def to_dict(self): |
| 109 | return {"id": self.id, |
| 110 | "name": self.name, |
| 111 | "subnet": self.subnet, |
| 112 | "gateway": self.gateway, |
| 113 | "segmentation_id": self.segmentation_id, |
| 114 | "type": self.type, |
| 115 | "providerNetworks": self.providerNetworks, |
| 116 | "subscriberNetworks": self.subscriberNetworks, |
| 117 | "ownerSliceName": self.ownerSliceName, |
| 118 | "ownerServiceName": self.ownerServiceName} |
| 119 | |
| 120 | def __eq__(self, other): |
| 121 | return self.to_dict() == other.to_dict() |
| 122 | |
Scott Baker | 2342903 | 2016-09-06 12:02:39 -0700 | [diff] [blame] | 123 | class VTNPort(object): |
| 124 | def __init__(self, xos_port=None):
|
| 125 | self.xos_port = xos_port
|
| 126 |
|
| 127 | def get_controller_network(self):
|
| 128 | for cn in self.xos_port.network.controllernetworks.all():
|
| 129 | # TODO: find the right one
|
| 130 | return cn
|
| 131 | return None
|
| 132 |
|
| 133 | def get_vsg_tenant(self):
|
| 134 | from services.vsg.models import VSGTenant
|
| 135 | for tenant in VSGTenant.get_tenant_objects().all():
|
| 136 | if tenant.instance == self.xos_port.instance:
|
| 137 | return tenant
|
| 138 | return None
|
| 139 |
|
| 140 | @property
|
| 141 | def vlan_id(self):
|
| 142 | if not self.xos_port.instance:
|
| 143 | return None
|
| 144 | tags = Tag.select_by_content_object(self.xos_port.instance).filter(name="s_tag")
|
| 145 | if not tags:
|
| 146 | return None
|
| 147 | return tags[0].value
|
| 148 |
|
| 149 | @property
|
| 150 | def floating_address_pairs(self):
|
| 151 | address_pairs = []
|
| 152 | vsg = self.get_vsg_tenant()
|
| 153 | if vsg:
|
| 154 | if vsg.wan_container_ip and vsg.wan_container_mac:
|
| 155 | address_pairs.append({"ip_address": vsg.wan_container_ip,
|
| 156 | "mac_address": vsg.wan_container_mac})
|
| 157 |
|
| 158 | if vsg.wan_vm_ip and vsg.wan_vm_mac:
|
| 159 | address_pairs.append({"ip_address": vsg.wan_vm_ip,
|
| 160 | "mac_address": vsg.wan_vm_mac})
|
| 161 |
|
| 162 | return address_pairs
|
| 163 |
|
| 164 | @property
|
| 165 | def id(self):
|
| 166 | return self.xos_port.port_id
|
| 167 |
|
| 168 | @property
|
| 169 | def name(self):
|
| 170 | return "port-%s" % self.xos_port.id
|
| 171 |
|
| 172 | @property
|
| 173 | def network_id(self):
|
| 174 | cn = self.get_controller_network()
|
| 175 | if not cn:
|
| 176 | return None
|
| 177 | return cn.net_id
|
| 178 |
|
| 179 | @property
|
| 180 | def mac_address(self):
|
| 181 | return self.xos_port.mac
|
| 182 |
|
| 183 | @property
|
| 184 | def ip_address(self):
|
| 185 | return self.xos_port.ip |
| 186 | |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 187 | def to_dict(self): |
| 188 | return {"id": self.id, |
| 189 | "name": self.name, |
| 190 | "network_id": self.network_id, |
| 191 | "mac_address": self.mac_address, |
| 192 | "ip_address": self.ip_address, |
| 193 | "floating_address_pairs": self.floating_address_pairs, |
| 194 | "vlan_id": self.vlan_id} |
| 195 | |
| 196 | def __eq__(self, other): |
| 197 | return self.to_dict() == other.to_dict() |
| 198 | |