blob: 67dc878afd10715d2d6caf5e6917096e3e734c77 [file] [log] [blame]
Matteo Scandolo0e7912c2017-08-08 13:05:24 -07001
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 Bavierdd9b4d92016-09-13 14:51:18 -040017import os
18import requests
19import socket
20import sys
21import base64
22import json
Scott Baker61b41a92017-03-14 13:04:57 -070023from synchronizers.new_base.syncstep import SyncStep
24from synchronizers.new_base.modelaccessor import *
Andy Bavierdd9b4d92016-09-13 14:51:18 -040025from xos.logger import Logger, logging
26
27logger = Logger(level=logging.INFO)
28
29class SyncONOSNetcfg(SyncStep):
Andy Bavier51d7ebb2016-12-06 16:20:34 -050030 provides=[VTNService]
31 observes=VTNService
32 watches=[ModelLink(Node,via='node'), ModelLink(AddressPool,via='addresspool')]
Andy Bavierdd9b4d92016-09-13 14:51:18 -040033 requested_interval=0
34
35 def __init__(self, **args):
36 SyncStep.__init__(self, **args)
37
Andy Bavier51d7ebb2016-12-06 16:20:34 -050038 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 Baviere45ce912016-09-13 17:25:43 -040045 def get_node_tag(self, node, tagname):
Scott Baker08a4df32017-03-15 14:13:59 -070046 tags = Tag.objects.filter(content_type=model_accessor.get_content_type_id(node),
47 object_id=node.id,
48 name=tagname)
Andy Bavierdd9b4d92016-09-13 14:51:18 -040049 return tags[0].value
50
Scott Bakerd2439532017-08-11 09:55:50 -070051 def get_service_instances_who_want_config(self):
52 service_instances = []
Andy Bavierdd9b4d92016-09-13 14:51:18 -040053 # attribute is comma-separated list
Scott Bakerd949b622017-07-18 12:10:35 -070054 for ta in ServiceInstanceAttribute.objects.filter(name="autogenerate"):
Andy Bavierdd9b4d92016-09-13 14:51:18 -040055 if ta.value:
56 for config in ta.value.split(','):
57 if config == "vtn-network-cfg":
Scott Bakerd2439532017-08-11 09:55:50 -070058 service_instances.append(ta.service_instance)
59 return service_instances
Andy Bavierdd9b4d92016-09-13 14:51:18 -040060
Scott Bakerd2439532017-08-11 09:55:50 -070061 def save_service_instance_attribute(self, service_instance, name, value):
62 tas = ServiceInstanceAttribute.objects.filter(service_instance_id=service_instance.id, name=name)
Andy Bavierdd9b4d92016-09-13 14:51:18 -040063 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 Bakerd2439532017-08-11 09:55:50 -070071 ta = model_accessor.create_obj(ServiceInstanceAttribute, service_instance=service_instance, name=name, value=value)
Andy Bavierdd9b4d92016-09-13 14:51:18 -040072 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 Bakerff016682016-11-28 09:53:13 -080087 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 Bavierdd9b4d92016-09-13 14:51:18 -040094 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 Bakerff016682016-11-28 09:53:13 -0800112 "nodes" : [],
113 "controllers": [controllerPort]
Andy Bavierdd9b4d92016-09-13 14:51:18 -0400114 }
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 Hartd6599202017-07-25 11:39:58 -0700138 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 Bavierdd9b4d92016-09-13 14:51:18 -0400144
145 try:
Andy Baviere45ce912016-09-13 17:25:43 -0400146 bridgeId = self.get_node_tag(node, "bridgeId")
147 dataPlaneIntf = self.get_node_tag(node, "dataPlaneIntf")
148 dataPlaneIp = self.get_node_tag(node, "dataPlaneIp")
Andy Bavierdd9b4d92016-09-13 14:51:18 -0400149 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 Bakerdcbc52b2016-10-12 17:30:34 -0700160
161 # this one is optional
162 try:
163 node_dict["hostManagementIface"] = self.get_node_tag(node, "hostManagementIface")
164 except IndexError:
165 pass
166
Andy Bavierdd9b4d92016-09-13 14:51:18 -0400167 data["apps"]["org.opencord.vtn"]["cordvtn"]["nodes"].append(node_dict)
168
169 # Generate apps->org.onosproject.cordvtn->cordvtn->publicGateways
Scott Bakerd2439532017-08-11 09:55:50 -0700170 # 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")
Andy Bavierdd9b4d92016-09-13 14:51:18 -0400184
185 return json.dumps(data, indent=4, sort_keys=True)
186
Scott Bakerd2439532017-08-11 09:55:50 -0700187 # TODO: Does this step execute every 5 seconds regardless of whether objects have changed?
188 # If so, what purpose does using watchers serve?
189
Andy Bavierdd9b4d92016-09-13 14:51:18 -0400190 def call(self, **args):
Scott Baker08a4df32017-03-15 14:13:59 -0700191 vtn_service = VTNService.objects.all()
Andy Bavierdd9b4d92016-09-13 14:51:18 -0400192 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
Scott Bakerd2439532017-08-11 09:55:50 -0700200 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)