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