blob: 509c7a0d239b1b978ebd9d9483c83f440d86c411 [file] [log] [blame]
Andy Bavierdd9b4d92016-09-13 14:51:18 -04001import os
2import requests
3import socket
4import sys
5import base64
6import json
7from synchronizers.base.syncstep import SyncStep
8from core.models import Service, Slice, Controller, ControllerSlice, ControllerUser, Node, TenantAttribute, Tag
9from services.vtn.models import VTNService
Andy Bavierdd9b4d92016-09-13 14:51:18 -040010from xos.logger import Logger, logging
11
12logger = Logger(level=logging.INFO)
13
14class SyncONOSNetcfg(SyncStep):
15 provides=[]
16 observes=None
17 requested_interval=0
18
19 def __init__(self, **args):
20 SyncStep.__init__(self, **args)
21
Andy Baviere45ce912016-09-13 17:25:43 -040022 def get_node_tag(self, node, tagname):
Andy Bavierdd9b4d92016-09-13 14:51:18 -040023 tags = Tag.select_by_content_object(node).filter(name=tagname)
24 return tags[0].value
25
26 def get_tenants_who_want_config(self):
27 tenants = []
28 # attribute is comma-separated list
29 for ta in TenantAttribute.objects.filter(name="autogenerate"):
30 if ta.value:
31 for config in ta.value.split(','):
32 if config == "vtn-network-cfg":
33 tenants.append(ta.tenant)
34 return tenants
35
36 def save_tenant_attribute(self, tenant, name, value):
37 tas = TenantAttribute.objects.filter(tenant=tenant, name=name)
38 if tas:
39 ta = tas[0]
40 if ta.value != value:
41 logger.info("updating %s with attribute" % name)
42 ta.value = value
43 ta.save()
44 else:
45 logger.info("saving autogenerated config %s" % name)
46 ta = TenantAttribute(tenant=tenant, name=name, value=value)
47 ta.save()
48
49 # This function currently assumes a single Deployment and Site
50 def get_onos_netcfg(self, vtn):
51 privateGatewayMac = vtn.privateGatewayMac
52 localManagementIp = vtn.localManagementIp
53 ovsdbPort = vtn.ovsdbPort
54 sshPort = vtn.sshPort
55 sshUser = vtn.sshUser
56 sshKeyFile = vtn.sshKeyFile
57 mgmtSubnetBits = vtn.mgmtSubnetBits
58 xosEndpoint = vtn.xosEndpoint
59 xosUser = vtn.xosUser
60 xosPassword = vtn.xosPassword
61
Scott Bakerff016682016-11-28 09:53:13 -080062 controllerPort = vtn.controllerPort
63 if ":" in controllerPort:
64 (c_hostname, c_port) = controllerPort.split(":",1)
65 controllerPort = socket.gethostbyname(c_hostname) + ":" + c_port
66 else:
67 controllerPort = ":" + controllerPort
68
Andy Bavierdd9b4d92016-09-13 14:51:18 -040069 data = {
70 "apps" : {
71 "org.opencord.vtn" : {
72 "cordvtn" : {
73 "privateGatewayMac" : privateGatewayMac,
74 "localManagementIp": localManagementIp,
75 "ovsdbPort": ovsdbPort,
76 "ssh": {
77 "sshPort": sshPort,
78 "sshUser": sshUser,
79 "sshKeyFile": sshKeyFile
80 },
81 "xos": {
82 "endpoint": xosEndpoint,
83 "user": xosUser,
84 "password": xosPassword
85 },
86 "publicGateways": [],
Scott Bakerff016682016-11-28 09:53:13 -080087 "nodes" : [],
88 "controllers": [controllerPort]
Andy Bavierdd9b4d92016-09-13 14:51:18 -040089 }
90 }
91 }
92 }
93
94 # Generate apps->org.opencord.vtn->cordvtn->openstack
95 controllers = Controller.objects.all()
96 if controllers:
97 controller = controllers[0]
98 keystone_server = controller.auth_url
99 user_name = controller.admin_user
100 tenant_name = controller.admin_tenant
101 password = controller.admin_password
102 openstack = {
103 "endpoint": keystone_server,
104 "tenant": tenant_name,
105 "user": user_name,
106 "password": password
107 }
108 data["apps"]["org.opencord.vtn"]["cordvtn"]["openstack"] = openstack
109
110 # Generate apps->org.opencord.vtn->cordvtn->nodes
111 nodes = Node.objects.all()
112 for node in nodes:
113 nodeip = socket.gethostbyname(node.name)
114
115 try:
Andy Baviere45ce912016-09-13 17:25:43 -0400116 bridgeId = self.get_node_tag(node, "bridgeId")
117 dataPlaneIntf = self.get_node_tag(node, "dataPlaneIntf")
118 dataPlaneIp = self.get_node_tag(node, "dataPlaneIp")
Andy Bavierdd9b4d92016-09-13 14:51:18 -0400119 except:
120 logger.error("not adding node %s to the VTN configuration" % node.name)
121 continue
122
123 node_dict = {
124 "hostname": node.name,
125 "hostManagementIp": "%s/%s" % (nodeip, mgmtSubnetBits),
126 "bridgeId": bridgeId,
127 "dataPlaneIntf": dataPlaneIntf,
128 "dataPlaneIp": dataPlaneIp
129 }
Scott Bakerdcbc52b2016-10-12 17:30:34 -0700130
131 # this one is optional
132 try:
133 node_dict["hostManagementIface"] = self.get_node_tag(node, "hostManagementIface")
134 except IndexError:
135 pass
136
Andy Bavierdd9b4d92016-09-13 14:51:18 -0400137 data["apps"]["org.opencord.vtn"]["cordvtn"]["nodes"].append(node_dict)
138
139 # Generate apps->org.onosproject.cordvtn->cordvtn->publicGateways
140 # Pull the gateway information from vRouter
Andy Bavier59012632016-09-14 08:41:56 -0400141 try:
142 from services.vrouter.models import VRouterService
143
144 vrouters = VRouterService.get_service_objects().all()
145 if vrouters:
146 for gateway in vrouters[0].get_gateways():
147 gatewayIp = gateway['gateway_ip'].split('/',1)[0]
148 gatewayMac = gateway['gateway_mac']
149 gateway_dict = {
150 "gatewayIp": gatewayIp,
151 "gatewayMac": gatewayMac
152 }
153 data["apps"]["org.opencord.vtn"]["cordvtn"]["publicGateways"].append(gateway_dict)
154 except:
155 logger.info("No VRouter service present, not adding publicGateways to config")
Andy Bavierdd9b4d92016-09-13 14:51:18 -0400156
157 return json.dumps(data, indent=4, sort_keys=True)
158
159 def call(self, **args):
160 vtn_service = VTNService.get_service_objects().all()
161 if not vtn_service:
162 raise Exception("No VTN Service")
163
164 vtn_service = vtn_service[0]
165
166 # Check for autogenerate attribute
167 netcfg = self.get_onos_netcfg(vtn_service)
168
169 tenants = self.get_tenants_who_want_config()
170 for tenant in tenants:
171 self.save_tenant_attribute(tenant, "rest_onos/v1/network/configuration/", netcfg)