blob: 87d7a50544257189de9454f16d4106c863f2a34a [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
10from services.vrouter.models import VRouterService
11from xos.logger import Logger, logging
12
13logger = Logger(level=logging.INFO)
14
15class SyncONOSNetcfg(SyncStep):
16 provides=[]
17 observes=None
18 requested_interval=0
19
20 def __init__(self, **args):
21 SyncStep.__init__(self, **args)
22
23 def get_node_tag(self, o, node, tagname):
24 tags = Tag.select_by_content_object(node).filter(name=tagname)
25 return tags[0].value
26
27 def get_tenants_who_want_config(self):
28 tenants = []
29 # attribute is comma-separated list
30 for ta in TenantAttribute.objects.filter(name="autogenerate"):
31 if ta.value:
32 for config in ta.value.split(','):
33 if config == "vtn-network-cfg":
34 tenants.append(ta.tenant)
35 return tenants
36
37 def save_tenant_attribute(self, tenant, name, value):
38 tas = TenantAttribute.objects.filter(tenant=tenant, name=name)
39 if tas:
40 ta = tas[0]
41 if ta.value != value:
42 logger.info("updating %s with attribute" % name)
43 ta.value = value
44 ta.save()
45 else:
46 logger.info("saving autogenerated config %s" % name)
47 ta = TenantAttribute(tenant=tenant, name=name, value=value)
48 ta.save()
49
50 # This function currently assumes a single Deployment and Site
51 def get_onos_netcfg(self, vtn):
52 privateGatewayMac = vtn.privateGatewayMac
53 localManagementIp = vtn.localManagementIp
54 ovsdbPort = vtn.ovsdbPort
55 sshPort = vtn.sshPort
56 sshUser = vtn.sshUser
57 sshKeyFile = vtn.sshKeyFile
58 mgmtSubnetBits = vtn.mgmtSubnetBits
59 xosEndpoint = vtn.xosEndpoint
60 xosUser = vtn.xosUser
61 xosPassword = vtn.xosPassword
62
63 data = {
64 "apps" : {
65 "org.opencord.vtn" : {
66 "cordvtn" : {
67 "privateGatewayMac" : privateGatewayMac,
68 "localManagementIp": localManagementIp,
69 "ovsdbPort": ovsdbPort,
70 "ssh": {
71 "sshPort": sshPort,
72 "sshUser": sshUser,
73 "sshKeyFile": sshKeyFile
74 },
75 "xos": {
76 "endpoint": xosEndpoint,
77 "user": xosUser,
78 "password": xosPassword
79 },
80 "publicGateways": [],
81 "nodes" : []
82 }
83 }
84 }
85 }
86
87 # Generate apps->org.opencord.vtn->cordvtn->openstack
88 controllers = Controller.objects.all()
89 if controllers:
90 controller = controllers[0]
91 keystone_server = controller.auth_url
92 user_name = controller.admin_user
93 tenant_name = controller.admin_tenant
94 password = controller.admin_password
95 openstack = {
96 "endpoint": keystone_server,
97 "tenant": tenant_name,
98 "user": user_name,
99 "password": password
100 }
101 data["apps"]["org.opencord.vtn"]["cordvtn"]["openstack"] = openstack
102
103 # Generate apps->org.opencord.vtn->cordvtn->nodes
104 nodes = Node.objects.all()
105 for node in nodes:
106 nodeip = socket.gethostbyname(node.name)
107
108 try:
109 bridgeId = self.get_node_tag(o, node, "bridgeId")
110 dataPlaneIntf = self.get_node_tag(o, node, "dataPlaneIntf")
111 dataPlaneIp = self.get_node_tag(o, node, "dataPlaneIp")
112 except:
113 logger.error("not adding node %s to the VTN configuration" % node.name)
114 continue
115
116 node_dict = {
117 "hostname": node.name,
118 "hostManagementIp": "%s/%s" % (nodeip, mgmtSubnetBits),
119 "bridgeId": bridgeId,
120 "dataPlaneIntf": dataPlaneIntf,
121 "dataPlaneIp": dataPlaneIp
122 }
123 data["apps"]["org.opencord.vtn"]["cordvtn"]["nodes"].append(node_dict)
124
125 # Generate apps->org.onosproject.cordvtn->cordvtn->publicGateways
126 # Pull the gateway information from vRouter
127 vrouters = VRouterService.get_service_objects().all()
128 if vrouters:
129 for gateway in vrouters[0].get_gateways():
130 gatewayIp = gateway['gateway_ip'].split('/',1)[0]
131 gatewayMac = gateway['gateway_mac']
132 gateway_dict = {
133 "gatewayIp": gatewayIp,
134 "gatewayMac": gatewayMac
135 }
136 data["apps"]["org.opencord.vtn"]["cordvtn"]["publicGateways"].append(gateway_dict)
137
138 return json.dumps(data, indent=4, sort_keys=True)
139
140 def call(self, **args):
141 vtn_service = VTNService.get_service_objects().all()
142 if not vtn_service:
143 raise Exception("No VTN Service")
144
145 vtn_service = vtn_service[0]
146
147 # Check for autogenerate attribute
148 netcfg = self.get_onos_netcfg(vtn_service)
149
150 tenants = self.get_tenants_who_want_config()
151 for tenant in tenants:
152 self.save_tenant_attribute(tenant, "rest_onos/v1/network/configuration/", netcfg)