Move autogeneration to services

Change-Id: I6bcf7c867b5b2a81694dce6c963ce9db1bc9bab6
diff --git a/xos/synchronizer/steps/sync_onosapp.py b/xos/synchronizer/steps/sync_onosapp.py
index d421aa1..26bb49b 100644
--- a/xos/synchronizer/steps/sync_onosapp.py
+++ b/xos/synchronizer/steps/sync_onosapp.py
@@ -16,9 +16,6 @@
 from core.models import Service, Slice, Controller, ControllerSlice, ControllerUser, Node, TenantAttribute, Tag
 from services.onos.models import ONOSService, ONOSApp
 from xos.logger import Logger, logging
-from services.vrouter.models import VRouterService
-from services.vtn.models import VTNService
-from services.volt.models import VOLTService, VOLTDevice, AccessDevice
 
 # hpclibrary will be in steps/..
 parentdir = os.path.join(os.path.dirname(__file__),"..")
@@ -119,250 +116,6 @@
                 raise Exception("Controller user object for %s does not exist" % instance.creator)
             return cuser.kuser_id
 
-    def get_node_tag(self, o, node, tagname):
-        tags = Tag.select_by_content_object(node).filter(name=tagname)
-        return tags[0].value
-
-    # Scan attrs for attribute name
-    # If it's not present, save it as a TenantAttribute
-    def attribute_default(self, tenant, attrs, name, default):
-        if name in attrs:
-            value = attrs[name]
-        else:
-            value = default
-            logger.info("saving default value %s for attribute %s" % (value, name))
-            ta = TenantAttribute(tenant=tenant, name=name, value=value)
-            ta.save()
-        return value
-
-    # This function currently assumes a single Deployment and Site
-    def get_vtn_config(self, o, attrs):
-
-        privateGatewayMac = None
-        localManagementIp = None
-        ovsdbPort = None
-        sshPort = None
-        sshUser = None
-        sshKeyFile = None
-        mgmtSubnetBits = None
-        xosEndpoint = None
-        xosUser = None
-        xosPassword = None
-
-        # VTN-specific configuration from the VTN Service
-        vtns = VTNService.get_service_objects().all()
-        if vtns:
-            vtn = vtns[0]
-            privateGatewayMac = vtn.privateGatewayMac
-            localManagementIp = vtn.localManagementIp
-            ovsdbPort = vtn.ovsdbPort
-            sshPort = vtn.sshPort
-            sshUser = vtn.sshUser
-            sshKeyFile = vtn.sshKeyFile
-            mgmtSubnetBits = vtn.mgmtSubnetBits
-            xosEndpoint = vtn.xosEndpoint
-            xosUser = vtn.xosUser
-            xosPassword = vtn.xosPassword
-
-        # OpenStack endpoints and credentials
-        keystone_server = "http://keystone:5000/v2.0/"
-        user_name = "admin"
-        password = "ADMIN_PASS"
-        controllers = Controller.objects.all()
-        if controllers:
-            controller = controllers[0]
-            keystone_server = controller.auth_url
-            user_name = controller.admin_user
-            tenant_name = controller.admin_tenant
-            password = controller.admin_password
-
-        data = {
-            "apps" : {
-                "org.opencord.vtn" : {
-                    "cordvtn" : {
-                        "privateGatewayMac" : privateGatewayMac,
-                        "localManagementIp": localManagementIp,
-                        "ovsdbPort": ovsdbPort,
-                        "ssh": {
-                            "sshPort": sshPort,
-                            "sshUser": sshUser,
-                            "sshKeyFile": sshKeyFile
-                        },
-                        "openstack": {
-                            "endpoint": keystone_server,
-                            "tenant": tenant_name,
-                            "user": user_name,
-                            "password": password
-                        },
-                        "xos": {
-                            "endpoint": xosEndpoint,
-                            "user": xosUser,
-                            "password": xosPassword
-                        },
-                        "publicGateways": [],
-                        "nodes" : []
-                    }
-                }
-            }
-        }
-
-        # Generate apps->org.opencord.vtn->cordvtn->nodes
-        nodes = Node.objects.all()
-        for node in nodes:
-            nodeip = socket.gethostbyname(node.name)
-
-            try:
-                bridgeId = self.get_node_tag(o, node, "bridgeId")
-                dataPlaneIntf = self.get_node_tag(o, node, "dataPlaneIntf")
-                dataPlaneIp = self.get_node_tag(o, node, "dataPlaneIp")
-            except:
-                logger.error("not adding node %s to the VTN configuration" % node.name)
-                continue
-
-            node_dict = {
-                "hostname": node.name,
-                "hostManagementIp": "%s/%s" % (nodeip, mgmtSubnetBits),
-                "bridgeId": bridgeId,
-                "dataPlaneIntf": dataPlaneIntf,
-                "dataPlaneIp": dataPlaneIp
-            }
-            data["apps"]["org.opencord.vtn"]["cordvtn"]["nodes"].append(node_dict)
-
-        # Generate apps->org.onosproject.cordvtn->cordvtn->publicGateways
-        # Pull the gateway information from vRouter
-        vrouters = VRouterService.get_service_objects().all()
-        if vrouters:
-            for gateway in vrouters[0].get_gateways():
-                gatewayIp = gateway['gateway_ip'].split('/',1)[0]
-                gatewayMac = gateway['gateway_mac']
-                gateway_dict = {
-                    "gatewayIp": gatewayIp,
-                    "gatewayMac": gatewayMac
-                }
-                data["apps"]["org.opencord.vtn"]["cordvtn"]["publicGateways"].append(gateway_dict)
-
-        return json.dumps(data, indent=4, sort_keys=True)
-
-    def get_volt_network_config(self, o, attrs):
-        try:
-            volt = VOLTService.get_service_objects().all()[0]
-        except:
-            return None
-
-        devices = []
-        for voltdev in volt.volt_devices.all():
-            access_devices = []
-            for access in voltdev.access_devices.all():
-                access_device = {
-                    "uplink" : access.uplink,
-                    "vlan" : access.vlan
-                }
-                access_devices.append(access_device)
-
-            if voltdev.access_agent:
-                agent = voltdev.access_agent
-                olts = {}
-                for port_mapping in agent.port_mappings.all():
-                    olts[port_mapping.port] = port_mapping.mac
-                agent_config = {
-                    "olts" : olts,
-                    "mac" : agent.mac
-                }
-
-            device = {
-                voltdev.openflow_id : {
-                    "accessDevice" : access_devices,
-                    "accessAgent" : agent_config
-                },
-                "basic" : {
-                    "driver" : voltdev.driver
-                }
-            }
-            devices.append(device)
-
-        data = {
-            "devices" : devices
-        }
-        return json.dumps(data, indent=4, sort_keys=True)
-
-    def get_volt_component_config(self, o, attrs):
-        data = {
-            "org.ciena.onos.ext_notifier.KafkaNotificationBridge":{
-                "rabbit.user": "<rabbit_user>",
-                "rabbit.password": "<rabbit_password>",
-                "rabbit.host": "<rabbit_host>",
-                "publish.kafka": "false",
-                "publish.rabbit": "true",
-                "volt.events.rabbit.topic": "notifications.info",
-                "volt.events.rabbit.exchange": "voltlistener",
-                "volt.events.opaque.info": "{project_id: <keystone_tenant_id>, user_id: <keystone_user_id>}",
-                "publish.volt.events": "true"
-            }
-        }
-        return json.dumps(data, indent=4, sort_keys=True)
-
-    def get_vrouter_network_config(self, o, attrs):
-        # From the onosproject wiki:
-        # https://wiki.onosproject.org/display/ONOS/vRouter
-        data = {
-            "devices" : {
-                "of:00000000000000b1" : {
-                    "basic" : {
-                        "driver" : "softrouter"
-                    }
-                }
-            },
-            "ports" : {
-                "of:00000000000000b1/1" : {
-                    "interfaces" : [
-                        {
-                            "name" : "b1-1",
-                            "ips"  : [ "10.0.1.2/24" ],
-                            "mac"  : "00:00:00:00:00:01"
-                        }
-                    ]
-                },
-                "of:00000000000000b1/2" : {
-                    "interfaces" : [
-                        {
-                            "name" : "b1-2",
-                            "ips"  : [ "10.0.2.2/24" ],
-                            "mac"  : "00:00:00:00:00:01"
-                        }
-                    ]
-                },
-                "of:00000000000000b1/3" : {
-                    "interfaces" : [
-                        {
-                            "name" : "b1-3",
-                            "ips"  : [ "10.0.3.2/24" ],
-                            "mac"  : "00:00:00:00:00:01"
-                        }
-                    ]
-                },
-                "of:00000000000000b1/4" : {
-                    "interfaces" : [
-                        {
-                            "name" : "b1-4",
-                            "ips"  : [ "10.0.4.2/24" ],
-                            "mac"  : "00:00:00:00:00:02",
-                            "vlan" : "100"
-                        }
-                    ]
-                }
-            },
-            "apps" : {
-                "org.onosproject.router" : {
-                    "router" : {
-                        "controlPlaneConnectPoint" : "of:00000000000000b1/5",
-                        "ospfEnabled" : "true",
-                        "interfaces" : [ "b1-1", "b1-2", "b1-2", "b1-4" ]
-                    }
-                }
-            }
-        }
-        return json.dumps(data, indent=4, sort_keys=True)
-
     def write_configs(self, o):
         o.config_fns = []
         o.rest_configs = []
