blob: 6f79ca2646be796b38c6ea17ddef785b888c2672 [file] [log] [blame]
Matteo Scandolodb7508e2018-03-19 15:06:06 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Matteo Scandolo7367b262018-05-23 14:56:23 -070015from synchronizers.new_base.syncstep import SyncStep, DeferredException
Matteo Scandoloa420aba2018-04-10 17:00:44 -070016from synchronizers.new_base.modelaccessor import model_accessor, VSGHWServiceInstance, ServiceInstance
Matteo Scandolodb7508e2018-03-19 15:06:06 -070017
18from xosconfig import Config
19from multistructlog import create_logger
Matteo Scandolo0bf7c552018-06-19 16:33:04 -070020import urllib
Matteo Scandolodb7508e2018-03-19 15:06:06 -070021import requests
Matteo Scandoloa420aba2018-04-10 17:00:44 -070022from requests.auth import HTTPBasicAuth
Matteo Scandolodb7508e2018-03-19 15:06:06 -070023
Matteo Scandolodb7508e2018-03-19 15:06:06 -070024
Matteo Scandolodb7508e2018-03-19 15:06:06 -070025class SyncVSGHWServiceInstance(SyncStep):
26 provides = [VSGHWServiceInstance]
Matteo Scandolo7367b262018-05-23 14:56:23 -070027 log = create_logger(Config().get('logging'))
Matteo Scandolodb7508e2018-03-19 15:06:06 -070028
29 observes = VSGHWServiceInstance
30
Matteo Scandoloa420aba2018-04-10 17:00:44 -070031 @staticmethod
32 def format_url(url):
33 if 'http' in url:
34 return url
35 else:
36 return 'http://%s' % url
37
38 @staticmethod
39 def get_fabric_onos_info(si):
40
41 # get the vsg-hw service
42 vsg_hw = si.owner
43
44 # get the onos_fabric service
45 fabric_onos = [s.leaf_model for s in vsg_hw.provider_services if "onos" in s.name.lower()]
46
47 if len(fabric_onos) == 0:
48 raise Exception('Cannot find ONOS service in provider_services of vSG-HW')
49
50 fabric_onos = fabric_onos[0]
51
52 return {
53 'url': SyncVSGHWServiceInstance.format_url("%s:%s" % (fabric_onos.rest_hostname, fabric_onos.rest_port)),
54 'user': fabric_onos.rest_username,
55 'pass': fabric_onos.rest_password
56 }
57
Matteo Scandolodb7508e2018-03-19 15:06:06 -070058 def sync_record(self, o):
Matteo Scandolo7367b262018-05-23 14:56:23 -070059 self.log.info("Sync'ing VSG-HW Service Instance", service_instance=o)
Matteo Scandolodb7508e2018-03-19 15:06:06 -070060
Matteo Scandoloa420aba2018-04-10 17:00:44 -070061
62 onos = SyncVSGHWServiceInstance.get_fabric_onos_info(o)
63
64 si = ServiceInstance.objects.get(id=o.id)
65
66 mac_address = si.get_westbound_service_instance_properties("mac_address")
Matteo Scandolo7367b262018-05-23 14:56:23 -070067 ip = si.get_westbound_service_instance_properties("ip_address")
Matteo Scandoloa420aba2018-04-10 17:00:44 -070068 s_tag = si.get_westbound_service_instance_properties("s_tag")
69 c_tag = si.get_westbound_service_instance_properties("c_tag")
Matteo Scandoloa420aba2018-04-10 17:00:44 -070070 dpid = si.get_westbound_service_instance_properties("switch_datapath_id")
71 port = si.get_westbound_service_instance_properties("switch_port")
72
Matteo Scandolo7367b262018-05-23 14:56:23 -070073 try:
74 if not mac_address:
75 raise ValueError("mac_address")
76 if not ip:
77 raise ValueError("ip_address")
78 if not s_tag:
79 raise ValueError("s_tag")
80 if not c_tag:
81 raise ValueError("c_tag")
82 if not dpid:
83 raise ValueError("switch_datapath_id")
84 if not port:
85 raise ValueError("switch_port")
86 except ValueError as e:
Matteo Scandolo0bf7c552018-06-19 16:33:04 -070087 raise Exception("Skipping synchronization for VSG-HW Service Instance with id %s as westbound value %s is not available" % (o.id, e.message))
88
89 subscriber_status = si.get_westbound_service_instance_properties("status")
90
91 if subscriber_status != "enabled":
92 if o.enacted:
93 return self.delete_record(o)
94 else:
95 raise DeferredException("Deferring synchronization for VSG-HW Service Instance with id %s as subscriber is not enabled" % o.id)
Matteo Scandolo7367b262018-05-23 14:56:23 -070096
Matteo Scandoloa420aba2018-04-10 17:00:44 -070097 data = {
98 'hosts': {
99 mac_address + "/" + str(s_tag): {
100 "basic": {
101 "ips": [ip],
102 "locations": ["%s/%s" % (dpid, port)],
103 "innerVlan": str(c_tag),
104 }
105 }
106 }
107 }
108
109 # Adding the optional tpid
110 tpid = si.get_westbound_service_instance_properties("outer_tpid")
111 if tpid:
112 data["hosts"][mac_address + "/" + str(s_tag)]["basic"]["outerTpid"] = str(tpid)
113
114 url = onos['url'] + '/onos/v1/network/configuration'
115
Matteo Scandolo7367b262018-05-23 14:56:23 -0700116 self.log.info("Sending requests to ONOS", url=url, body=data)
Matteo Scandoloa420aba2018-04-10 17:00:44 -0700117
118 r = requests.post(url, json=data, auth=HTTPBasicAuth(onos['user'], onos['pass']))
119
120 if r.status_code != 200:
121 raise Exception("Failed to terminate subscriber in ONOS: %s" % r.text)
122
Matteo Scandolo7367b262018-05-23 14:56:23 -0700123 self.log.info("ONOS response", res=r.text)
Matteo Scandoloa420aba2018-04-10 17:00:44 -0700124
Matteo Scandolodb7508e2018-03-19 15:06:06 -0700125 def delete_record(self, o):
Matteo Scandolo7367b262018-05-23 14:56:23 -0700126 self.log.info("Deleting VSG-HW Service Instance", service_instance=o)
Matteo Scandolo0bf7c552018-06-19 16:33:04 -0700127 if o.enacted:
128 onos = SyncVSGHWServiceInstance.get_fabric_onos_info(o)
129
130 si = ServiceInstance.objects.get(id=o.id)
131
132 mac_address = si.get_westbound_service_instance_properties("mac_address")
133 s_tag = si.get_westbound_service_instance_properties("s_tag")
134
135 key = "%s/%s" % (mac_address, str(s_tag))
136 key = urllib.quote(key, safe='')
137
138 url = onos['url'] + '/onos/v1/network/configuration/hosts/%s' % key
139
140 r = requests.delete(url, auth=HTTPBasicAuth(onos['user'], onos['pass']))
141
142 if r.status_code != 200:
143 raise Exception("Failed to remove subscriber termination in ONOS: %s" % r.text)
144
145 self.log.info("ONOS response", res=r.text)
Matteo Scandolodb7508e2018-03-19 15:06:06 -0700146 pass