blob: 93af41f7dc8d5a0d096a5a625f782a77ba2e7cb2 [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
19from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
20from synchronizers.new_base.ansible_helper import run_template
21from synchronizers.new_base.modelaccessor import MCordSubscriberInstance
22
23from xosconfig import Config
24from multistructlog import create_logger
25import json
Matteo Scandoloe9754312018-01-31 12:38:30 -080026import requests
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
36class SyncProgranIMSI(SyncInstanceUsingAnsible):
37 provides = [MCordSubscriberInstance]
38
39 observes = MCordSubscriberInstance
40
41 def skip_ansible_fields(self, o):
42 # FIXME This model does not have an instance, this is a workaroung to make it work,
43 # but it need to be cleaned up creating a general SyncUsingAnsible base class
44 return True
45
46 def get_progran_imsi_field(self, o):
47
48 imsi = {
49 "IMSI": o.imsi_number,
50 }
51 imsi = json.dumps(imsi)
52 return imsi
53
Matteo Scandoloe9754312018-01-31 12:38:30 -080054 def get_fields(self, o):
Matteo Scandoload7f3b42018-01-30 16:41:19 -080055 onos = ProgranHelpers.get_progran_onos_info()
56 fields = {
57 'onos_url': onos['url'],
58 'onos_username': onos['username'],
59 'onos_password': onos['password'],
60 'onos_port': onos['port'],
Matteo Scandoloe9754312018-01-31 12:38:30 -080061 }
62
63 return fields
64
65 def sync_record(self, o):
66 # NOTE overriding the default method as we need to read from progran
67 base_fields = self.get_fields(o)
68
69 create_fields = {
Matteo Scandoload7f3b42018-01-30 16:41:19 -080070 'endpoint': 'imsi',
71 'body': self.get_progran_imsi_field(o),
72 'method': 'POST'
73 }
74
Matteo Scandoloe9754312018-01-31 12:38:30 -080075 create_fields["ansible_tag"] = getattr(o, "ansible_tag", o.__class__.__name__ + "_" + str(o.id))
76 create_fields.update(base_fields)
77
78 self.run_playbook(o, create_fields)
79
80 # fetch the IMSI we just created
Matteo Scandoloc1102a52018-02-01 17:26:04 -080081 # NOTE we won't need this method once we'll have polling in place
Matteo Scandoloe9754312018-01-31 12:38:30 -080082 imsi_url = "http://%s:%s/onos/progran/imsi/%s" % (base_fields['onos_url'], base_fields['onos_port'], o.imsi_number)
83 r = requests.get(imsi_url)
84 o.ue_status = r.json()['ImsiArray'][0]['UeStatus']
85
86 o.save()
Matteo Scandoload7f3b42018-01-30 16:41:19 -080087
88 # FIXME we need to override this as the default expect to ssh into a VM
89 def run_playbook(self, o, fields):
Matteo Scandoloe9754312018-01-31 12:38:30 -080090 return run_template("progran_curl.yaml", fields, object=o)
Matteo Scandoload7f3b42018-01-30 16:41:19 -080091
92 def delete_record(self, o):
93 log.info("deleting object", object=str(o), **o.tologdict())
94 onos = ProgranHelpers.get_progran_onos_info()
95 fields = {
96 'onos_url': onos['url'],
97 'onos_username': onos['username'],
98 'onos_password': onos['password'],
99 'onos_port': onos['port'],
100 'endpoint': 'imsi/%s' % o.imsi_number,
101 'body': '',
102 'method': 'DELETE'
103 }
104 res = self.run_playbook(o, fields)