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