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 | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 20 | import requests |
Matteo Scandolo | a420aba | 2018-04-10 17:00:44 -0700 | [diff] [blame] | 21 | from requests.auth import HTTPBasicAuth |
Matteo Scandolo | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 22 | |
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 | class SyncVSGHWServiceInstance(SyncStep): |
| 25 | provides = [VSGHWServiceInstance] |
Matteo Scandolo | 7367b26 | 2018-05-23 14:56:23 -0700 | [diff] [blame^] | 26 | log = create_logger(Config().get('logging')) |
Matteo Scandolo | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 27 | |
| 28 | observes = VSGHWServiceInstance |
| 29 | |
Matteo Scandolo | a420aba | 2018-04-10 17:00:44 -0700 | [diff] [blame] | 30 | @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 Scandolo | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 57 | def sync_record(self, o): |
Matteo Scandolo | 7367b26 | 2018-05-23 14:56:23 -0700 | [diff] [blame^] | 58 | self.log.info("Sync'ing VSG-HW Service Instance", service_instance=o) |
Matteo Scandolo | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 59 | |
Matteo Scandolo | a420aba | 2018-04-10 17:00:44 -0700 | [diff] [blame] | 60 | |
| 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 Scandolo | 7367b26 | 2018-05-23 14:56:23 -0700 | [diff] [blame^] | 66 | ip = si.get_westbound_service_instance_properties("ip_address") |
Matteo Scandolo | a420aba | 2018-04-10 17:00:44 -0700 | [diff] [blame] | 67 | s_tag = si.get_westbound_service_instance_properties("s_tag") |
| 68 | c_tag = si.get_westbound_service_instance_properties("c_tag") |
Matteo Scandolo | a420aba | 2018-04-10 17:00:44 -0700 | [diff] [blame] | 69 | dpid = si.get_westbound_service_instance_properties("switch_datapath_id") |
| 70 | port = si.get_westbound_service_instance_properties("switch_port") |
| 71 | |
Matteo Scandolo | 7367b26 | 2018-05-23 14:56:23 -0700 | [diff] [blame^] | 72 | 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 Scandolo | a420aba | 2018-04-10 17:00:44 -0700 | [diff] [blame] | 88 | 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 Scandolo | 7367b26 | 2018-05-23 14:56:23 -0700 | [diff] [blame^] | 107 | self.log.info("Sending requests to ONOS", url=url, body=data) |
Matteo Scandolo | a420aba | 2018-04-10 17:00:44 -0700 | [diff] [blame] | 108 | |
| 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 Scandolo | 7367b26 | 2018-05-23 14:56:23 -0700 | [diff] [blame^] | 114 | self.log.info("ONOS response", res=r.text) |
Matteo Scandolo | a420aba | 2018-04-10 17:00:44 -0700 | [diff] [blame] | 115 | |
Matteo Scandolo | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 116 | def delete_record(self, o): |
Matteo Scandolo | 7367b26 | 2018-05-23 14:56:23 -0700 | [diff] [blame^] | 117 | self.log.info("Deleting VSG-HW Service Instance", service_instance=o) |
Matteo Scandolo | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 118 | pass |