amit.ghosh | 6104cf5 | 2021-01-07 16:53:28 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | package org.opencord.olttopology.impl; |
| 17 | |
| 18 | import org.apache.commons.lang.ArrayUtils; |
| 19 | import org.onlab.packet.BasePacket; |
| 20 | import org.onlab.packet.Ethernet; |
| 21 | import org.onlab.packet.EthType; |
| 22 | import org.onlab.packet.Ip4Address; |
| 23 | import org.onlab.packet.LLDP; |
| 24 | import org.onlab.packet.LLDPTLV; |
| 25 | import org.onlab.packet.MacAddress; |
| 26 | |
| 27 | |
| 28 | import org.onosproject.net.ConnectPoint; |
| 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.flow.criteria.Criterion; |
| 31 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
| 32 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 33 | import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter; |
| 34 | import org.onosproject.net.packet.DefaultInboundPacket; |
| 35 | import org.onosproject.net.packet.InboundPacket; |
| 36 | import org.onosproject.net.packet.OutboundPacket; |
| 37 | import org.onosproject.net.packet.DefaultPacketContext; |
| 38 | import org.onosproject.net.packet.PacketContext; |
| 39 | import org.onosproject.net.packet.PacketProcessor; |
| 40 | import org.onosproject.net.packet.PacketServiceAdapter; |
| 41 | |
| 42 | import static org.hamcrest.Matchers.is; |
| 43 | import static org.hamcrest.Matchers.instanceOf; |
| 44 | import static org.hamcrest.Matchers.notNullValue; |
| 45 | |
| 46 | import org.opencord.olttopology.OltNeighborInfo; |
| 47 | import org.slf4j.Logger; |
| 48 | |
| 49 | import static org.junit.Assert.assertThat; |
| 50 | |
| 51 | import java.nio.ByteBuffer; |
| 52 | import java.util.Collection; |
| 53 | import java.util.LinkedList; |
| 54 | import java.util.List; |
| 55 | import java.util.Map; |
| 56 | |
| 57 | import static org.junit.Assert.fail; |
| 58 | |
| 59 | |
| 60 | import static org.slf4j.LoggerFactory.getLogger; |
| 61 | |
| 62 | /* |
| 63 | Olt topology test base class. |
| 64 | */ |
| 65 | public class OltTopologyTestBase { |
| 66 | private final Logger log = getLogger(getClass()); |
| 67 | |
| 68 | //Time in ms to wait before checking packets |
| 69 | static final int ASSERTION_DELAY = 250; |
| 70 | //Duration in ms of the assertion for packets |
| 71 | static final int ASSERTION_LENGTH = 250; |
| 72 | |
| 73 | private static final String EXPECTED_IP = "10.10.10.10"; |
| 74 | private static final String EXPECTED_OLT_DEV_ID = "of:0000c6b1cd40dc93"; |
| 75 | private static final DeviceId EXPECTED_DEVICE_ID_1 = DeviceId.deviceId(EXPECTED_OLT_DEV_ID); |
| 76 | |
| 77 | |
| 78 | |
| 79 | List<BasePacket> savedPackets = new LinkedList<>(); |
| 80 | PacketProcessor packetProcessor; |
| 81 | MacAddress srcMac = MacAddress.valueOf("c6:b1:cd:40:dc:93"); |
| 82 | MacAddress dstMac = MacAddress.valueOf(OsgiPropertyConstants.DEFAULT_DEST_MAC_ADDRESS_DEFAULT); |
| 83 | |
| 84 | |
| 85 | /** |
| 86 | * Saves the given packet onto the saved packets list. |
| 87 | * |
| 88 | * @param packet packet to save |
| 89 | */ |
| 90 | void savePacket(BasePacket packet) { |
| 91 | savedPackets.add(packet); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Sends packet to Packer process to test Handle packet functionality. |
| 96 | * @param packet LLDP ethernet packet |
| 97 | * @param cp Device connect point info |
| 98 | */ |
| 99 | void sendInboundPacket(Ethernet packet, ConnectPoint cp) { |
| 100 | final ByteBuffer byteBuffer = ByteBuffer.wrap(packet.serialize()); |
| 101 | InboundPacket inPacket = new DefaultInboundPacket(cp, packet, byteBuffer); |
| 102 | PacketContext context = new TestPacketContext(127L, inPacket, null, false); |
| 103 | packetProcessor.process(context); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Validates LLDP packet out functionality. |
| 108 | * @param responsePacket LLDP packet |
| 109 | */ |
| 110 | void checkLldpPacket(Ethernet responsePacket) { |
| 111 | assertThat(responsePacket.getSourceMAC(), is(srcMac)); |
| 112 | assertThat(responsePacket.getDestinationMAC(), is(dstMac)); |
| 113 | assertThat(responsePacket.getPayload(), instanceOf(LLDP.class)); |
| 114 | assertThat(responsePacket.getEtherType(), is(Ethernet.TYPE_LLDP)); |
| 115 | LLDP lldp = (LLDP) responsePacket.getPayload(); |
| 116 | assertThat(lldp, notNullValue()); |
| 117 | assertThat(lldp.getPortId(), notNullValue()); |
| 118 | String portName = "nni-"; |
| 119 | byte[] port = ArrayUtils.addAll(new byte[] {5}, portName.getBytes()); |
| 120 | assertThat(lldp.getPortId().getValue(), is(port)); |
| 121 | assertThat(lldp.getChassisId(), notNullValue()); |
| 122 | int chassisId = 0; |
| 123 | byte[] chassis = ArrayUtils.addAll(new byte[] {1}, |
| 124 | ByteBuffer.allocate(String.valueOf(chassisId).length()) |
| 125 | .put(String.valueOf(chassisId).getBytes()).array()); |
| 126 | assertThat(lldp.getChassisId().getValue(), is(chassis)); |
| 127 | assertThat(lldp.getTtl().getValue(), notNullValue()); |
| 128 | short ttl = 120; |
| 129 | byte[] time = ByteBuffer.allocate(2).putShort(ttl).array(); |
| 130 | assertThat(lldp.getTtl().getValue(), is(time)); |
| 131 | assertThat(lldp.getOptionalTLVList(), notNullValue()); |
| 132 | List<LLDPTLV> optionalTlvs = lldp.getOptionalTLVList(); |
| 133 | for (LLDPTLV tlv: optionalTlvs) { |
| 134 | if (tlv.getType() == OltTopology.SYSTEMNAME_TLV_TYPE) { |
| 135 | assertThat(tlv.getValue(), notNullValue()); |
| 136 | String[] systemName = EXPECTED_DEVICE_ID_1.toString().split(":", 2); |
| 137 | byte[] deviceId = systemName[1].getBytes(); |
| 138 | assertThat(tlv.getValue(), is(deviceId)); |
| 139 | } else if (tlv.getType() == OltTopology.MANAGEMENT_ADDR_TLV_TYPE) { |
| 140 | assertThat(tlv.getValue(), notNullValue()); |
| 141 | final byte ipAddressSubType = 0x1; // IPv4 |
| 142 | // 5 below is address subtype + IP4 address len |
| 143 | final byte ipAddrStrLen = 0x5; |
| 144 | final byte interfaceSubtype = 0x1; |
| 145 | final int interfaceNum = 0; |
| 146 | final byte oidString = 0x0; |
| 147 | Ip4Address ipAddr = Ip4Address.valueOf(EXPECTED_IP); |
| 148 | |
| 149 | byte[] addrStr = ArrayUtils.addAll(new byte[] {ipAddressSubType}, |
| 150 | ipAddr.toOctets()); |
| 151 | |
| 152 | byte[] ipAddrBytes = ArrayUtils.addAll(new byte[] {ipAddrStrLen}, |
| 153 | addrStr); |
| 154 | byte[] bytesInterfacetype = ArrayUtils.addAll(ipAddrBytes, |
| 155 | ByteBuffer.allocate(1).put(interfaceSubtype).array()); |
| 156 | byte[] bytesInterfaceNumber = ArrayUtils.addAll(bytesInterfacetype, |
| 157 | ByteBuffer.allocate(4).putInt(interfaceNum).array()); |
| 158 | byte[] finalMgmtAddrBytes = ArrayUtils.addAll(bytesInterfaceNumber, |
| 159 | ByteBuffer.allocate(1).put(oidString).array()); |
| 160 | assertThat(tlv.getValue(), is(finalMgmtAddrBytes)); |
| 161 | |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Validates Neightbour list table. |
| 168 | * @param neighborList Neighbour list table created using inbound LLDP packets. |
| 169 | */ |
| 170 | void validateNeighborList(Map<ConnectPoint, OltNeighborInfo> neighborList) { |
| 171 | assertThat(neighborList, notNullValue()); |
| 172 | assertThat(neighborList.size(), is(1)); |
| 173 | for (Map.Entry<ConnectPoint, OltNeighborInfo> entry: neighborList.entrySet()) { |
| 174 | assertThat(entry.getValue().mgmtAddr(), is("192.168.1.1")); |
| 175 | assertThat(entry.getValue().neighborName(), is("switch-1")); |
| 176 | assertThat(entry.getValue().neighborPort(), is("p0")); |
| 177 | assertThat(entry.getValue().oltName(), is("0000c6b1cd40dc93")); |
| 178 | assertThat(entry.getValue().oltPort().annotations().value("portName"), is("nni-")); |
| 179 | } |
| 180 | } |
| 181 | /** |
| 182 | * Keeps a reference to the PacketProcessor and saves the OutboundPackets. |
| 183 | */ |
| 184 | class MockPacketService extends PacketServiceAdapter { |
| 185 | |
| 186 | @Override |
| 187 | public void addProcessor(PacketProcessor processor, int priority) { |
| 188 | packetProcessor = processor; |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public void emit(OutboundPacket packet) { |
| 193 | try { |
| 194 | Ethernet eth = Ethernet.deserializer().deserialize(packet.data().array(), |
| 195 | 0, packet.data().array().length); |
| 196 | savePacket(eth); |
| 197 | } catch (Exception e) { |
| 198 | fail(e.getMessage()); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | /** |
| 203 | * Mock Flow Objective Service. |
| 204 | */ |
| 205 | public static class MockFlowObjectiveService extends FlowObjectiveServiceAdapter { |
| 206 | |
| 207 | @Override |
| 208 | public void filter(DeviceId deviceId, FilteringObjective filter) { |
| 209 | assertThat(deviceId, notNullValue()); |
| 210 | assertThat(filter, notNullValue()); |
| 211 | EthTypeCriterion ethType = (EthTypeCriterion) |
| 212 | filterForCriterion(filter.conditions(), Criterion.Type.ETH_TYPE); |
| 213 | assertThat(ethType, notNullValue()); |
| 214 | assertThat(ethType.ethType(), is(EthType.EtherType.LLDP.ethType())); |
| 215 | assertThat(filter.key().type(), is(Criterion.Type.IN_PORT)); |
| 216 | |
| 217 | } |
| 218 | private Criterion filterForCriterion(Collection<Criterion> criteria, Criterion.Type type) { |
| 219 | return criteria.stream() |
| 220 | .filter(c -> c.type().equals(type)) |
| 221 | .limit(1) |
| 222 | .findFirst().orElse(null); |
| 223 | } |
| 224 | |
| 225 | } |
| 226 | /** |
| 227 | * Mocks the DefaultPacketContext. |
| 228 | */ |
| 229 | static final class TestPacketContext extends DefaultPacketContext { |
| 230 | |
| 231 | private TestPacketContext(long time, InboundPacket inPkt, |
| 232 | OutboundPacket outPkt, boolean block) { |
| 233 | super(time, inPkt, outPkt, block); |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public void send() { |
| 238 | // We don't send anything out. |
| 239 | } |
| 240 | } |
| 241 | } |