From e04276a2acd93fc8c39a5e3268e89668a672babc Mon Sep 17 00:00:00 2001 From: Volodymyr Samotiy Date: Mon, 27 Jun 2016 14:02:05 +0300 Subject: [PATCH] Add bv-ctl command and API for GetSwitchProperties and GetSystemFeature Change-Id: I290c2e7c0b7286a179a88a7353f5d3f4b4365394 --- broadview_lib/config/broadview.py | 137 +++++++++++++++++++++++++ broadview_lib/tools/bv-ctl.py | 163 ++++++++++++++++++++++++++++++ broadview_lib/tools/examples.sh | 4 + 3 files changed, 304 insertions(+) create mode 100644 broadview_lib/config/broadview.py create mode 100644 broadview_lib/tools/bv-ctl.py diff --git a/broadview_lib/config/broadview.py b/broadview_lib/config/broadview.py new file mode 100644 index 0000000..ff40ba5 --- /dev/null +++ b/broadview_lib/config/broadview.py @@ -0,0 +1,137 @@ +# (C) Copyright Broadcom Corporation 2016 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from agentapi import AgentAPI +from broadview_lib.config.broadviewconfig import BroadViewBSTSwitches +import unittest + +class GetSwitchProperties(AgentAPI): + def __init__(self, host, port): + super(GetSwitchProperties, self).__init__() + self.setFeature("") + self.setHttpMethod("POST") + self.setHost(host) + self.setPort(port) + self.__asic_id = "1" + self.__json = None + + def setASIC(self, val): + self.__asic_id = val + + def toDict(self): + ret = {} + params = {} + ret["asic-id"] = self.__asic_id + ret["params"] = params + ret["method"] = "get-switch-properties" + return ret + + def getJSON(self): + return self.__json + + def send(self, timeout=30): + status, json = self._send(self.toDict(), timeout) + if status == 200: + self.__version = json["version"] + res = json["result"] + self.__json = res + return status + +class GetSystemFeature(AgentAPI): + def __init__(self, host, port): + super(GetSystemFeature, self).__init__() + self.setFeature("") + self.setHttpMethod("POST") + self.setHost(host) + self.setPort(port) + self.__asic_id = "1" + self.__json = None + + def setASIC(self, val): + self.__asic_id = val + + def toDict(self): + ret = {} + params = {} + ret["asic-id"] = self.__asic_id + ret["params"] = params + ret["method"] = "get-system-feature" + return ret + + def getJSON(self): + return self.__json + + def send(self, timeout=30): + status, json = self._send(self.toDict(), timeout) + if status == 200: + self.__version = json["version"] + res = json["result"] + self.__json = res + return status + +class TestTAPIParams(unittest.TestCase): + + def setUp(self): + pass + + def test_GetSwitchProperties(self): + + sw = BroadViewBSTSwitches() + if len(sw): + for x in sw: + host = x["ip"] + port = x["port"] + break + else: + host = "192.168.3.1" + port = 8080 + + x = GetSwitchProperties(host, port) + d = x.toDict() + self.assertTrue("asic-id" in d) + self.assertTrue("params" in d) + self.assertTrue("method" in d) + self.assertTrue(x.getFeature() == "") + self.assertTrue(x.getHttpMethod() == "POST") + self.assertTrue(x.getHost() == host) + self.assertTrue(x.getPort() == port) + self.assertTrue(d["asic-id"] == "1") + self.assertTrue(d["method"] == "get-switch-properties") + + def test_GetSystemFeature(self): + + sw = BroadViewBSTSwitches() + if len(sw): + for x in sw: + host = x["ip"] + port = x["port"] + break + else: + host = "192.168.3.1" + port = 8080 + + x = GetSystemFeature(host, port) + d = x.toDict() + self.assertTrue("asic-id" in d) + self.assertTrue("params" in d) + self.assertTrue("method" in d) + self.assertTrue(x.getFeature() == "") + self.assertTrue(x.getHttpMethod() == "POST") + self.assertTrue(x.getHost() == host) + self.assertTrue(x.getPort() == port) + self.assertTrue(d["asic-id"] == "1") + self.assertTrue(d["method"] == "get-system-feature") + +if __name__ == "__main__": + unittest.main() diff --git a/broadview_lib/tools/bv-ctl.py b/broadview_lib/tools/bv-ctl.py new file mode 100644 index 0000000..da6588c --- /dev/null +++ b/broadview_lib/tools/bv-ctl.py @@ -0,0 +1,163 @@ +#!/usr/bin/python + +# (C) Copyright Broadcom Corporation 2016 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from broadview_lib.config.broadview import * +from broadview_lib.config.broadviewconfig import BroadViewBSTSwitches +import sys +import json + +class BroadViewCommand(): + def __init__(self): + self._timeout = 30 + self.__cmds = { + "get-switch-properties" : self.handleGetSwitchProperties, + "get-system-feature" : self.handleGetSystemFeature, + "help": self.handleHelp, + } + + self.__help = { + "get-switch-properties" : self.helpGetSwitchProperties, + "get-system-feature" : self.helpGetSystemFeature, + } + + def getTimeout(self, args): + timeout = 30 + usage = False + for x in args: + if "timeout:" in x: + v = x.split(":") + if len(v) == 2: + timeout = int(v[1]) + else: + print "invalid timeout" + usage = True + return usage, timeout + + def getASICHostPort(self, args): + usage = False + asic = "1" + port = 8080 + host = None + + for x in args: + if "asic-id:" in x: + v = x.split(":") + if len(v) == 2: + asic = v[1] + else: + print "invalid asic-id" + usage = True + if "host:" in x: + v = x.split(":") + if len(v) == 2: + host = v[1] + else: + print "invalid host" + usage = True + if "port:" in x: + v = x.split(":") + if len(v) == 2: + port = int(v[1]) + else: + print "invalid port" + usage = True + + if host == None: + # host is required + print "missing host" + usage = True + + return usage, asic, host, port + + def usage(self): + print "usage: %s cmd host:ipv4 [timeout:seconds] [port:port] [asic-id:id] [args]" % (sys.argv[0]) + print + print "Commands:" + print + for key, val in self.__help.iteritems(): + print + val(key) + + def handleHelp(self, args): + usage = True + + if len(args): + cmd = args[0] + if cmd in self.__help: + self.__help[cmd](cmd) + usage = None + + return usage, None + + def handleGetSwitchProperties(self, args): + usage = False + ret = None + usage, asic, host, port = self.getASICHostPort(args) + if not usage: + x = GetSwitchProperties(host, port) + x.setASIC(asic) + status = x.send(self._timeout) + if status == 200: + ret = json.dumps(x.getJSON()) + print ret + else: + print "failure: %d" % (status) + + return usage, ret + + def helpGetSwitchProperties(self, name): + print name + + def handleGetSystemFeature(self, args): + usage = False + ret = None + usage, asic, host, port = self.getASICHostPort(args) + if not usage: + x = GetSystemFeature(host, port) + x.setASIC(asic) + status = x.send(self._timeout) + if status == 200: + ret = json.dumps(x.getJSON()) + print ret + else: + print "failure: %d" % (status) + + return usage, ret + + def helpGetSystemFeature(self, name): + print name + + def isCmd(self, cmd): + return cmd in self.__cmds + + def handle(self, args): + usage = True + ret = {} + status = False + if len(args): + cmd = args.pop(0) + if self.isCmd(cmd): + usage, ret = self.__cmds[cmd](args) + return usage, ret + +def main(argv): + x = BroadViewCommand() + usage, ret = x.handle(argv) + if usage: + x.usage() + +if __name__ == "__main__": + main(sys.argv[1:]) diff --git a/broadview_lib/tools/examples.sh b/broadview_lib/tools/examples.sh index 147de06..d222d07 100644 --- a/broadview_lib/tools/examples.sh +++ b/broadview_lib/tools/examples.sh @@ -15,6 +15,10 @@ host=10.14.244.128 port=8082 +echo "********** get-switch-properties **********" +python bv-ctl.py get-switch-properties timeout:30 host:$host port:$port +echo "********** get-system-feature **********" +python bv-ctl.py get-system-feature timeout:30 host:$host port:$port echo "********** cfg-feature **********" python bv-bstctl.py cfg-feature timeout:30 host:$host port:$port enable send_async_reports echo "********** get-feature **********"