blob: 0f2d7a9e886c8c428ed47fc0dc6f15083fd1bd66 [file] [log] [blame]
Charles Chan1a3b02a2017-08-18 17:11:34 -07001/*
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
17package org.onosproject.segmentrouting;
18
19import org.onlab.packet.MacAddress;
20import org.onlab.packet.VlanId;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.PortNumber;
23import org.onosproject.net.flow.TrafficSelector;
24import org.onosproject.net.flow.TrafficTreatment;
25import org.onosproject.net.flow.criteria.Criterion;
26import org.onosproject.net.flow.criteria.EthCriterion;
27import org.onosproject.net.flow.criteria.VlanIdCriterion;
28import org.onosproject.net.flow.instructions.Instruction;
29import org.onosproject.net.flow.instructions.Instructions;
30import org.onosproject.net.flow.instructions.L2ModificationInstruction;
31import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter;
32import org.onosproject.net.flowobjective.ForwardingObjective;
33import org.onosproject.net.flowobjective.Objective;
Charles Chan9797ebb2020-02-14 13:23:57 -080034import org.onosproject.net.flowobjective.ObjectiveError;
Charles Chan1a3b02a2017-08-18 17:11:34 -070035
36import java.util.Map;
37
38/**
39 * Mock Flow Objective Service.
40 */
41public class MockFlowObjectiveService extends FlowObjectiveServiceAdapter {
42 private Map<MockBridgingTableKey, MockBridgingTableValue> bridgingTable;
43 private Map<Integer, TrafficTreatment> nextTable;
44
45 MockFlowObjectiveService(Map<MockBridgingTableKey, MockBridgingTableValue> bridgingTable,
46 Map<Integer, TrafficTreatment> nextTable) {
47 this.bridgingTable = bridgingTable;
48 this.nextTable = nextTable;
49 }
50
51 @Override
52 public void forward(DeviceId deviceId, ForwardingObjective forwardingObjective) {
53 TrafficSelector selector = forwardingObjective.selector();
54 TrafficTreatment treatment = nextTable.get(forwardingObjective.nextId());
55 MacAddress macAddress = ((EthCriterion) selector.getCriterion(Criterion.Type.ETH_DST)).mac();
56 VlanId vlanId = ((VlanIdCriterion) selector.getCriterion(Criterion.Type.VLAN_VID)).vlanId();
57
58 boolean popVlan = treatment.allInstructions().stream()
59 .filter(instruction -> instruction.type().equals(Instruction.Type.L2MODIFICATION))
60 .anyMatch(instruction -> ((L2ModificationInstruction) instruction).subtype()
61 .equals(L2ModificationInstruction.L2SubType.VLAN_POP));
62 PortNumber portNumber = treatment.allInstructions().stream()
63 .filter(instruction -> instruction.type().equals(Instruction.Type.OUTPUT))
64 .map(instruction -> ((Instructions.OutputInstruction) instruction).port()).findFirst().orElse(null);
65 if (portNumber == null) {
66 throw new IllegalArgumentException();
67 }
68
69 Objective.Operation op = forwardingObjective.op();
70
71 MockBridgingTableKey btKey = new MockBridgingTableKey(deviceId, macAddress, vlanId);
72 MockBridgingTableValue btValue = new MockBridgingTableValue(popVlan, portNumber);
73
74 if (op.equals(Objective.Operation.ADD)) {
75 bridgingTable.put(btKey, btValue);
Charles Chan9797ebb2020-02-14 13:23:57 -080076 forwardingObjective.context().ifPresent(context -> context.onSuccess(forwardingObjective));
Charles Chan1a3b02a2017-08-18 17:11:34 -070077 } else if (op.equals(Objective.Operation.REMOVE)) {
78 bridgingTable.remove(btKey, btValue);
Charles Chan9797ebb2020-02-14 13:23:57 -080079 forwardingObjective.context().ifPresent(context -> context.onSuccess(forwardingObjective));
Charles Chan1a3b02a2017-08-18 17:11:34 -070080 } else {
Charles Chan9797ebb2020-02-14 13:23:57 -080081 forwardingObjective.context().ifPresent(context ->
82 context.onError(forwardingObjective, ObjectiveError.UNKNOWN));
Charles Chan1a3b02a2017-08-18 17:11:34 -070083 throw new IllegalArgumentException();
84 }
85 }
86}