blob: 8156a2049baa38886c0458d35f700c4aaf4c3e6d [file] [log] [blame]
Andy Bavierdd9b4d92016-09-13 14:51:18 -04001import os
2import requests
3import socket
4import sys
5import base64
6import json
Scott Baker61b41a92017-03-14 13:04:57 -07007from synchronizers.new_base.syncstep import SyncStep
8from synchronizers.new_base.modelaccessor import *
Andy Bavierdd9b4d92016-09-13 14:51:18 -04009from xos.logger import Logger, logging
10
11logger = Logger(level=logging.INFO)
12
13class SyncONOSNetcfg(SyncStep):
Andy Bavier51d7ebb2016-12-06 16:20:34 -050014 provides=[VTNService]
15 observes=VTNService
16 watches=[ModelLink(Node,via='node'), ModelLink(AddressPool,via='addresspool')]
Andy Bavierdd9b4d92016-09-13 14:51:18 -040017 requested_interval=0
18
19 def __init__(self, **args):
20 SyncStep.__init__(self, **args)
21
Andy Bavier51d7ebb2016-12-06 16:20:34 -050022 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 Baviere45ce912016-09-13 17:25:43 -040029 def get_node_tag(self, node, tagname):
Scott Baker08a4df32017-03-15 14:13:59 -070030 tags = Tag.objects.filter(content_type=model_accessor.get_content_type_id(node),
31 object_id=node.id,
32 name=tagname)
Andy Bavierdd9b4d92016-09-13 14:51:18 -040033 return tags[0].value
34
35 def get_tenants_who_want_config(self):
36 tenants = []
37 # attribute is comma-separated list
Scott Bakerd949b622017-07-18 12:10:35 -070038 for ta in ServiceInstanceAttribute.objects.filter(name="autogenerate"):
Andy Bavierdd9b4d92016-09-13 14:51:18 -040039 if ta.value:
40 for config in ta.value.split(','):
41 if config == "vtn-network-cfg":
Scott Bakerd949b622017-07-18 12:10:35 -070042 tenants.append(ta.service_instance)
Andy Bavierdd9b4d92016-09-13 14:51:18 -040043 return tenants
44
45 def save_tenant_attribute(self, tenant, name, value):
Scott Bakerd949b622017-07-18 12:10:35 -070046 tas = ServiceInstanceAttribute.objects.filter(service_instance_id=tenant.id, name=name)
Andy Bavierdd9b4d92016-09-13 14:51:18 -040047 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)
Scott Bakerd949b622017-07-18 12:10:35 -070055 ta = model_accessor.create_obj(ServiceInstanceAttribute, service_instance=tenant, name=name, value=value)
Andy Bavierdd9b4d92016-09-13 14:51:18 -040056 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 Bakerff016682016-11-28 09:53:13 -080071 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 Bavierdd9b4d92016-09-13 14:51:18 -040078 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 Bakerff016682016-11-28 09:53:13 -080096 "nodes" : [],
97 "controllers": [controllerPort]
Andy Bavierdd9b4d92016-09-13 14:51:18 -040098 }
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 Baviere45ce912016-09-13 17:25:43 -0400125 bridgeId = self.get_node_tag(node, "bridgeId")
126 dataPlaneIntf = self.get_node_tag(node, "dataPlaneIntf")
127 dataPlaneIp = self.get_node_tag(node, "dataPlaneIp")
Andy Bavierdd9b4d92016-09-13 14:51:18 -0400128 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 Bakerdcbc52b2016-10-12 17:30:34 -0700139
140 # this one is optional
141 try:
142 node_dict["hostManagementIface"] = self.get_node_tag(node, "hostManagementIface")
143 except IndexError:
144 pass
145
Andy Bavierdd9b4d92016-09-13 14:51:18 -0400146 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 Baker08a4df32017-03-15 14:13:59 -0700150 if model_accessor.has_model_class("VRouterService"):
151 vrouters = VRouterService.objects.all()
Andy Bavier59012632016-09-14 08:41:56 -0400152 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 Baker08a4df32017-03-15 14:13:59 -0700161 else:
Andy Bavier59012632016-09-14 08:41:56 -0400162 logger.info("No VRouter service present, not adding publicGateways to config")
Andy Bavierdd9b4d92016-09-13 14:51:18 -0400163
164 return json.dumps(data, indent=4, sort_keys=True)
165
166 def call(self, **args):
Scott Baker08a4df32017-03-15 14:13:59 -0700167 vtn_service = VTNService.objects.all()
Andy Bavierdd9b4d92016-09-13 14:51:18 -0400168 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)