ivoutsas
Committed by Jonathan Hart

Cisco Ios PortDiscovery

Change-Id: Ic51466d027f45de8ee91c9904e92b450bf9d0bb7
/*
* 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
/*
* 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.Lists;
import org.onosproject.net.AnnotationKeys;
import org.onosproject.net.DefaultAnnotations;
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.
*/
public final class TextBlockParserCisco {
private static final String BANDWIDTH = "BW ";
private static final String SPEED = " Kbit/sec";
private static final String ETHERNET = "Eth";
private static final String FASTETHERNET = "Fas";
private static final String GIGABITETHERNET = "Gig";
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 TextBlockParserCisco() {
//not called, preventing any allocation
}
/**
* Calls methods to create information about Ports.
* @param interfacesReply the interfaces as plain text
* @return the Port description list
*/
public static List<PortDescription> parseCiscoIosPorts(String interfacesReply) {
String parentInterface;
String[] parentArray;
String tempString;
List<PortDescription> portDesc = Lists.newArrayList();
int interfacesCounter = interfacesCounterMethod(interfacesReply);
for (int i = 0; i < interfacesCounter; i++) {
parentInterface = parentInterfaceMethod(interfacesReply);
portDesc.add(findPortInfo(parentInterface));
parentArray = parentInterface.split(SPACE);
tempString = parentArray[0] + SPACE;
interfacesReply = interfacesReply.replace(tempString, SPACE + tempString);
}
return portDesc;
}
/**
* Creates the port information object.
* @param interfaceTree the interfaces as plain text
* @return the Port description object
*/
private static DefaultPortDescription findPortInfo(String interfaceTree) {
String[] textStr = interfaceTree.split(NEWLINE_SPLITTER);
String[] firstLine = textStr[0].split(SPACE);
String firstWord = firstLine[0];
Type type = getPortType(textStr);
boolean isEnabled = getIsEnabled(textStr);
String port = getPort(textStr);
long portSpeed = getPortSpeed(textStr);
DefaultAnnotations.Builder annotations = DefaultAnnotations.builder()
.set(AnnotationKeys.PORT_NAME, firstWord);
return port == "-1" ? null : new DefaultPortDescription(PortNumber.portNumber(port),
isEnabled, type, portSpeed, annotations.build());
}
/**
* Counts the number of existing interfaces.
* @param interfacesReply the interfaces as plain text
* @return interfaces counter
*/
private static int interfacesCounterMethod(String interfacesReply) {
int counter;
String first3Characters;
String[] textStr = interfacesReply.split(NEWLINE_SPLITTER);
int lastLine = textStr.length - 1;
counter = 0;
for (int i = 1; i < lastLine; i++) {
first3Characters = textStr[i].substring(0, 3);
if (INTERFACES.contains(first3Characters)) {
counter++;
}
}
return counter;
}
/**
* Parses the text and seperates to Parent Interfaces.
* @param interfacesReply the interfaces as plain text
* @return Parent interface
*/
private static String parentInterfaceMethod(String interfacesReply) {
String firstCharacter;
String first3Characters;
boolean isChild = false;
StringBuilder anInterface = new StringBuilder("");
String[] textStr = interfacesReply.split("\\n");
int lastLine = textStr.length - 1;
for (int i = 1; i < lastLine; i++) {
firstCharacter = textStr[i].substring(0, 1);
first3Characters = textStr[i].substring(0, 3);
if (!(firstCharacter.equals(SPACE)) && isChild) {
break;
} else if (firstCharacter.equals(SPACE) && isChild) {
anInterface.append(textStr[i] + NEWLINE_SPLITTER);
} else if (INTERFACES.contains(first3Characters)) {
isChild = true;
anInterface.append(textStr[i] + NEWLINE_SPLITTER);
}
}
return anInterface.toString();
}
/**
* Get the port type for an interface.
* @param textStr interface splitted as an array
* @return Port type
*/
private static Type getPortType(String[] textStr) {
String first3Characters;
first3Characters = textStr[0].substring(0, 3);
return FIBERINTERFACES.contains(first3Characters) ? Type.FIBER : Type.COPPER;
}
/**
* Get the state for an interface.
* @param textStr interface splitted as an array
* @return isEnabled state
*/
private static boolean getIsEnabled(String[] textStr) {
return textStr[0].contains(IS_UP);
}
/**
* Get the port number for an interface.
* @param textStr interface splitted as an array
* @return port number
*/
private static String getPort(String[] textStr) {
String port;
try {
if (textStr[0].indexOf(PORT_DELIMITER) > 0) {
port = textStr[0].substring(textStr[0].lastIndexOf(PORT_DELIMITER) + 1,
textStr[0].indexOf(SPACE));
} else {
port = "-1";
}
} catch (RuntimeException e) {
port = "-1";
}
return port;
}
/**
* Get the port speed for an interface.
* @param textStr interface splitted as an array
* @return port speed
*/
private static long getPortSpeed(String[] textStr) {
long portSpeed = 0;
String result;
int lastLine = textStr.length - 1;
for (int i = 0; i < lastLine; i++) {
if ((textStr[i].indexOf(BANDWIDTH) > 0) && (textStr[i].indexOf(SPEED) > 0)) {
result = textStr[i].substring(textStr[i].indexOf(BANDWIDTH) + 3, textStr[i].indexOf(SPEED));
portSpeed = Long.valueOf(result);
break;
}
}
return portSpeed;
}
}
......@@ -19,5 +19,7 @@
hwVersion="" swVersion="IOS">
<behaviour api="org.onosproject.net.behaviour.InterfaceConfig"
impl="org.onosproject.drivers.cisco.InterfaceConfigCiscoIosImpl"/>
<behaviour api="org.onosproject.net.behaviour.PortDiscovery"
impl="org.onosproject.drivers.cisco.PortGetterCiscoIosImpl"/>
</driver>
</drivers>
......
/*
* 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 org.junit.Test;
import org.onosproject.net.AnnotationKeys;
import org.onosproject.net.DefaultAnnotations;
import org.onosproject.net.Port;
import org.onosproject.net.PortNumber;
import org.onosproject.net.device.DefaultPortDescription;
import org.onosproject.net.device.PortDescription;
import java.io.InputStream;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
/**
* Tests the parser for Netconf TextBlock configurations and replies from Cisco devices.
*/
public class TextBlockParserCiscoTest {
private static final PortNumber INTF1_PORT = PortNumber.portNumber(0);
private static final String INTF1_NAME = "FastEthernet0/0";
private static final PortNumber INTF2_PORT = PortNumber.portNumber(0);
private static final String INTF2_NAME = "Ethernet1/0";
private static final PortNumber INTF3_PORT = PortNumber.portNumber(0);
private static final String INTF3_NAME = "GigabitEthernet2/0";
private static final PortNumber INTF4_PORT = PortNumber.portNumber(0);
private static final String INTF4_NAME = "Serial3/0";
private static final PortNumber INTF5_PORT = PortNumber.portNumber(0);
private static final String INTF5_NAME = "POS4/0";
private static final PortNumber INTF6_PORT = PortNumber.portNumber(0);
private static final String INTF6_NAME = "Fddi5/0";
private static final Port.Type COPPER = Port.Type.COPPER;
private static final Port.Type FIBER = Port.Type.FIBER;
private static final long CONNECTION_SPEED_ETHERNET = 100000;
private static final long CONNECTION_SPEED_SERIAL = 1544;
private static final long CONNECTION_SPEED_POS = 9952000;
private static final long CONNECTION_SPEED_FDDI = 100000;
private static final boolean IS_ENABLED = true;
private static final boolean IS_NOT_ENABLED = false;
private static final String TEXT_FILE = "/CiscoIosInterfaces.xml";
@Test
public void controllersConfig() {
InputStream streamOrig = getClass().getResourceAsStream(TEXT_FILE);
String rpcReply = new Scanner(streamOrig, "UTF-8").useDelimiter("\\Z").next();
List<PortDescription> actualIntfs = TextBlockParserCisco.parseCiscoIosPorts(rpcReply);
assertEquals("Interfaces were not retrieved from configuration",
getExpectedIntfs(), actualIntfs);
}
private List<PortDescription> getExpectedIntfs() {
DefaultAnnotations.Builder int1Annotations = DefaultAnnotations.builder()
.set(AnnotationKeys.PORT_NAME, INTF1_NAME);
DefaultAnnotations.Builder int2Annotations = DefaultAnnotations.builder()
.set(AnnotationKeys.PORT_NAME, INTF2_NAME);
DefaultAnnotations.Builder int3Annotations = DefaultAnnotations.builder()
.set(AnnotationKeys.PORT_NAME, INTF3_NAME);
DefaultAnnotations.Builder int4Annotations = DefaultAnnotations.builder()
.set(AnnotationKeys.PORT_NAME, INTF4_NAME);
DefaultAnnotations.Builder int5Annotations = DefaultAnnotations.builder()
.set(AnnotationKeys.PORT_NAME, INTF5_NAME);
DefaultAnnotations.Builder int6Annotations = DefaultAnnotations.builder()
.set(AnnotationKeys.PORT_NAME, INTF6_NAME);
List<PortDescription> intfs = new ArrayList<>();
intfs.add(new DefaultPortDescription(INTF1_PORT, IS_ENABLED, COPPER, CONNECTION_SPEED_ETHERNET,
int1Annotations.build()));
intfs.add(new DefaultPortDescription(INTF2_PORT, IS_NOT_ENABLED, COPPER, CONNECTION_SPEED_ETHERNET,
int2Annotations.build()));
intfs.add(new DefaultPortDescription(INTF3_PORT, IS_NOT_ENABLED, COPPER, CONNECTION_SPEED_ETHERNET,
int3Annotations.build()));
intfs.add(new DefaultPortDescription(INTF4_PORT, IS_ENABLED, COPPER, CONNECTION_SPEED_SERIAL,
int4Annotations.build()));
intfs.add(new DefaultPortDescription(INTF5_PORT, IS_ENABLED, FIBER, CONNECTION_SPEED_POS,
int5Annotations.build()));
intfs.add(new DefaultPortDescription(INTF6_PORT, IS_ENABLED, FIBER, CONNECTION_SPEED_FDDI,
int6Annotations.build()));
return intfs;
}
}
<?xml version="1.0" encoding="UTF-8"?><rpc-reply message-id="7" xmlns="urn:ietf:params:netconf:base:1.0"><data><cli-oper-data-block><item><show>interfaces</show><response>
FastEthernet0/0 is up, line protocol is up
Hardware is i82543 (Livengood), address is ca00.12b5.0008 (bia ca00.12b5.0008)
Internet address is 192.168.1.20/24
MTU 1500 bytes, BW 100000 Kbit/sec, DLY 100 usec,
reliability 255/255, txload 1/255, rxload 1/255
Encapsulation ARPA, loopback not set
Keepalive set (10 sec)
Full-duplex, 100Mb/s, 100BaseTX/FX
ARP type: ARPA, ARP Timeout 04:00:00
Last input 00:00:00, output 00:00:00, output hang never
Last clearing of &quot;show interface&quot; counters never
Input queue: 0/75/0/0 (size/max/drops/flushes); Total output drops: 0
Queueing strategy: fifo
Output queue: 0/40 (size/max)
5 minute input rate 2000 bits/sec, 1 packets/sec
5 minute output rate 0 bits/sec, 0 packets/sec
3589 packets input, 681498 bytes
Received 2459 broadcasts, 0 runts, 0 giants, 0 throttles
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
0 watchdog
0 input packets with dribble condition detected
2518 packets output, 242991 bytes, 0 underruns
0 output errors, 0 collisions, 2 interface resets
149 unknown protocol drops
0 babbles, 0 late collision, 0 deferred
0 lost carrier, 0 no carrier
0 output buffer failures, 0 output buffers swapped out
Ethernet1/0 is administratively down, line protocol is down
Hardware is i82543 (Livengood), address is ca00.12b5.0006 (bia ca00.12b5.0006)
MTU 1500 bytes, BW 100000 Kbit/sec, DLY 100 usec,
reliability 255/255, txload 1/255, rxload 1/255
Encapsulation ARPA, loopback not set
Keepalive set (10 sec)
Full-duplex, 100Mb/s, 100BaseTX/FX
ARP type: ARPA, ARP Timeout 04:00:00
Last input never, output never, output hang never
Last clearing of &quot;show interface&quot; counters never
Input queue: 0/75/0/0 (size/max/drops/flushes); Total output drops: 0
Queueing strategy: fifo
Output queue: 0/40 (size/max)
5 minute input rate 0 bits/sec, 0 packets/sec
5 minute output rate 0 bits/sec, 0 packets/sec
0 packets input, 0 bytes
Received 0 broadcasts, 0 runts, 0 giants, 0 throttles
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
0 watchdog
0 input packets with dribble condition detected
0 packets output, 0 bytes, 0 underruns
0 output errors, 0 collisions, 0 interface resets
0 unknown protocol drops
0 babbles, 0 late collision, 0 deferred
0 lost carrier, 0 no carrier
0 output buffer failures, 0 output buffers swapped out
GigabitEthernet2/0 is administratively down, line protocol is down
Hardware is i82543 (Livengood), address is ca00.12b5.0006 (bia ca00.12b5.0006)
MTU 1500 bytes, BW 100000 Kbit/sec, DLY 100 usec,
reliability 255/255, txload 1/255, rxload 1/255
Encapsulation ARPA, loopback not set
Keepalive set (10 sec)
Full-duplex, 100Mb/s, 100BaseTX/FX
ARP type: ARPA, ARP Timeout 04:00:00
Last input never, output never, output hang never
Last clearing of &quot;show interface&quot; counters never
Input queue: 0/75/0/0 (size/max/drops/flushes); Total output drops: 0
Queueing strategy: fifo
Output queue: 0/40 (size/max)
5 minute input rate 0 bits/sec, 0 packets/sec
5 minute output rate 0 bits/sec, 0 packets/sec
0 packets input, 0 bytes
Received 0 broadcasts, 0 runts, 0 giants, 0 throttles
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
0 watchdog
0 input packets with dribble condition detected
0 packets output, 0 bytes, 0 underruns
0 output errors, 0 collisions, 0 interface resets
0 unknown protocol drops
0 babbles, 0 late collision, 0 deferred
0 lost carrier, 0 no carrier
0 output buffer failures, 0 output buffers swapped out
Serial3/0 is up, line protocol is up
Hardware is MCI Serial
Internet address is 192.168.10.203, subnet mask is 255.255.255.0
MTU 1500 bytes, BW 1544 Kbit/sec, DLY 20000 usec, rely 255/255, load 1/255
Encapsulation HDLC, loopback not set, keepalive set (10 sec)
Last input 0:00:07, output 0:00:00, output hang never
Output queue 0/40, 0 drops; input queue 0/75, 0 drops
5 minute input rate 0 bits/sec, 0 packets/sec
5 minute output rate 0 bits/sec, 0 packets/sec
16263 packets input, 1347238 bytes, 0 no buffer
Received 13983 broadcasts, 0 runts, 0 giants
2 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 2 abort
1 carrier transitions
22146 packets output, 2383680 bytes, 0 underruns
0 output errors, 0 collisions, 2 interface resets, 0 restarts
POS4/0 is up, line protocol is up
Hardware is Packet over SONET
Internet address is 10.41.41.2/24
MTU 4470 bytes, BW 9952000 Kbit/sec, DLY 100 usec, rely 255/255, load 1/255
Encapsulation HDLC, crc 32, loopback not set
Keepalive not set
Scramble enabled
Last input 00:00:59, output 00:00:11, output hang never
Last clearing of "show interface" counters 00:00:14
Queueing strategy: fifo
Output queue 0/40, 0 drops; input queue 0/75, 0 drops
Available Bandwidth 9582482 kilobits/sec
5 minute input rate 0 bits/sec, 0 packets/sec
5 minute output rate 0 bits/sec, 0 packets/sec
0 packets input, 0 bytes, 0 no buffer
Received 0 broadcasts, 0 runts, 0 giants, 0 throttles
0 parity
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort
1 packets output, 314 bytes, 0 underruns
0 output errors, 0 applique, 0 interface resets
0 output buffer failures, 0 output buffers swapped out
0 carrier transitions
Fddi5/0 is up, line protocol is up
Hardware is cxBus Fddi, address is 0000.0c02.adf1 (bia 0000.0c02.adf1)
Internet address is 10.108.33.14, subnet mask is 255.255.255.0
MTU 4470 bytes, BW 100000 Kbit/sec, DLY 100 usec, rely 255/255, load 1/255
Encapsulation SNAP, loopback not set, keepalive not set
ARP type: SNAP, ARP Timeout 4:00:00
Phy-A state is active, neighbor is B, cmt signal bits 008/20C, status ILS
Phy-B state is active, neighbor is A, cmt signal bits 20C/008, status ILS
ECM is in, CFM is thru, RMT is ring_op
Token rotation 5000 usec, ring operational 21:32:34
Upstream neighbor 0000.0c02.ba83, downstream neighbor 0000.0c02.ba83
Last input 0:00:05, output 0:00:00, output hang never
Last clearing of “show interface” counters 0:59:10
Output queue 0/40, 0 drops; input queue 0/75, 0 drops
5 minute input rate 69000 bits/sec, 44 packets/sec
5 minute output rate 0 bits/sec, 1 packets/sec
113157 packets input, 21622582 bytes, 0 no buffer
Received 276 broadcasts, 0 runts, 0 giants
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort
4740 packets output, 487346 bytes, 0 underruns
0 output errors, 0 collisions, 0 interface resets, 0 restarts
0 transitions, 2 traces, 3 claims, 2 beacons</response></item></cli-oper-data-block></data></rpc-reply>]]>]]>
\ No newline at end of file