blob: bccfe0c81d618b54d621270d5cec0fc897bc0b89 [file] [log] [blame]
Scott Bakerb3a80de2016-09-06 16:51:27 -07001import os
2import requests
3import socket
4import sys
5import base64
6from django.db.models import F, Q
7from xos.config import Config
8from synchronizers.base.syncstep import SyncStep
9from core.models import Service, Network, Port
10from core.models.service import COARSE_KIND
11from services.vtn.vtnnetport import VTNNetwork, VTNPort
12from services.vtn.models import VTNService
13from xos.logger import Logger, logging
14from requests.auth import HTTPBasicAuth
15
16# hpclibrary will be in steps/..
17parentdir = os.path.join(os.path.dirname(__file__),"..")
18sys.path.insert(0,parentdir)
19
20logger = Logger(level=logging.INFO)
21
22# XXX should save and load this
23glo_saved_vtn_maps = []
24glo_saved_networks = {}
25glo_saved_ports = {}
26
27class SyncVTNService(SyncStep):
28 provides=[Service]
29 observes=Service
30 requested_interval=0
31
32 def __init__(self, **args):
33 SyncStep.__init__(self, **args)
34
35 def get_vtn_onos_service(self):
36 from services.onos.models import ONOSService
37
38 vtn_service = ONOSService.get_service_objects().filter(name="ONOS_CORD") # XXX fixme - harcoded
39 if not vtn_service:
40 raise "No VTN Onos Service"
41
42 return vtn_service[0]
43
44 def get_vtn_auth(self):
45 return HTTPBasicAuth('karaf', 'karaf') # XXX fixme - hardcoded auth
46
47 def get_vtn_addr(self):
48 vtn_service = self.get_vtn_onos_service()
49
50 if vtn_service.rest_hostname:
51 return vtn_service.rest_hostname
52
53 if not vtn_service.slices.exists():
54 raise "VTN Service has no slices"
55
56 vtn_slice = vtn_service.slices.all()[0]
57
58 if not vtn_slice.instances.exists():
59 raise "VTN Slice has no instances"
60
61 vtn_instance = vtn_slice.instances.all()[0]
62
63 return vtn_instance.node.name
64
65 def sync_legacy_vtn_api(self):
66 global glo_saved_vtn_maps
67
68 logger.info("sync'ing vtn services")
69
70 vtn_maps = []
71 for service in Service.objects.all():
72 for id in service.get_vtn_src_ids():
73 dependencies = service.get_vtn_dependencies_ids()
74 if dependencies:
75 for dependency in dependencies:
76 vtn_maps.append( (id, dependency) )
77
78 for vtn_map in vtn_maps:
79 if not (vtn_map in glo_saved_vtn_maps):
80 # call vtn rest api to add map
81 url = "http://" + self.get_vtn_addr() + ":8181/onos/cordvtn/service-dependency/%s/%s" % (vtn_map[0], vtn_map[1])
82
83 print "POST %s" % url
84 r = requests.post(url, auth=self.get_vtn_auth() )
85 if (r.status_code != 200):
86 raise Exception("Received error from vtn service (%d)" % r.status_code)
87
88 for vtn_map in glo_saved_vtn_maps:
89 if not vtn_map in vtn_maps:
90 # call vtn rest api to delete map
91 url = "http://" + self.get_vtn_addr() + ":8181/onos/cordvtn/service-dependency/%s/%s" % (vtn_map[0],vtn_map[1])
92
93 print "DELETE %s" % url
94 r = requests.delete(url, auth=self.get_vtn_auth() )
95 if (r.status_code != 200):
96 raise Exception("Received error from vtn service (%d)" % r.status_code)
97
98 glo_saved_vtn_maps = vtn_maps
99 # TODO: save this
100
101 def sync_service_networks(self):
102 valid_ids = []
103 for network in Network.objects.all():
104 network = VTNNetwork(network)
105 valid_ids.append(network.id)
106 if (glo_saved_networks.get(network.id, None) != network.to_dict()):
107 logger.info("POSTing VTN API for network %s" % network.id)
108
109 url = "http://" + self.get_vtn_addr() + ":8181/onos/serviceNetworks/%s" % network.id
110 logger.info("URL: %s" % url)
111
112 data = {"id": network.id,
113 "type": network.type,
114 "providerNetworks": network.providerNetworks}
115 logger.info("DATA: %s" % str(data))
116
117 r=requests.post(url, data=data, auth=self.get_vtn_auth() )
118 if (r.status_code in [200]):
119 glo_saved_networks[network.id] = network.to_dict()
120 else:
121 logger.error("Received error from vtn service (%d)" % r.status_code)
122
123 for network_id in glo_saved_networks.keys():
124 if network_id not in valid_ids:
125 logger.info("DELETEing VTN API for network %s" % network_id)
126
127 url = "http://" + self.get_vtn_addr() + ":8181/onos/serviceNetworks/%s" % network_id
128 logger.info("URL: %s" % url)
129
130 r = requests.delete(url, auth=self.get_vtn_auth() )
131 if (r.status_code in [200,204]):
132 del glo_saved_networks[network_id]
133 else:
134 logger.error("Received error from vtn service (%d)" % r.status_code)
135
136 def sync_service_ports(self):
137 valid_ids = []
138 for port in Port.objects.all():
139 port = VTNPort(port)
140 valid_ids.append(port.id)
141 if (glo_saved_ports.get(port.id, None) != port.to_dict()):
142 logger.info("POSTing VTN API for port %s" % port.id)
143
144 url = "http://" + self.get_vtn_addr() + ":8181/onos/servicePorts/%s" % port.id
145 logger.info("URL: %s" % url)
146
147 data = {"id": port.id,
148 "vlan_id": port.vlan_id,
149 "floating_address_pairs": port.floating_address_pairs}
150 logger.info("DATA: %s" % str(data))
151
152 r=requests.post(url, data=data, auth=self.get_vtn_auth() )
153 if (r.status_code in [200]):
154 glo_saved_ports[port.id] = port.to_dict()
155 else:
156 logger.error("Received error from vtn service (%d)" % r.status_code)
157
158 for port_id in glo_saved_ports.keys():
159 if port_id not in valid_ids:
160 logger.info("DELETEing VTN API for port %s" % port_id)
161
162 url = "http://" + self.get_vtn_addr() + ":8181/onos/servicePorts/%s" % port_id
163 logger.info("URL: %s" % url)
164
165 r = requests.delete(url, auth=self.get_vtn_auth() )
166 if (r.status_code in [200,204]):
167 del glo_saved_ports[port_id]
168 else:
169 logger.error("Received error from vtn service (%d)" % r.status_code)
170
171 def call(self, **args):
172 vtn_service = VTNService.get_service_objects().all()
173 if not vtn_service:
174 raise Exception("No VTN Service")
175
176 vtn_service = vtn_service[0]
177
178 if vtn_service.vtnAPIVersion==2:
179 # version 2 means use new API
180 logger.info("Using New API")
181 self.sync_service_networks()
182 self.sync_service_ports()
183 else:
184 # default to legacy
185 logger.info("Using Old API")
186 self.sync_legacy_vtn_api()
187
188