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 |
Scott Baker | 7934f9c | 2017-09-21 11:44:44 -0700 | [diff] [blame] | 23 | from synchronizers.new_base.syncstep import DeferredException |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 24 | from synchronizers.new_base.syncstep import SyncStep |
| 25 | from synchronizers.new_base.modelaccessor import * |
| 26 | from xos.logger import Logger, logging |
| 27 | |
| 28 | logger = Logger(level=logging.INFO) |
| 29 | |
| 30 | class SyncONOSNetcfg(SyncStep): |
| 31 | provides=[VTNService] |
| 32 | observes=VTNService |
| 33 | watches=[ModelLink(Node,via='node'), ModelLink(AddressPool,via='addresspool')] |
| 34 | requested_interval=0 |
| 35 | |
| 36 | def __init__(self, **args): |
| 37 | SyncStep.__init__(self, **args) |
| 38 | |
| 39 | def handle_watched_object(self, o): |
| 40 | logger.info("handle_watched_object is invoked for object %s" % (str(o)),extra=o.tologdict()) |
| 41 | if (type(o) is Node): # For Node add/delete/modify |
| 42 | self.call() |
| 43 | if (type(o) is AddressPool): # For public gateways |
| 44 | self.call() |
| 45 | |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 46 | def get_service_instances_who_want_config(self): |
| 47 | service_instances = [] |
| 48 | # attribute is comma-separated list |
| 49 | for ta in ServiceInstanceAttribute.objects.filter(name="autogenerate"): |
| 50 | if ta.value: |
| 51 | for config in ta.value.split(','): |
| 52 | if config == "vtn-network-cfg": |
| 53 | service_instances.append(ta.service_instance) |
| 54 | return service_instances |
| 55 | |
| 56 | def save_service_instance_attribute(self, service_instance, name, value): |
Scott Baker | ed645f5 | 2017-09-06 13:51:18 -0700 | [diff] [blame] | 57 | # returns True if something changed |
| 58 | something_changed=False |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 59 | tas = ServiceInstanceAttribute.objects.filter(service_instance_id=service_instance.id, name=name) |
| 60 | if tas: |
| 61 | ta = tas[0] |
| 62 | if ta.value != value: |
| 63 | logger.info("updating %s with attribute" % name) |
| 64 | ta.value = value |
Scott Baker | ed645f5 | 2017-09-06 13:51:18 -0700 | [diff] [blame] | 65 | ta.save(update_fields=["value", "updated"], always_update_timestamp=True) |
| 66 | something_changed = True |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 67 | else: |
| 68 | logger.info("saving autogenerated config %s" % name) |
| 69 | ta = model_accessor.create_obj(ServiceInstanceAttribute, service_instance=service_instance, name=name, value=value) |
| 70 | ta.save() |
Scott Baker | ed645f5 | 2017-09-06 13:51:18 -0700 | [diff] [blame] | 71 | something_changed = True |
| 72 | return something_changed |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 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() |
Matteo Scandolo | cf9102a | 2017-12-07 08:31:54 -0800 | [diff] [blame] | 137 | |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 138 | for node in nodes: |
| 139 | try: |
| 140 | nodeip = socket.gethostbyname(node.name) |
| 141 | except socket.gaierror: |
| 142 | logger.warn("unable to resolve hostname %s: node will not be added to config" |
| 143 | % node.name) |
| 144 | continue |
| 145 | |
| 146 | try: |
Matteo Scandolo | cf9102a | 2017-12-07 08:31:54 -0800 | [diff] [blame] | 147 | bridgeId = node.bridgeId |
| 148 | dataPlaneIntf = node.dataPlaneIntf |
| 149 | dataPlaneIp = node.dataPlaneIp |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 150 | except: |
| 151 | logger.error("not adding node %s to the VTN configuration" % node.name) |
| 152 | continue |
| 153 | |
| 154 | node_dict = { |
| 155 | "hostname": node.name, |
| 156 | "hostManagementIp": "%s/%s" % (nodeip, mgmtSubnetBits), |
| 157 | "bridgeId": bridgeId, |
| 158 | "dataPlaneIntf": dataPlaneIntf, |
| 159 | "dataPlaneIp": dataPlaneIp |
| 160 | } |
| 161 | |
| 162 | # this one is optional |
| 163 | try: |
Matteo Scandolo | cf9102a | 2017-12-07 08:31:54 -0800 | [diff] [blame] | 164 | node_dict["hostManagementIface"] = node.hostManagementIface |
| 165 | except AttributeError: |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 166 | pass |
| 167 | |
| 168 | data["apps"]["org.opencord.vtn"]["cordvtn"]["nodes"].append(node_dict) |
| 169 | |
Scott Baker | 7934f9c | 2017-09-21 11:44:44 -0700 | [diff] [blame] | 170 | if not (data["apps"]["org.opencord.vtn"]["cordvtn"]["nodes"]): |
| 171 | raise DeferredException("Waiting for there to be valid nodes") |
| 172 | |
Andy Bavier | 7b321c5 | 2017-08-30 15:33:59 -0700 | [diff] [blame] | 173 | # Generate apps->org.onosproject.cordvtn->cordvtn->publicGateways |
| 174 | # Pull the gateway information from Address Pool objects |
| 175 | for ap in AddressPool.objects.all(): |
| 176 | if (not ap.gateway_ip) or (not ap.gateway_mac): |
| 177 | logger.info("Gateway_ip or gateway_mac is blank for addresspool %s. Skipping." % ap) |
| 178 | continue |
| 179 | |
| 180 | gateway_dict = { |
| 181 | "gatewayIp": ap.gateway_ip, |
| 182 | "gatewayMac": ap.gateway_mac |
| 183 | } |
| 184 | data["apps"]["org.opencord.vtn"]["cordvtn"]["publicGateways"].append(gateway_dict) |
| 185 | |
| 186 | if not AddressPool.objects.all().exists(): |
| 187 | logger.info("No Address Pools present, not adding publicGateways to config") |
| 188 | |
| 189 | return json.dumps(data, indent=4, sort_keys=True) |
| 190 | |
| 191 | # TODO: Does this step execute every 5 seconds regardless of whether objects have changed? |
| 192 | # If so, what purpose does using watchers serve? |
| 193 | |
| 194 | def call(self, **args): |
| 195 | vtn_service = VTNService.objects.all() |
| 196 | if not vtn_service: |
| 197 | raise Exception("No VTN Service") |
| 198 | |
| 199 | vtn_service = vtn_service[0] |
| 200 | |
| 201 | # Check for autogenerate attribute |
| 202 | netcfg = self.get_onos_netcfg(vtn_service) |
| 203 | |
| 204 | service_instances = self.get_service_instances_who_want_config() |
| 205 | for service_instance in service_instances: |
Scott Baker | ed645f5 | 2017-09-06 13:51:18 -0700 | [diff] [blame] | 206 | something_changed = self.save_service_instance_attribute(service_instance, "rest_onos/v1/network/configuration/", netcfg) |
| 207 | if (something_changed): |
| 208 | # make sure we cause the ServiceInstance to be updated as well as the attribute |
| 209 | # TODO: revisit this after synchronizer multi-object-watching is complete |
| 210 | service_instance.save(update_fields=["updated"], always_update_timestamp=True) |