@@ -399,45 +152,6 @@
             file(os.path.join(o.files_dir, fn),"w").write(" " +value)
             o.early_rest_configs.append( {"endpoint": endpoint, "fn": fn} )
 
-        # Generate config files and save them to the appropriate tenant attributes
-        configs = []
-        for key, value in attrs.iteritems():
-            if key == "autogenerate" and value:
-                for config in value.split(','):
-                    configs.append(config.strip())
-
-        for label in configs:
-            config = None
-            value = None
-            if label == "vtn-network-cfg":
-                # Generate the VTN config file... where should this live?
-                config = "rest_onos/v1/network/configuration/"
-                value = self.get_vtn_config(o, attrs)
-            elif label == "volt-network-cfg":
-                config = "rest_onos/v1/network/configuration/"
-                value = self.get_volt_network_config(o, attrs)
-            elif label == "volt-component-cfg":
-                config = "component_config"
-                value = self.get_volt_component_config(o, attrs)
-            elif label == "vrouter-network-cfg":
-                config = "rest_onos/v1/network/configuration/"
-                value = self.get_vrouter_network_config(o, attrs)
-
-            if config:
-                tas = TenantAttribute.objects.filter(tenant=o, name=config)
-                if tas:
-                    ta = tas[0]
-                    if ta.value != value:
-                        logger.info("updating %s with autogenerated config" % config)
-                        ta.value = value
-                        ta.save()
-                        attrs[config] = value
-                else:
-                    logger.info("saving autogenerated config %s" % config)
-                    ta = TenantAttribute(tenant=o, name=config, value=value)
-                    ta.save()
-                    attrs[config] = value
-
         for name in attrs.keys():
             value = attrs[name]
             if name.startswith("config_"):