blob: b0e41722552070df07cb1ea7b1d3618b0b1651b9 [file] [log] [blame]
Matteo Scandolo0a207b52018-01-29 13:39:43 -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 Scandolo0a207b52018-01-29 13:39:43 -080020from synchronizers.new_base.modelaccessor import ENodeB
21
22from xosconfig import Config
23from multistructlog import create_logger
24import json
Matteo Scandolo830403a2018-02-05 10:51:59 -080025import requests
26from requests.auth import HTTPBasicAuth
Matteo Scandolo0a207b52018-01-29 13:39:43 -080027
28log = create_logger(Config().get('logging'))
29
30parentdir = os.path.join(os.path.dirname(__file__), "..")
Matteo Scandoload7f3b42018-01-30 16:41:19 -080031
Matteo Scandolo0a207b52018-01-29 13:39:43 -080032sys.path.insert(0, parentdir)
Matteo Scandoload7f3b42018-01-30 16:41:19 -080033sys.path.insert(0, os.path.dirname(__file__))
34
35from helpers import ProgranHelpers
Matteo Scandolo0a207b52018-01-29 13:39:43 -080036
Matteo Scandolo830403a2018-02-05 10:51:59 -080037class SyncProgranEnodeB(SyncStep):
Matteo Scandolo0a207b52018-01-29 13:39:43 -080038 provides = [ENodeB]
39
40 observes = ENodeB
41
Matteo Scandolo0a207b52018-01-29 13:39:43 -080042 def get_progran_enodeb_field(self, o):
43
44 enodeb = {
45 "eNBId": o.enbId,
46 "Description": o.description,
47 "IpAddr": o.ipAddr
48 }
Matteo Scandolo0a207b52018-01-29 13:39:43 -080049 return enodeb
50
Matteo Scandolo830403a2018-02-05 10:51:59 -080051 def sync_record(self, o):
52 log.info("sync'ing enodeb", object=str(o), **o.tologdict())
53
Matteo Scandolo0a207b52018-01-29 13:39:43 -080054 onos = ProgranHelpers.get_progran_onos_info()
Matteo Scandolo0a207b52018-01-29 13:39:43 -080055
Matteo Scandolo830403a2018-02-05 10:51:59 -080056 enodeb_url = "http://%s:%s/onos/progran/enodeb/" % (onos['url'], onos['port'])
57 data = self.get_progran_enodeb_field(o)
58 log.debug("Sync'ing enodeb with data", request_data=data)
Matteo Scandolo0a207b52018-01-29 13:39:43 -080059
Matteo Scandolo830403a2018-02-05 10:51:59 -080060 if o.previously_sync == False:
Matteo Scandolo72a38902018-02-15 09:13:31 -080061 log.debug("Sending POST", url=enodeb_url, data=json.dumps(data))
Matteo Scandolo830403a2018-02-05 10:51:59 -080062 r = requests.post(enodeb_url, data=json.dumps(data), auth=HTTPBasicAuth(onos['username'], onos['password']))
63 else:
Matteo Scandolo72a38902018-02-15 09:13:31 -080064 data = {
65 "EnodeB": data
66 }
67 log.debug("Sending PUT", url=enodeb_url, data=json.dumps(data))
Matteo Scandolo830403a2018-02-05 10:51:59 -080068 r = requests.put(enodeb_url, data=json.dumps(data),
69 auth=HTTPBasicAuth(onos['username'], onos['password']))
70
71 ProgranHelpers.get_progran_rest_errors(r)
72 log.info("Enodeb synchronized", response=r.json())
73
74 o.previously_sync = True
75 o.save()
Matteo Scandolo0a207b52018-01-29 13:39:43 -080076
77 def delete_record(self, o):
Matteo Scandolo830403a2018-02-05 10:51:59 -080078 log.info("deleting enodeb", object=str(o), **o.tologdict())
Matteo Scandolo0a207b52018-01-29 13:39:43 -080079 onos = ProgranHelpers.get_progran_onos_info()
Matteo Scandolo830403a2018-02-05 10:51:59 -080080 enode_url = "http://%s:%s/onos/progran/enodeb/%s" % (onos['url'], onos['port'], o.enbId)
81 r = requests.delete(enode_url, auth=HTTPBasicAuth(onos['username'], onos['password']))
82 ProgranHelpers.get_progran_rest_errors(r)
83 log.info("enodeb deleted", response=r.json())