blob: b30987f8f8b0cc6e394fa7daf7d4bbb493340ea6 [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 Scandoloa594eba2018-11-01 14:59:54 -070024log = create_logger(Config().get('logging'))
Matteo Scandolodb7508e2018-03-19 15:06:06 -070025
Matteo Scandolodb7508e2018-03-19 15:06:06 -070026class SyncVSGHWServiceInstance(SyncStep):
27 provides = [VSGHWServiceInstance]
Matteo Scandolo7367b262018-05-23 14:56:23 -070028 log = create_logger(Config().get('logging'))
Matteo Scandolodb7508e2018-03-19 15:06:06 -070029
30 observes = VSGHWServiceInstance
31
Matteo Scandoloa420aba2018-04-10 17:00:44 -070032 @staticmethod
33 def format_url(url):
34 if 'http' in url:
35 return url
36 else:
37 return 'http://%s' % url
38
39 @staticmethod
40 def get_fabric_onos_info(si):
41
42 # get the vsg-hw service
43 vsg_hw = si.owner
44
45 # get the onos_fabric service
46 fabric_onos = [s.leaf_model for s in vsg_hw.provider_services if "onos" in s.name.lower()]
47
48 if len(fabric_onos) == 0:
49 raise Exception('Cannot find ONOS service in provider_services of vSG-HW')
50
51 fabric_onos = fabric_onos[0]
52
53 return {
54 'url': SyncVSGHWServiceInstance.format_url("%s:%s" % (fabric_onos.rest_hostname, fabric_onos.rest_port)),
55 'user': fabric_onos.rest_username,
56 'pass': fabric_onos.rest_password
57 }
58
Matteo Scandolodb7508e2018-03-19 15:06:06 -070059 def sync_record(self, o):
Matteo Scandoloa594eba2018-11-01 14:59:54 -070060 log.info("Sync'ing VSG-HW Service Instance", service_instance=o)
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 Scandoloa594eba2018-11-01 14:59:54 -070067 ips = si.get_westbound_service_instance_properties("ips")
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 Scandoloa0398fb2018-06-21 11:38:05 -070073 subscriber_status = si.get_westbound_service_instance_properties("status")
74
75 if subscriber_status != "enabled":
76 if o.enacted:
77 return self.delete_record(o)
78 else:
79 raise DeferredException("Deferring synchronization for VSG-HW Service Instance with id %s as subscriber is not enabled" % o.id)
80
Matteo Scandolo7367b262018-05-23 14:56:23 -070081 try:
82 if not mac_address:
83 raise ValueError("mac_address")
Matteo Scandoloa594eba2018-11-01 14:59:54 -070084 if len(ips.all()) == 0:
85 raise ValueError("ips")
Matteo Scandolo7367b262018-05-23 14:56:23 -070086 if not s_tag:
87 raise ValueError("s_tag")
88 if not c_tag:
89 raise ValueError("c_tag")
90 if not dpid:
91 raise ValueError("switch_datapath_id")
92 if not port:
93 raise ValueError("switch_port")
94 except ValueError as e:
Matteo Scandoloa594eba2018-11-01 14:59:54 -070095 log.info("Skipping synchronization for VSG-HW Service Instance with id %s as westbound value %s is not available" % (o.id, e.message))
Matteo Scandolo0bf7c552018-06-19 16:33:04 -070096 raise Exception("Skipping synchronization for VSG-HW Service Instance with id %s as westbound value %s is not available" % (o.id, e.message))
97
Matteo Scandoloa420aba2018-04-10 17:00:44 -070098 data = {
99 'hosts': {
100 mac_address + "/" + str(s_tag): {
101 "basic": {
Matteo Scandoloa594eba2018-11-01 14:59:54 -0700102 "ips": [ i.ip for i in ips.all() ],
Matteo Scandoloa420aba2018-04-10 17:00:44 -0700103 "locations": ["%s/%s" % (dpid, port)],
104 "innerVlan": str(c_tag),
105 }
106 }
107 }
108 }
109
110 # Adding the optional tpid
111 tpid = si.get_westbound_service_instance_properties("outer_tpid")
112 if tpid:
113 data["hosts"][mac_address + "/" + str(s_tag)]["basic"]["outerTpid"] = str(tpid)
114
115 url = onos['url'] + '/onos/v1/network/configuration'
116
Matteo Scandoloa594eba2018-11-01 14:59:54 -0700117 log.info("Sending requests to ONOS", url=url, body=data)
Matteo Scandoloa420aba2018-04-10 17:00:44 -0700118
119 r = requests.post(url, json=data, auth=HTTPBasicAuth(onos['user'], onos['pass']))
120
121 if r.status_code != 200:
122 raise Exception("Failed to terminate subscriber in ONOS: %s" % r.text)
123
Matteo Scandoloa594eba2018-11-01 14:59:54 -0700124 log.info("ONOS response", res=r.text)
Matteo Scandoloa420aba2018-04-10 17:00:44 -0700125
Matteo Scandolodb7508e2018-03-19 15:06:06 -0700126 def delete_record(self, o):
Matteo Scandoloa594eba2018-11-01 14:59:54 -0700127 log.info("Deleting VSG-HW Service Instance", service_instance=o)
Matteo Scandolo0bf7c552018-06-19 16:33:04 -0700128 if o.enacted:
129 onos = SyncVSGHWServiceInstance.get_fabric_onos_info(o)
130
131 si = ServiceInstance.objects.get(id=o.id)
132
133 mac_address = si.get_westbound_service_instance_properties("mac_address")
134 s_tag = si.get_westbound_service_instance_properties("s_tag")
135
136 key = "%s/%s" % (mac_address, str(s_tag))
137 key = urllib.quote(key, safe='')
138
139 url = onos['url'] + '/onos/v1/network/configuration/hosts/%s' % key
140
141 r = requests.delete(url, auth=HTTPBasicAuth(onos['user'], onos['pass']))
142
Matteo Scandolo5d424c12018-06-20 17:53:45 -0700143 if r.status_code != 204:
Matteo Scandolo0bf7c552018-06-19 16:33:04 -0700144 raise Exception("Failed to remove subscriber termination in ONOS: %s" % r.text)
145
Matteo Scandoloa594eba2018-11-01 14:59:54 -0700146 log.info("ONOS response", res=r.text)
Matteo Scandolodb7508e2018-03-19 15:06:06 -0700147 pass