blob: d3a7c5c8782cd4602b2a24a83b49b6f8dbd0890b [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;
Wailok Shumfb7e7872021-06-18 17:30:08 +080052import static org.onosproject.pipelines.fabric.impl.behaviour.Constants.PORT_TYPE_INTERNAL;
Yi Tseng0b809722017-11-03 10:23:26 -070053
Carmelo Cascone41644362018-08-09 16:56:43 -070054public class FabricPipelinerTest {
Yi Tseng0b809722017-11-03 10:23:26 -070055
Carmelo Cascone2388cc12021-05-26 19:30:30 +020056 private static final ApplicationId APP_ID = TestApplicationId.create("FabricPipelinerTest");
57 private static final DeviceId DEVICE_ID = DeviceId.deviceId("device:1");
58 private static final int DEFAULT_FLOW_PRIORITY = 100;
59 private static final int CPU_PORT = 320;
60 private static final byte FWD_IPV4_ROUTING = 2;
61 private static final int DEFAULT_VLAN = 4094;
62 public static final byte[] ONE = new byte[]{1};
63 public static final byte[] ZERO = new byte[]{0};
Yi Tseng0b809722017-11-03 10:23:26 -070064
Carmelo Cascone2388cc12021-05-26 19:30:30 +020065 private FabricPipeliner pipeliner;
66 private FlowRuleService flowRuleService;
67
68 @Before
69 public void setup() throws IOException {
70 FabricCapabilities capabilities = createMock(FabricCapabilities.class);
71 expect(capabilities.cpuPort()).andReturn(Optional.of(CPU_PORT)).anyTimes();
72 replay(capabilities);
73
74 // Services mock
75 flowRuleService = createMock(FlowRuleService.class);
76
77 pipeliner = new FabricPipeliner(capabilities);
78 pipeliner.flowRuleService = flowRuleService;
79 pipeliner.appId = APP_ID;
80 pipeliner.deviceId = DEVICE_ID;
Yi Tseng0b809722017-11-03 10:23:26 -070081 }
Carmelo Cascone41644362018-08-09 16:56:43 -070082
83 @Test
Carmelo Cascone2388cc12021-05-26 19:30:30 +020084 public void testInitializePipeline() {
85 final Capture<FlowRule> capturedCpuIgVlanRule = newCapture(CaptureType.ALL);
86 final Capture<FlowRule> capturedCpuFwdClsRule = newCapture(CaptureType.ALL);
87
88 // ingress_port_vlan table for cpu port
89 final TrafficSelector cpuIgVlanSelector = DefaultTrafficSelector.builder()
90 .add(Criteria.matchInPort(PortNumber.portNumber(CPU_PORT)))
91 .add(PiCriterion.builder()
92 .matchExact(FabricConstants.HDR_VLAN_IS_VALID, ZERO)
93 .build())
94 .build();
95 final TrafficTreatment cpuIgVlanTreatment = DefaultTrafficTreatment.builder()
96 .piTableAction(PiAction.builder()
97 .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT_WITH_INTERNAL_VLAN)
98 .withParameter(new PiActionParam(FabricConstants.VLAN_ID, DEFAULT_VLAN))
Wailok Shumfb7e7872021-06-18 17:30:08 +080099 .withParameter(new PiActionParam(FabricConstants.PORT_TYPE, PORT_TYPE_INTERNAL))
Carmelo Cascone2388cc12021-05-26 19:30:30 +0200100 .build())
101 .build();
102 final FlowRule expectedCpuIgVlanRule = DefaultFlowRule.builder()
103 .withSelector(cpuIgVlanSelector)
104 .withTreatment(cpuIgVlanTreatment)
105 .forTable(FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN)
106 .makePermanent()
107 .withPriority(DEFAULT_FLOW_PRIORITY)
108 .forDevice(DEVICE_ID)
109 .fromApp(APP_ID)
110 .build();
111
112 final TrafficSelector cpuFwdClsSelector = DefaultTrafficSelector.builder()
113 .matchInPort(PortNumber.portNumber(CPU_PORT))
114 .matchPi(PiCriterion.builder()
115 .matchExact(FabricConstants.HDR_IP_ETH_TYPE, Ethernet.TYPE_IPV4)
116 .build())
117 .build();
118 final TrafficTreatment cpuFwdClsTreatment = DefaultTrafficTreatment.builder()
119 .piTableAction(PiAction.builder()
120 .withId(FabricConstants.FABRIC_INGRESS_FILTERING_SET_FORWARDING_TYPE)
121 .withParameter(new PiActionParam(FabricConstants.FWD_TYPE, FWD_IPV4_ROUTING))
122 .build())
123 .build();
124 final FlowRule expectedCpuFwdClsRule = DefaultFlowRule.builder()
125 .withSelector(cpuFwdClsSelector)
126 .withTreatment(cpuFwdClsTreatment)
127 .forTable(FabricConstants.FABRIC_INGRESS_FILTERING_FWD_CLASSIFIER)
128 .makePermanent()
129 .withPriority(DEFAULT_FLOW_PRIORITY)
130 .forDevice(DEVICE_ID)
131 .fromApp(APP_ID)
132 .build();
133 flowRuleService.applyFlowRules(
134 capture(capturedCpuIgVlanRule),
135 capture(capturedCpuFwdClsRule));
136
137 replay(flowRuleService);
138 pipeliner.initializePipeline();
139
140 assertTrue(expectedCpuIgVlanRule.exactMatch(capturedCpuIgVlanRule.getValue()));
141 assertTrue(expectedCpuFwdClsRule.exactMatch(capturedCpuFwdClsRule.getValue()));
142
143 verify(flowRuleService);
144 reset(flowRuleService);
Carmelo Cascone41644362018-08-09 16:56:43 -0700145 }
Yi Tseng0b809722017-11-03 10:23:26 -0700146}