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" |
Srikanth Vavilapalli | 9c1c66f | 2016-12-03 23:52:16 +0000 | [diff] [blame^] | 57 | for net in tenant.provider_service.get_composable_networks(): |
| 58 | if not net.controllernetworks.exists(): |
| 59 | continue |
Scott Baker | 2342903 | 2016-09-06 12:02:39 -0700 | [diff] [blame] | 60 | |
Srikanth Vavilapalli | 9c1c66f | 2016-12-03 23:52:16 +0000 | [diff] [blame^] | 61 | cn = net.controllernetworks.all()[0] |
| 62 | nets.append({"id": cn.net_id, |
| 63 | "name": net.name, |
| 64 | "bidirectional": bidirectional}) |
Scott Baker | 2342903 | 2016-09-06 12:02:39 -0700 | [diff] [blame] | 65 | return nets |
| 66 | |
| 67 | @property |
| 68 | def subscriberNetworks(self): |
| 69 | slice = self.xos_network.owner |
| 70 | service = slice.service |
| 71 | if not service: |
| 72 | return [] |
| 73 | |
| 74 | nets=[] |
| 75 | for tenant in service.provided_tenants.all(): |
| 76 | if tenant.subscriber_service: |
| 77 | bidirectional = tenant.connect_method!="private-unidirectional" |
Srikanth Vavilapalli | 9c1c66f | 2016-12-03 23:52:16 +0000 | [diff] [blame^] | 78 | for net in tenant.subscriber_service.get_composable_networks(): |
| 79 | if not net.controllernetworks.exists(): |
| 80 | continue |
Scott Baker | 2342903 | 2016-09-06 12:02:39 -0700 | [diff] [blame] | 81 | |
Srikanth Vavilapalli | 9c1c66f | 2016-12-03 23:52:16 +0000 | [diff] [blame^] | 82 | cn = net.controllernetworks.all()[0] |
| 83 | nets.append({"id": cn.net_id, |
| 84 | "name": net.name, |
| 85 | "bidirectional": bidirectional}) |
Scott Baker | 2342903 | 2016-09-06 12:02:39 -0700 | [diff] [blame] | 86 | return nets |
| 87 | |
| 88 | @property |
| 89 | def ownerSliceName(self): |
| 90 | if self.xos_network.owner: |
| 91 | return self.xos_network.owner.name |
| 92 | return None |
| 93 | |
| 94 | @property |
| 95 | def ownerServiceName(self): |
| 96 | if self.xos_network.owner and self.xos_network.owner.service: |
| 97 | return self.xos_network.owner.service.name |
| 98 | return None |
| 99 | |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 100 | def to_dict(self): |
| 101 | return {"id": self.id, |
| 102 | "name": self.name, |
| 103 | "subnet": self.subnet, |
| 104 | "gateway": self.gateway, |
| 105 | "segmentation_id": self.segmentation_id, |
| 106 | "type": self.type, |
| 107 | "providerNetworks": self.providerNetworks, |
| 108 | "subscriberNetworks": self.subscriberNetworks, |
| 109 | "ownerSliceName": self.ownerSliceName, |
| 110 | "ownerServiceName": self.ownerServiceName} |
| 111 | |
| 112 | def __eq__(self, other): |
| 113 | return self.to_dict() == other.to_dict() |
| 114 | |
Scott Baker | 2342903 | 2016-09-06 12:02:39 -0700 | [diff] [blame] | 115 | class VTNPort(object): |
| 116 | def __init__(self, xos_port=None):
|
| 117 | self.xos_port = xos_port
|
| 118 |
|
| 119 | def get_controller_network(self):
|
| 120 | for cn in self.xos_port.network.controllernetworks.all():
|
| 121 | # TODO: find the right one
|
| 122 | return cn
|
| 123 | return None
|
| 124 |
|
| 125 | def get_vsg_tenant(self):
|
| 126 | from services.vsg.models import VSGTenant
|
| 127 | for tenant in VSGTenant.get_tenant_objects().all():
|
| 128 | if tenant.instance == self.xos_port.instance:
|
| 129 | return tenant
|
| 130 | return None
|
| 131 |
|
| 132 | @property
|
| 133 | def vlan_id(self):
|
| 134 | if not self.xos_port.instance:
|
| 135 | return None
|
Scott Baker | 9837fb8 | 2016-09-26 13:43:57 -0700 | [diff] [blame] | 136 | # Only some kinds of networks can have s-tags associated with them.
|
| 137 | # Currently, only VSG access networks qualify.
|
| 138 | if not self.xos_port.network.template.vtn_kind in ["VSG",]:
|
| 139 | return None
|
Scott Baker | 2342903 | 2016-09-06 12:02:39 -0700 | [diff] [blame] | 140 | tags = Tag.select_by_content_object(self.xos_port.instance).filter(name="s_tag")
|
| 141 | if not tags:
|
| 142 | return None
|
| 143 | return tags[0].value
|
| 144 |
|
| 145 | @property
|
| 146 | def floating_address_pairs(self):
|
Scott Baker | 9837fb8 | 2016-09-26 13:43:57 -0700 | [diff] [blame] | 147 | # Floating_address_pairs is the set of WAN addresses that should be
|
| 148 | # applied to this port.
|
Scott Baker | 2342903 | 2016-09-06 12:02:39 -0700 | [diff] [blame] | 149 |
|
Scott Baker | 9837fb8 | 2016-09-26 13:43:57 -0700 | [diff] [blame] | 150 | address_pairs = []
|
| 151 |
|
| 152 | # only look apply the VSG addresses if the Network is of the VSG vtn_kind
|
| 153 | if self.xos_port.network.template.vtn_kind in ["VSG", ]:
|
| 154 | vsg = self.get_vsg_tenant()
|
| 155 | if vsg:
|
| 156 | if vsg.wan_container_ip and vsg.wan_container_mac:
|
| 157 | address_pairs.append({"ip_address": vsg.wan_container_ip,
|
| 158 | "mac_address": vsg.wan_container_mac})
|
| 159 |
|
| 160 | if vsg.wan_vm_ip and vsg.wan_vm_mac:
|
| 161 | address_pairs.append({"ip_address": vsg.wan_vm_ip,
|
| 162 | "mac_address": vsg.wan_vm_mac})
|
Scott Baker | 2342903 | 2016-09-06 12:02:39 -0700 | [diff] [blame] | 163 |
|
| 164 | return address_pairs
|
| 165 |
|
| 166 | @property
|
| 167 | def id(self):
|
| 168 | return self.xos_port.port_id
|
| 169 |
|
| 170 | @property
|
| 171 | def name(self):
|
| 172 | return "port-%s" % self.xos_port.id
|
| 173 |
|
| 174 | @property
|
| 175 | def network_id(self):
|
| 176 | cn = self.get_controller_network()
|
| 177 | if not cn:
|
| 178 | return None
|
| 179 | return cn.net_id
|
| 180 |
|
| 181 | @property
|
Scott Baker | 9837fb8 | 2016-09-26 13:43:57 -0700 | [diff] [blame] | 182 | def network_name(self):
|
| 183 | return self.xos_port.network.name
|
| 184 |
|
| 185 | @property
|
Scott Baker | 2342903 | 2016-09-06 12:02:39 -0700 | [diff] [blame] | 186 | def mac_address(self):
|
| 187 | return self.xos_port.mac
|
| 188 |
|
| 189 | @property
|
| 190 | def ip_address(self):
|
| 191 | return self.xos_port.ip |
| 192 | |
Scott Baker | b3a80de | 2016-09-06 16:51:27 -0700 | [diff] [blame] | 193 | def to_dict(self): |
| 194 | return {"id": self.id, |
| 195 | "name": self.name, |
| 196 | "network_id": self.network_id, |
| 197 | "mac_address": self.mac_address, |
| 198 | "ip_address": self.ip_address, |
| 199 | "floating_address_pairs": self.floating_address_pairs, |
| 200 | "vlan_id": self.vlan_id} |
| 201 | |
| 202 | def __eq__(self, other): |
| 203 | return self.to_dict() == other.to_dict() |
| 204 | |