blob: 138b98ad851830a71b367fb9ded1e4e71a25aff6 [file] [log] [blame]
Charles Chan6c624992017-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;
34
35import java.util.Map;
36
37/**
38 * Mock Flow Objective Service.
39 */
40public 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}