Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 1 | import os |
| 2 | import requests |
| 3 | import socket |
| 4 | import sys |
| 5 | import base64 |
| 6 | import json |
| 7 | from synchronizers.base.syncstep import SyncStep |
| 8 | from core.models import Service, Slice, Controller, ControllerSlice, ControllerUser, Node, TenantAttribute, Tag |
| 9 | from services.vtn.models import VTNService |
Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 10 | from xos.logger import Logger, logging |
| 11 | |
| 12 | logger = Logger(level=logging.INFO) |
| 13 | |
| 14 | class SyncONOSNetcfg(SyncStep): |
| 15 | provides=[] |
| 16 | observes=None |
| 17 | requested_interval=0 |
| 18 | |
| 19 | def __init__(self, **args): |
| 20 | SyncStep.__init__(self, **args) |
| 21 | |
Andy Bavier | e45ce91 | 2016-09-13 17:25:43 -0400 | [diff] [blame] | 22 | def get_node_tag(self, node, tagname): |
Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 23 | tags = Tag.select_by_content_object(node).filter(name=tagname) |
| 24 | return tags[0].value |
| 25 | |
| 26 | def get_tenants_who_want_config(self): |
| 27 | tenants = [] |
| 28 | # attribute is comma-separated list |
| 29 | for ta in TenantAttribute.objects.filter(name="autogenerate"): |
| 30 | if ta.value: |
| 31 | for config in ta.value.split(','): |
| 32 | if config == "vtn-network-cfg": |
| 33 | tenants.append(ta.tenant) |
| 34 | return tenants |
| 35 | |
| 36 | def save_tenant_attribute(self, tenant, name, value): |
| 37 | tas = TenantAttribute.objects.filter(tenant=tenant, name=name) |
| 38 | if tas: |
| 39 | ta = tas[0] |
| 40 | if ta.value != value: |
| 41 | logger.info("updating %s with attribute" % name) |
| 42 | ta.value = value |
| 43 | ta.save() |
| 44 | else: |
| 45 | logger.info("saving autogenerated config %s" % name) |
| 46 | ta = TenantAttribute(tenant=tenant, name=name, value=value) |
| 47 | ta.save() |
| 48 | |
| 49 | # This function currently assumes a single Deployment and Site |
| 50 | def get_onos_netcfg(self, vtn): |
| 51 | privateGatewayMac = vtn.privateGatewayMac |
| 52 | localManagementIp = vtn.localManagementIp |
| 53 | ovsdbPort = vtn.ovsdbPort |
| 54 | sshPort = vtn.sshPort |
| 55 | sshUser = vtn.sshUser |
| 56 | sshKeyFile = vtn.sshKeyFile |
| 57 | mgmtSubnetBits = vtn.mgmtSubnetBits |
| 58 | xosEndpoint = vtn.xosEndpoint |
| 59 | xosUser = vtn.xosUser |
| 60 | xosPassword = vtn.xosPassword |
| 61 | |
Scott Baker | ff01668 | 2016-11-28 09:53:13 -0800 | [diff] [blame] | 62 | controllerPort = vtn.controllerPort |
| 63 | if ":" in controllerPort: |
| 64 | (c_hostname, c_port) = controllerPort.split(":",1) |
| 65 | controllerPort = socket.gethostbyname(c_hostname) + ":" + c_port |
| 66 | else: |
| 67 | controllerPort = ":" + controllerPort |
| 68 | |
Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 69 | data = { |
| 70 | "apps" : { |
| 71 | "org.opencord.vtn" : { |
| 72 | "cordvtn" : { |
| 73 | "privateGatewayMac" : privateGatewayMac, |
| 74 | "localManagementIp": localManagementIp, |
| 75 | "ovsdbPort": ovsdbPort, |
| 76 | "ssh": { |
| 77 | "sshPort": sshPort, |
| 78 | "sshUser": sshUser, |
| 79 | "sshKeyFile": sshKeyFile |
| 80 | }, |
| 81 | "xos": { |
| 82 | "endpoint": xosEndpoint, |
| 83 | "user": xosUser, |
| 84 | "password": xosPassword |
| 85 | }, |
| 86 | "publicGateways": [], |
Scott Baker | ff01668 | 2016-11-28 09:53:13 -0800 | [diff] [blame] | 87 | "nodes" : [], |
| 88 | "controllers": [controllerPort] |
Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | # Generate apps->org.opencord.vtn->cordvtn->openstack |
| 95 | controllers = Controller.objects.all() |
| 96 | if controllers: |
| 97 | controller = controllers[0] |
| 98 | keystone_server = controller.auth_url |
| 99 | user_name = controller.admin_user |
| 100 | tenant_name = controller.admin_tenant |
| 101 | password = controller.admin_password |
| 102 | openstack = { |
| 103 | "endpoint": keystone_server, |
| 104 | "tenant": tenant_name, |
| 105 | "user": user_name, |
| 106 | "password": password |
| 107 | } |
| 108 | data["apps"]["org.opencord.vtn"]["cordvtn"]["openstack"] = openstack |
| 109 | |
| 110 | # Generate apps->org.opencord.vtn->cordvtn->nodes |
| 111 | nodes = Node.objects.all() |
| 112 | for node in nodes: |
| 113 | nodeip = socket.gethostbyname(node.name) |
| 114 | |
| 115 | try: |
Andy Bavier | e45ce91 | 2016-09-13 17:25:43 -0400 | [diff] [blame] | 116 | bridgeId = self.get_node_tag(node, "bridgeId") |
| 117 | dataPlaneIntf = self.get_node_tag(node, "dataPlaneIntf") |
| 118 | dataPlaneIp = self.get_node_tag(node, "dataPlaneIp") |
Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 119 | except: |
| 120 | logger.error("not adding node %s to the VTN configuration" % node.name) |
| 121 | continue |
| 122 | |
| 123 | node_dict = { |
| 124 | "hostname": node.name, |
| 125 | "hostManagementIp": "%s/%s" % (nodeip, mgmtSubnetBits), |
| 126 | "bridgeId": bridgeId, |
| 127 | "dataPlaneIntf": dataPlaneIntf, |
| 128 | "dataPlaneIp": dataPlaneIp |
| 129 | } |
Scott Baker | dcbc52b | 2016-10-12 17:30:34 -0700 | [diff] [blame] | 130 | |
| 131 | # this one is optional |
| 132 | try: |
| 133 | node_dict["hostManagementIface"] = self.get_node_tag(node, "hostManagementIface") |
| 134 | except IndexError: |
| 135 | pass |
| 136 | |
Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 137 | data["apps"]["org.opencord.vtn"]["cordvtn"]["nodes"].append(node_dict) |
| 138 | |
| 139 | # Generate apps->org.onosproject.cordvtn->cordvtn->publicGateways |
| 140 | # Pull the gateway information from vRouter |
Andy Bavier | 5901263 | 2016-09-14 08:41:56 -0400 | [diff] [blame] | 141 | try: |
| 142 | from services.vrouter.models import VRouterService |
| 143 | |
| 144 | vrouters = VRouterService.get_service_objects().all() |
| 145 | if vrouters: |
| 146 | for gateway in vrouters[0].get_gateways(): |
| 147 | gatewayIp = gateway['gateway_ip'].split('/',1)[0] |
| 148 | gatewayMac = gateway['gateway_mac'] |
| 149 | gateway_dict = { |
| 150 | "gatewayIp": gatewayIp, |
| 151 | "gatewayMac": gatewayMac |
| 152 | } |
| 153 | data["apps"]["org.opencord.vtn"]["cordvtn"]["publicGateways"].append(gateway_dict) |
| 154 | except: |
| 155 | logger.info("No VRouter service present, not adding publicGateways to config") |
Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 156 | |
| 157 | return json.dumps(data, indent=4, sort_keys=True) |
| 158 | |
| 159 | def call(self, **args): |
| 160 | vtn_service = VTNService.get_service_objects().all() |
| 161 | if not vtn_service: |
| 162 | raise Exception("No VTN Service") |
| 163 | |
| 164 | vtn_service = vtn_service[0] |
| 165 | |
| 166 | # Check for autogenerate attribute |
| 167 | netcfg = self.get_onos_netcfg(vtn_service) |
| 168 | |
| 169 | tenants = self.get_tenants_who_want_config() |
| 170 | for tenant in tenants: |
| 171 | self.save_tenant_attribute(tenant, "rest_onos/v1/network/configuration/", netcfg) |