ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 1 | /* |
ivoutsas | e78f988 | 2016-06-28 23:32:14 +0300 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | */ |
ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 16 | |
| 17 | package org.onosproject.drivers.cisco; |
| 18 | |
| 19 | import com.google.common.collect.Lists; |
| 20 | import org.onosproject.net.AnnotationKeys; |
| 21 | import org.onosproject.net.DefaultAnnotations; |
| 22 | import org.onosproject.net.PortNumber; |
| 23 | import org.onosproject.net.device.DefaultPortDescription; |
| 24 | import org.onosproject.net.device.PortDescription; |
ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 25 | import java.util.Arrays; |
| 26 | import java.util.List; |
| 27 | |
| 28 | import static org.onosproject.net.Port.Type; |
| 29 | |
| 30 | /** |
ivoutsas | e78f988 | 2016-06-28 23:32:14 +0300 | [diff] [blame] | 31 | *Parser for Netconf XML configurations and replys as plain text. |
ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 32 | */ |
ivoutsas | e78f988 | 2016-06-28 23:32:14 +0300 | [diff] [blame] | 33 | final class TextBlockParserCisco { |
ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 34 | |
ivoutsas | e78f988 | 2016-06-28 23:32:14 +0300 | [diff] [blame] | 35 | private static final String PHRASE = "bytes of memory."; |
| 36 | private static final String VERSION = "Version "; |
| 37 | private static final String EOF_VERSION1 = "Software"; |
| 38 | private static final String EOF_VERSION2 = "Software,"; |
| 39 | private static final String EOF_VERSION3 = "RELEASE"; |
| 40 | private static final String PROCESSOR_BOARD = "Processor board ID "; |
ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 41 | private static final String BANDWIDTH = "BW "; |
| 42 | private static final String SPEED = " Kbit/sec"; |
| 43 | private static final String ETHERNET = "Eth"; |
| 44 | private static final String FASTETHERNET = "Fas"; |
| 45 | private static final String GIGABITETHERNET = "Gig"; |
| 46 | private static final String FDDI = "Fdd"; |
| 47 | private static final String POS = "POS"; |
| 48 | private static final String SERIAL = "Ser"; |
ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 49 | private static final String NEWLINE_SPLITTER = "\n"; |
| 50 | private static final String PORT_DELIMITER = "/"; |
| 51 | private static final String SPACE = " "; |
| 52 | private static final String IS_UP = "is up, line protocol is up"; |
ivoutsas | e78f988 | 2016-06-28 23:32:14 +0300 | [diff] [blame] | 53 | private static final List INTERFACES = Arrays.asList(ETHERNET, FASTETHERNET, GIGABITETHERNET, SERIAL, FDDI, POS); |
| 54 | private static final List FIBERINTERFACES = Arrays.asList(FDDI, POS); |
ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 55 | |
| 56 | |
| 57 | private TextBlockParserCisco() { |
ivoutsas | e78f988 | 2016-06-28 23:32:14 +0300 | [diff] [blame] | 58 | //not called |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Adding information in an array for CiscoIosDeviceDescriptin call. |
| 63 | * @param version the return of show version command |
| 64 | * @return the array with the information |
| 65 | */ |
| 66 | static String[] parseCiscoIosDeviceDetails(String version) { |
| 67 | String[] details = new String[4]; |
| 68 | details[0] = getManufacturer(version); |
| 69 | details[1] = getHwVersion(version); |
| 70 | details[2] = getSwVersion(version); |
| 71 | details[3] = serialNumber(version); |
| 72 | return details; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Retrieving manufacturer of device. |
| 77 | * @param version the return of show version command |
| 78 | * @return the manufacturer of the device |
| 79 | */ |
| 80 | private static String getManufacturer(String version) { |
| 81 | int i; |
| 82 | String[] textStr = version.split(NEWLINE_SPLITTER); |
| 83 | String[] lineStr = textStr[0].trim().split(SPACE); |
| 84 | return lineStr[0]; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Retrieving hardware version of device. |
| 89 | * @param version the return of show version command |
| 90 | * @return the hardware version of the device |
| 91 | */ |
| 92 | private static String getHwVersion(String version) { |
| 93 | String[] textStr = version.split(NEWLINE_SPLITTER); |
| 94 | String processor = SPACE; |
| 95 | int i; |
| 96 | for (i = 0; i < textStr.length; i++) { |
| 97 | if (textStr[i].indexOf(PHRASE) > 0) { |
| 98 | String[] lineStr = textStr[i].trim().split(SPACE); |
| 99 | processor = lineStr[1]; |
| 100 | break; |
| 101 | } else { |
| 102 | processor = SPACE; |
| 103 | } |
| 104 | } |
| 105 | return processor; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Retrieving software version of device. |
| 110 | * @param version the return of show version command |
| 111 | * @return the software version of the device |
| 112 | */ |
| 113 | private static String getSwVersion(String version) { |
| 114 | String[] textStr = version.split(NEWLINE_SPLITTER); |
| 115 | int i; |
| 116 | for (i = 0; i < textStr.length; i++) { |
| 117 | if (textStr[i].indexOf(VERSION) > 0) { |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | String[] lineStr = textStr[i].trim().split(SPACE); |
| 122 | StringBuilder sw = new StringBuilder(); |
| 123 | for (int j = 0; j < lineStr.length; j++) { |
| 124 | if (lineStr[j].equals(EOF_VERSION1) || lineStr[j].equals(EOF_VERSION2) |
| 125 | ) { |
| 126 | sw.append(lineStr[j - 1]).append(SPACE); |
| 127 | } else if (lineStr[j].equals(EOF_VERSION3)) { |
| 128 | sw.append(lineStr[j - 1]); |
| 129 | sw.setLength(sw.length() - 1); |
| 130 | } |
| 131 | } |
| 132 | return sw.toString(); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Retrieving serial number of device. |
| 137 | * @param version the return of show version command |
| 138 | * @return the serial number of the device |
| 139 | */ |
| 140 | private static String serialNumber(String version) { |
| 141 | String[] textStr = version.split(NEWLINE_SPLITTER); |
| 142 | int i; |
| 143 | for (i = 0; i < textStr.length; i++) { |
| 144 | if (textStr[i].indexOf(PROCESSOR_BOARD) > 0) { |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | return textStr[i].substring(textStr[i].indexOf(PROCESSOR_BOARD) + PROCESSOR_BOARD.length(), |
| 149 | textStr[i].length()); |
ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Calls methods to create information about Ports. |
| 154 | * @param interfacesReply the interfaces as plain text |
| 155 | * @return the Port description list |
| 156 | */ |
| 157 | public static List<PortDescription> parseCiscoIosPorts(String interfacesReply) { |
| 158 | String parentInterface; |
| 159 | String[] parentArray; |
| 160 | String tempString; |
| 161 | List<PortDescription> portDesc = Lists.newArrayList(); |
| 162 | int interfacesCounter = interfacesCounterMethod(interfacesReply); |
| 163 | for (int i = 0; i < interfacesCounter; i++) { |
| 164 | parentInterface = parentInterfaceMethod(interfacesReply); |
| 165 | portDesc.add(findPortInfo(parentInterface)); |
| 166 | parentArray = parentInterface.split(SPACE); |
| 167 | tempString = parentArray[0] + SPACE; |
| 168 | interfacesReply = interfacesReply.replace(tempString, SPACE + tempString); |
| 169 | } |
| 170 | return portDesc; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Creates the port information object. |
| 175 | * @param interfaceTree the interfaces as plain text |
| 176 | * @return the Port description object |
| 177 | */ |
| 178 | private static DefaultPortDescription findPortInfo(String interfaceTree) { |
| 179 | String[] textStr = interfaceTree.split(NEWLINE_SPLITTER); |
| 180 | String[] firstLine = textStr[0].split(SPACE); |
| 181 | String firstWord = firstLine[0]; |
| 182 | Type type = getPortType(textStr); |
| 183 | boolean isEnabled = getIsEnabled(textStr); |
| 184 | String port = getPort(textStr); |
| 185 | long portSpeed = getPortSpeed(textStr); |
| 186 | DefaultAnnotations.Builder annotations = DefaultAnnotations.builder() |
| 187 | .set(AnnotationKeys.PORT_NAME, firstWord); |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 188 | return "-1".equals(port) ? null : new DefaultPortDescription(PortNumber.portNumber(port), |
| 189 | isEnabled, type, portSpeed, annotations.build()); |
ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Counts the number of existing interfaces. |
| 194 | * @param interfacesReply the interfaces as plain text |
| 195 | * @return interfaces counter |
| 196 | */ |
| 197 | private static int interfacesCounterMethod(String interfacesReply) { |
| 198 | int counter; |
| 199 | String first3Characters; |
| 200 | String[] textStr = interfacesReply.split(NEWLINE_SPLITTER); |
| 201 | int lastLine = textStr.length - 1; |
| 202 | counter = 0; |
| 203 | for (int i = 1; i < lastLine; i++) { |
| 204 | first3Characters = textStr[i].substring(0, 3); |
| 205 | if (INTERFACES.contains(first3Characters)) { |
| 206 | counter++; |
| 207 | } |
| 208 | } |
| 209 | return counter; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Parses the text and seperates to Parent Interfaces. |
| 214 | * @param interfacesReply the interfaces as plain text |
| 215 | * @return Parent interface |
| 216 | */ |
| 217 | private static String parentInterfaceMethod(String interfacesReply) { |
| 218 | String firstCharacter; |
| 219 | String first3Characters; |
| 220 | boolean isChild = false; |
| 221 | StringBuilder anInterface = new StringBuilder(""); |
| 222 | String[] textStr = interfacesReply.split("\\n"); |
| 223 | int lastLine = textStr.length - 1; |
| 224 | for (int i = 1; i < lastLine; i++) { |
| 225 | firstCharacter = textStr[i].substring(0, 1); |
| 226 | first3Characters = textStr[i].substring(0, 3); |
| 227 | if (!(firstCharacter.equals(SPACE)) && isChild) { |
| 228 | break; |
| 229 | } else if (firstCharacter.equals(SPACE) && isChild) { |
ivoutsas | e78f988 | 2016-06-28 23:32:14 +0300 | [diff] [blame] | 230 | anInterface.append(textStr[i]).append(NEWLINE_SPLITTER); |
ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 231 | } else if (INTERFACES.contains(first3Characters)) { |
| 232 | isChild = true; |
ivoutsas | e78f988 | 2016-06-28 23:32:14 +0300 | [diff] [blame] | 233 | anInterface.append(textStr[i]).append(NEWLINE_SPLITTER); |
ivoutsas | 69b9763 | 2016-05-25 13:36:44 +0300 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | return anInterface.toString(); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Get the port type for an interface. |
| 241 | * @param textStr interface splitted as an array |
| 242 | * @return Port type |
| 243 | */ |
| 244 | private static Type getPortType(String[] textStr) { |
| 245 | String first3Characters; |
| 246 | first3Characters = textStr[0].substring(0, 3); |
| 247 | return FIBERINTERFACES.contains(first3Characters) ? Type.FIBER : Type.COPPER; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Get the state for an interface. |
| 252 | * @param textStr interface splitted as an array |
| 253 | * @return isEnabled state |
| 254 | */ |
| 255 | private static boolean getIsEnabled(String[] textStr) { |
| 256 | return textStr[0].contains(IS_UP); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Get the port number for an interface. |
| 261 | * @param textStr interface splitted as an array |
| 262 | * @return port number |
| 263 | */ |
| 264 | private static String getPort(String[] textStr) { |
| 265 | String port; |
| 266 | try { |
| 267 | if (textStr[0].indexOf(PORT_DELIMITER) > 0) { |
| 268 | port = textStr[0].substring(textStr[0].lastIndexOf(PORT_DELIMITER) + 1, |
| 269 | textStr[0].indexOf(SPACE)); |
| 270 | } else { |
| 271 | port = "-1"; |
| 272 | } |
| 273 | } catch (RuntimeException e) { |
| 274 | port = "-1"; |
| 275 | } |
| 276 | return port; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Get the port speed for an interface. |
| 281 | * @param textStr interface splitted as an array |
| 282 | * @return port speed |
| 283 | */ |
| 284 | private static long getPortSpeed(String[] textStr) { |
| 285 | long portSpeed = 0; |
| 286 | String result; |
| 287 | int lastLine = textStr.length - 1; |
| 288 | for (int i = 0; i < lastLine; i++) { |
| 289 | if ((textStr[i].indexOf(BANDWIDTH) > 0) && (textStr[i].indexOf(SPEED) > 0)) { |
| 290 | result = textStr[i].substring(textStr[i].indexOf(BANDWIDTH) + 3, textStr[i].indexOf(SPEED)); |
| 291 | portSpeed = Long.valueOf(result); |
| 292 | break; |
| 293 | } |
| 294 | } |
| 295 | return portSpeed; |
| 296 | } |
| 297 | |
ivoutsas | e78f988 | 2016-06-28 23:32:14 +0300 | [diff] [blame] | 298 | } |