blob: a91d2a29b020d4b7dc98ecd85dfaf09eb6298d7f [file] [log] [blame]
Matteo Scandoload7f3b42018-01-30 16:41:19 -08001
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
17import os
18import sys
Scott Bakerb06daad2019-02-04 15:46:54 -080019from xossynchronizer.steps.syncstep import SyncStep
20from xossynchronizer.modelaccessor import MCordSubscriberInstance, ServiceInstanceLink, ProgranServiceInstance
Matteo Scandoload7f3b42018-01-30 16:41:19 -080021
22from xosconfig import Config
23from multistructlog import create_logger
24import json
Matteo Scandolo0ec03c12018-01-31 17:23:28 -080025import requests
26from requests.auth import HTTPBasicAuth
Matteo Scandoload7f3b42018-01-30 16:41:19 -080027
28
29log = create_logger(Config().get('logging'))
30
31parentdir = os.path.join(os.path.dirname(__file__), "..")
32sys.path.insert(0, parentdir)
33sys.path.insert(0, os.path.dirname(__file__))
34from helpers import ProgranHelpers
35
Matteo Scandolo0ec03c12018-01-31 17:23:28 -080036class SyncProgranIMSILink(SyncStep):
Matteo Scandoload7f3b42018-01-30 16:41:19 -080037 provides = [ServiceInstanceLink]
38
39 observes = ServiceInstanceLink
40
Matteo Scandolo0ec03c12018-01-31 17:23:28 -080041 # NOTE Override the default fetch_pending method to receive on links between MCordSubscriberInstances and ProgranServiceInstances
42 def fetch_pending(self, deleted):
Matteo Scandoload7f3b42018-01-30 16:41:19 -080043
Matteo Scandolo0ec03c12018-01-31 17:23:28 -080044 objs = super(SyncProgranIMSILink, self).fetch_pending(deleted)
45 objs = list(objs)
Matteo Scandoload7f3b42018-01-30 16:41:19 -080046
Matteo Scandolo0ec03c12018-01-31 17:23:28 -080047 to_be_sync = []
Matteo Scandoload7f3b42018-01-30 16:41:19 -080048
Matteo Scandolo0ec03c12018-01-31 17:23:28 -080049 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 Scandoload7f3b42018-01-30 16:41:19 -080052
Matteo Scandolo0ec03c12018-01-31 17:23:28 -080053 return to_be_sync
Matteo Scandoload7f3b42018-01-30 16:41:19 -080054
55 def sync_record(self, o):
Matteo Scandoload7f3b42018-01-30 16:41:19 -080056
Matteo Scandolo0ec03c12018-01-31 17:23:28 -080057 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 Bakerb06daad2019-02-04 15:46:54 -080060 onos = ProgranHelpers.get_progran_onos_info(self.model_accessor)
Matteo Scandolo0ec03c12018-01-31 17:23:28 -080061
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 Scandolo830403a2018-02-05 10:51:59 -080074 ProgranHelpers.get_progran_rest_errors(r)
Matteo Scandoload7f3b42018-01-30 16:41:19 -080075
76 def delete_record(self, o):
Matteo Scandolo0ec03c12018-01-31 17:23:28 -080077
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 Bakerb06daad2019-02-04 15:46:54 -080081 onos = ProgranHelpers.get_progran_onos_info(self.model_accessor)
Matteo Scandolo0ec03c12018-01-31 17:23:28 -080082
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 Scandolo830403a2018-02-05 10:51:59 -080089 ProgranHelpers.get_progran_rest_errors(r)