Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [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 | |
| 17 | import os |
| 18 | import sys |
Matteo Scandolo | 0ec03c1 | 2018-01-31 17:23:28 -0800 | [diff] [blame] | 19 | from synchronizers.new_base.SyncInstanceUsingAnsible import SyncStep |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 20 | from synchronizers.new_base.ansible_helper import run_template |
Matteo Scandolo | 0ec03c1 | 2018-01-31 17:23:28 -0800 | [diff] [blame] | 21 | from synchronizers.new_base.modelaccessor import MCordSubscriberInstance, ServiceInstanceLink, ProgranServiceInstance |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 22 | |
| 23 | from xosconfig import Config |
| 24 | from multistructlog import create_logger |
| 25 | import json |
Matteo Scandolo | 0ec03c1 | 2018-01-31 17:23:28 -0800 | [diff] [blame] | 26 | import requests |
| 27 | from requests.auth import HTTPBasicAuth |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 28 | |
| 29 | |
| 30 | log = create_logger(Config().get('logging')) |
| 31 | |
| 32 | parentdir = os.path.join(os.path.dirname(__file__), "..") |
| 33 | sys.path.insert(0, parentdir) |
| 34 | sys.path.insert(0, os.path.dirname(__file__)) |
| 35 | from helpers import ProgranHelpers |
| 36 | |
Matteo Scandolo | 0ec03c1 | 2018-01-31 17:23:28 -0800 | [diff] [blame] | 37 | class SyncProgranIMSILink(SyncStep): |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 38 | provides = [ServiceInstanceLink] |
| 39 | |
| 40 | observes = ServiceInstanceLink |
| 41 | |
Matteo Scandolo | 0ec03c1 | 2018-01-31 17:23:28 -0800 | [diff] [blame] | 42 | # NOTE Override the default fetch_pending method to receive on links between MCordSubscriberInstances and ProgranServiceInstances |
| 43 | def fetch_pending(self, deleted): |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 44 | |
Matteo Scandolo | 0ec03c1 | 2018-01-31 17:23:28 -0800 | [diff] [blame] | 45 | objs = super(SyncProgranIMSILink, self).fetch_pending(deleted) |
| 46 | objs = list(objs) |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 47 | |
Matteo Scandolo | 0ec03c1 | 2018-01-31 17:23:28 -0800 | [diff] [blame] | 48 | to_be_sync = [] |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 49 | |
Matteo Scandolo | 0ec03c1 | 2018-01-31 17:23:28 -0800 | [diff] [blame] | 50 | for link in objs: |
| 51 | if link.provider_service_instance.leaf_model_name == "ProgranServiceInstance" and link.subscriber_service_instance.leaf_model_name == "MCordSubscriberInstance": |
| 52 | to_be_sync.append(link) |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 53 | |
Matteo Scandolo | 0ec03c1 | 2018-01-31 17:23:28 -0800 | [diff] [blame] | 54 | return to_be_sync |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 55 | |
| 56 | def sync_record(self, o): |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 57 | |
Matteo Scandolo | 0ec03c1 | 2018-01-31 17:23:28 -0800 | [diff] [blame] | 58 | if o.provider_service_instance.leaf_model_name == "ProgranServiceInstance" and o.subscriber_service_instance.leaf_model_name == "MCordSubscriberInstance": |
| 59 | log.info("sync'ing link", object=str(o), **o.tologdict()) |
| 60 | |
| 61 | onos = ProgranHelpers.get_progran_onos_info() |
| 62 | |
| 63 | profile_name = o.provider_service_instance.name |
| 64 | imsi_number = o.subscriber_service_instance.leaf_model.imsi_number |
| 65 | |
| 66 | data = { |
| 67 | "IMSIRuleArray": [ |
| 68 | imsi_number |
| 69 | ] |
| 70 | } |
| 71 | |
| 72 | url = "http://%s:%s/onos/progran/profile/%s/imsi" % (onos['url'], onos['port'], profile_name) |
| 73 | |
| 74 | r = requests.post(url, data=json.dumps(data), auth=HTTPBasicAuth(onos['username'], onos['password'])) |
| 75 | print r.json() |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 76 | |
| 77 | def delete_record(self, o): |
Matteo Scandolo | 0ec03c1 | 2018-01-31 17:23:28 -0800 | [diff] [blame] | 78 | |
| 79 | if o.provider_service_instance.leaf_model_name == "ProgranServiceInstance" and o.subscriber_service_instance.leaf_model_name == "MCordSubscriberInstance": |
| 80 | log.info("deleting link", object=str(o), **o.tologdict()) |
| 81 | |
| 82 | onos = ProgranHelpers.get_progran_onos_info() |
| 83 | |
| 84 | profile_name = o.provider_service_instance.name |
| 85 | imsi_number = o.subscriber_service_instance.leaf_model.imsi_number |
| 86 | |
| 87 | url = "http://%s:%s/onos/progran/profile/%s/%s" % (onos['url'], onos['port'], profile_name, imsi_number) |
| 88 | |
| 89 | r = requests.delete(url, auth=HTTPBasicAuth(onos['username'], onos['password'])) |
| 90 | print r.json() |