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 | |
| 62 | data = { |
| 63 | "apps" : { |
| 64 | "org.opencord.vtn" : { |
| 65 | "cordvtn" : { |
| 66 | "privateGatewayMac" : privateGatewayMac, |
| 67 | "localManagementIp": localManagementIp, |
| 68 | "ovsdbPort": ovsdbPort, |
| 69 | "ssh": { |
| 70 | "sshPort": sshPort, |
| 71 | "sshUser": sshUser, |
| 72 | "sshKeyFile": sshKeyFile |
| 73 | }, |
| 74 | "xos": { |
| 75 | "endpoint": xosEndpoint, |
| 76 | "user": xosUser, |
| 77 | "password": xosPassword |
| 78 | }, |
| 79 | "publicGateways": [], |
| 80 | "nodes" : [] |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | # Generate apps->org.opencord.vtn->cordvtn->openstack |
| 87 | controllers = Controller.objects.all() |
| 88 | if controllers: |
| 89 | controller = controllers[0] |
| 90 | keystone_server = controller.auth_url |
| 91 | user_name = controller.admin_user |
| 92 | tenant_name = controller.admin_tenant |
| 93 | password = controller.admin_password |
| 94 | openstack = { |
| 95 | "endpoint": keystone_server, |
| 96 | "tenant": tenant_name, |
| 97 | "user": user_name, |
| 98 | "password": password |
| 99 | } |
| 100 | data["apps"]["org.opencord.vtn"]["cordvtn"]["openstack"] = openstack |
| 101 | |
| 102 | # Generate apps->org.opencord.vtn->cordvtn->nodes |
| 103 | nodes = Node.objects.all() |
| 104 | for node in nodes: |
| 105 | nodeip = socket.gethostbyname(node.name) |
| 106 | |
| 107 | try: |
Andy Bavier | e45ce91 | 2016-09-13 17:25:43 -0400 | [diff] [blame] | 108 | bridgeId = self.get_node_tag(node, "bridgeId") |
| 109 | dataPlaneIntf = self.get_node_tag(node, "dataPlaneIntf") |
| 110 | dataPlaneIp = self.get_node_tag(node, "dataPlaneIp") |
Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 111 | except: |
| 112 | logger.error("not adding node %s to the VTN configuration" % node.name) |
| 113 | continue |
| 114 | |
| 115 | node_dict = { |
| 116 | "hostname": node.name, |
| 117 | "hostManagementIp": "%s/%s" % (nodeip, mgmtSubnetBits), |
| 118 | "bridgeId": bridgeId, |
| 119 | "dataPlaneIntf": dataPlaneIntf, |
| 120 | "dataPlaneIp": dataPlaneIp |
| 121 | } |
Scott Baker | dcbc52b | 2016-10-12 17:30:34 -0700 | [diff] [blame] | 122 | |
| 123 | # this one is optional |
| 124 | try: |
| 125 | node_dict["hostManagementIface"] = self.get_node_tag(node, "hostManagementIface") |
| 126 | except IndexError: |
| 127 | pass |
| 128 | |
Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 129 | data["apps"]["org.opencord.vtn"]["cordvtn"]["nodes"].append(node_dict) |
| 130 | |
| 131 | # Generate apps->org.onosproject.cordvtn->cordvtn->publicGateways |
| 132 | # Pull the gateway information from vRouter |
Andy Bavier | 5901263 | 2016-09-14 08:41:56 -0400 | [diff] [blame] | 133 | try: |
| 134 | from services.vrouter.models import VRouterService |
| 135 | |
| 136 | vrouters = VRouterService.get_service_objects().all() |
| 137 | if vrouters: |
| 138 | for gateway in vrouters[0].get_gateways(): |
| 139 | gatewayIp = gateway['gateway_ip'].split('/',1)[0] |
| 140 | gatewayMac = gateway['gateway_mac'] |
| 141 | gateway_dict = { |
| 142 | "gatewayIp": gatewayIp, |
| 143 | "gatewayMac": gatewayMac |
| 144 | } |
| 145 | data["apps"]["org.opencord.vtn"]["cordvtn"]["publicGateways"].append(gateway_dict) |
| 146 | except: |
| 147 | logger.info("No VRouter service present, not adding publicGateways to config") |
Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 148 | |
| 149 | return json.dumps(data, indent=4, sort_keys=True) |
| 150 | |
| 151 | def call(self, **args): |
| 152 | vtn_service = VTNService.get_service_objects().all() |
| 153 | if not vtn_service: |
| 154 | raise Exception("No VTN Service") |
| 155 | |
| 156 | vtn_service = vtn_service[0] |
| 157 | |
| 158 | # Check for autogenerate attribute |
| 159 | netcfg = self.get_onos_netcfg(vtn_service) |
| 160 | |
| 161 | tenants = self.get_tenants_who_want_config() |
| 162 | for tenant in tenants: |
| 163 | self.save_tenant_attribute(tenant, "rest_onos/v1/network/configuration/", netcfg) |