Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 1 | from header import * |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 2 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 3 | |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 4 | |
| 5 | |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 6 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 7 | |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 8 | |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 9 | |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 10 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 11 | |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | #from core.models.service import Service |
| 26 | from core.models import Service |
| 27 | |
| 28 | |
| 29 | |
| 30 | from core.models import AddressPool#from core.models.tenant import Tenant |
| 31 | from core.models import Tenant |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | class VRouterApp(PlCoreBase): |
| 38 | |
| 39 | KIND = "vROUTER" |
| 40 | |
| 41 | class Meta: |
| 42 | app_label = "vrouter" |
| 43 | name = "vrouter" |
| 44 | verbose_name = "vRouter Service" |
| 45 | |
| 46 | # Primitive Fields (Not Relations) |
| 47 | name = CharField( help_text = "application name", max_length = 50, null = False, db_index = False, blank = False ) |
| 48 | control_plane_connect_point = CharField( help_text = "port identifier in ONOS", max_length = 21, null = False, db_index = False, blank = False ) |
| 49 | ospf_enabled = BooleanField( default = True, help_text = "ospf enabled", null = False, db_index = False, blank = True ) |
| 50 | |
| 51 | |
| 52 | # Relations |
| 53 | |
| 54 | vrouter_service = ForeignKey(VRouterService, db_index = True, related_name = 'apps', null = False, blank = False ) |
| 55 | |
| 56 | def _get_interfaces(self): |
| 57 | app_interfaces = [] |
| 58 | devices = VRouterDevice.objects.filter(vrouter_service=self.vrouter_service) |
| 59 | for device in devices: |
| 60 | ports = VRouterPort.objects.filter(vrouter_device=device.id) |
| 61 | for port in ports: |
| 62 | interfaces = VRouterInterface.objects.filter(vrouter_port=port.id) |
| 63 | for iface in interfaces: |
| 64 | app_interfaces.append(iface.name) |
| 65 | return app_interfaces |
| 66 | |
| 67 | pass |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 72 | class VRouterDevice(PlCoreBase): |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 73 | |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 74 | KIND = "vROUTER" |
| 75 | |
| 76 | class Meta: |
| 77 | app_label = "vrouter" |
| 78 | name = "vrouter" |
| 79 | verbose_name = "vRouter Service" |
| 80 | |
| 81 | # Primitive Fields (Not Relations) |
| 82 | name = CharField( help_text = "device friendly name", max_length = 20, null = True, db_index = False, blank = True ) |
| 83 | openflow_id = CharField( help_text = "device identifier in ONOS", max_length = 20, null = False, db_index = False, blank = False ) |
| 84 | config_key = CharField( default = "basic", max_length = 32, blank = False, help_text = "configuration key", null = False, db_index = False ) |
| 85 | driver = CharField( help_text = "driver type", max_length = 32, null = False, db_index = False, blank = False ) |
| 86 | |
| 87 | |
| 88 | # Relations |
| 89 | |
| 90 | vrouter_service = ForeignKey(VRouterService, db_index = True, related_name = 'devices', null = False, blank = False ) |
| 91 | |
| 92 | |
| 93 | pass |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 94 | |
| 95 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 96 | |
| 97 | |
| 98 | class VRouterInterface(PlCoreBase): |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 99 | |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 100 | KIND = "vROUTER" |
| 101 | |
| 102 | class Meta: |
| 103 | app_label = "vrouter" |
| 104 | name = "vrouter" |
| 105 | verbose_name = "vRouter Service" |
| 106 | |
| 107 | # Primitive Fields (Not Relations) |
| 108 | name = CharField( help_text = "interface name", max_length = 10, null = False, db_index = False, blank = False ) |
| 109 | mac = CharField( help_text = "interface mac", max_length = 17, null = False, db_index = False, blank = False ) |
| 110 | vlan = CharField( help_text = "interface vlan id", max_length = 10, null = True, db_index = False, blank = True ) |
| 111 | |
| 112 | |
| 113 | # Relations |
| 114 | |
| 115 | vrouter_port = ForeignKey(VRouterPort, db_index = True, related_name = 'interfaces', null = False, blank = False ) |
| 116 | |
| 117 | |
| 118 | pass |
| 119 | |
| 120 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 121 | |
| 122 | |
| 123 | class VRouterIp(PlCoreBase): |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 124 | |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 125 | KIND = "vROUTER" |
| 126 | |
| 127 | class Meta: |
| 128 | app_label = "vrouter" |
| 129 | name = "vrouter" |
| 130 | verbose_name = "vRouter Service" |
| 131 | |
| 132 | # Primitive Fields (Not Relations) |
| 133 | name = CharField( help_text = "ip friendly name", max_length = 20, null = True, db_index = False, blank = True ) |
| 134 | ip = CharField( help_text = "interface ips", max_length = 19, null = False, db_index = False, blank = False ) |
| 135 | |
| 136 | |
| 137 | # Relations |
| 138 | |
| 139 | vrouter_interface = ForeignKey(VRouterInterface, db_index = True, related_name = 'ips', null = False, blank = False ) |
| 140 | |
| 141 | |
| 142 | pass |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 143 | |
| 144 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 145 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 146 | |
Sapan Bhatia | 0a56fdc | 2017-04-21 15:12:57 +0200 | [diff] [blame] | 147 | class VRouterPort(PlCoreBase): |
| 148 | |
| 149 | KIND = "vROUTER" |
| 150 | |
| 151 | class Meta: |
| 152 | app_label = "vrouter" |
| 153 | name = "vrouter" |
| 154 | verbose_name = "vRouter Service" |
| 155 | |
| 156 | # Primitive Fields (Not Relations) |
| 157 | name = CharField( help_text = "port friendly name", max_length = 20, null = True, db_index = False, blank = True ) |
| 158 | openflow_id = CharField( help_text = "port identifier in ONOS", max_length = 21, null = False, db_index = False, blank = False ) |
| 159 | |
| 160 | |
| 161 | # Relations |
| 162 | |
| 163 | vrouter_device = ForeignKey(VRouterDevice, db_index = True, related_name = 'ports', null = False, blank = False ) |
| 164 | vrouter_service = ForeignKey(VRouterService, db_index = True, related_name = 'device_ports', null = False, blank = False ) |
| 165 | |
| 166 | |
| 167 | pass |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | class VRouterService(Service): |
| 173 | |
| 174 | KIND = "vROUTER" |
| 175 | |
| 176 | class Meta: |
| 177 | app_label = "vrouter" |
| 178 | name = "vrouter" |
| 179 | verbose_name = "vRouter Service" |
| 180 | |
| 181 | # Primitive Fields (Not Relations) |
| 182 | rest_hostname = StrippedCharField( db_index = False, max_length = 255, null = True, blank = True ) |
| 183 | rest_port = IntegerField( default = 8181, null = False, blank = False, db_index = False ) |
| 184 | rest_user = StrippedCharField( default = "onos", max_length = 255, null = False, db_index = False, blank = False ) |
| 185 | rest_pass = StrippedCharField( default = "rocks", max_length = 255, null = False, db_index = False, blank = False ) |
| 186 | |
| 187 | |
| 188 | # Relations |
| 189 | |
| 190 | |
| 191 | def ip_to_mac(self, ip): |
| 192 | (a, b, c, d) = ip.split('.') |
| 193 | return "02:42:%02x:%02x:%02x:%02x" % (int(a), int(b), int(c), int(d)) |
| 194 | |
| 195 | def get_gateways(self): |
| 196 | gateways = [] |
| 197 | |
| 198 | aps = self.addresspools.all() |
| 199 | for ap in aps: |
| 200 | gateways.append({"gateway_ip": ap.gateway_ip, "gateway_mac": ap.gateway_mac}) |
| 201 | |
| 202 | return gateways |
| 203 | |
| 204 | def get_address_pool(self, name): |
| 205 | ap = AddressPool.objects.filter(name=name, service=self) |
| 206 | if not ap: |
| 207 | raise Exception("vRouter unable to find addresspool %s" % name) |
| 208 | return ap[0] |
| 209 | |
| 210 | def get_tenant(self, **kwargs): |
| 211 | address_pool_name = kwargs.pop("address_pool_name") |
| 212 | |
| 213 | ap = self.get_address_pool(address_pool_name) |
| 214 | |
| 215 | ip = ap.get_address() |
| 216 | if not ip: |
| 217 | raise Exception("AddressPool '%s' has run out of addresses." % ap.name) |
| 218 | |
| 219 | t = VRouterTenant(provider_service=self, **kwargs) |
| 220 | t.public_ip = ip |
| 221 | t.public_mac = self.ip_to_mac(ip) |
| 222 | t.address_pool_id = ap.id |
| 223 | t.save() |
| 224 | |
| 225 | return t |
| 226 | |
| 227 | pass |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | class VRouterTenant(Tenant): |
| 233 | |
| 234 | KIND = "vROUTER" |
| 235 | |
| 236 | class Meta: |
| 237 | app_label = "vrouter" |
| 238 | name = "vrouter" |
| 239 | verbose_name = "vRouter Service" |
| 240 | |
| 241 | # Primitive Fields (Not Relations) |
| 242 | public_ip = StrippedCharField( db_index = False, max_length = 30, null = True, blank = True ) |
| 243 | public_mac = StrippedCharField( db_index = False, max_length = 30, null = True, blank = True ) |
| 244 | |
| 245 | |
| 246 | # Relations |
| 247 | |
| 248 | address_pool = ForeignKey(AddressPool, db_index = True, related_name = 'vrouter_tenants', null = True, blank = True ) |
| 249 | |
| 250 | @property |
| 251 | def gateway_ip(self): |
| 252 | if not self.address_pool: |
| 253 | return None |
| 254 | return self.address_pool.gateway_ip |
| 255 | |
| 256 | @property |
| 257 | def gateway_mac(self): |
| 258 | if not self.address_pool: |
| 259 | return None |
| 260 | return self.address_pool.gateway_mac |
| 261 | |
| 262 | @property |
| 263 | def cidr(self): |
| 264 | if not self.address_pool: |
| 265 | return None |
| 266 | return self.address_pool.cidr |
| 267 | |
| 268 | @property |
| 269 | def netbits(self): |
| 270 | # return number of bits in the network portion of the cidr |
| 271 | if self.cidr: |
| 272 | parts = self.cidr.split("/") |
| 273 | if len(parts) == 2: |
| 274 | return int(parts[1].strip()) |
| 275 | return None |
| 276 | |
| 277 | def cleanup_addresspool(self): |
| 278 | if self.address_pool: |
| 279 | ap = self.address_pool |
| 280 | if ap: |
| 281 | ap[0].put_address(self.public_ip) |
| 282 | self.public_ip = None |
| 283 | |
| 284 | def delete(self, *args, **kwargs): |
| 285 | self.cleanup_addresspool() |
| 286 | super(VRouterTenant, self).delete(*args, **kwargs) |
| 287 | |
| 288 | pass |
| 289 | |
| 290 | |