Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 1 | |
| 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 | |
| 17 | import os |
| 18 | import sys |
Woojoong Kim | f5dee56 | 2019-10-16 17:11:49 -0700 | [diff] [blame] | 19 | from xossynchronizer.steps.syncstep import SyncStep |
Scott Baker | b06daad | 2019-02-04 15:46:54 -0800 | [diff] [blame] | 20 | from xossynchronizer.modelaccessor import ENodeB |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 21 | |
| 22 | from xosconfig import Config |
| 23 | from multistructlog import create_logger |
| 24 | import json |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 25 | import requests |
| 26 | from requests.auth import HTTPBasicAuth |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 27 | |
| 28 | log = create_logger(Config().get('logging')) |
| 29 | |
| 30 | parentdir = os.path.join(os.path.dirname(__file__), "..") |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 31 | |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 32 | sys.path.insert(0, parentdir) |
Matteo Scandolo | ad7f3b4 | 2018-01-30 16:41:19 -0800 | [diff] [blame] | 33 | sys.path.insert(0, os.path.dirname(__file__)) |
| 34 | |
| 35 | from helpers import ProgranHelpers |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 36 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 37 | class SyncProgranEnodeB(SyncStep): |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 38 | provides = [ENodeB] |
| 39 | |
| 40 | observes = ENodeB |
| 41 | |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 42 | def get_progran_enodeb_field(self, o): |
| 43 | |
| 44 | enodeb = { |
| 45 | "eNBId": o.enbId, |
| 46 | "Description": o.description, |
| 47 | "IpAddr": o.ipAddr |
| 48 | } |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 49 | return enodeb |
| 50 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 51 | def sync_record(self, o): |
| 52 | log.info("sync'ing enodeb", object=str(o), **o.tologdict()) |
| 53 | |
Scott Baker | b06daad | 2019-02-04 15:46:54 -0800 | [diff] [blame] | 54 | onos = ProgranHelpers.get_progran_onos_info(self.model_accessor) |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 55 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 56 | 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 Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 59 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 60 | if o.previously_sync == False: |
Matteo Scandolo | 72a3890 | 2018-02-15 09:13:31 -0800 | [diff] [blame] | 61 | log.debug("Sending POST", url=enodeb_url, data=json.dumps(data)) |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 62 | r = requests.post(enodeb_url, data=json.dumps(data), auth=HTTPBasicAuth(onos['username'], onos['password'])) |
| 63 | else: |
Matteo Scandolo | 72a3890 | 2018-02-15 09:13:31 -0800 | [diff] [blame] | 64 | data = { |
| 65 | "EnodeB": data |
| 66 | } |
| 67 | log.debug("Sending PUT", url=enodeb_url, data=json.dumps(data)) |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 68 | 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 Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 76 | |
| 77 | def delete_record(self, o): |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 78 | log.info("deleting enodeb", object=str(o), **o.tologdict()) |
Scott Baker | b06daad | 2019-02-04 15:46:54 -0800 | [diff] [blame] | 79 | onos = ProgranHelpers.get_progran_onos_info(self.model_accessor) |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 80 | 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()) |