blob: a0e6cdb9a35041d2e6e41825711abf5947aae7c9 [file] [log] [blame]
Scott Bakerc3ce3e72016-06-20 17:35:19 -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, Tenant
10from core.models.service import COARSE_KIND
11from xos.logger import Logger, logging
12from requests.auth import HTTPBasicAuth
13
14# hpclibrary will be in steps/..
15parentdir = os.path.join(os.path.dirname(__file__),"..")
16sys.path.insert(0,parentdir)
17
18logger = Logger(level=logging.INFO)
19
20# XXX should save and load this
21glo_saved_vtn_maps = []
22
23class SyncTenant(SyncStep):
24 provides=[Tenant]
25 observes=Tenant
26 requested_interval=0
27
28 def __init__(self, **args):
29 SyncStep.__init__(self, **args)
30
31 def get_vtn_onos_service(self):
32# vtn_tenant = Tenant.objects.filter(name="VTN_ONOS_app") # XXX fixme - hardcoded
33# if not vtn_tenant:
34# raise "No VTN Onos App found"
35# vtn_tenant = vtn_tenant[0]
36#
37# vtn_service = vtn_tenant.provider_service
38 vtn_service = Service.objects.filter(name="service_ONOS_VTN") # 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_addr(self):
45 vtn_service = self.get_vtn_onos_service()
46
47 if not vtn_service.slices.exists():
48 raise "VTN Service has no slices"
49
50 vtn_slice = vtn_service.slices.all()[0]
51
52 if not vtn_slice.instances.exists():
53 raise "VTN Slice has no instances"
54
55 vtn_instance = vtn_slice.instances.all()[0]
56
57 return vtn_instance.node.name
58
59 def call(self, **args):
60 global glo_saved_vtn_maps
61
62 logger.info("sync'ing vtn services")
63
64 vtn_maps = []
65 for service in Service.objects.all():
66 for id in service.get_vtn_src_ids():
67 dependencies = service.get_vtn_dependencies_ids()
68 if dependencies:
69 for dependency in dependencies:
70 vtn_maps.append( (id, dependency) )
71
72 for vtn_map in vtn_maps:
73 if not (vtn_map in glo_saved_vtn_maps):
74 # call vtn rest api to add map
75 url = "http://" + self.get_vtn_addr() + ":8181/onos/cordvtn/service-dependency/%s/%s" % (vtn_map[0], vtn_map[1])
76
77 print "POST %s" % url
78 r = requests.post(url, auth=HTTPBasicAuth('karaf', 'karaf') ) # XXX fixme - hardcoded auth
79 if (r.status_code != 200):
80 raise Exception("Received error from vtn service (%d)" % r.status_code)
81
82 for vtn_map in glo_saved_vtn_maps:
83 if not vtn_map in vtn_maps:
84 # call vtn rest api to delete map
85 url = "http://" + self.get_vtn_addr() + ":8181/onos/cordvtn/service-dependency/%s/%s" % (vtn_map[0],vtn_map[1])
86
87 print "DELETE %s" % url
88 r = requests.delete(url, auth=HTTPBasicAuth('karaf', 'karaf') ) # XXX fixme - hardcoded auth
89 if (r.status_code != 200):
90 raise Exception("Received error from vtn service (%d)" % r.status_code)
91
92 glo_saved_vtn_maps = vtn_maps
93 # TODO: save this
94