CORD-2476 migrate vrouter service to dynamic load

Change-Id: I773d0a597bc8c2e7001c51d7b2d43d472a7e7716
diff --git a/xos/synchronizer/models/models.py b/xos/synchronizer/models/models.py
new file mode 100644
index 0000000..3b1035b
--- /dev/null
+++ b/xos/synchronizer/models/models.py
@@ -0,0 +1,156 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+from django.db import models
+from core.models import Service, XOSBase, Slice, Instance, ServiceInstance, Node, Image, User, Flavor, NetworkParameter, NetworkParameterType, Port, AddressPool
+from core.models.xosbase import StrippedCharField
+import os
+from django.db import models, transaction
+from django.forms.models import model_to_dict
+from django.db.models import *
+from operator import itemgetter, attrgetter, methodcaller
+from core.models import Tag
+from core.models.service import LeastLoadedNodeScheduler
+import traceback
+from xos.exceptions import *
+from models_decl import *
+
+
+class ConfigurationError(Exception):
+    pass
+
+
+class VRouterService (VRouterService_decl):
+    class Meta:
+        proxy = True
+
+    def ip_to_mac(self, ip):
+        (a, b, c, d) = ip.split('.')
+        return "02:42:%02x:%02x:%02x:%02x" % (int(a), int(b), int(c), int(d))
+
+    def get_gateways(self):
+        gateways = []
+        aps = self.addresspools.all()
+        for ap in aps:
+            gateways.append({"gateway_ip": ap.gateway_ip, "gateway_mac": ap.gateway_mac})
+
+        return gateways
+
+    def get_address_pool(self, name):
+        ap = AddressPool.objects.filter(name=name, service=self)
+        if not ap:
+            raise Exception("vRouter unable to find addresspool %s" % name)
+        return ap[0]
+
+    def get_tenant(self, **kwargs):
+        address_pool_name = kwargs.pop("address_pool_name")
+
+        ap = self.get_address_pool(address_pool_name)
+
+        ip = ap.get_address()
+        if not ip:
+            raise Exception("AddressPool '%s' has run out of addresses." % ap.name)
+
+        t = VRouterTenant(owner=self, **kwargs)
+        t.public_ip = ip
+        t.public_mac = self.ip_to_mac(ip)
+        t.address_pool_id = ap.id
+        t.save()
+
+        return t
+
+
+class VRouterDevice (VRouterDevice_decl):
+
+    class Meta:
+        proxy = True
+    pass
+
+class VRouterPort (VRouterPort_decl):
+
+    class Meta:
+        proxy = True
+    pass
+
+class VRouterApp (VRouterApp_decl):
+
+    class Meta:
+        proxy = True
+    def _get_interfaces(self):
+        app_interfaces = []
+        devices = VRouterDevice.objects.filter(vrouter_service=self.vrouter_service)
+        for device in devices:
+            ports = VRouterPort.objects.filter(vrouter_device=device.id)
+            for port in ports:
+                interfaces = VRouterInterface.objects.filter(vrouter_port=port.id)
+                for iface in interfaces:
+                    app_interfaces.append(iface.name)
+        return app_interfaces
+
+class VRouterInterface (VRouterInterface_decl):
+
+    class Meta:
+        proxy = True
+    pass
+
+class VRouterIp (VRouterIp_decl):
+
+    class Meta:
+        proxy = True
+    pass
+
+class VRouterTenant (VRouterTenant_decl):
+
+    class Meta:
+        proxy = True
+    @property
+    def gateway_ip(self):
+        if not self.address_pool:
+            return None
+        return self.address_pool.gateway_ip
+
+    @property
+    def gateway_mac(self):
+        if not self.address_pool:
+            return None
+        return self.address_pool.gateway_mac
+
+    @property
+    def cidr(self):
+        if not self.address_pool:
+            return None
+        return self.address_pool.cidr
+
+    @property
+    def netbits(self):
+        # return number of bits in the network portion of the cidr
+        if self.cidr:
+            parts = self.cidr.split("/")
+            if len(parts) == 2:
+                return int(parts[1].strip())
+        return None
+
+    def cleanup_addresspool(self):
+        if self.address_pool:
+            ap = self.address_pool
+            if ap:
+                ap.put_address(self.public_ip)
+                self.public_ip = None
+
+    def delete(self, *args, **kwargs):
+        self.cleanup_addresspool()
+        super(VRouterTenant, self).delete(*args, **kwargs)
+
diff --git a/xos/synchronizer/models/vrouter.xproto b/xos/synchronizer/models/vrouter.xproto
new file mode 100644
index 0000000..8c2da1d
--- /dev/null
+++ b/xos/synchronizer/models/vrouter.xproto
@@ -0,0 +1,66 @@
+option kind="vROUTER";
+option name="vrouter";
+option legacy="True";
+option app_label = "vrouter";
+
+message VRouterService (Service){
+     option verbose_name="vRouter Service";
+
+     optional string rest_hostname = 1 [db_index = False, max_length = 255, null = True, content_type = "stripped", blank = True];
+     required int32 rest_port = 2 [default = 8181, null = False, db_index = False, blank = False];
+     required string rest_user = 3 [default = "onos", max_length = 255, content_type = "stripped", blank = False, null = False, db_index = False];
+     required string rest_pass = 4 [default = "rocks", max_length = 255, content_type = "stripped", blank = False, null = False, db_index = False];
+}
+
+message VRouterDevice (XOSBase){
+     option verbose_name="vRouter Device";
+
+     optional string name = 1 [help_text = "device friendly name", max_length = 20, null = True, db_index = False, blank = True];
+     required string openflow_id = 2 [help_text = "device identifier in ONOS", max_length = 20, null = False, db_index = False, blank = False];
+     required string config_key = 3 [default = "basic", max_length = 32, blank = False, help_text = "configuration key", null = False, db_index = False];
+     required string driver = 4 [help_text = "driver type", max_length = 32, null = False, db_index = False, blank = False];
+     required manytoone vrouter_service->VRouterService:devices = 5 [db_index = True, null = False, blank = False];
+}
+
+message VRouterPort (XOSBase){
+     option verbose_name="vRouter Port";
+
+     optional string name = 1 [help_text = "port friendly name", max_length = 20, null = True, db_index = False, blank = True];
+     required string openflow_id = 2 [help_text = "port identifier in ONOS", max_length = 21, null = False, db_index = False, blank = False];
+     required manytoone vrouter_device->VRouterDevice:ports = 3 [db_index = True, null = False, blank = False];
+     required manytoone vrouter_service->VRouterService:device_ports = 4 [db_index = True, null = False, blank = False];
+}
+
+message VRouterApp (XOSBase){
+     option verbose_name="vRouter App";
+
+     required manytoone vrouter_service->VRouterService:apps = 1 [db_index = True, null = False, blank = False];
+     required string name = 2 [help_text = "application name", max_length = 50, null = False, db_index = False, blank = False];
+     required string control_plane_connect_point = 3 [help_text = "port identifier in ONOS", max_length = 21, null = False, db_index = False, blank = False];
+     required bool ospf_enabled = 4 [help_text = "ospf enabled", default = True, null = False, db_index = False, blank = True];
+}
+
+message VRouterInterface (XOSBase) {
+     option verbose_name="vRouter Interface";
+
+     required manytoone vrouter_port->VRouterPort:interfaces = 1 [db_index = True, null = False, blank = False];
+     required string name = 2 [help_text = "interface name", max_length = 10, null = False, db_index = False, blank = False];
+     required string mac = 3 [help_text = "interface mac", max_length = 17, null = False, db_index = False, blank = False];
+     optional string vlan = 4 [help_text = "interface vlan id", max_length = 10, null = True, db_index = False, blank = True];
+}
+
+message VRouterIp (XOSBase){
+     option verbose_name="vRouter Ip";
+
+     optional string name = 1 [help_text = "ip friendly name", max_length = 20, null = True, db_index = False, blank = True];
+     required manytoone vrouter_interface->VRouterInterface:ips = 2 [db_index = True, null = False, blank = False];
+     required string ip = 3 [help_text = "interface ips", max_length = 19, null = False, db_index = False, blank = False];
+}
+
+message VRouterTenant (ServiceInstance){
+     option verbose_name="vRouter Tenant";
+
+     optional string public_ip = 1 [db_index = False, max_length = 30, null = True, content_type = "stripped", blank = True];
+     optional string public_mac = 2 [db_index = False, max_length = 30, null = True, content_type = "stripped", blank = True];
+     optional manytoone address_pool->AddressPool:vrouter_tenants = 3 [db_index = True, null = True, blank = True];
+}
diff --git a/xos/synchronizer/vrouter_config.yaml b/xos/synchronizer/vrouter_config.yaml
index 050503a..7fe4f18 100644
--- a/xos/synchronizer/vrouter_config.yaml
+++ b/xos/synchronizer/vrouter_config.yaml
@@ -14,10 +14,16 @@
 # limitations under the License.
 
 
-name: vrouter-synchronizer
+name: vrouter
 accessor:
   username: xosadmin@opencord.org
   password: "@/opt/xos/services/vrouter/credentials/xosadmin@opencord.org"
+required_models:
+  - VRouterService
+  - VRouterDevice
+  - VRouterApp
+  - VRouterPort
 dependency_graph: "/opt/xos/synchronizers/vrouter/model-deps"
 steps_dir: "/opt/xos/synchronizers/vrouter/steps"
-sys_dir: "/opt/xos/synchronizers/vrouter/sys"
\ No newline at end of file
+sys_dir: "/opt/xos/synchronizers/vrouter/sys"
+models_dir: "/opt/xos/synchronizers/vrouter/models"