blob: 3b1ba98fb63e93751ae28b1a60a82335d0e16d77 [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 Scandolo0ec03c12018-01-31 17:23:28 -080019from synchronizers.new_base.SyncInstanceUsingAnsible import SyncStep
Matteo Scandoload7f3b42018-01-30 16:41:19 -080020from synchronizers.new_base.ansible_helper import run_template
Matteo Scandolo0ec03c12018-01-31 17:23:28 -080021from synchronizers.new_base.modelaccessor import MCordSubscriberInstance, ServiceInstanceLink, ProgranServiceInstance
Matteo Scandoload7f3b42018-01-30 16:41:19 -080022
23from xosconfig import Config
24from multistructlog import create_logger
25import json
Matteo Scandolo0ec03c12018-01-31 17:23:28 -080026import requests
27from requests.auth import HTTPBasicAuth
Matteo Scandoload7f3b42018-01-30 16:41:19 -080028
29
30log = create_logger(Config().get('logging'))
31
32parentdir = os.path.join(os.path.dirname(__file__), "..")
33sys.path.insert(0, parentdir)
34sys.path.insert(0, os.path.dirname(__file__))
35from helpers import ProgranHelpers
36
Matteo Scandolo0ec03c12018-01-31 17:23:28 -080037class SyncProgranIMSILink(SyncStep):
Matteo Scandoload7f3b42018-01-30 16:41:19 -080038 provides = [ServiceInstanceLink]
39
40 observes = ServiceInstanceLink
41
Matteo Scandolo0ec03c12018-01-31 17:23:28 -080042 # NOTE Override the default fetch_pending method to receive on links between MCordSubscriberInstances and ProgranServiceInstances
43 def fetch_pending(self, deleted):
Matteo Scandoload7f3b42018-01-30 16:41:19 -080044
Matteo Scandolo0ec03c12018-01-31 17:23:28 -080045 objs = super(SyncProgranIMSILink, self).fetch_pending(deleted)
46 objs = list(objs)
Matteo Scandoload7f3b42018-01-30 16:41:19 -080047
Matteo Scandolo0ec03c12018-01-31 17:23:28 -080048 to_be_sync = []
Matteo Scandoload7f3b42018-01-30 16:41:19 -080049
Matteo Scandolo0ec03c12018-01-31 17:23:28 -080050 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 Scandoload7f3b42018-01-30 16:41:19 -080053
Matteo Scandolo0ec03c12018-01-31 17:23:28 -080054 return to_be_sync
Matteo Scandoload7f3b42018-01-30 16:41:19 -080055
56 def sync_record(self, o):
Matteo Scandoload7f3b42018-01-30 16:41:19 -080057
Matteo Scandolo0ec03c12018-01-31 17:23:28 -080058 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 Scandoload7f3b42018-01-30 16:41:19 -080076
77 def delete_record(self, o):
Matteo Scandolo0ec03c12018-01-31 17:23:28 -080078
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()