[ONOS-6529] Add Cisco REST device drivers
Change-Id: I97de0a7d326492a90d1541e5c028255f0b735aa0
diff --git a/drivers/cisco/rest/src/main/java/org/onosproject/drivers/cisco/rest/CiscoNxosDeviceDescription.java b/drivers/cisco/rest/src/main/java/org/onosproject/drivers/cisco/rest/CiscoNxosDeviceDescription.java
new file mode 100644
index 0000000..92a9b7f
--- /dev/null
+++ b/drivers/cisco/rest/src/main/java/org/onosproject/drivers/cisco/rest/CiscoNxosDeviceDescription.java
@@ -0,0 +1,155 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * 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.
+ */
+
+package org.onosproject.drivers.cisco.rest;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.Lists;
+import org.onlab.packet.ChassisId;
+import org.onosproject.net.AnnotationKeys;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Port;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.SparseAnnotations;
+import org.onosproject.net.device.DefaultDeviceDescription;
+import org.onosproject.net.device.DefaultPortDescription;
+import org.onosproject.net.device.DeviceDescription;
+import org.onosproject.net.device.DeviceDescriptionDiscovery;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.device.PortDescription;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.protocol.rest.RestSBController;
+import org.slf4j.Logger;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Discovers device information from a Cisco NXOS device.
+ */
+public class CiscoNxosDeviceDescription extends AbstractHandlerBehaviour
+ implements DeviceDescriptionDiscovery {
+ private final Logger log = getLogger(getClass());
+
+ private static final String UNKNOWN = "unknown";
+
+ private static final String SHOW_INTERFACES_CMD = "show interface";
+ private static final String SHOW_VERSION_CMD = "show version";
+
+ private static final String MANUFACTURER = "manufacturer";
+ private static final String CHASSIS_ID = "chassis_id";
+ private static final String KICKSTART_VER = "kickstart_ver_str";
+ private static final String ROW_INTERFACE = "ROW_interface";
+ private static final String INTERFACE = "interface";
+ private static final String ETH = "Eth";
+ private static final String ETHERNET = "Ethernet";
+ private static final String STATE = "state";
+ private static final String UP = "up";
+ private static final String ETH_BW = "eth_bw";
+ private static final String SLASH = "/";
+ private static final String ZERO = "0";
+ private static final int ONE_THOUSAND = 1000;
+
+
+ @Override
+ public DeviceDescription discoverDeviceDetails() {
+ DriverHandler handler = handler();
+ RestSBController controller = checkNotNull(handler.get(RestSBController.class));
+ DeviceId deviceId = handler.data().deviceId();
+
+ ArrayList<String> cmd = new ArrayList<>();
+ cmd.add(SHOW_VERSION_CMD);
+
+ String req = NxApiRequest.generate(cmd, NxApiRequest.CommandType.CLI);
+
+ String response = NxApiRequest.post(controller, deviceId, req);
+
+ String mrf = UNKNOWN;
+ String hwVer = UNKNOWN;
+ String swVer = UNKNOWN;
+ String serialNum = UNKNOWN;
+
+ try {
+ ObjectMapper om = new ObjectMapper();
+ JsonNode json = om.readTree(response);
+
+ JsonNode body = json.findValue("body");
+ if (body != null) {
+ mrf = body.get(MANUFACTURER).asText();
+ hwVer = body.get(CHASSIS_ID).asText();
+ swVer = body.get(KICKSTART_VER).asText();
+ }
+ } catch (IOException e) {
+ log.error("Failed to to retrieve Device Information {}", e);
+ }
+
+ DeviceService deviceService = checkNotNull(handler().get(DeviceService.class));
+ Device device = deviceService.getDevice(deviceId);
+ return new DefaultDeviceDescription(device.id().uri(), Device.Type.SWITCH,
+ mrf, hwVer, swVer, serialNum,
+ new ChassisId(), (SparseAnnotations) device.annotations());
+ }
+
+ @Override
+ public List<PortDescription> discoverPortDetails() {
+ DriverHandler handler = handler();
+ RestSBController controller = checkNotNull(handler.get(RestSBController.class));
+ DeviceId deviceId = handler.data().deviceId();
+
+ ArrayList<String> cmd = new ArrayList<>();
+ cmd.add(SHOW_INTERFACES_CMD);
+
+ String req = NxApiRequest.generate(cmd, NxApiRequest.CommandType.CLI);
+
+ String response = NxApiRequest.post(controller, deviceId, req);
+
+ // parse interface information from response
+ List<PortDescription> ports = Lists.newArrayList();
+ try {
+ ObjectMapper om = new ObjectMapper();
+ JsonNode json = om.readTree(response);
+
+ JsonNode interfaces = json.findValue(ROW_INTERFACE);
+ if (interfaces != null) {
+ interfaces.forEach(itf -> {
+ String ifName = itf.get(INTERFACE).asText();
+ if (ifName.startsWith(ETH)) {
+ String ifNum = ifName.substring(ETHERNET.length()).replace(SLASH, ZERO);
+ boolean state = itf.get(STATE).asText().equals(UP);
+ long portSpeed = itf.get(ETH_BW).asLong() / ONE_THOUSAND; //in Mbps
+ DefaultAnnotations.Builder annotations = DefaultAnnotations.builder()
+ .set(AnnotationKeys.PORT_NAME, ifName);
+ PortDescription desc = new DefaultPortDescription(PortNumber.portNumber(ifNum), state,
+ Port.Type.FIBER, portSpeed, annotations.build());
+ ports.add(desc);
+ }
+ });
+ }
+ } catch (IOException e) {
+ log.error("Failed to to retrieve Interfaces {}", e);
+ }
+
+ return ports;
+ }
+}
diff --git a/drivers/cisco/rest/src/main/java/org/onosproject/drivers/cisco/rest/CiscoRestDriversLoader.java b/drivers/cisco/rest/src/main/java/org/onosproject/drivers/cisco/rest/CiscoRestDriversLoader.java
new file mode 100644
index 0000000..7450a90
--- /dev/null
+++ b/drivers/cisco/rest/src/main/java/org/onosproject/drivers/cisco/rest/CiscoRestDriversLoader.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * 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.
+ */
+
+package org.onosproject.drivers.cisco.rest;
+
+import org.apache.felix.scr.annotations.Component;
+import org.onosproject.net.driver.AbstractDriverLoader;
+
+/**
+ * Loader for Cisco REST device drivers.
+ */
+@Component(immediate = true)
+public class CiscoRestDriversLoader extends AbstractDriverLoader {
+ public CiscoRestDriversLoader() {
+ super("/cisco-rest-drivers.xml");
+ }
+}
diff --git a/drivers/cisco/rest/src/main/java/org/onosproject/drivers/cisco/rest/NxApiRequest.java b/drivers/cisco/rest/src/main/java/org/onosproject/drivers/cisco/rest/NxApiRequest.java
new file mode 100644
index 0000000..79a1d58
--- /dev/null
+++ b/drivers/cisco/rest/src/main/java/org/onosproject/drivers/cisco/rest/NxApiRequest.java
@@ -0,0 +1,120 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * 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.
+ */
+
+package org.onosproject.drivers.cisco.rest;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.stream.IntStream;
+
+import org.onosproject.net.DeviceId;
+import org.onosproject.protocol.rest.RestSBController;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import javax.ws.rs.core.MediaType;
+
+/**
+ * Generate json-rpc request for Cisco NX-API.
+ */
+public final class NxApiRequest {
+ public enum CommandType {
+ /**
+ * Command outputs are in JSON format.
+ */
+ CLI,
+ /**
+ * Command outputs are in raw ASCII text.
+ */
+ CLI_ASCII,
+ }
+
+ private static final String CMD = "cmd";
+ private static final String VERSION = "version";
+ private static final String JSONRPC = "jsonrpc";
+ private static final String TWO_POINT_ZERO = "2.0";
+ private static final String METHOD = "method";
+ private static final String CLI = "cli";
+ private static final String CLI_ASCII = "cli_ascii";
+ private static final String PARAMS = "params";
+ private static final String ID = "id";
+ private static final int ONE = 1;
+
+ private static final String API_URI = "/ins";
+ private static final String APP_JSON_RPC = "application/json-rpc";
+
+ private NxApiRequest() {
+ }
+
+ /**
+ * Generates a NX-API request message to execute on the Cisco NXOS device.
+ * @param commands list of commands to execute
+ * @param type response message format
+ * @return the NX-API request string
+ */
+ public static String generate(List<String> commands, CommandType type) {
+ ObjectMapper om = new ObjectMapper();
+ ArrayNode aryNode = om.createArrayNode();
+
+ if (commands == null) {
+ return aryNode.toString();
+ }
+
+ IntStream.range(0, commands.size()).forEach(idx -> {
+ ObjectNode parm = om.createObjectNode();
+ parm.put(CMD, commands.get(idx));
+ parm.put(VERSION, ONE);
+
+ ObjectNode node = om.createObjectNode();
+ node.put(JSONRPC, TWO_POINT_ZERO);
+ switch (type) {
+ case CLI_ASCII:
+ node.put(METHOD, CLI_ASCII);
+ break;
+ case CLI:
+ default:
+ node.put(METHOD, CLI);
+ break;
+ }
+
+ node.set(PARAMS, parm);
+ node.put(ID, idx + 1);
+
+ aryNode.add(node);
+ });
+
+ return aryNode.toString();
+ }
+
+ /**
+ * Sends NX-API request message to the device.
+ * @param controller RestSBController for Cisco REST device
+ * @param deviceId DeviceId for Cisco REST device
+ * @param request NX-API request string
+ * @return the response string
+ */
+ public static String post(RestSBController controller, DeviceId deviceId, String request) {
+ InputStream stream = new ByteArrayInputStream(request.getBytes(StandardCharsets.UTF_8));
+ String response = controller.post(deviceId, API_URI, stream,
+ MediaType.valueOf(APP_JSON_RPC), String.class);
+
+ return response;
+ }
+}
diff --git a/drivers/cisco/rest/src/main/java/org/onosproject/drivers/cisco/rest/package-info.java b/drivers/cisco/rest/src/main/java/org/onosproject/drivers/cisco/rest/package-info.java
new file mode 100644
index 0000000..80840f4
--- /dev/null
+++ b/drivers/cisco/rest/src/main/java/org/onosproject/drivers/cisco/rest/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * 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.
+ */
+
+/**
+ * Package for Cisco REST device drivers.
+ */
+package org.onosproject.drivers.cisco.rest;
\ No newline at end of file
diff --git a/drivers/cisco/rest/src/main/resources/cisco-rest-drivers.xml b/drivers/cisco/rest/src/main/resources/cisco-rest-drivers.xml
new file mode 100644
index 0000000..9b8abdf
--- /dev/null
+++ b/drivers/cisco/rest/src/main/resources/cisco-rest-drivers.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Copyright 2017-present Open Networking Laboratory
+ ~
+ ~ 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.
+ -->
+<drivers>
+ <driver name="cisco-rest" manufacturer="Cisco" hwVersion="" swVersion="NXOS">
+ <behaviour api="org.onosproject.net.device.DeviceDescriptionDiscovery"
+ impl="org.onosproject.drivers.cisco.rest.CiscoNxosDeviceDescription"/>
+ </driver>
+</drivers>
diff --git a/drivers/cisco/rest/src/test/java/org/onosproject/drivers/cisco/rest/CiscoRestDriversLoaderTest.java b/drivers/cisco/rest/src/test/java/org/onosproject/drivers/cisco/rest/CiscoRestDriversLoaderTest.java
new file mode 100644
index 0000000..838e437
--- /dev/null
+++ b/drivers/cisco/rest/src/test/java/org/onosproject/drivers/cisco/rest/CiscoRestDriversLoaderTest.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * 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.
+ */
+package org.onosproject.drivers.cisco.rest;
+
+import org.junit.Before;
+import org.onosproject.net.driver.AbstractDriverLoaderTest;
+
+
+/**
+ * Cisco REST drivers loader test.
+ */
+public class CiscoRestDriversLoaderTest extends AbstractDriverLoaderTest {
+
+ @Before
+ public void setUp() {
+ loader = new CiscoRestDriversLoader();
+ }
+}
diff --git a/drivers/cisco/rest/src/test/java/org/onosproject/drivers/cisco/rest/NxApiRequestTest.java b/drivers/cisco/rest/src/test/java/org/onosproject/drivers/cisco/rest/NxApiRequestTest.java
new file mode 100644
index 0000000..4ef61da
--- /dev/null
+++ b/drivers/cisco/rest/src/test/java/org/onosproject/drivers/cisco/rest/NxApiRequestTest.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * 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.
+ */
+
+package org.onosproject.drivers.cisco.rest;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Tests Cisco NX-API request message generator for Cisco NXOS devices.
+ */
+public class NxApiRequestTest {
+ public static final String REQ_FILE1 = "/testNxApiRequest1.json";
+ public static final String REQ_FILE2 = "/testNxApiRequest2.json";
+
+ @Test
+ public void oneRequestTest() throws IOException {
+ InputStream streamOrig = getClass().getResourceAsStream(REQ_FILE1);
+ ObjectMapper om = new ObjectMapper();
+ JsonNode node1 = om.readTree(streamOrig);
+
+ ArrayList<String> cmds = new ArrayList<>();
+ cmds.add("show interface");
+
+ JsonNode node2 = om.readTree(NxApiRequest.generate(cmds, NxApiRequest.CommandType.CLI));
+
+ assertEquals(node1.toString(), node2.toString());
+ }
+
+ @Test
+ public void manyRequestsTest() throws IOException {
+ InputStream streamOrig = getClass().getResourceAsStream(REQ_FILE2);
+ ObjectMapper om = new ObjectMapper();
+ JsonNode node1 = om.readTree(streamOrig);
+
+ ArrayList<String> cmds = new ArrayList<>();
+ cmds.add("show interface");
+ cmds.add("show ver");
+
+ JsonNode node2 = om.readTree(NxApiRequest.generate(cmds, NxApiRequest.CommandType.CLI));
+
+ assertEquals(node1.toString(), node2.toString());
+ }
+}
diff --git a/drivers/cisco/rest/src/test/resources/testNxApiRequest1.json b/drivers/cisco/rest/src/test/resources/testNxApiRequest1.json
new file mode 100644
index 0000000..84335fe
--- /dev/null
+++ b/drivers/cisco/rest/src/test/resources/testNxApiRequest1.json
@@ -0,0 +1,11 @@
+[
+ {
+ "jsonrpc": "2.0",
+ "method": "cli",
+ "params": {
+ "cmd": "show interface",
+ "version": 1
+ },
+ "id": 1
+ }
+]
\ No newline at end of file
diff --git a/drivers/cisco/rest/src/test/resources/testNxApiRequest2.json b/drivers/cisco/rest/src/test/resources/testNxApiRequest2.json
new file mode 100644
index 0000000..687e4c6
--- /dev/null
+++ b/drivers/cisco/rest/src/test/resources/testNxApiRequest2.json
@@ -0,0 +1,20 @@
+[
+ {
+ "jsonrpc": "2.0",
+ "method": "cli",
+ "params": {
+ "cmd": "show interface",
+ "version": 1
+ },
+ "id": 1
+ },
+ {
+ "jsonrpc": "2.0",
+ "method": "cli",
+ "params": {
+ "cmd": "show ver",
+ "version": 1
+ },
+ "id": 2
+ }
+]
\ No newline at end of file