blob: d7be6a0f1e5bf1b9e93d9b39d33fa30ac77fd61b [file] [log] [blame]
ivoutsas69b97632016-05-25 13:36:44 +03001/*
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 */
16
17package org.onosproject.drivers.cisco;
18
19import com.google.common.collect.Lists;
20import org.onosproject.net.AnnotationKeys;
21import org.onosproject.net.DefaultAnnotations;
22import org.onosproject.net.PortNumber;
23import org.onosproject.net.device.DefaultPortDescription;
24import org.onosproject.net.device.PortDescription;
25
26import java.util.Arrays;
27import java.util.List;
28
29import static org.onosproject.net.Port.Type;
30
31/**
32 *Parser for Netconf configurations and replys as plain text.
33 */
34public final class TextBlockParserCisco {
35
36 private static final String BANDWIDTH = "BW ";
37 private static final String SPEED = " Kbit/sec";
38 private static final String ETHERNET = "Eth";
39 private static final String FASTETHERNET = "Fas";
40 private static final String GIGABITETHERNET = "Gig";
41 private static final String FDDI = "Fdd";
42 private static final String POS = "POS";
43 private static final String SERIAL = "Ser";
44 private static final List INTERFACES = Arrays.asList(ETHERNET, FASTETHERNET, GIGABITETHERNET, SERIAL, FDDI, POS);
45 private static final List FIBERINTERFACES = Arrays.asList(FDDI, POS);
46
47 private static final String NEWLINE_SPLITTER = "\n";
48 private static final String PORT_DELIMITER = "/";
49 private static final String SPACE = " ";
50 private static final String IS_UP = "is up, line protocol is up";
51
52
53 private TextBlockParserCisco() {
54 //not called, preventing any allocation
55 }
56
57 /**
58 * Calls methods to create information about Ports.
59 * @param interfacesReply the interfaces as plain text
60 * @return the Port description list
61 */
62 public static List<PortDescription> parseCiscoIosPorts(String interfacesReply) {
63 String parentInterface;
64 String[] parentArray;
65 String tempString;
66 List<PortDescription> portDesc = Lists.newArrayList();
67 int interfacesCounter = interfacesCounterMethod(interfacesReply);
68 for (int i = 0; i < interfacesCounter; i++) {
69 parentInterface = parentInterfaceMethod(interfacesReply);
70 portDesc.add(findPortInfo(parentInterface));
71 parentArray = parentInterface.split(SPACE);
72 tempString = parentArray[0] + SPACE;
73 interfacesReply = interfacesReply.replace(tempString, SPACE + tempString);
74 }
75 return portDesc;
76 }
77
78 /**
79 * Creates the port information object.
80 * @param interfaceTree the interfaces as plain text
81 * @return the Port description object
82 */
83 private static DefaultPortDescription findPortInfo(String interfaceTree) {
84 String[] textStr = interfaceTree.split(NEWLINE_SPLITTER);
85 String[] firstLine = textStr[0].split(SPACE);
86 String firstWord = firstLine[0];
87 Type type = getPortType(textStr);
88 boolean isEnabled = getIsEnabled(textStr);
89 String port = getPort(textStr);
90 long portSpeed = getPortSpeed(textStr);
91 DefaultAnnotations.Builder annotations = DefaultAnnotations.builder()
92 .set(AnnotationKeys.PORT_NAME, firstWord);
93 return port == "-1" ? null : new DefaultPortDescription(PortNumber.portNumber(port),
94 isEnabled, type, portSpeed, annotations.build());
95 }
96
97 /**
98 * Counts the number of existing interfaces.
99 * @param interfacesReply the interfaces as plain text
100 * @return interfaces counter
101 */
102 private static int interfacesCounterMethod(String interfacesReply) {
103 int counter;
104 String first3Characters;
105 String[] textStr = interfacesReply.split(NEWLINE_SPLITTER);
106 int lastLine = textStr.length - 1;
107 counter = 0;
108 for (int i = 1; i < lastLine; i++) {
109 first3Characters = textStr[i].substring(0, 3);
110 if (INTERFACES.contains(first3Characters)) {
111 counter++;
112 }
113 }
114 return counter;
115 }
116
117 /**
118 * Parses the text and seperates to Parent Interfaces.
119 * @param interfacesReply the interfaces as plain text
120 * @return Parent interface
121 */
122 private static String parentInterfaceMethod(String interfacesReply) {
123 String firstCharacter;
124 String first3Characters;
125 boolean isChild = false;
126 StringBuilder anInterface = new StringBuilder("");
127 String[] textStr = interfacesReply.split("\\n");
128 int lastLine = textStr.length - 1;
129 for (int i = 1; i < lastLine; i++) {
130 firstCharacter = textStr[i].substring(0, 1);
131 first3Characters = textStr[i].substring(0, 3);
132 if (!(firstCharacter.equals(SPACE)) && isChild) {
133 break;
134 } else if (firstCharacter.equals(SPACE) && isChild) {
135 anInterface.append(textStr[i] + NEWLINE_SPLITTER);
136 } else if (INTERFACES.contains(first3Characters)) {
137 isChild = true;
138 anInterface.append(textStr[i] + NEWLINE_SPLITTER);
139 }
140 }
141 return anInterface.toString();
142 }
143
144 /**
145 * Get the port type for an interface.
146 * @param textStr interface splitted as an array
147 * @return Port type
148 */
149 private static Type getPortType(String[] textStr) {
150 String first3Characters;
151 first3Characters = textStr[0].substring(0, 3);
152 return FIBERINTERFACES.contains(first3Characters) ? Type.FIBER : Type.COPPER;
153 }
154
155 /**
156 * Get the state for an interface.
157 * @param textStr interface splitted as an array
158 * @return isEnabled state
159 */
160 private static boolean getIsEnabled(String[] textStr) {
161 return textStr[0].contains(IS_UP);
162 }
163
164 /**
165 * Get the port number for an interface.
166 * @param textStr interface splitted as an array
167 * @return port number
168 */
169 private static String getPort(String[] textStr) {
170 String port;
171 try {
172 if (textStr[0].indexOf(PORT_DELIMITER) > 0) {
173 port = textStr[0].substring(textStr[0].lastIndexOf(PORT_DELIMITER) + 1,
174 textStr[0].indexOf(SPACE));
175 } else {
176 port = "-1";
177 }
178 } catch (RuntimeException e) {
179 port = "-1";
180 }
181 return port;
182 }
183
184 /**
185 * Get the port speed for an interface.
186 * @param textStr interface splitted as an array
187 * @return port speed
188 */
189 private static long getPortSpeed(String[] textStr) {
190 long portSpeed = 0;
191 String result;
192 int lastLine = textStr.length - 1;
193 for (int i = 0; i < lastLine; i++) {
194 if ((textStr[i].indexOf(BANDWIDTH) > 0) && (textStr[i].indexOf(SPEED) > 0)) {
195 result = textStr[i].substring(textStr[i].indexOf(BANDWIDTH) + 3, textStr[i].indexOf(SPEED));
196 portSpeed = Long.valueOf(result);
197 break;
198 }
199 }
200 return portSpeed;
201 }
202
203}