blob: 863af21bc039ae2695d41fada2f389615371fe9c [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
Matteo Scandolo830403a2018-02-05 10:51:59 -080019from synchronizers.new_base.syncstep import SyncStep
Matteo Scandolo0ec03c12018-01-31 17:23:28 -080020from synchronizers.new_base.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
60 onos = ProgranHelpers.get_progran_onos_info()
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 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
81 onos = ProgranHelpers.get_progran_onos_info()
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 Scandolo830403a2018-02-05 10:51:59 -080089 ProgranHelpers.get_progran_rest_errors(r)