Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | import requests |
| 17 | from requests.auth import HTTPBasicAuth |
| 18 | from synchronizers.new_base.syncstep import SyncStep, DeferredException |
| 19 | from synchronizers.new_base.modelaccessor import FabricService, SwitchPort |
| 20 | |
| 21 | from xosconfig import Config |
| 22 | from multistructlog import create_logger |
| 23 | |
Matteo Scandolo | 04e5e12 | 2018-05-04 16:21:53 -0700 | [diff] [blame] | 24 | from helpers import Helpers |
| 25 | |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 26 | log = create_logger(Config().get('logging')) |
| 27 | |
| 28 | class SyncFabricPort(SyncStep): |
| 29 | provides = [SwitchPort] |
| 30 | observes = SwitchPort |
| 31 | |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 32 | def sync_record(self, model): |
| 33 | interfaces = [] |
| 34 | for intf in model.interfaces.all(): |
| 35 | i = { |
| 36 | "name" : intf.name, |
| 37 | "ips" : [ intf.ips ] |
| 38 | } |
| 39 | if intf.vlanUntagged: |
| 40 | i["vlan-untagged"] = intf.vlanUntagged |
| 41 | interfaces.append(i) |
| 42 | |
| 43 | # Send port config to onos-fabric netcfg |
| 44 | data = { |
| 45 | "ports": { |
| 46 | "%s/%s" % (model.switch.ofId, model.portId) : { |
| 47 | "interfaces" : interfaces |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
Matteo Scandolo | 04e5e12 | 2018-05-04 16:21:53 -0700 | [diff] [blame] | 52 | onos = Helpers.get_onos_fabric_service() |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 53 | |
| 54 | url = 'http://%s:%s/onos/v1/network/configuration/' % (onos.rest_hostname, onos.rest_port) |
| 55 | r = requests.post(url, json=data, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password)) |
| 56 | |
| 57 | if r.status_code != 200: |
| 58 | log.error(r.text) |
| 59 | raise Exception("Failed to add port %s into ONOS" % model.name) |
| 60 | else: |
| 61 | try: |
| 62 | print r.json() |
| 63 | except Exception: |
| 64 | print r.text |
| 65 | |
| 66 | def delete_record(self, switch): |
| 67 | pass |