blob: 352ea1cd25f0840ed624816e1a159485ef307dbd [file] [log] [blame]
Phaneendra Mandab88b3f82016-02-12 21:51:30 +05301/*
2 * Copyright 2016 Open Networking Laboratory
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 */
16package org.onosproject.sfc.forwarder.impl;
17
18import static org.easymock.EasyMock.createMock;
19import static org.easymock.EasyMock.expect;
20import static org.easymock.EasyMock.replay;
21import static org.hamcrest.Matchers.instanceOf;
22import static org.hamcrest.Matchers.is;
23import static org.junit.Assert.assertThat;
24
25import java.util.List;
26import java.util.Map;
27import java.util.Set;
28
29import org.junit.Test;
30import org.onlab.packet.IpAddress;
31import org.onlab.packet.MacAddress;
32import org.onosproject.core.ApplicationId;
33import org.onosproject.core.DefaultApplicationId;
34import org.onosproject.net.DeviceId;
35import org.onosproject.net.NshServicePathId;
36import org.onosproject.net.PortNumber;
37import org.onosproject.net.device.DeviceService;
38import org.onosproject.net.device.DeviceServiceAdapter;
39import org.onosproject.net.driver.DriverHandler;
40import org.onosproject.net.driver.DriverService;
41import org.onosproject.net.flow.criteria.Criterion;
42import org.onosproject.net.flow.criteria.PortCriterion;
43import org.onosproject.net.flow.instructions.Instruction;
44import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
45import org.onosproject.net.flowobjective.FlowObjectiveService;
46import org.onosproject.net.flowobjective.ForwardingObjective;
47import org.onosproject.net.host.HostService;
48import org.onosproject.net.host.HostServiceAdapter;
49import org.onosproject.sfc.util.FlowClassifierAdapter;
50import org.onosproject.sfc.util.FlowObjectiveAdapter;
51import org.onosproject.sfc.util.MockDriverHandler;
52import org.onosproject.sfc.util.PortPairAdapter;
53import org.onosproject.sfc.util.PortPairGroupAdapter;
54import org.onosproject.sfc.util.VirtualPortAdapter;
55import org.onosproject.sfc.util.VtnRscAdapter;
56import org.onosproject.vtnrsc.AllowedAddressPair;
57import org.onosproject.vtnrsc.BindingHostId;
58import org.onosproject.vtnrsc.DefaultPortPair;
59import org.onosproject.vtnrsc.DefaultVirtualPort;
60import org.onosproject.vtnrsc.FixedIp;
61import org.onosproject.vtnrsc.PortPair;
62import org.onosproject.vtnrsc.PortPairId;
63import org.onosproject.vtnrsc.SecurityGroup;
64import org.onosproject.vtnrsc.SubnetId;
65import org.onosproject.vtnrsc.TenantId;
66import org.onosproject.vtnrsc.TenantNetworkId;
67import org.onosproject.vtnrsc.VirtualPort;
68import org.onosproject.vtnrsc.VirtualPortId;
69import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService;
70import org.onosproject.vtnrsc.portpair.PortPairService;
71import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService;
72import org.onosproject.vtnrsc.service.VtnRscService;
73import org.onosproject.vtnrsc.virtualport.VirtualPortService;
74
75import com.google.common.collect.Lists;
76import com.google.common.collect.Maps;
77import com.google.common.collect.Sets;
78
79public class ServiceFunctionForwarderImplTest {
80
81 FlowObjectiveService flowObjectiveService = new FlowObjectiveAdapter();
82 DeviceService deviceService = new DeviceServiceAdapter();
83 HostService hostService = new HostServiceAdapter();
84 VirtualPortService virtualPortService = new VirtualPortAdapter();
85 VtnRscService vtnRscService = new VtnRscAdapter();
86 PortPairService portPairService = new PortPairAdapter();
87 PortPairGroupService portPairGroupService = new PortPairGroupAdapter();
88 FlowClassifierService flowClassifierService = new FlowClassifierAdapter();
89
90 final DriverService driverService = createMock(DriverService.class);
91
92 DeviceId deviceId = DeviceId.deviceId("of:000000000000001");
93 final TenantId tenantId = TenantId.tenantId("1");
94
95 final DriverHandler driverHandler = new MockDriverHandler();
96
97 private VirtualPort createVirtualPort(VirtualPortId id) {
98 Set<FixedIp> fixedIps;
99 Map<String, String> propertyMap;
100 Set<AllowedAddressPair> allowedAddressPairs;
101 Set<SecurityGroup> securityGroups = Sets.newHashSet();
102
103 String macAddressStr = "fa:12:3e:56:ee:a2";
104 String ipAddress = "10.1.1.1";
105 String tenantNetworkId = "1234567";
106 String subnet = "1212";
107 String hostIdStr = "fa:e2:3e:56:ee:a2";
108 String deviceOwner = "james";
109 propertyMap = Maps.newHashMap();
110 propertyMap.putIfAbsent("deviceOwner", deviceOwner);
111
112 TenantNetworkId networkId = TenantNetworkId.networkId(tenantNetworkId);
113 MacAddress macAddress = MacAddress.valueOf(macAddressStr);
114 BindingHostId bindingHostId = BindingHostId.bindingHostId(hostIdStr);
115 FixedIp fixedIp = FixedIp.fixedIp(SubnetId.subnetId(subnet),
116 IpAddress.valueOf(ipAddress));
117 fixedIps = Sets.newHashSet();
118 fixedIps.add(fixedIp);
119
120 allowedAddressPairs = Sets.newHashSet();
121 AllowedAddressPair allowedAddressPair = AllowedAddressPair
122 .allowedAddressPair(IpAddress.valueOf(ipAddress),
123 MacAddress.valueOf(macAddressStr));
124 allowedAddressPairs.add(allowedAddressPair);
125
126 VirtualPort d1 = new DefaultVirtualPort(id, networkId, true,
127 propertyMap,
128 VirtualPort.State.ACTIVE,
129 macAddress, tenantId, deviceId,
130 fixedIps, bindingHostId,
131 allowedAddressPairs,
132 securityGroups);
133 return d1;
134 }
135
136 @Test
137 public void testInstallFlowClassifier() {
138
139 PortPairId portPairId1 = PortPairId.of("73333333-fc23-aeb6-f44b-56dc5e2fb3ae");
140 PortPairId portPairId2 = PortPairId.of("74444444-fc23-aeb6-f44b-56dc5e2fb3ae");
141
142 final String ppName1 = "PortPair1";
143 final String ppDescription1 = "PortPair1";
144 final String ingress1 = "d3333333-24fc-4fae-af4b-321c5e2eb3d1";
145 final String egress1 = "a4444444-4a56-2a6e-cd3a-9dee4e2ec345";
146 DefaultPortPair.Builder portPairBuilder = new DefaultPortPair.Builder();
147 PortPair portPair1 = portPairBuilder.setId(portPairId1).setName(ppName1).setTenantId(tenantId)
148 .setDescription(ppDescription1).setIngress(ingress1).setEgress(egress1).build();
149
150 final String ppName2 = "PortPair2";
151 final String ppDescription2 = "PortPair2";
152 final String ingress2 = "d5555555-24fc-4fae-af4b-321c5e2eb3d1";
153 final String egress2 = "a6666666-4a56-2a6e-cd3a-9dee4e2ec345";
154 PortPair portPair2 = portPairBuilder.setId(portPairId2).setName(ppName2).setTenantId(tenantId)
155 .setDescription(ppDescription2).setIngress(ingress2).setEgress(egress2).build();
156
157 ApplicationId appId = new DefaultApplicationId(1, "test");
158 ServiceFunctionForwarderImpl serviceFunctionForwarder = new ServiceFunctionForwarderImpl();
159 serviceFunctionForwarder.virtualPortService = virtualPortService;
160 serviceFunctionForwarder.vtnRscService = vtnRscService;
161 serviceFunctionForwarder.portPairService = portPairService;
162 serviceFunctionForwarder.portPairGroupService = portPairGroupService;
163 serviceFunctionForwarder.flowClassifierService = flowClassifierService;
164 serviceFunctionForwarder.driverService = driverService;
165 serviceFunctionForwarder.hostService = hostService;
166 serviceFunctionForwarder.flowObjectiveService = flowObjectiveService;
167 serviceFunctionForwarder.appId = appId;
168
169 NshServicePathId nshSpiId = NshServicePathId.of(10);
170
171 portPairService.createPortPair(portPair1);
172 portPairService.createPortPair(portPair2);
173
174 List<PortPairId> path = Lists.newArrayList();
175 path.add(portPairId1);
176 path.add(portPairId2);
177
178 List<VirtualPort> virtualPortList = Lists.newArrayList();
179 virtualPortList.add(createVirtualPort(VirtualPortId.portId(egress1)));
180 virtualPortList.add(createVirtualPort(VirtualPortId.portId(ingress2)));
181 virtualPortService.createPorts(virtualPortList);
182
183 expect(driverService.createHandler(deviceId)).andReturn(driverHandler).anyTimes();
184 replay(driverService);
185
186 serviceFunctionForwarder.installLoadBalancedForwardingRule(path, nshSpiId);
187
188 ForwardingObjective forObj = ((FlowObjectiveAdapter) flowObjectiveService).forwardingObjective();
189
190 // Check for Selector
191 assertThat(forObj.selector().getCriterion(Criterion.Type.IN_PORT), instanceOf(PortCriterion.class));
192
193 // Check for treatment
194 List<Instruction> instructions = forObj.treatment().allInstructions();
195 for (Instruction instruction : instructions) {
196 if (instruction.type() == Instruction.Type.OUTPUT) {
197 assertThat(((OutputInstruction) instruction).port(), is(PortNumber.P0));
198 }
199 }
200 }
201}