Matteo Scandolo | 0e7912c | 2017-08-08 13:05:24 -0700 | [diff] [blame^] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 17 | import os |
| 18 | import requests |
| 19 | import socket |
| 20 | import sys |
| 21 | import base64 |
| 22 | import json |
Scott Baker | 61b41a9 | 2017-03-14 13:04:57 -0700 | [diff] [blame] | 23 | from synchronizers.new_base.syncstep import SyncStep |
| 24 | from synchronizers.new_base.modelaccessor import * |
Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 25 | from xos.logger import Logger, logging |
| 26 | |
| 27 | logger = Logger(level=logging.INFO) |
| 28 | |
| 29 | class SyncONOSNetcfg(SyncStep): |
Andy Bavier | 51d7ebb | 2016-12-06 16:20:34 -0500 | [diff] [blame] | 30 | provides=[VTNService] |
| 31 | observes=VTNService |
| 32 | watches=[ModelLink(Node,via='node'), ModelLink(AddressPool,via='addresspool')] |
Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 33 | requested_interval=0 |
| 34 | |
| 35 | def __init__(self, **args): |
| 36 | SyncStep.__init__(self, **args) |
| 37 | |
Andy Bavier | 51d7ebb | 2016-12-06 16:20:34 -0500 | [diff] [blame] | 38 | def handle_watched_object(self, o): |
| 39 | logger.info("handle_watched_object is invoked for object %s" % (str(o)),extra=o.tologdict()) |
| 40 | if (type(o) is Node): # For Node add/delete/modify |
| 41 | self.call() |
| 42 | if (type(o) is AddressPool): # For public gateways |
| 43 | self.call() |
| 44 | |
Andy Bavier | e45ce91 | 2016-09-13 17:25:43 -0400 | [diff] [blame] | 45 | def get_node_tag(self, node, tagname): |
Scott Baker | 08a4df3 | 2017-03-15 14:13:59 -0700 | [diff] [blame] | 46 | tags = Tag.objects.filter(content_type=model_accessor.get_content_type_id(node), |
| 47 | object_id=node.id, |
| 48 | name=tagname) |
Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 49 | return tags[0].value |
| 50 | |
| 51 | def get_tenants_who_want_config(self): |
| 52 | tenants = [] |
| 53 | # attribute is comma-separated list |
Scott Baker | d949b62 | 2017-07-18 12:10:35 -0700 | [diff] [blame] | 54 | for ta in ServiceInstanceAttribute.objects.filter(name="autogenerate"): |
Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 55 | if ta.value: |
| 56 | for config in ta.value.split(','): |
| 57 | if config == "vtn-network-cfg": |
Scott Baker | d949b62 | 2017-07-18 12:10:35 -0700 | [diff] [blame] | 58 | tenants.append(ta.service_instance) |
Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 59 | return tenants |
| 60 | |
| 61 | def save_tenant_attribute(self, tenant, name, value): |
Scott Baker | d949b62 | 2017-07-18 12:10:35 -0700 | [diff] [blame] | 62 | tas = ServiceInstanceAttribute.objects.filter(service_instance_id=tenant.id, name=name) |
Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 63 | if tas: |
| 64 | ta = tas[0] |
| 65 | if ta.value != value: |
| 66 | logger.info("updating %s with attribute" % name) |
| 67 | ta.value = value |
| 68 | ta.save() |
| 69 | else: |
| 70 | logger.info("saving autogenerated config %s" % name) |
Scott Baker | d949b62 | 2017-07-18 12:10:35 -0700 | [diff] [blame] | 71 | ta = model_accessor.create_obj(ServiceInstanceAttribute, service_instance=tenant, name=name, value=value) |
Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 72 | ta.save() |
| 73 | |
| 74 | # This function currently assumes a single Deployment and Site |
| 75 | def get_onos_netcfg(self, vtn): |
| 76 | privateGatewayMac = vtn.privateGatewayMac |
| 77 | localManagementIp = vtn.localManagementIp |
| 78 | ovsdbPort = vtn.ovsdbPort |
| 79 | sshPort = vtn.sshPort |
| 80 | sshUser = vtn.sshUser |
| 81 | sshKeyFile = vtn.sshKeyFile |
| 82 | mgmtSubnetBits = vtn.mgmtSubnetBits |
| 83 | xosEndpoint = vtn.xosEndpoint |
| 84 | xosUser = vtn.xosUser |
| 85 | xosPassword = vtn.xosPassword |
| 86 | |
Scott Baker | ff01668 | 2016-11-28 09:53:13 -0800 | [diff] [blame] | 87 | controllerPort = vtn.controllerPort |
| 88 | if ":" in controllerPort: |
| 89 | (c_hostname, c_port) = controllerPort.split(":",1) |
| 90 | controllerPort = socket.gethostbyname(c_hostname) + ":" + c_port |
| 91 | else: |
| 92 | controllerPort = ":" + controllerPort |
| 93 | |
Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 94 | data = { |
| 95 | "apps" : { |
| 96 | "org.opencord.vtn" : { |
| 97 | "cordvtn" : { |
| 98 | "privateGatewayMac" : privateGatewayMac, |
| 99 | "localManagementIp": localManagementIp, |
| 100 | "ovsdbPort": ovsdbPort, |
| 101 | "ssh": { |
| 102 | "sshPort": sshPort, |
| 103 | "sshUser": sshUser, |
| 104 | "sshKeyFile": sshKeyFile |
| 105 | }, |
| 106 | "xos": { |
| 107 | "endpoint": xosEndpoint, |
| 108 | "user": xosUser, |
| 109 | "password": xosPassword |
| 110 | }, |
| 111 | "publicGateways": [], |
Scott Baker | ff01668 | 2016-11-28 09:53:13 -0800 | [diff] [blame] | 112 | "nodes" : [], |
| 113 | "controllers": [controllerPort] |
Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | # Generate apps->org.opencord.vtn->cordvtn->openstack |
| 120 | controllers = Controller.objects.all() |
| 121 | if controllers: |
| 122 | controller = controllers[0] |
| 123 | keystone_server = controller.auth_url |
| 124 | user_name = controller.admin_user |
| 125 | tenant_name = controller.admin_tenant |
| 126 | password = controller.admin_password |
| 127 | openstack = { |
| 128 | "endpoint": keystone_server, |
| 129 | "tenant": tenant_name, |
| 130 | "user": user_name, |
| 131 | "password": password |
| 132 | } |
| 133 | data["apps"]["org.opencord.vtn"]["cordvtn"]["openstack"] = openstack |
| 134 | |
| 135 | # Generate apps->org.opencord.vtn->cordvtn->nodes |
| 136 | nodes = Node.objects.all() |
| 137 | for node in nodes: |
Jonathan Hart | d659920 | 2017-07-25 11:39:58 -0700 | [diff] [blame] | 138 | try: |
| 139 | nodeip = socket.gethostbyname(node.name) |
| 140 | except socket.gaierror: |
| 141 | logger.warn("unable to resolve hostname %s: node will not be added to config" |
| 142 | % node.name) |
| 143 | continue |
Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 144 | |
| 145 | try: |
Andy Bavier | e45ce91 | 2016-09-13 17:25:43 -0400 | [diff] [blame] | 146 | bridgeId = self.get_node_tag(node, "bridgeId") |
| 147 | dataPlaneIntf = self.get_node_tag(node, "dataPlaneIntf") |
| 148 | dataPlaneIp = self.get_node_tag(node, "dataPlaneIp") |
Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 149 | except: |
| 150 | logger.error("not adding node %s to the VTN configuration" % node.name) |
| 151 | continue |
| 152 | |
| 153 | node_dict = { |
| 154 | "hostname": node.name, |
| 155 | "hostManagementIp": "%s/%s" % (nodeip, mgmtSubnetBits), |
| 156 | "bridgeId": bridgeId, |
| 157 | "dataPlaneIntf": dataPlaneIntf, |
| 158 | "dataPlaneIp": dataPlaneIp |
| 159 | } |
Scott Baker | dcbc52b | 2016-10-12 17:30:34 -0700 | [diff] [blame] | 160 | |
| 161 | # this one is optional |
| 162 | try: |
| 163 | node_dict["hostManagementIface"] = self.get_node_tag(node, "hostManagementIface") |
| 164 | except IndexError: |
| 165 | pass |
| 166 | |
Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 167 | data["apps"]["org.opencord.vtn"]["cordvtn"]["nodes"].append(node_dict) |
| 168 | |
| 169 | # Generate apps->org.onosproject.cordvtn->cordvtn->publicGateways |
| 170 | # Pull the gateway information from vRouter |
Scott Baker | 08a4df3 | 2017-03-15 14:13:59 -0700 | [diff] [blame] | 171 | if model_accessor.has_model_class("VRouterService"): |
| 172 | vrouters = VRouterService.objects.all() |
Andy Bavier | 5901263 | 2016-09-14 08:41:56 -0400 | [diff] [blame] | 173 | if vrouters: |
| 174 | for gateway in vrouters[0].get_gateways(): |
| 175 | gatewayIp = gateway['gateway_ip'].split('/',1)[0] |
| 176 | gatewayMac = gateway['gateway_mac'] |
| 177 | gateway_dict = { |
| 178 | "gatewayIp": gatewayIp, |
| 179 | "gatewayMac": gatewayMac |
| 180 | } |
| 181 | data["apps"]["org.opencord.vtn"]["cordvtn"]["publicGateways"].append(gateway_dict) |
Scott Baker | 08a4df3 | 2017-03-15 14:13:59 -0700 | [diff] [blame] | 182 | else: |
Andy Bavier | 5901263 | 2016-09-14 08:41:56 -0400 | [diff] [blame] | 183 | logger.info("No VRouter service present, not adding publicGateways to config") |
Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 184 | |
| 185 | return json.dumps(data, indent=4, sort_keys=True) |
| 186 | |
| 187 | def call(self, **args): |
Scott Baker | 08a4df3 | 2017-03-15 14:13:59 -0700 | [diff] [blame] | 188 | vtn_service = VTNService.objects.all() |
Andy Bavier | dd9b4d9 | 2016-09-13 14:51:18 -0400 | [diff] [blame] | 189 | if not vtn_service: |
| 190 | raise Exception("No VTN Service") |
| 191 | |
| 192 | vtn_service = vtn_service[0] |
| 193 | |
| 194 | # Check for autogenerate attribute |
| 195 | netcfg = self.get_onos_netcfg(vtn_service) |
| 196 | |
| 197 | tenants = self.get_tenants_who_want_config() |
| 198 | for tenant in tenants: |
| 199 | self.save_tenant_attribute(tenant, "rest_onos/v1/network/configuration/", netcfg) |