Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 org.onlab.packet.MacAddress; |
| 20 | import org.onlab.packet.VlanId; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.PortNumber; |
| 23 | import org.onosproject.net.flow.TrafficSelector; |
| 24 | import org.onosproject.net.flow.TrafficTreatment; |
| 25 | import org.onosproject.net.flow.criteria.Criterion; |
| 26 | import org.onosproject.net.flow.criteria.EthCriterion; |
| 27 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
| 28 | import org.onosproject.net.flow.instructions.Instruction; |
| 29 | import org.onosproject.net.flow.instructions.Instructions; |
| 30 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 31 | import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter; |
| 32 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 33 | import org.onosproject.net.flowobjective.Objective; |
| 34 | |
| 35 | import java.util.Map; |
| 36 | |
| 37 | /** |
| 38 | * Mock Flow Objective Service. |
| 39 | */ |
| 40 | public class MockFlowObjectiveService extends FlowObjectiveServiceAdapter { |
| 41 | private Map<MockBridgingTableKey, MockBridgingTableValue> bridgingTable; |
| 42 | private Map<Integer, TrafficTreatment> nextTable; |
| 43 | |
| 44 | MockFlowObjectiveService(Map<MockBridgingTableKey, MockBridgingTableValue> bridgingTable, |
| 45 | Map<Integer, TrafficTreatment> nextTable) { |
| 46 | this.bridgingTable = bridgingTable; |
| 47 | this.nextTable = nextTable; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public void forward(DeviceId deviceId, ForwardingObjective forwardingObjective) { |
| 52 | TrafficSelector selector = forwardingObjective.selector(); |
| 53 | TrafficTreatment treatment = nextTable.get(forwardingObjective.nextId()); |
| 54 | MacAddress macAddress = ((EthCriterion) selector.getCriterion(Criterion.Type.ETH_DST)).mac(); |
| 55 | VlanId vlanId = ((VlanIdCriterion) selector.getCriterion(Criterion.Type.VLAN_VID)).vlanId(); |
| 56 | |
| 57 | boolean popVlan = treatment.allInstructions().stream() |
| 58 | .filter(instruction -> instruction.type().equals(Instruction.Type.L2MODIFICATION)) |
| 59 | .anyMatch(instruction -> ((L2ModificationInstruction) instruction).subtype() |
| 60 | .equals(L2ModificationInstruction.L2SubType.VLAN_POP)); |
| 61 | PortNumber portNumber = treatment.allInstructions().stream() |
| 62 | .filter(instruction -> instruction.type().equals(Instruction.Type.OUTPUT)) |
| 63 | .map(instruction -> ((Instructions.OutputInstruction) instruction).port()).findFirst().orElse(null); |
| 64 | if (portNumber == null) { |
| 65 | throw new IllegalArgumentException(); |
| 66 | } |
| 67 | |
| 68 | Objective.Operation op = forwardingObjective.op(); |
| 69 | |
| 70 | MockBridgingTableKey btKey = new MockBridgingTableKey(deviceId, macAddress, vlanId); |
| 71 | MockBridgingTableValue btValue = new MockBridgingTableValue(popVlan, portNumber); |
| 72 | |
| 73 | if (op.equals(Objective.Operation.ADD)) { |
| 74 | bridgingTable.put(btKey, btValue); |
| 75 | } else if (op.equals(Objective.Operation.REMOVE)) { |
| 76 | bridgingTable.remove(btKey, btValue); |
| 77 | } else { |
| 78 | throw new IllegalArgumentException(); |
| 79 | } |
| 80 | } |
| 81 | } |