blob: a2fa47b25f8b0db0feabfe089e16cc37c03c0b77 [file] [log] [blame]
Yi Tseng0b809722017-11-03 10:23:26 -07001/*
Carmelo Cascone2388cc12021-05-26 19:30:30 +02002 * Copyright 2021-present Open Networking Foundation
Yi Tseng0b809722017-11-03 10:23:26 -07003 *
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 */
Carmelo Cascone356ab8b2019-09-25 01:02:53 -070016package org.onosproject.pipelines.fabric.impl.behaviour.pipeliner;
Yi Tseng0b809722017-11-03 10:23:26 -070017
Carmelo Cascone2388cc12021-05-26 19:30:30 +020018import org.easymock.Capture;
19import org.easymock.CaptureType;
20import org.junit.Before;
Carmelo Cascone41644362018-08-09 16:56:43 -070021import org.junit.Test;
Carmelo Cascone2388cc12021-05-26 19:30:30 +020022import org.onlab.packet.Ethernet;
Yi Tseng0b809722017-11-03 10:23:26 -070023import org.onosproject.TestApplicationId;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.PortNumber;
Carmelo Cascone2388cc12021-05-26 19:30:30 +020027import org.onosproject.net.flow.DefaultFlowRule;
Yi Tseng20f9e7b2018-05-24 23:27:39 +080028import org.onosproject.net.flow.DefaultTrafficSelector;
Carmelo Cascone2388cc12021-05-26 19:30:30 +020029import org.onosproject.net.flow.DefaultTrafficTreatment;
30import org.onosproject.net.flow.FlowRule;
31import org.onosproject.net.flow.FlowRuleService;
Yi Tseng20f9e7b2018-05-24 23:27:39 +080032import org.onosproject.net.flow.TrafficSelector;
Carmelo Cascone2388cc12021-05-26 19:30:30 +020033import org.onosproject.net.flow.TrafficTreatment;
34import org.onosproject.net.flow.criteria.Criteria;
35import org.onosproject.net.flow.criteria.PiCriterion;
36import org.onosproject.net.pi.runtime.PiAction;
37import org.onosproject.net.pi.runtime.PiActionParam;
38import org.onosproject.pipelines.fabric.FabricConstants;
Carmelo Cascone356ab8b2019-09-25 01:02:53 -070039import org.onosproject.pipelines.fabric.impl.behaviour.FabricCapabilities;
Yi Tseng0b809722017-11-03 10:23:26 -070040
Carmelo Cascone2388cc12021-05-26 19:30:30 +020041import java.io.IOException;
42import java.util.Optional;
43
44import static org.easymock.EasyMock.capture;
45import static org.easymock.EasyMock.createMock;
Yi Tseng0b809722017-11-03 10:23:26 -070046import static org.easymock.EasyMock.expect;
Carmelo Cascone2388cc12021-05-26 19:30:30 +020047import static org.easymock.EasyMock.newCapture;
Yi Tseng0b809722017-11-03 10:23:26 -070048import static org.easymock.EasyMock.replay;
Carmelo Cascone2388cc12021-05-26 19:30:30 +020049import static org.easymock.EasyMock.reset;
50import static org.easymock.EasyMock.verify;
51import static org.junit.Assert.assertTrue;
Yi Tseng0b809722017-11-03 10:23:26 -070052
Carmelo Cascone41644362018-08-09 16:56:43 -070053public class FabricPipelinerTest {
Yi Tseng0b809722017-11-03 10:23:26 -070054
Carmelo Cascone2388cc12021-05-26 19:30:30 +020055 private static final ApplicationId APP_ID = TestApplicationId.create("FabricPipelinerTest");
56 private static final DeviceId DEVICE_ID = DeviceId.deviceId("device:1");
57 private static final int DEFAULT_FLOW_PRIORITY = 100;
58 private static final int CPU_PORT = 320;
59 private static final byte FWD_IPV4_ROUTING = 2;
60 private static final int DEFAULT_VLAN = 4094;
61 public static final byte[] ONE = new byte[]{1};
62 public static final byte[] ZERO = new byte[]{0};
Yi Tseng0b809722017-11-03 10:23:26 -070063
Carmelo Cascone2388cc12021-05-26 19:30:30 +020064 private FabricPipeliner pipeliner;
65 private FlowRuleService flowRuleService;
66
67 @Before
68 public void setup() throws IOException {
69 FabricCapabilities capabilities = createMock(FabricCapabilities.class);
70 expect(capabilities.cpuPort()).andReturn(Optional.of(CPU_PORT)).anyTimes();
71 replay(capabilities);
72
73 // Services mock
74 flowRuleService = createMock(FlowRuleService.class);
75
76 pipeliner = new FabricPipeliner(capabilities);
77 pipeliner.flowRuleService = flowRuleService;
78 pipeliner.appId = APP_ID;
79 pipeliner.deviceId = DEVICE_ID;
Yi Tseng0b809722017-11-03 10:23:26 -070080 }
Carmelo Cascone41644362018-08-09 16:56:43 -070081
82 @Test
Carmelo Cascone2388cc12021-05-26 19:30:30 +020083 public void testInitializePipeline() {
84 final Capture<FlowRule> capturedCpuIgVlanRule = newCapture(CaptureType.ALL);
85 final Capture<FlowRule> capturedCpuFwdClsRule = newCapture(CaptureType.ALL);
86
87 // ingress_port_vlan table for cpu port
88 final TrafficSelector cpuIgVlanSelector = DefaultTrafficSelector.builder()
89 .add(Criteria.matchInPort(PortNumber.portNumber(CPU_PORT)))
90 .add(PiCriterion.builder()
91 .matchExact(FabricConstants.HDR_VLAN_IS_VALID, ZERO)
92 .build())
93 .build();
94 final TrafficTreatment cpuIgVlanTreatment = DefaultTrafficTreatment.builder()
95 .piTableAction(PiAction.builder()
96 .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT_WITH_INTERNAL_VLAN)
97 .withParameter(new PiActionParam(FabricConstants.VLAN_ID, DEFAULT_VLAN))
98 .build())
99 .build();
100 final FlowRule expectedCpuIgVlanRule = DefaultFlowRule.builder()
101 .withSelector(cpuIgVlanSelector)
102 .withTreatment(cpuIgVlanTreatment)
103 .forTable(FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN)
104 .makePermanent()
105 .withPriority(DEFAULT_FLOW_PRIORITY)
106 .forDevice(DEVICE_ID)
107 .fromApp(APP_ID)
108 .build();
109
110 final TrafficSelector cpuFwdClsSelector = DefaultTrafficSelector.builder()
111 .matchInPort(PortNumber.portNumber(CPU_PORT))
112 .matchPi(PiCriterion.builder()
113 .matchExact(FabricConstants.HDR_IP_ETH_TYPE, Ethernet.TYPE_IPV4)
114 .build())
115 .build();
116 final TrafficTreatment cpuFwdClsTreatment = DefaultTrafficTreatment.builder()
117 .piTableAction(PiAction.builder()
118 .withId(FabricConstants.FABRIC_INGRESS_FILTERING_SET_FORWARDING_TYPE)
119 .withParameter(new PiActionParam(FabricConstants.FWD_TYPE, FWD_IPV4_ROUTING))
120 .build())
121 .build();
122 final FlowRule expectedCpuFwdClsRule = DefaultFlowRule.builder()
123 .withSelector(cpuFwdClsSelector)
124 .withTreatment(cpuFwdClsTreatment)
125 .forTable(FabricConstants.FABRIC_INGRESS_FILTERING_FWD_CLASSIFIER)
126 .makePermanent()
127 .withPriority(DEFAULT_FLOW_PRIORITY)
128 .forDevice(DEVICE_ID)
129 .fromApp(APP_ID)
130 .build();
131 flowRuleService.applyFlowRules(
132 capture(capturedCpuIgVlanRule),
133 capture(capturedCpuFwdClsRule));
134
135 replay(flowRuleService);
136 pipeliner.initializePipeline();
137
138 assertTrue(expectedCpuIgVlanRule.exactMatch(capturedCpuIgVlanRule.getValue()));
139 assertTrue(expectedCpuFwdClsRule.exactMatch(capturedCpuFwdClsRule.getValue()));
140
141 verify(flowRuleService);
142 reset(flowRuleService);
Carmelo Cascone41644362018-08-09 16:56:43 -0700143 }
Yi Tseng0b809722017-11-03 10:23:26 -0700144}