blob: 12918f14fc5b3057d7065afc4c2e37efc38c660f [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.SyncInstanceUsingAnsible import SyncStep
Matteo Scandoload7f3b42018-01-30 16:41:19 -080020from synchronizers.new_base.modelaccessor import MCordSubscriberInstance
21
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())
50 onos = ProgranHelpers.get_progran_onos_info()
51 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())
Matteo Scandolo8c096ef2018-02-16 22:42:04 -080060 onos = ProgranHelpers.get_progran_onos_info()
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())