|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Copyright 2017-2022 Univertity of Bristol - High Performance Networks Group |
| 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 | +import json |
| 17 | +import requests |
| 18 | + |
| 19 | +_headers = { |
| 20 | + 'Content-type': 'application/json', |
| 21 | + "Accept": "application/json", |
| 22 | + 'Cache-Control': 'no-cache' |
| 23 | +} |
| 24 | + |
| 25 | +class I2catController(object): |
| 26 | + """Integration with FlexRAN solution |
| 27 | + |
| 28 | + Arguments: |
| 29 | + Wireless {[type]} -- [description] |
| 30 | + """ |
| 31 | + def __init__(self, |
| 32 | + controller_id=None, |
| 33 | + ip='127.0.0.1', |
| 34 | + port=8080, |
| 35 | + url=None): |
| 36 | + self.id = controller_id |
| 37 | + self._ip = ip |
| 38 | + self._port = port |
| 39 | + self._url = url.format(ip,port) |
| 40 | + |
| 41 | + |
| 42 | +# chunkete-topology-controller implementation |
| 43 | + def getChunketeTopology(self): |
| 44 | + url = (self._url + "/chunkete/topology").format(self._ip, str(self._port)) |
| 45 | + |
| 46 | + resp = requests.get(url) |
| 47 | + data = json.loads(resp.text) |
| 48 | + return data, resp.status_code |
| 49 | + |
| 50 | + def putInterfaceLTEConfig(self, phy_id, parameters): |
| 51 | + url = self._url + \ |
| 52 | + "/chunkete/topology/physicalInterface/{}/LTEConfig".format( |
| 53 | + phy_id) |
| 54 | + resp = requests.put( |
| 55 | + url, |
| 56 | + data=parameters, |
| 57 | + headers=_headers |
| 58 | + ) |
| 59 | + data = json.loads(resp.text) |
| 60 | + return data, resp.status_code |
| 61 | + |
| 62 | + def putInterfaceType(self, phy_id, phy_type): |
| 63 | + url = "http://{}:{}/chunkete/topology/physicalInterface/{}/type/{}".format( |
| 64 | + self._ip, str(self._port), phy_id, phy_type) |
| 65 | + resp = requests.put( |
| 66 | + url, |
| 67 | + headers=_headers |
| 68 | + ) |
| 69 | + data = json.loads(resp.text) |
| 70 | + return data, resp.status_code |
| 71 | + |
| 72 | + def putInterfaceWiredConfig(self, phy_id, parameters): |
| 73 | + url = "http://{}:{}/chunkete/topology/physicalInterface/{}/wiredConfig".format( |
| 74 | + self._ip, str(self._port), phy_id) |
| 75 | + resp = requests.put( |
| 76 | + url, |
| 77 | + data=parameters, |
| 78 | + headers=_headers |
| 79 | + ) |
| 80 | + data = json.loads(resp.text) |
| 81 | + return data, resp.status_code |
| 82 | + |
| 83 | + def putInterfaceWirelessConfig(self, phy_id, parameters): |
| 84 | + url = self._url + "/chunkete/topology/physicalInterface/{}/wirelessConfig".format( |
| 85 | + phy_id) |
| 86 | + resp = requests.put( |
| 87 | + url, |
| 88 | + data=parameters, |
| 89 | + headers=_headers |
| 90 | + ) |
| 91 | + data = json.loads(resp.text) |
| 92 | + return data, resp.status_code |
| 93 | + |
| 94 | + |
| 95 | +# chunkete-chunk-controller implementation |
| 96 | + def getAllChunks(self): |
| 97 | + url = "http://{}:{}/chunkete/chunk".format( |
| 98 | + self._ip, |
| 99 | + str(self._port)) |
| 100 | + |
| 101 | + resp = requests.get(url) |
| 102 | + data = json.loads(resp.text) |
| 103 | + return data, resp.status_code |
| 104 | + |
| 105 | + def registerNewChunk(self, content): |
| 106 | + pre_chunk_list, code = self.getAllChunks() |
| 107 | + pre_chunk_ids = [x["id"] for x in pre_chunk_list] |
| 108 | + |
| 109 | + url = "http://{}:{}/chunkete/chunk".format( |
| 110 | + self._ip, str(self._port)) |
| 111 | + resp = requests.post( |
| 112 | + url, |
| 113 | + data=content, |
| 114 | + headers=_headers |
| 115 | + ) |
| 116 | + |
| 117 | + post_chunk_list, code = self.getAllChunks() |
| 118 | + post_chunk_ids = [x["id"] for x in post_chunk_list] |
| 119 | + |
| 120 | + chunk_id = [x for x in post_chunk_ids if x not in pre_chunk_ids][0] |
| 121 | + |
| 122 | + data = json.loads(resp.text) |
| 123 | + return data, resp.status_code |
| 124 | + |
| 125 | + def getChunkById(self, chunk_id): |
| 126 | + url = "http://{}:{}/chunkete/chunk/{}".format( |
| 127 | + self._ip, str(self._port), chunk_id) |
| 128 | + resp = requests.get( |
| 129 | + url, |
| 130 | + headers=_headers |
| 131 | + ) |
| 132 | + data = json.loads(resp.text) |
| 133 | + return data, resp.status_code |
| 134 | + |
| 135 | + def removeExistingChunk(self, chunk_id): |
| 136 | + url = "http://{}:{}/chunkete/chunk/{}".format( |
| 137 | + self._ip, str(self._port), chunk_id) |
| 138 | + resp = requests.delete( |
| 139 | + url, |
| 140 | + headers=_headers |
| 141 | + ) |
| 142 | + if resp.status_code == 200: |
| 143 | + data = resp.text |
| 144 | + else: |
| 145 | + data = json.loads(resp.text) |
| 146 | + return data, resp.status_code |
| 147 | + |
| 148 | + |
| 149 | +# chunkete-swam-controller implementation |
| 150 | + def getAllSWAMServices(self, chunk_id): |
| 151 | + url = "http://{}:{}/chunkete/chunk/{}/service/SWAM".format( |
| 152 | + self._ip, str(self._port), chunk_id) |
| 153 | + resp = requests.get( |
| 154 | + url, |
| 155 | + headers=_headers |
| 156 | + ) |
| 157 | + data = json.loads(resp.text) |
| 158 | + return data, resp.status_code |
| 159 | + |
| 160 | + def registerNewSWAMService(self, chunk_id, content): |
| 161 | + url = "http://{}:{}/chunkete/chunk/{}/service/SWAM".format( |
| 162 | + self._ip, str(self._port), chunk_id) |
| 163 | + resp = requests.post( |
| 164 | + url, |
| 165 | + data=content, |
| 166 | + headers=_headers |
| 167 | + ) |
| 168 | + data = json.loads(resp.text) |
| 169 | + return data, resp.status_code |
| 170 | + |
| 171 | + def getSWAMServiceById(self, chunk_id, service_id): |
| 172 | + url = "http://{}:{}/chunkete/chunk/{}/service/SWAM/{}".format( |
| 173 | + self._ip, str(self._port), chunk_id, service_id) |
| 174 | + resp = requests.get( |
| 175 | + url, |
| 176 | + headers=_headers |
| 177 | + ) |
| 178 | + data = json.loads(resp.text) |
| 179 | + return data, resp.status_code |
| 180 | + |
| 181 | + def removeExistingSWAMService(self, chunk_id, service_id): |
| 182 | + url = "http://{}:{}/chunkete/chunk/{}/service/SWAM/{}".format( |
| 183 | + self._ip, str(self._port), chunk_id, service_id) |
| 184 | + resp = requests.delete( |
| 185 | + url, |
| 186 | + headers=_headers |
| 187 | + ) |
| 188 | + if resp.text: |
| 189 | + data = json.loads(resp.text) |
| 190 | + else: |
| 191 | + data = '' |
| 192 | + return data, resp.status_code |
0 commit comments