Matteo Scandolo | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 1 | # 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 Scandolo | 7367b26 | 2018-05-23 14:56:23 -0700 | [diff] [blame] | 15 | from synchronizers.new_base.syncstep import SyncStep, DeferredException |
Matteo Scandolo | a420aba | 2018-04-10 17:00:44 -0700 | [diff] [blame] | 16 | from synchronizers.new_base.modelaccessor import model_accessor, VSGHWServiceInstance, ServiceInstance |
Matteo Scandolo | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 17 | |
| 18 | from xosconfig import Config |
| 19 | from multistructlog import create_logger |
Matteo Scandolo | 0bf7c55 | 2018-06-19 16:33:04 -0700 | [diff] [blame] | 20 | import urllib |
Matteo Scandolo | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 21 | import requests |
Matteo Scandolo | a420aba | 2018-04-10 17:00:44 -0700 | [diff] [blame] | 22 | from requests.auth import HTTPBasicAuth |
Matteo Scandolo | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 23 | |
Matteo Scandolo | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 24 | |
Matteo Scandolo | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 25 | class SyncVSGHWServiceInstance(SyncStep): |
| 26 | provides = [VSGHWServiceInstance] |
Matteo Scandolo | 7367b26 | 2018-05-23 14:56:23 -0700 | [diff] [blame] | 27 | log = create_logger(Config().get('logging')) |
Matteo Scandolo | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 28 | |
| 29 | observes = VSGHWServiceInstance |
| 30 | |
Matteo Scandolo | a420aba | 2018-04-10 17:00:44 -0700 | [diff] [blame] | 31 | @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 Scandolo | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 58 | def sync_record(self, o): |
Matteo Scandolo | 7367b26 | 2018-05-23 14:56:23 -0700 | [diff] [blame] | 59 | self.log.info("Sync'ing VSG-HW Service Instance", service_instance=o) |
Matteo Scandolo | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 60 | |
Matteo Scandolo | a420aba | 2018-04-10 17:00:44 -0700 | [diff] [blame] | 61 | |
| 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 Scandolo | 7367b26 | 2018-05-23 14:56:23 -0700 | [diff] [blame] | 67 | ip = si.get_westbound_service_instance_properties("ip_address") |
Matteo Scandolo | a420aba | 2018-04-10 17:00:44 -0700 | [diff] [blame] | 68 | s_tag = si.get_westbound_service_instance_properties("s_tag") |
| 69 | c_tag = si.get_westbound_service_instance_properties("c_tag") |
Matteo Scandolo | a420aba | 2018-04-10 17:00:44 -0700 | [diff] [blame] | 70 | dpid = si.get_westbound_service_instance_properties("switch_datapath_id") |
| 71 | port = si.get_westbound_service_instance_properties("switch_port") |
| 72 | |
Matteo Scandolo | a0398fb | 2018-06-21 11:38:05 -0700 | [diff] [blame] | 73 | 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 Scandolo | 7367b26 | 2018-05-23 14:56:23 -0700 | [diff] [blame] | 81 | try: |
| 82 | if not mac_address: |
| 83 | raise ValueError("mac_address") |
| 84 | if not ip: |
| 85 | raise ValueError("ip_address") |
| 86 | 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 Scandolo | 0bf7c55 | 2018-06-19 16:33:04 -0700 | [diff] [blame] | 95 | raise Exception("Skipping synchronization for VSG-HW Service Instance with id %s as westbound value %s is not available" % (o.id, e.message)) |
| 96 | |
Matteo Scandolo | a420aba | 2018-04-10 17:00:44 -0700 | [diff] [blame] | 97 | 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 Scandolo | 7367b26 | 2018-05-23 14:56:23 -0700 | [diff] [blame] | 116 | self.log.info("Sending requests to ONOS", url=url, body=data) |
Matteo Scandolo | a420aba | 2018-04-10 17:00:44 -0700 | [diff] [blame] | 117 | |
| 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 Scandolo | 7367b26 | 2018-05-23 14:56:23 -0700 | [diff] [blame] | 123 | self.log.info("ONOS response", res=r.text) |
Matteo Scandolo | a420aba | 2018-04-10 17:00:44 -0700 | [diff] [blame] | 124 | |
Matteo Scandolo | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 125 | def delete_record(self, o): |
Matteo Scandolo | 7367b26 | 2018-05-23 14:56:23 -0700 | [diff] [blame] | 126 | self.log.info("Deleting VSG-HW Service Instance", service_instance=o) |
Matteo Scandolo | 0bf7c55 | 2018-06-19 16:33:04 -0700 | [diff] [blame] | 127 | 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 | |
Matteo Scandolo | 5d424c1 | 2018-06-20 17:53:45 -0700 | [diff] [blame] | 142 | if r.status_code != 204: |
Matteo Scandolo | 0bf7c55 | 2018-06-19 16:33:04 -0700 | [diff] [blame] | 143 | raise Exception("Failed to remove subscriber termination in ONOS: %s" % r.text) |
| 144 | |
| 145 | self.log.info("ONOS response", res=r.text) |
Matteo Scandolo | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 146 | pass |