blob: 86b3e506780bcb878ae474b3625be44b54979948 [file] [log] [blame]
Phaneendra Manda0f21ad62016-02-12 19:32:13 +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.installer.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.is;
22import static org.junit.Assert.assertThat;
23
24import java.util.LinkedList;
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.IpPrefix;
32import org.onlab.packet.MacAddress;
33import org.onosproject.core.ApplicationId;
34import org.onosproject.core.DefaultApplicationId;
35import org.onosproject.net.ConnectPoint;
36import org.onosproject.net.DeviceId;
37import org.onosproject.net.HostLocation;
38import org.onosproject.net.NshServicePathId;
39import org.onosproject.net.device.DeviceService;
40import org.onosproject.net.device.DeviceServiceAdapter;
41import org.onosproject.net.driver.DriverHandler;
42import org.onosproject.net.driver.DriverService;
43import org.onosproject.net.flowobjective.FlowObjectiveService;
44import org.onosproject.net.host.HostService;
45import org.onosproject.net.host.HostServiceAdapter;
46import org.onosproject.sfc.util.FlowClassifierAdapter;
47import org.onosproject.sfc.util.FlowObjectiveAdapter;
48import org.onosproject.sfc.util.MockDriverHandler;
49import org.onosproject.sfc.util.PortPairAdapter;
50import org.onosproject.sfc.util.PortPairGroupAdapter;
51import org.onosproject.sfc.util.VirtualPortAdapter;
52import org.onosproject.sfc.util.VtnRscAdapter;
53import org.onosproject.vtnrsc.AllowedAddressPair;
54import org.onosproject.vtnrsc.BindingHostId;
55import org.onosproject.vtnrsc.DefaultFlowClassifier;
56import org.onosproject.vtnrsc.DefaultPortChain;
57import org.onosproject.vtnrsc.DefaultPortPair;
58import org.onosproject.vtnrsc.DefaultPortPairGroup;
59import org.onosproject.vtnrsc.DefaultVirtualPort;
60import org.onosproject.vtnrsc.FixedIp;
61import org.onosproject.vtnrsc.FlowClassifier;
62import org.onosproject.vtnrsc.FlowClassifierId;
63import org.onosproject.vtnrsc.PortChain;
64import org.onosproject.vtnrsc.PortChainId;
65import org.onosproject.vtnrsc.PortPair;
66import org.onosproject.vtnrsc.PortPairGroup;
67import org.onosproject.vtnrsc.PortPairGroupId;
68import org.onosproject.vtnrsc.PortPairId;
69import org.onosproject.vtnrsc.SecurityGroup;
70import org.onosproject.vtnrsc.SubnetId;
71import org.onosproject.vtnrsc.TenantId;
72import org.onosproject.vtnrsc.TenantNetworkId;
73import org.onosproject.vtnrsc.VirtualPort;
74import org.onosproject.vtnrsc.VirtualPortId;
75import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService;
76import org.onosproject.vtnrsc.portpair.PortPairService;
77import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService;
78import org.onosproject.vtnrsc.service.VtnRscService;
79import org.onosproject.vtnrsc.virtualport.VirtualPortService;
80
81import com.google.common.collect.Lists;
82import com.google.common.collect.Maps;
83import com.google.common.collect.Sets;
84
85public class FlowClassifierInstallerImplTest {
86
87 FlowObjectiveService flowObjectiveService = new FlowObjectiveAdapter();
88 DeviceService deviceService = new DeviceServiceAdapter();
89 HostService hostService = new HostServiceAdapter();
90 VirtualPortService virtualPortService = new VirtualPortAdapter();
91 VtnRscService vtnRscService = new VtnRscAdapter();
92 PortPairService portPairService = new PortPairAdapter();
93 PortPairGroupService portPairGroupService = new PortPairGroupAdapter();
94 FlowClassifierService flowClassifierService = new FlowClassifierAdapter();
95
96 final DriverService driverService = createMock(DriverService.class);
97
98 final PortChainId portChainId = PortChainId.of("78888888-fc23-aeb6-f44b-56dc5e2fb3ae");
99 final TenantId tenantId = TenantId.tenantId("1");
100 final String name = "PortChain";
101 final String description = "PortChain";
102 final List<PortPairGroupId> portPairGroups = new LinkedList<PortPairGroupId>();
103 final List<FlowClassifierId> flowClassifiers = new LinkedList<FlowClassifierId>();
104 PortPairGroupId portPairGroupId1 = PortPairGroupId.of("73333333-fc23-aeb6-f44b-56dc5e2fb3ae");
105 PortPairGroupId portPairGroupId2 = PortPairGroupId.of("73343531-fc23-aeb6-f44b-56dc5e2fb3af");
106
107 PortPairId portPairId1 = PortPairId.of("73333333-fc23-aeb6-f44b-56dc5e2fb3ae");
108 PortPairId portPairId2 = PortPairId.of("74444444-fc23-aeb6-f44b-56dc5e2fb3ae");
109
110 FlowClassifierId flowClassifierId1 = FlowClassifierId.of("74444444-fc23-aeb6-f44b-56dc5e2fb3ae");
111 FlowClassifierId flowClassifierId2 = FlowClassifierId.of("74444444-fc23-aeb6-f44b-56dc5e2fb3af");
112
113 final String ppName = "PortPair";
114 final String ppDescription = "PortPair";
115 final String ingress = "d3333333-24fc-4fae-af4b-321c5e2eb3d1";
116 final String egress = "a4444444-4a56-2a6e-cd3a-9dee4e2ec345";
117
118
119 final String ppgName = "PortPairGroup";
120 final String ppgDescription = "PortPairGroup";
121 final List<PortPairId> portPairList = new LinkedList<PortPairId>();
122
123 VirtualPortId id1 = VirtualPortId.portId(ingress);
124 VirtualPortId id2 = VirtualPortId.portId("3414");
125
126 DeviceId deviceId = DeviceId.deviceId("of:000000000000001");
127
128 final DriverHandler driverHandler = new MockDriverHandler();
129
130 private PortPair createPortPair(PortPairId ppId) {
131 DefaultPortPair.Builder portPairBuilder = new DefaultPortPair.Builder();
132 PortPair portPair = portPairBuilder.setId(ppId).setName(ppName).setTenantId(tenantId)
133 .setDescription(ppDescription).setIngress(ingress).setEgress(egress).build();
134 return portPair;
135 }
136
137 private PortPairGroup createPortPairGroup(PortPairGroupId ppgId) {
138
139 portPairList.clear();
140 // Create same two port-pair-group objects.
141 portPairList.add(portPairId1);
142 portPairList.add(portPairId2);
143
144 DefaultPortPairGroup.Builder portPairGroupBuilder = new DefaultPortPairGroup.Builder();
145 PortPairGroup portPairGroup = portPairGroupBuilder.setId(ppgId).setTenantId(tenantId)
146 .setName(ppgName).setDescription(ppgDescription).setPortPairs(portPairList).build();
147
148 return portPairGroup;
149
150 }
151
152 private PortChain createPortChain() {
153
154 portPairGroups.clear();
155 flowClassifiers.clear();
156 // create list of Port Pair Groups.
157
158 portPairGroups.add(portPairGroupId1);
159 portPairGroups.add(portPairGroupId2);
160 // create list of Flow classifiers.
161 flowClassifiers.add(flowClassifierId1);
162 flowClassifiers.add(flowClassifierId2);
163
164 DefaultPortChain.Builder portChainBuilder = new DefaultPortChain.Builder();
165 final PortChain portChain = portChainBuilder.setId(portChainId).setTenantId(tenantId).setName(name)
166 .setDescription(description).setPortPairGroups(portPairGroups).setFlowClassifiers(flowClassifiers)
167 .build();
168
169 return portChain;
170 }
171
172 private FlowClassifier createFlowClassifier(FlowClassifierId id) {
173 final String name = "FlowClassifier1";
174 final String description = "FlowClassifier1";
175 final String ethType = "IPv4";
176 final String protocol = "tcp";
177 final int minSrcPortRange = 5;
178 final int maxSrcPortRange = 10;
179 final int minDstPortRange = 5;
180 final int maxDstPortRange = 10;
181 final TenantId tenantId = TenantId.tenantId("1");
182 final IpPrefix srcIpPrefix = IpPrefix.valueOf("0.0.0.0/0");
183 final IpPrefix dstIpPrefix = IpPrefix.valueOf("10.10.10.10/0");
184 final VirtualPortId virtualSrcPort = id1;
185 final VirtualPortId virtualDstPort = id2;
186
187 DefaultFlowClassifier.Builder flowClassifierBuilder = new DefaultFlowClassifier.Builder();
188 final FlowClassifier flowClassifier = flowClassifierBuilder.setFlowClassifierId(id)
189 .setTenantId(tenantId).setName(name).setDescription(description).setEtherType(ethType)
190 .setProtocol(protocol).setMinSrcPortRange(minSrcPortRange).setMaxSrcPortRange(maxSrcPortRange)
191 .setMinDstPortRange(minDstPortRange).setMaxDstPortRange(maxDstPortRange).setSrcIpPrefix(srcIpPrefix)
192 .setDstIpPrefix(dstIpPrefix).setSrcPort(virtualSrcPort).setDstPort(virtualDstPort).build();
193 return flowClassifier;
194 }
195
196 private VirtualPort createVirtualPort() {
197 Set<FixedIp> fixedIps;
198 Map<String, String> propertyMap;
199 Set<AllowedAddressPair> allowedAddressPairs;
200 Set<SecurityGroup> securityGroups = Sets.newHashSet();
201
202 String macAddressStr = "fa:12:3e:56:ee:a2";
203 String ipAddress = "10.1.1.1";
204 String tenantNetworkId = "1234567";
205 String subnet = "1212";
206 String hostIdStr = "fa:e2:3e:56:ee:a2";
207 String deviceOwner = "james";
208 propertyMap = Maps.newHashMap();
209 propertyMap.putIfAbsent("deviceOwner", deviceOwner);
210
211 TenantNetworkId networkId = TenantNetworkId.networkId(tenantNetworkId);
212 MacAddress macAddress = MacAddress.valueOf(macAddressStr);
213 BindingHostId bindingHostId = BindingHostId.bindingHostId(hostIdStr);
214 FixedIp fixedIp = FixedIp.fixedIp(SubnetId.subnetId(subnet),
215 IpAddress.valueOf(ipAddress));
216 fixedIps = Sets.newHashSet();
217 fixedIps.add(fixedIp);
218
219 allowedAddressPairs = Sets.newHashSet();
220 AllowedAddressPair allowedAddressPair = AllowedAddressPair
221 .allowedAddressPair(IpAddress.valueOf(ipAddress),
222 MacAddress.valueOf(macAddressStr));
223 allowedAddressPairs.add(allowedAddressPair);
224
225 VirtualPort d1 = new DefaultVirtualPort(id1, networkId, true,
226 propertyMap,
227 VirtualPort.State.ACTIVE,
228 macAddress, tenantId, deviceId,
229 fixedIps, bindingHostId,
230 allowedAddressPairs,
231 securityGroups);
232 return d1;
233 }
234
235 @Test
236 public void testInstallFlowClassifier() {
237
238 ApplicationId appId = new DefaultApplicationId(1, "test");
239 FlowClassifierInstallerImpl flowClassifierInstaller = new FlowClassifierInstallerImpl();
240 flowClassifierInstaller.virtualPortService = virtualPortService;
241 flowClassifierInstaller.vtnRscService = vtnRscService;
242 flowClassifierInstaller.portPairService = portPairService;
243 flowClassifierInstaller.portPairGroupService = portPairGroupService;
244 flowClassifierInstaller.flowClassifierService = flowClassifierService;
245 flowClassifierInstaller.driverService = driverService;
246 flowClassifierInstaller.deviceService = deviceService;
247 flowClassifierInstaller.hostService = hostService;
248 flowClassifierInstaller.flowObjectiveService = flowObjectiveService;
249 flowClassifierInstaller.appId = appId;
250
251 final PortChain portChain = createPortChain();
252 NshServicePathId nshSpiId = NshServicePathId.of(10);
253
254 portPairGroupService.createPortPairGroup(createPortPairGroup(portPairGroupId1));
255 portPairGroupService.createPortPairGroup(createPortPairGroup(portPairGroupId2));
256 portPairService.createPortPair(createPortPair(portPairId1));
257 portPairService.createPortPair(createPortPair(portPairId2));
258 FlowClassifier fc1 = createFlowClassifier(flowClassifierId1);
259 FlowClassifier fc2 = createFlowClassifier(flowClassifierId2);
260 flowClassifierService.createFlowClassifier(fc1);
261 flowClassifierService.createFlowClassifier(fc2);
262
263 List<VirtualPort> virtualPortList = Lists.newArrayList();
264 virtualPortList.add(createVirtualPort());
265 virtualPortService.createPorts(virtualPortList);
266
267 expect(driverService.createHandler(deviceId)).andReturn(driverHandler).anyTimes();
268 replay(driverService);
269
270 ConnectPoint connectPoint = flowClassifierInstaller.installFlowClassifier(portChain, nshSpiId);
271
272 assertThat(connectPoint, is(HostLocation.NONE));
273 }
274}