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 | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 15 | from synchronizers.new_base.SyncInstanceUsingAnsible import SyncStep |
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 | |
| 23 | log = create_logger(Config().get('logging')) |
| 24 | |
Matteo Scandolo | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 25 | class SyncVSGHWServiceInstance(SyncStep): |
| 26 | provides = [VSGHWServiceInstance] |
| 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): |
| 58 | log.info("Sync'ing VSG-HW Service Instance", service_instance=o) |
| 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") |
| 66 | s_tag = si.get_westbound_service_instance_properties("s_tag") |
| 67 | c_tag = si.get_westbound_service_instance_properties("c_tag") |
| 68 | ip = si.get_westbound_service_instance_properties("ip_address") |
| 69 | dpid = si.get_westbound_service_instance_properties("switch_datapath_id") |
| 70 | port = si.get_westbound_service_instance_properties("switch_port") |
| 71 | |
| 72 | data = { |
| 73 | 'hosts': { |
| 74 | mac_address + "/" + str(s_tag): { |
| 75 | "basic": { |
| 76 | "ips": [ip], |
| 77 | "locations": ["%s/%s" % (dpid, port)], |
| 78 | "innerVlan": str(c_tag), |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | # Adding the optional tpid |
| 85 | tpid = si.get_westbound_service_instance_properties("outer_tpid") |
| 86 | if tpid: |
| 87 | data["hosts"][mac_address + "/" + str(s_tag)]["basic"]["outerTpid"] = str(tpid) |
| 88 | |
| 89 | url = onos['url'] + '/onos/v1/network/configuration' |
| 90 | |
| 91 | log.info("Sending requests to ONOS", url=url, body=data) |
| 92 | |
| 93 | r = requests.post(url, json=data, auth=HTTPBasicAuth(onos['user'], onos['pass'])) |
| 94 | |
| 95 | if r.status_code != 200: |
| 96 | raise Exception("Failed to terminate subscriber in ONOS: %s" % r.text) |
| 97 | |
| 98 | log.info("ONOS response", res=r.text) |
| 99 | |
Matteo Scandolo | db7508e | 2018-03-19 15:06:06 -0700 | [diff] [blame] | 100 | def delete_record(self, o): |
| 101 | log.info("Deleting VSG-HW Service Instance", service_instance=o) |
| 102 | pass |