blob: aabec76d6815ae0fe92281522fb0f610a346b94d [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.SyncInstanceUsingAnsible import SyncStep
20from xossynchronizer.modelaccessor import MCordSubscriberInstance
Matteo Scandoload7f3b42018-01-30 16:41:19 -080021
22from xosconfig import Config
23from multistructlog import create_logger
24import json
Matteo Scandoloe9754312018-01-31 12:38:30 -080025import requests
Matteo Scandolo830403a2018-02-05 10:51:59 -080026from 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 Scandolo830403a2018-02-05 10:51:59 -080036class SyncProgranIMSI(SyncStep):
Matteo Scandoload7f3b42018-01-30 16:41:19 -080037 provides = [MCordSubscriberInstance]
38
39 observes = MCordSubscriberInstance
40
Matteo Scandoload7f3b42018-01-30 16:41:19 -080041 def get_progran_imsi_field(self, o):
42
43 imsi = {
44 "IMSI": o.imsi_number,
45 }
Matteo Scandoload7f3b42018-01-30 16:41:19 -080046 return imsi
47
Matteo Scandoloe9754312018-01-31 12:38:30 -080048 def sync_record(self, o):
Matteo Scandolo830403a2018-02-05 10:51:59 -080049 log.info("sync'ing imsi", object=str(o), **o.tologdict())
Scott Bakerb06daad2019-02-04 15:46:54 -080050 onos = ProgranHelpers.get_progran_onos_info(self.model_accessor)
Matteo Scandolo830403a2018-02-05 10:51:59 -080051 imsi_url = "http://%s:%s/onos/progran/imsi/" % (onos['url'], onos['port'])
52 data = self.get_progran_imsi_field(o)
53 r = requests.post(imsi_url, data=json.dumps(data), auth=HTTPBasicAuth(onos['username'], onos['password']))
Matteo Scandoloe9754312018-01-31 12:38:30 -080054
Matteo Scandolo830403a2018-02-05 10:51:59 -080055 ProgranHelpers.get_progran_rest_errors(r)
56 log.info("Profile synchronized", response=r.json())
Matteo Scandoload7f3b42018-01-30 16:41:19 -080057
58 def delete_record(self, o):
Matteo Scandolo830403a2018-02-05 10:51:59 -080059 log.info("deleting imsi", object=str(o), **o.tologdict())
Scott Bakerb06daad2019-02-04 15:46:54 -080060 onos = ProgranHelpers.get_progran_onos_info(self.model_accessor)
Matteo Scandolo830403a2018-02-05 10:51:59 -080061 profile_url = "http://%s:%s/onos/progran/imsi/%s" % (onos['url'], onos['port'], o.imsi_number)
62 r = requests.delete(profile_url, auth=HTTPBasicAuth(onos['username'], onos['password']))
Matteo Scandolo830403a2018-02-05 10:51:59 -080063 log.info("IMSI synchronized", response=r.json())