pier | 6a2052b | 2019-06-28 22:17:31 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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 | |
| 17 | package org.onosproject.segmentrouting; |
| 18 | |
| 19 | import com.google.common.collect.Maps; |
| 20 | import org.apache.commons.lang3.tuple.Pair; |
| 21 | import org.onlab.packet.DeserializationException; |
| 22 | import org.onlab.packet.Ethernet; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onosproject.net.packet.OutboundPacket; |
| 25 | import org.onosproject.net.packet.PacketServiceAdapter; |
| 26 | |
| 27 | import java.util.Map; |
| 28 | |
| 29 | /** |
| 30 | * Mock Packet Service. |
| 31 | * It is used for tests related to packet-ins management. |
| 32 | */ |
| 33 | public class MockPacketService extends PacketServiceAdapter { |
| 34 | |
| 35 | private final Map<MacAddress, Pair<OutboundPacket, Ethernet>> outBoundPackets = Maps.newHashMap(); |
| 36 | |
| 37 | @Override |
| 38 | public void emit(OutboundPacket packet) { |
| 39 | try { |
| 40 | Ethernet ethernetPacket = Ethernet.deserializer().deserialize(packet.data().array(), |
| 41 | packet.data().arrayOffset(), |
| 42 | packet.data().array().length); |
| 43 | outBoundPackets.put(ethernetPacket.getDestinationMAC(), Pair.of(packet, ethernetPacket)); |
| 44 | } catch (DeserializationException e) { |
| 45 | |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | Ethernet getEthernetPacket(MacAddress key) { |
| 50 | Pair<OutboundPacket, Ethernet> pair = outBoundPackets.get(key); |
| 51 | return pair != null ? pair.getRight() : null; |
| 52 | } |
| 53 | } |