Cisco Ios DeviceDescription

Change-Id: I51561c51fe18a6ee9676a894ec698e2a2fc222b4
diff --git a/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/CiscoIosDeviceDescription.java b/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/CiscoIosDeviceDescription.java
new file mode 100644
index 0000000..c9552ba
--- /dev/null
+++ b/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/CiscoIosDeviceDescription.java
@@ -0,0 +1,126 @@
+/*
+ * Copyright 2016-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;
+
+import com.google.common.collect.ImmutableList;
+
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.device.DefaultDeviceDescription;
+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.netconf.NetconfController;
+import org.onosproject.netconf.NetconfException;
+import org.onosproject.netconf.NetconfSession;
+import org.slf4j.Logger;
+import java.io.IOException;
+import java.util.List;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.slf4j.LoggerFactory.getLogger;
+
+public class CiscoIosDeviceDescription extends AbstractHandlerBehaviour
+        implements DeviceDescriptionDiscovery {
+
+
+    private final Logger log = getLogger(getClass());
+    private String version;
+    private String interfaces;
+
+    @Override
+    public DeviceDescription discoverDeviceDetails() {
+        NetconfController controller = checkNotNull(handler().get(NetconfController.class));
+        NetconfSession session = controller.getDevicesMap().get(handler().data().deviceId()).getSession();
+        try {
+            version = session.get(showVersionRequestBuilder());
+        } catch (IOException e) {
+            throw new RuntimeException(new NetconfException("Failed to retrieve version info.", e));
+        }
+
+        String[] details = TextBlockParserCisco.parseCiscoIosDeviceDetails(version);
+
+        DeviceService deviceService = checkNotNull(handler().get(DeviceService.class));
+        DeviceId deviceId = handler().data().deviceId();
+        Device device = deviceService.getDevice(deviceId);
+
+        return new DefaultDeviceDescription(device.id().uri(), Device.Type.SWITCH,
+                                            details[0], details[1],
+                                            details[2], details[3],
+                                            device.chassisId());
+    }
+
+    @Override
+    public List<PortDescription> discoverPortDetails() {
+        NetconfController controller = checkNotNull(handler().get(NetconfController.class));
+        NetconfSession session = controller.getDevicesMap().get(handler().data().deviceId()).getSession();
+        try {
+            interfaces = session.get(showInterfacesRequestBuilder());
+        } catch (IOException e) {
+            log.error("Failed to retrieve Interfaces");
+            return ImmutableList.of();
+        }
+        return ImmutableList.copyOf(TextBlockParserCisco.parseCiscoIosPorts(interfaces));
+    }
+
+    /**
+     * Builds a request crafted to get the configuration required to create
+     * details descriptions for the device.
+     *
+     * @return The request string.
+     */
+    private String showVersionRequestBuilder() {
+        StringBuilder rpc = new StringBuilder("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">");
+        rpc.append("<get>");
+        rpc.append("<filter>");
+        rpc.append("<config-format-text-block>");
+        rpc.append("<text-filter-spec> | include exp_to_match_run_conf </text-filter-spec>");
+        rpc.append("</config-format-text-block>");
+        rpc.append("<oper-data-format-text-block>");
+        rpc.append("<show>version</show>");
+        rpc.append("</oper-data-format-text-block>");
+        rpc.append("</filter>");
+        rpc.append("</get>");
+        rpc.append("</rpc>]]>]]>");
+        return rpc.toString();
+    }
+
+    /**
+     * Builds a request crafted to get the configuration required to create
+     * details descriptions for the device.
+     *
+     * @return The request string.
+     */
+    private String showInterfacesRequestBuilder() {
+        //Message ID is injected later.
+        StringBuilder rpc = new StringBuilder("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">");
+        rpc.append("<get>");
+        rpc.append("<filter>");
+        rpc.append("<config-format-text-block>");
+        rpc.append("<text-filter-spec> | include exp_to_match_run_conf </text-filter-spec>");
+        rpc.append("</config-format-text-block>");
+        rpc.append("<oper-data-format-text-block>");
+        rpc.append("<show>interfaces</show>");
+        rpc.append("</oper-data-format-text-block>");
+        rpc.append("</filter>");
+        rpc.append("</get>");
+        rpc.append("</rpc>]]>]]>");
+        return rpc.toString();
+    }
+
+}
\ No newline at end of file
diff --git a/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/PortGetterCiscoIosImpl.java b/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/PortGetterCiscoIosImpl.java
deleted file mode 100644
index 3a14a29..0000000
--- a/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/PortGetterCiscoIosImpl.java
+++ /dev/null
@@ -1,78 +0,0 @@
-/*
- * Copyright 2016-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;
-
-import com.google.common.collect.ImmutableList;
-import org.onosproject.net.behaviour.PortDiscovery;
-import org.onosproject.net.device.PortDescription;
-import org.onosproject.net.driver.AbstractHandlerBehaviour;
-import org.onosproject.netconf.NetconfController;
-import org.onosproject.netconf.NetconfSession;
-import org.slf4j.Logger;
-
-import java.io.IOException;
-import java.util.List;
-
-import static com.google.common.base.Preconditions.checkNotNull;
-import static org.slf4j.LoggerFactory.getLogger;
-
-/**
- * Retrieves the ports from Cisco IOS devices via netconf.
- */
-public class PortGetterCiscoIosImpl extends AbstractHandlerBehaviour
-        implements PortDiscovery {
-
-    private final Logger log = getLogger(getClass());
-    private String interfaces;
-
-    @Override
-    public List<PortDescription> getPorts() {
-        NetconfController controller = checkNotNull(handler().get(NetconfController.class));
-        NetconfSession session = controller.getDevicesMap().get(handler().data().deviceId()).getSession();
-        try {
-            interfaces = session.get(showInterfacesRequestBuilder());
-        } catch (IOException e) {
-            log.error("Failed to retrieve Interfaces");
-            return ImmutableList.of();
-        }
-        return ImmutableList.copyOf(TextBlockParserCisco.parseCiscoIosPorts(interfaces));
-    }
-
-    /**
-     * Builds a request crafted to get the configuration required to create port
-     * descriptions for the device.
-     *
-     * @return The request string.
-     */
-    private String showInterfacesRequestBuilder() {
-        //Message ID is injected later.
-        StringBuilder rpc = new StringBuilder("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">");
-        rpc.append("<get>");
-        rpc.append("<filter>");
-        rpc.append("<config-format-text-block>");
-        rpc.append("<text-filter-spec> | include exp_to_match_run_conf </text-filter-spec>");
-        rpc.append("</config-format-text-block>");
-        rpc.append("<oper-data-format-text-block>");
-        rpc.append("<show>interfaces</show>");
-        rpc.append("</oper-data-format-text-block>");
-        rpc.append("</filter>");
-        rpc.append("</get>");
-        rpc.append("</rpc>]]>]]>");
-        return rpc.toString();
-    }
-
-}
\ No newline at end of file
diff --git a/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/TextBlockParserCisco.java b/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/TextBlockParserCisco.java
index d7be6a0..2f533b5 100644
--- a/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/TextBlockParserCisco.java
+++ b/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/TextBlockParserCisco.java
@@ -1,18 +1,18 @@
 /*
- * Copyright 2016-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.
- */
+* Copyright 2016-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;
 
@@ -22,17 +22,22 @@
 import org.onosproject.net.PortNumber;
 import org.onosproject.net.device.DefaultPortDescription;
 import org.onosproject.net.device.PortDescription;
-
 import java.util.Arrays;
 import java.util.List;
 
 import static org.onosproject.net.Port.Type;
 
 /**
- *Parser for Netconf configurations and replys as plain text.
+ *Parser for Netconf XML configurations and replys as plain text.
  */
-public final class TextBlockParserCisco {
+final class TextBlockParserCisco {
 
+    private static final String PHRASE = "bytes of memory.";
+    private static final String VERSION = "Version ";
+    private static final String EOF_VERSION1 = "Software";
+    private static final String EOF_VERSION2 = "Software,";
+    private static final String EOF_VERSION3 = "RELEASE";
+    private static final String PROCESSOR_BOARD = "Processor board ID ";
     private static final String BANDWIDTH = "BW ";
     private static final String SPEED = " Kbit/sec";
     private static final String ETHERNET = "Eth";
@@ -41,17 +46,107 @@
     private static final String FDDI = "Fdd";
     private static final String POS = "POS";
     private static final String SERIAL = "Ser";
-    private static final List INTERFACES = Arrays.asList(ETHERNET, FASTETHERNET, GIGABITETHERNET, SERIAL, FDDI, POS);
-    private static final List FIBERINTERFACES = Arrays.asList(FDDI, POS);
-
     private static final String NEWLINE_SPLITTER = "\n";
     private static final String PORT_DELIMITER = "/";
     private static final String SPACE = " ";
     private static final String IS_UP = "is up, line protocol is up";
+    private static final List INTERFACES = Arrays.asList(ETHERNET, FASTETHERNET, GIGABITETHERNET, SERIAL, FDDI, POS);
+    private static final List FIBERINTERFACES = Arrays.asList(FDDI, POS);
 
 
     private TextBlockParserCisco() {
-        //not called, preventing any allocation
+        //not called
+    }
+
+    /**
+     * Adding information in an array for CiscoIosDeviceDescriptin call.
+     * @param version the return of show version command
+     * @return the array with the information
+     */
+    static String[] parseCiscoIosDeviceDetails(String version) {
+        String[] details = new String[4];
+        details[0] = getManufacturer(version);
+        details[1] = getHwVersion(version);
+        details[2] = getSwVersion(version);
+        details[3] = serialNumber(version);
+        return details;
+    }
+
+    /**
+     * Retrieving manufacturer of device.
+     * @param version the return of show version command
+     * @return the manufacturer of the device
+     */
+    private static String getManufacturer(String version) {
+        int i;
+        String[] textStr = version.split(NEWLINE_SPLITTER);
+        String[] lineStr = textStr[0].trim().split(SPACE);
+        return lineStr[0];
+    }
+
+    /**
+     * Retrieving hardware version of device.
+     * @param version the return of show version command
+     * @return the hardware version of the device
+     */
+    private static String getHwVersion(String version) {
+        String[] textStr = version.split(NEWLINE_SPLITTER);
+        String processor = SPACE;
+        int i;
+        for (i = 0; i < textStr.length; i++) {
+            if (textStr[i].indexOf(PHRASE) > 0) {
+                String[] lineStr = textStr[i].trim().split(SPACE);
+                processor = lineStr[1];
+                break;
+            } else {
+                processor = SPACE;
+            }
+        }
+        return processor;
+    }
+
+    /**
+     * Retrieving software version of device.
+     * @param version the return of show version command
+     * @return the software version of the device
+     */
+    private static String getSwVersion(String version) {
+        String[] textStr = version.split(NEWLINE_SPLITTER);
+        int i;
+        for (i = 0; i < textStr.length; i++) {
+            if (textStr[i].indexOf(VERSION) > 0) {
+                break;
+            }
+        }
+        String[] lineStr = textStr[i].trim().split(SPACE);
+        StringBuilder sw = new StringBuilder();
+        for (int j = 0; j < lineStr.length; j++) {
+            if (lineStr[j].equals(EOF_VERSION1) || lineStr[j].equals(EOF_VERSION2)
+                    ) {
+                sw.append(lineStr[j - 1]).append(SPACE);
+            } else if (lineStr[j].equals(EOF_VERSION3)) {
+                sw.append(lineStr[j - 1]);
+                sw.setLength(sw.length() - 1);
+            }
+        }
+        return sw.toString();
+    }
+
+    /**
+     * Retrieving serial number of device.
+     * @param version the return of show version command
+     * @return the serial number of the device
+     */
+    private static String serialNumber(String version) {
+        String[] textStr = version.split(NEWLINE_SPLITTER);
+        int i;
+        for (i = 0; i < textStr.length; i++) {
+            if (textStr[i].indexOf(PROCESSOR_BOARD) > 0) {
+                break;
+            }
+        }
+        return textStr[i].substring(textStr[i].indexOf(PROCESSOR_BOARD) + PROCESSOR_BOARD.length(),
+                                    textStr[i].length());
     }
 
     /**
@@ -90,7 +185,7 @@
         long portSpeed = getPortSpeed(textStr);
         DefaultAnnotations.Builder annotations = DefaultAnnotations.builder()
                 .set(AnnotationKeys.PORT_NAME, firstWord);
-        return port == "-1" ? null : new DefaultPortDescription(PortNumber.portNumber(port),
+        return port.equals("-1") ? null : new DefaultPortDescription(PortNumber.portNumber(port),
                                                                 isEnabled, type, portSpeed, annotations.build());
     }
 
@@ -132,10 +227,10 @@
             if (!(firstCharacter.equals(SPACE)) && isChild) {
                 break;
             } else if (firstCharacter.equals(SPACE) && isChild) {
-                anInterface.append(textStr[i] + NEWLINE_SPLITTER);
+                anInterface.append(textStr[i]).append(NEWLINE_SPLITTER);
             } else if (INTERFACES.contains(first3Characters)) {
                 isChild = true;
-                anInterface.append(textStr[i] + NEWLINE_SPLITTER);
+                anInterface.append(textStr[i]).append(NEWLINE_SPLITTER);
             }
         }
         return anInterface.toString();
@@ -200,4 +295,4 @@
         return portSpeed;
     }
 
-}
+}
\ No newline at end of file