Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -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 | |
| 17 | import os |
| 18 | import requests |
| 19 | import socket |
| 20 | import sys |
| 21 | import base64 |
| 22 | import json |
| 23 | from synchronizers.new_base.syncstep import SyncStep |
| 24 | from synchronizers.new_base.modelaccessor import * |
| 25 | from xos.logger import Logger, logging |
| 26 | |
| 27 | logger = Logger(level=logging.INFO) |
| 28 | |
| 29 | class SyncONOSNetcfg(SyncStep): |
| 30 | provides=[VTNService] |
| 31 | observes=VTNService |
| 32 | watches=[ModelLink(Node,via='node'), ModelLink(AddressPool,via='addresspool')] |
| 33 | requested_interval=0 |
| 34 | |
| 35 | def __init__(self, **args): |
| 36 | SyncStep.__init__(self, **args) |
| 37 | |
| 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 | |
| 45 | def get_node_tag(self, node, tagname): |
| 46 | tags = Tag.objects.filter(content_type=model_accessor.get_content_type_id(node), |
| 47 | object_id=node.id, |
| 48 | name=tagname) |
| 49 | return tags[0].value |
| 50 | |
| 51 | def get_service_instances_who_want_config(self): |
| 52 | service_instances = [] |
| 53 | # attribute is comma-separated list |
| 54 | for ta in ServiceInstanceAttribute.objects.filter(name="autogenerate"): |
| 55 | if ta.value: |
| 56 | for config in ta.value.split(','): |
| 57 | if config == "vtn-network-cfg": |
| 58 | service_instances.append(ta.service_instance) |
| 59 | return service_instances |
| 60 | |
| 61 | def save_service_instance_attribute(self, service_instance, name, value): |
| 62 | tas = ServiceInstanceAttribute.objects.filter(service_instance_id=service_instance.id, name=name) |
| 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) |
| 71 | ta = model_accessor.create_obj(ServiceInstanceAttribute, service_instance=service_instance, name=name, value=value) |
| 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 | |
| 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 | |
| 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": [], |
| 112 | "nodes" : [], |
| 113 | "controllers": [controllerPort] |
| 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: |
| 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 |
| 144 | |
| 145 | try: |
| 146 | bridgeId = self.get_node_tag(node, "bridgeId") |
| 147 | dataPlaneIntf = self.get_node_tag(node, "dataPlaneIntf") |
| 148 | dataPlaneIp = self.get_node_tag(node, "dataPlaneIp") |
| 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 | } |
| 160 | |
| 161 | # this one is optional |
| 162 | try: |
| 163 | node_dict["hostManagementIface"] = self.get_node_tag(node, "hostManagementIface") |
| 164 | except IndexError: |
| 165 | pass |
| 166 | |
| 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 Address Pool objects |
| 171 | for ap in AddressPool.objects.all(): |
| 172 | if (not ap.gateway_ip) or (not ap.gateway_mac): |
| 173 | logger.info("Gateway_ip or gateway_mac is blank for addresspool %s. Skipping." % ap) |
| 174 | continue |
| 175 | |
| 176 | gateway_dict = { |
| 177 | "gatewayIp": ap.gateway_ip, |
| 178 | "gatewayMac": ap.gateway_mac |
| 179 | } |
| 180 | data["apps"]["org.opencord.vtn"]["cordvtn"]["publicGateways"].append(gateway_dict) |
| 181 | |
| 182 | if not AddressPool.objects.all().exists(): |
| 183 | logger.info("No Address Pools present, not adding publicGateways to config") |
| 184 | |
| 185 | return json.dumps(data, indent=4, sort_keys=True) |
| 186 | |
| 187 | # TODO: Does this step execute every 5 seconds regardless of whether objects have changed? |
| 188 | # If so, what purpose does using watchers serve? |
| 189 | |
| 190 | def call(self, **args): |
| 191 | vtn_service = VTNService.objects.all() |
| 192 | if not vtn_service: |
| 193 | raise Exception("No VTN Service") |
| 194 | |
| 195 | vtn_service = vtn_service[0] |
| 196 | |
| 197 | # Check for autogenerate attribute |
| 198 | netcfg = self.get_onos_netcfg(vtn_service) |
| 199 | |
| 200 | service_instances = self.get_service_instances_who_want_config() |
| 201 | for service_instance in service_instances: |
| 202 | self.save_service_instance_attribute(service_instance, "rest_onos/v1/network/configuration/", netcfg) |