blob: 05b16cb001abfb0d2b8dd6677087fa9b10057e66 [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 Scandolodb7508e2018-03-19 15:06:06 -070020import requests
Matteo Scandoloa420aba2018-04-10 17:00:44 -070021from requests.auth import HTTPBasicAuth
Matteo Scandolodb7508e2018-03-19 15:06:06 -070022
Matteo Scandolodb7508e2018-03-19 15:06:06 -070023
Matteo Scandolodb7508e2018-03-19 15:06:06 -070024class SyncVSGHWServiceInstance(SyncStep):
25 provides = [VSGHWServiceInstance]
Matteo Scandolo7367b262018-05-23 14:56:23 -070026 log = create_logger(Config().get('logging'))
Matteo Scandolodb7508e2018-03-19 15:06:06 -070027
28 observes = VSGHWServiceInstance
29
Matteo Scandoloa420aba2018-04-10 17:00:44 -070030 @staticmethod
31 def format_url(url):
32 if 'http' in url:
33 return url
34 else:
35 return 'http://%s' % url
36
37 @staticmethod
38 def get_fabric_onos_info(si):
39
40 # get the vsg-hw service
41 vsg_hw = si.owner
42
43 # get the onos_fabric service
44 fabric_onos = [s.leaf_model for s in vsg_hw.provider_services if "onos" in s.name.lower()]
45
46 if len(fabric_onos) == 0:
47 raise Exception('Cannot find ONOS service in provider_services of vSG-HW')
48
49 fabric_onos = fabric_onos[0]
50
51 return {
52 'url': SyncVSGHWServiceInstance.format_url("%s:%s" % (fabric_onos.rest_hostname, fabric_onos.rest_port)),
53 'user': fabric_onos.rest_username,
54 'pass': fabric_onos.rest_password
55 }
56
Matteo Scandolodb7508e2018-03-19 15:06:06 -070057 def sync_record(self, o):
Matteo Scandolo7367b262018-05-23 14:56:23 -070058 self.log.info("Sync'ing VSG-HW Service Instance", service_instance=o)
Matteo Scandolodb7508e2018-03-19 15:06:06 -070059
Matteo Scandoloa420aba2018-04-10 17:00:44 -070060
61 onos = SyncVSGHWServiceInstance.get_fabric_onos_info(o)
62
63 si = ServiceInstance.objects.get(id=o.id)
64
65 mac_address = si.get_westbound_service_instance_properties("mac_address")
Matteo Scandolo7367b262018-05-23 14:56:23 -070066 ip = si.get_westbound_service_instance_properties("ip_address")
Matteo Scandoloa420aba2018-04-10 17:00:44 -070067 s_tag = si.get_westbound_service_instance_properties("s_tag")
68 c_tag = si.get_westbound_service_instance_properties("c_tag")
Matteo Scandoloa420aba2018-04-10 17:00:44 -070069 dpid = si.get_westbound_service_instance_properties("switch_datapath_id")
70 port = si.get_westbound_service_instance_properties("switch_port")
71
Matteo Scandolo7367b262018-05-23 14:56:23 -070072 try:
73 if not mac_address:
74 raise ValueError("mac_address")
75 if not ip:
76 raise ValueError("ip_address")
77 if not s_tag:
78 raise ValueError("s_tag")
79 if not c_tag:
80 raise ValueError("c_tag")
81 if not dpid:
82 raise ValueError("switch_datapath_id")
83 if not port:
84 raise ValueError("switch_port")
85 except ValueError as e:
86 raise DeferredException("Skipping synchronization for VSG-HW Service Instance with id %s as %s is not available" % (o.id, e.message))
87
Matteo Scandoloa420aba2018-04-10 17:00:44 -070088 data = {
89 'hosts': {
90 mac_address + "/" + str(s_tag): {
91 "basic": {
92 "ips": [ip],
93 "locations": ["%s/%s" % (dpid, port)],
94 "innerVlan": str(c_tag),
95 }
96 }
97 }
98 }
99
100 # Adding the optional tpid
101 tpid = si.get_westbound_service_instance_properties("outer_tpid")
102 if tpid:
103 data["hosts"][mac_address + "/" + str(s_tag)]["basic"]["outerTpid"] = str(tpid)
104
105 url = onos['url'] + '/onos/v1/network/configuration'
106
Matteo Scandolo7367b262018-05-23 14:56:23 -0700107 self.log.info("Sending requests to ONOS", url=url, body=data)
Matteo Scandoloa420aba2018-04-10 17:00:44 -0700108
109 r = requests.post(url, json=data, auth=HTTPBasicAuth(onos['user'], onos['pass']))
110
111 if r.status_code != 200:
112 raise Exception("Failed to terminate subscriber in ONOS: %s" % r.text)
113
Matteo Scandolo7367b262018-05-23 14:56:23 -0700114 self.log.info("ONOS response", res=r.text)
Matteo Scandoloa420aba2018-04-10 17:00:44 -0700115
Matteo Scandolodb7508e2018-03-19 15:06:06 -0700116 def delete_record(self, o):
Matteo Scandolo7367b262018-05-23 14:56:23 -0700117 self.log.info("Deleting VSG-HW Service Instance", service_instance=o)
Matteo Scandolodb7508e2018-03-19 15:06:06 -0700118 pass