blob: 7e76cc954e45257a33a8a15fec743f6276cefd4c [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
62 data = {
63 "apps" : {
64 "org.opencord.vtn" : {
65 "cordvtn" : {
66 "privateGatewayMac" : privateGatewayMac,
67 "localManagementIp": localManagementIp,
68 "ovsdbPort": ovsdbPort,
69 "ssh": {
70 "sshPort": sshPort,
71 "sshUser": sshUser,
72 "sshKeyFile": sshKeyFile
73 },
74 "xos": {
75 "endpoint": xosEndpoint,
76 "user": xosUser,
77 "password": xosPassword
78 },
79 "publicGateways": [],
80 "nodes" : []
81 }
82 }
83 }
84 }
85
86 # Generate apps->org.opencord.vtn->cordvtn->openstack
87 controllers = Controller.objects.all()
88 if controllers:
89 controller = controllers[0]
90 keystone_server = controller.auth_url
91 user_name = controller.admin_user
92 tenant_name = controller.admin_tenant
93 password = controller.admin_password
94 openstack = {
95 "endpoint": keystone_server,
96 "tenant": tenant_name,
97 "user": user_name,
98 "password": password
99 }
100 data["apps"]["org.opencord.vtn"]["cordvtn"]["openstack"] = openstack
101
102 # Generate apps->org.opencord.vtn->cordvtn->nodes
103 nodes = Node.objects.all()
104 for node in nodes:
105 nodeip = socket.gethostbyname(node.name)
106
107 try:
Andy Baviere45ce912016-09-13 17:25:43 -0400108 bridgeId = self.get_node_tag(node, "bridgeId")
109 dataPlaneIntf = self.get_node_tag(node, "dataPlaneIntf")
110 dataPlaneIp = self.get_node_tag(node, "dataPlaneIp")
Andy Bavierdd9b4d92016-09-13 14:51:18 -0400111 except:
112 logger.error("not adding node %s to the VTN configuration" % node.name)
113 continue
114
115 node_dict = {
116 "hostname": node.name,
117 "hostManagementIp": "%s/%s" % (nodeip, mgmtSubnetBits),
118 "bridgeId": bridgeId,
119 "dataPlaneIntf": dataPlaneIntf,
120 "dataPlaneIp": dataPlaneIp
121 }
Scott Bakerdcbc52b2016-10-12 17:30:34 -0700122
123 # this one is optional
124 try:
125 node_dict["hostManagementIface"] = self.get_node_tag(node, "hostManagementIface")
126 except IndexError:
127 pass
128
Andy Bavierdd9b4d92016-09-13 14:51:18 -0400129 data["apps"]["org.opencord.vtn"]["cordvtn"]["nodes"].append(node_dict)
130
131 # Generate apps->org.onosproject.cordvtn->cordvtn->publicGateways
132 # Pull the gateway information from vRouter
Andy Bavier59012632016-09-14 08:41:56 -0400133 try:
134 from services.vrouter.models import VRouterService
135
136 vrouters = VRouterService.get_service_objects().all()
137 if vrouters:
138 for gateway in vrouters[0].get_gateways():
139 gatewayIp = gateway['gateway_ip'].split('/',1)[0]
140 gatewayMac = gateway['gateway_mac']
141 gateway_dict = {
142 "gatewayIp": gatewayIp,
143 "gatewayMac": gatewayMac
144 }
145 data["apps"]["org.opencord.vtn"]["cordvtn"]["publicGateways"].append(gateway_dict)
146 except:
147 logger.info("No VRouter service present, not adding publicGateways to config")
Andy Bavierdd9b4d92016-09-13 14:51:18 -0400148
149 return json.dumps(data, indent=4, sort_keys=True)
150
151 def call(self, **args):
152 vtn_service = VTNService.get_service_objects().all()
153 if not vtn_service:
154 raise Exception("No VTN Service")
155
156 vtn_service = vtn_service[0]
157
158 # Check for autogenerate attribute
159 netcfg = self.get_onos_netcfg(vtn_service)
160
161 tenants = self.get_tenants_who_want_config()
162 for tenant in tenants:
163 self.save_tenant_attribute(tenant, "rest_onos/v1/network/configuration/", netcfg)