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 | a594eba | 2018-11-01 14:59:54 -0700 | [diff] [blame^] | 24 | log = create_logger(Config().get('logging')) |
Matteo Scandolo | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 25 | |
Matteo Scandolo | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 26 | class SyncVSGHWServiceInstance(SyncStep): |
| 27 | provides = [VSGHWServiceInstance] |
Matteo Scandolo | 7367b26 | 2018-05-23 14:56:23 -0700 | [diff] [blame] | 28 | log = create_logger(Config().get('logging')) |
Matteo Scandolo | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 29 | |
| 30 | observes = VSGHWServiceInstance |
| 31 | |
Matteo Scandolo | a420aba | 2018-04-10 17:00:44 -0700 | [diff] [blame] | 32 | @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 Scandolo | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 59 | def sync_record(self, o): |
Matteo Scandolo | a594eba | 2018-11-01 14:59:54 -0700 | [diff] [blame^] | 60 | log.info("Sync'ing VSG-HW Service Instance", service_instance=o) |
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 | a594eba | 2018-11-01 14:59:54 -0700 | [diff] [blame^] | 67 | ips = si.get_westbound_service_instance_properties("ips") |
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") |
Matteo Scandolo | a594eba | 2018-11-01 14:59:54 -0700 | [diff] [blame^] | 84 | if len(ips.all()) == 0: |
| 85 | raise ValueError("ips") |
Matteo Scandolo | 7367b26 | 2018-05-23 14:56:23 -0700 | [diff] [blame] | 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 | a594eba | 2018-11-01 14:59:54 -0700 | [diff] [blame^] | 95 | log.info("Skipping synchronization for VSG-HW Service Instance with id %s as westbound value %s is not available" % (o.id, e.message)) |
Matteo Scandolo | 0bf7c55 | 2018-06-19 16:33:04 -0700 | [diff] [blame] | 96 | 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 Scandolo | a420aba | 2018-04-10 17:00:44 -0700 | [diff] [blame] | 98 | data = { |
| 99 | 'hosts': { |
| 100 | mac_address + "/" + str(s_tag): { |
| 101 | "basic": { |
Matteo Scandolo | a594eba | 2018-11-01 14:59:54 -0700 | [diff] [blame^] | 102 | "ips": [ i.ip for i in ips.all() ], |
Matteo Scandolo | a420aba | 2018-04-10 17:00:44 -0700 | [diff] [blame] | 103 | "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 Scandolo | a594eba | 2018-11-01 14:59:54 -0700 | [diff] [blame^] | 117 | log.info("Sending requests to ONOS", url=url, body=data) |
Matteo Scandolo | a420aba | 2018-04-10 17:00:44 -0700 | [diff] [blame] | 118 | |
| 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 Scandolo | a594eba | 2018-11-01 14:59:54 -0700 | [diff] [blame^] | 124 | log.info("ONOS response", res=r.text) |
Matteo Scandolo | a420aba | 2018-04-10 17:00:44 -0700 | [diff] [blame] | 125 | |
Matteo Scandolo | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 126 | def delete_record(self, o): |
Matteo Scandolo | a594eba | 2018-11-01 14:59:54 -0700 | [diff] [blame^] | 127 | log.info("Deleting VSG-HW Service Instance", service_instance=o) |
Matteo Scandolo | 0bf7c55 | 2018-06-19 16:33:04 -0700 | [diff] [blame] | 128 | 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 Scandolo | 5d424c1 | 2018-06-20 17:53:45 -0700 | [diff] [blame] | 143 | if r.status_code != 204: |
Matteo Scandolo | 0bf7c55 | 2018-06-19 16:33:04 -0700 | [diff] [blame] | 144 | raise Exception("Failed to remove subscriber termination in ONOS: %s" % r.text) |
| 145 | |
Matteo Scandolo | a594eba | 2018-11-01 14:59:54 -0700 | [diff] [blame^] | 146 | log.info("ONOS response", res=r.text) |
Matteo Scandolo | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 147 | pass |