Phaneendra Manda
Committed by Gerrit Code Review

[ONOS-3834] Install load balanced forwarding rules

Change-Id: I681d0b047feb332543307eac1a54ed20114993ef
...@@ -15,37 +15,48 @@ ...@@ -15,37 +15,48 @@
15 */ 15 */
16 package org.onosproject.sfc.forwarder; 16 package org.onosproject.sfc.forwarder;
17 17
18 -import org.onosproject.net.flowobjective.Objective; 18 +import java.util.List;
19 +
19 import org.onosproject.net.NshServicePathId; 20 import org.onosproject.net.NshServicePathId;
20 import org.onosproject.vtnrsc.PortChain; 21 import org.onosproject.vtnrsc.PortChain;
22 +import org.onosproject.vtnrsc.PortPairId;
21 23
22 /** 24 /**
23 - * Abstraction of an entity which provides Service function forwarder. 25 + * Abstraction of an entity which provides service function forwarder.
24 */ 26 */
25 public interface ServiceFunctionForwarderService { 27 public interface ServiceFunctionForwarderService {
26 28
27 /** 29 /**
28 - * Install Forwarding rule. 30 + * Install forwarding rule.
29 * 31 *
30 * @param portChain port-chain 32 * @param portChain port-chain
31 * @param nshSpi nsh spi 33 * @param nshSpi nsh spi
32 */ 34 */
35 + @Deprecated
33 void installForwardingRule(PortChain portChain, NshServicePathId nshSpi); 36 void installForwardingRule(PortChain portChain, NshServicePathId nshSpi);
34 37
35 /** 38 /**
36 - * Uninstall Forwarding rule. 39 + * Uninstall forwarding rule.
37 * 40 *
38 * @param portChain port-chain 41 * @param portChain port-chain
39 * @param nshSpi nsh spi 42 * @param nshSpi nsh spi
40 */ 43 */
44 + @Deprecated
41 void unInstallForwardingRule(PortChain portChain, NshServicePathId nshSpi); 45 void unInstallForwardingRule(PortChain portChain, NshServicePathId nshSpi);
42 46
43 /** 47 /**
44 - * Prepare forwarding object for Service Function. 48 + * Install load balanced forwarding rules.
45 * 49 *
46 - * @param portChain port-chain 50 + * @param path load balanced path of port pairs
47 - * @param nshSpi nsh spi 51 + * @param nshSpi nsh service path index
48 - * @param type forwarding objective operation type 52 + */
53 + void installLoadBalancedForwardingRule(List<PortPairId> path, NshServicePathId nshSpi);
54 +
55 + /**
56 + * Uninstall load balanced forwarding rules.
57 + *
58 + * @param path load balanced path of port pairs
59 + * @param nshSpi nsh service path index
49 */ 60 */
50 - void prepareServiceFunctionForwarder(PortChain portChain, NshServicePathId nshSpi, Objective.Operation type); 61 + void unInstallLoadBalancedForwardingRule(List<PortPairId> path, NshServicePathId nshSpi);
51 } 62 }
......
...@@ -15,13 +15,17 @@ ...@@ -15,13 +15,17 @@
15 */ 15 */
16 package org.onosproject.sfc.forwarder.impl; 16 package org.onosproject.sfc.forwarder.impl;
17 17
18 -import org.apache.felix.scr.annotations.Component; 18 +import static com.google.common.base.Preconditions.checkNotNull;
19 -import org.apache.felix.scr.annotations.Reference; 19 +import static org.onosproject.net.flow.criteria.ExtensionSelectorType.ExtensionSelectorTypes.NICIRA_MATCH_NSH_SPI;
20 -import org.apache.felix.scr.annotations.ReferenceCardinality; 20 +import static org.slf4j.LoggerFactory.getLogger;
21 -import org.apache.felix.scr.annotations.Service; 21 +
22 +import java.util.List;
23 +import java.util.ListIterator;
24 +
22 import org.onlab.osgi.DefaultServiceDirectory; 25 import org.onlab.osgi.DefaultServiceDirectory;
23 import org.onlab.osgi.ServiceDirectory; 26 import org.onlab.osgi.ServiceDirectory;
24 import org.onlab.packet.MacAddress; 27 import org.onlab.packet.MacAddress;
28 +import org.onlab.packet.TpPort;
25 import org.onlab.packet.VlanId; 29 import org.onlab.packet.VlanId;
26 import org.onosproject.core.ApplicationId; 30 import org.onosproject.core.ApplicationId;
27 import org.onosproject.net.DeviceId; 31 import org.onosproject.net.DeviceId;
...@@ -46,8 +50,6 @@ import org.onosproject.net.host.HostService; ...@@ -46,8 +50,6 @@ import org.onosproject.net.host.HostService;
46 import org.onosproject.sfc.forwarder.ServiceFunctionForwarderService; 50 import org.onosproject.sfc.forwarder.ServiceFunctionForwarderService;
47 import org.onosproject.vtnrsc.PortChain; 51 import org.onosproject.vtnrsc.PortChain;
48 import org.onosproject.vtnrsc.PortPair; 52 import org.onosproject.vtnrsc.PortPair;
49 -import org.onosproject.vtnrsc.PortPairGroup;
50 -import org.onosproject.vtnrsc.PortPairGroupId;
51 import org.onosproject.vtnrsc.PortPairId; 53 import org.onosproject.vtnrsc.PortPairId;
52 import org.onosproject.vtnrsc.VirtualPortId; 54 import org.onosproject.vtnrsc.VirtualPortId;
53 import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService; 55 import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService;
...@@ -58,46 +60,25 @@ import org.onosproject.vtnrsc.service.VtnRscService; ...@@ -58,46 +60,25 @@ import org.onosproject.vtnrsc.service.VtnRscService;
58 import org.onosproject.vtnrsc.virtualport.VirtualPortService; 60 import org.onosproject.vtnrsc.virtualport.VirtualPortService;
59 import org.slf4j.Logger; 61 import org.slf4j.Logger;
60 62
61 -import java.util.List;
62 -import java.util.ListIterator;
63 -
64 -import static com.google.common.base.Preconditions.checkNotNull;
65 -import static org.onosproject.net.flow.criteria.ExtensionSelectorType.ExtensionSelectorTypes.NICIRA_MATCH_NSH_SPI;
66 -import static org.slf4j.LoggerFactory.getLogger;
67 -
68 /** 63 /**
69 - * Provides Service Function Forwarder implementation. 64 + * Provides service function forwarder implementation.
70 */ 65 */
71 -@Component(immediate = true)
72 -@Service
73 public class ServiceFunctionForwarderImpl implements ServiceFunctionForwarderService { 66 public class ServiceFunctionForwarderImpl implements ServiceFunctionForwarderService {
74 67
75 - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 68 + private final Logger log = getLogger(getClass());
76 - protected HostService hostService;
77 - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
78 - protected DriverService driverService;
79 - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
80 protected VirtualPortService virtualPortService; 69 protected VirtualPortService virtualPortService;
81 - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
82 protected VtnRscService vtnRscService; 70 protected VtnRscService vtnRscService;
83 - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected PortPairService portPairService; 71 protected PortPairService portPairService;
85 - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
86 protected PortPairGroupService portPairGroupService; 72 protected PortPairGroupService portPairGroupService;
87 - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
88 protected FlowClassifierService flowClassifierService; 73 protected FlowClassifierService flowClassifierService;
89 - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
90 protected PortChainService portChainService; 74 protected PortChainService portChainService;
91 - 75 + protected DriverService driverService;
92 - private final Logger log = getLogger(getClass());
93 - protected ApplicationId appId;
94 protected FlowObjectiveService flowObjectiveService; 76 protected FlowObjectiveService flowObjectiveService;
77 + protected HostService hostService;
78 + protected ApplicationId appId;
95 79
96 - private static final String DRIVER_NAME = "onosfw"; 80 + private static final String PATH_NOT_NULL = "Load balanced path cannot be null";
97 - private static final String PORT_CHAIN_NOT_NULL = "Port-Chain cannot be null";
98 - private static final String PORT_CHAIN_ID_NOT_NULL = "Port-Chain-Id cannot be null";
99 private static final String APP_ID_NOT_NULL = "Application-Id cannot be null"; 81 private static final String APP_ID_NOT_NULL = "Application-Id cannot be null";
100 - private static final int NULL = 0;
101 82
102 /** 83 /**
103 * Default constructor. 84 * Default constructor.
...@@ -108,129 +89,118 @@ public class ServiceFunctionForwarderImpl implements ServiceFunctionForwarderSer ...@@ -108,129 +89,118 @@ public class ServiceFunctionForwarderImpl implements ServiceFunctionForwarderSer
108 /** 89 /**
109 * Explicit constructor. 90 * Explicit constructor.
110 * 91 *
111 - * @param appId Application id 92 + * @param appId application id
112 */ 93 */
113 public ServiceFunctionForwarderImpl(ApplicationId appId) { 94 public ServiceFunctionForwarderImpl(ApplicationId appId) {
114 this.appId = checkNotNull(appId, APP_ID_NOT_NULL); 95 this.appId = checkNotNull(appId, APP_ID_NOT_NULL);
115 ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); 96 ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
116 this.flowObjectiveService = serviceDirectory.get(FlowObjectiveService.class); 97 this.flowObjectiveService = serviceDirectory.get(FlowObjectiveService.class);
98 + this.driverService = serviceDirectory.get(DriverService.class);
99 + this.virtualPortService = serviceDirectory.get(VirtualPortService.class);
100 + this.vtnRscService = serviceDirectory.get(VtnRscService.class);
101 + this.portPairService = serviceDirectory.get(PortPairService.class);
102 + this.portPairGroupService = serviceDirectory.get(PortPairGroupService.class);
103 + this.flowClassifierService = serviceDirectory.get(FlowClassifierService.class);
104 + this.hostService = serviceDirectory.get(HostService.class);
105 + this.portChainService = serviceDirectory.get(PortChainService.class);
117 } 106 }
118 107
119 @Override 108 @Override
120 public void installForwardingRule(PortChain portChain, NshServicePathId nshSpi) { 109 public void installForwardingRule(PortChain portChain, NshServicePathId nshSpi) {
121 - checkNotNull(portChain, PORT_CHAIN_NOT_NULL); 110 + //TODO this method will be removed
122 - prepareServiceFunctionForwarder(portChain, nshSpi, Objective.Operation.ADD);
123 } 111 }
124 112
125 @Override 113 @Override
126 public void unInstallForwardingRule(PortChain portChain, NshServicePathId nshSpi) { 114 public void unInstallForwardingRule(PortChain portChain, NshServicePathId nshSpi) {
127 - checkNotNull(portChain, PORT_CHAIN_NOT_NULL); 115 + //TODO this method will be removed
128 - prepareServiceFunctionForwarder(portChain, nshSpi, Objective.Operation.REMOVE);
129 } 116 }
130 117
131 @Override 118 @Override
132 - public void prepareServiceFunctionForwarder(PortChain portChain, NshServicePathId nshSpi, 119 + public void installLoadBalancedForwardingRule(List<PortPairId> path, NshServicePathId nshSpi) {
133 - Objective.Operation type) { 120 + checkNotNull(path, PATH_NOT_NULL);
134 - 121 + processForwardingRule(path, nshSpi, Objective.Operation.ADD);
135 - // Go through the port pair group list 122 + }
136 - List<PortPairGroupId> portPairGrpList = portChain.portPairGroups();
137 - ListIterator<PortPairGroupId> listGrpIterator = portPairGrpList.listIterator();
138 -
139 - // Get source port pair group
140 - if (!listGrpIterator.hasNext()) {
141 - return;
142 - }
143 - PortPairGroupId portPairGrpId = listGrpIterator.next();
144 - PortPairGroup currentPortPairGroup = portPairGroupService.getPortPairGroup(portPairGrpId);
145 -
146 - // Get destination port pair group
147 - if (!listGrpIterator.hasNext()) {
148 - return;
149 - }
150 - portPairGrpId = listGrpIterator.next();
151 - PortPairGroup nextPortPairGroup = portPairGroupService.getPortPairGroup(portPairGrpId);
152 123
153 - // push SFF to OVS 124 + @Override
154 - pushServiceFunctionForwarder(currentPortPairGroup, nextPortPairGroup, listGrpIterator, nshSpi, type); 125 + public void unInstallLoadBalancedForwardingRule(List<PortPairId> path, NshServicePathId nshSpi) {
126 + checkNotNull(path, PATH_NOT_NULL);
127 + processForwardingRule(path, nshSpi, Objective.Operation.REMOVE);
155 } 128 }
156 129
157 /** 130 /**
158 - * Push service-function-forwarder to OVS. 131 + * Process the required forwarding rules for the given path.
159 * 132 *
160 - * @param currentPortPairGroup current port-pair-group 133 + * @param path list of port pair ids
161 - * @param nextPortPairGroup next port-pair-group 134 + * @param nshSpi service path index
162 - * @param listGrpIterator pointer to port-pair-group list 135 + * @param type operation type ADD/REMOVE
163 - * @param nshSpi nsh service path id
164 - * @param type objective type
165 */ 136 */
166 - public void pushServiceFunctionForwarder(PortPairGroup currentPortPairGroup, PortPairGroup nextPortPairGroup, 137 + private void processForwardingRule(List<PortPairId> path, NshServicePathId nshSpi,
167 - ListIterator<PortPairGroupId> listGrpIterator, NshServicePathId nshSpi, Objective.Operation type) { 138 + Objective.Operation type) {
168 - DeviceId deviceId = null; 139 +
140 + // Get the first port pair
141 + ListIterator<PortPairId> portPairListIterator = path.listIterator();
142 + PortPair currentPortPair = portPairService.getPortPair(portPairListIterator.next());
143 +
144 + // Get destination port pair group
145 + if (!portPairListIterator.hasNext()) {
146 + log.debug("Path is empty");
147 + return;
148 + }
149 + PortPair nextPortPair = portPairService.getPortPair(portPairListIterator.next());
169 DeviceId currentDeviceId = null; 150 DeviceId currentDeviceId = null;
170 DeviceId nextDeviceId = null; 151 DeviceId nextDeviceId = null;
171 - PortPairGroupId portPairGrpId = null;
172 152
173 // Travel from SF to SF. 153 // Travel from SF to SF.
174 do { 154 do {
175 - // Get the required information on port pairs from source port pair 155 + currentDeviceId = vtnRscService.getSfToSffMaping(VirtualPortId.portId(currentPortPair.egress()));
176 - // group 156 + nextDeviceId = vtnRscService.getSfToSffMaping(VirtualPortId.portId(nextPortPair.ingress()));
177 - List<PortPairId> portPairList = currentPortPairGroup.portPairs();
178 - ListIterator<PortPairId> portPLIterator = portPairList.listIterator();
179 - if (!portPLIterator.hasNext()) {
180 - break;
181 - }
182 -
183 - PortPairId portPairId = portPLIterator.next();
184 - PortPair portPair = portPairService.getPortPair(portPairId);
185 -
186 - currentDeviceId = vtnRscService.getSfToSffMaping(VirtualPortId.portId(portPair.ingress()));
187 - if (deviceId == null) {
188 - deviceId = currentDeviceId;
189 - }
190 -
191 // pack traffic selector 157 // pack traffic selector
192 - TrafficSelector.Builder selector = packTrafficSelector(deviceId, portPair, nshSpi); 158 + TrafficSelector.Builder selector = packTrafficSelector(currentDeviceId, currentPortPair, nshSpi);
193 - 159 + // Pack treatment
194 - // Get the required information on port pairs from destination port 160 + if (currentDeviceId.equals(nextDeviceId)) {
195 - // pair group 161 + TrafficTreatment.Builder treatment = packTrafficTreatment(nextPortPair, true);
196 - portPairList = nextPortPairGroup.portPairs(); 162 + // Send SFF to SFF
197 - portPLIterator = portPairList.listIterator(); 163 + sendServiceFunctionForwarder(selector, treatment, currentDeviceId, type);
198 - if (!portPLIterator.hasNext()) { 164 + } else {
199 - break; 165 + TrafficTreatment.Builder treatment = packTrafficTreatment(nextPortPair, false);
166 + // Send SFF to OVS
167 + sendServiceFunctionForwarder(selector, treatment, currentDeviceId, type);
168 +
169 + // At the other device get the packet from vlan and send to first port pair
170 + TrafficSelector.Builder selectorDst = DefaultTrafficSelector.builder();
171 + selectorDst.matchVlanId((VlanId.vlanId(Short.parseShort((vtnRscService
172 + .getL3vni(nextPortPair.tenantId()).toString())))));
173 + TrafficTreatment.Builder treatmentDst = DefaultTrafficTreatment.builder();
174 + MacAddress macAddr = virtualPortService.getPort(VirtualPortId.portId(nextPortPair.ingress()))
175 + .macAddress();
176 + Host host = hostService.getHost(HostId.hostId(macAddr));
177 + PortNumber port = host.location().port();
178 + treatmentDst.setOutput(port);
179 + // Send OVS to SFF
180 + sendServiceFunctionForwarder(selectorDst, treatmentDst, nextDeviceId, type);
200 } 181 }
201 182
202 - portPairId = portPLIterator.next(); 183 + // Move to next service function
203 - portPair = portPairService.getPortPair(portPairId); 184 + currentPortPair = nextPortPair;
204 - 185 + if (!portPairListIterator.hasNext()) {
205 - nextDeviceId = vtnRscService.getSfToSffMaping(VirtualPortId.portId(portPair.ingress()));
206 -
207 - // pack traffic treatment
208 - TrafficTreatment.Builder treatment = packTrafficTreatment(currentDeviceId, nextDeviceId, portPair);
209 -
210 - // Send SFF to OVS
211 - sendServiceFunctionForwarder(selector, treatment, deviceId, type);
212 -
213 - // Replace source port pair group with destination port pair group
214 - // for moving to next SFF processing.
215 - currentPortPairGroup = nextPortPairGroup;
216 - if (!listGrpIterator.hasNext()) {
217 break; 186 break;
218 } 187 }
219 - portPairGrpId = listGrpIterator.next(); 188 + nextPortPair = portPairService.getPortPair(portPairListIterator.next());
220 - nextPortPairGroup = portPairGroupService.getPortPairGroup(portPairGrpId);
221 } while (true); 189 } while (true);
222 } 190 }
223 191
224 /** 192 /**
225 - * Pack Traffic selector. 193 + * Pack traffic selector.
226 * 194 *
227 * @param deviceId device id 195 * @param deviceId device id
228 * @param portPair port-pair 196 * @param portPair port-pair
229 - * @param nshSpi nsh spi 197 + * @param nshSpi nsh service path index
230 - * @return traffic treatment 198 + * @return traffic selector
231 */ 199 */
232 - public TrafficSelector.Builder packTrafficSelector(DeviceId deviceId, PortPair portPair, NshServicePathId nshSpi) { 200 + public TrafficSelector.Builder packTrafficSelector(DeviceId deviceId,
201 + PortPair portPair, NshServicePathId nshSpi) {
233 TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); 202 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
203 +
234 MacAddress dstMacAddress = virtualPortService.getPort(VirtualPortId.portId(portPair.egress())).macAddress(); 204 MacAddress dstMacAddress = virtualPortService.getPort(VirtualPortId.portId(portPair.egress())).macAddress();
235 Host host = hostService.getHost(HostId.hostId(dstMacAddress)); 205 Host host = hostService.getHost(HostId.hostId(dstMacAddress));
236 PortNumber port = host.location().port(); 206 PortNumber port = host.location().port();
...@@ -252,29 +222,30 @@ public class ServiceFunctionForwarderImpl implements ServiceFunctionForwarderSer ...@@ -252,29 +222,30 @@ public class ServiceFunctionForwarderImpl implements ServiceFunctionForwarderSer
252 } 222 }
253 223
254 /** 224 /**
255 - * Pack Traffic treatment. 225 + * Pack traffic treatment.
256 * 226 *
257 - * @param currentDeviceId current device id 227 + * @param portPair port pair
258 - * @param nextDeviceId next device id 228 + * @param isSameOvs whether the next port pair is in the same ovs
259 - * @param portPair port-pair
260 * @return traffic treatment 229 * @return traffic treatment
261 */ 230 */
262 - public TrafficTreatment.Builder packTrafficTreatment(DeviceId currentDeviceId, DeviceId nextDeviceId, 231 + public TrafficTreatment.Builder packTrafficTreatment(PortPair portPair, boolean isSameOvs) {
263 - PortPair portPair) {
264 MacAddress srcMacAddress = null; 232 MacAddress srcMacAddress = null;
265 233
266 // Check the treatment whether destination SF is on same OVS or in 234 // Check the treatment whether destination SF is on same OVS or in
267 // different OVS. 235 // different OVS.
268 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); 236 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
269 - if (currentDeviceId.equals(nextDeviceId)) { 237 + if (isSameOvs) {
270 srcMacAddress = virtualPortService.getPort(VirtualPortId.portId(portPair.ingress())).macAddress(); 238 srcMacAddress = virtualPortService.getPort(VirtualPortId.portId(portPair.ingress())).macAddress();
271 -
272 Host host = hostService.getHost(HostId.hostId(srcMacAddress)); 239 Host host = hostService.getHost(HostId.hostId(srcMacAddress));
273 PortNumber port = host.location().port(); 240 PortNumber port = host.location().port();
274 treatment.setOutput(port); 241 treatment.setOutput(port);
275 } else { 242 } else {
243 + // Vxlan tunnel port for NSH header(Vxlan + NSH).
244 + TpPort nshDstPort = TpPort.tpPort(6633);
245 + // TODO check whether this logic is correct
276 VlanId vlanId = VlanId.vlanId(Short.parseShort((vtnRscService.getL3vni(portPair.tenantId()).toString()))); 246 VlanId vlanId = VlanId.vlanId(Short.parseShort((vtnRscService.getL3vni(portPair.tenantId()).toString())));
277 treatment.setVlanId(vlanId); 247 treatment.setVlanId(vlanId);
248 + treatment.setUdpDst(nshDstPort);
278 } 249 }
279 250
280 return treatment; 251 return treatment;
...@@ -289,7 +260,9 @@ public class ServiceFunctionForwarderImpl implements ServiceFunctionForwarderSer ...@@ -289,7 +260,9 @@ public class ServiceFunctionForwarderImpl implements ServiceFunctionForwarderSer
289 * @param type operation type 260 * @param type operation type
290 */ 261 */
291 public void sendServiceFunctionForwarder(TrafficSelector.Builder selector, TrafficTreatment.Builder treatment, 262 public void sendServiceFunctionForwarder(TrafficSelector.Builder selector, TrafficTreatment.Builder treatment,
292 - DeviceId deviceId, Objective.Operation type) { 263 + DeviceId deviceId, Objective.Operation type) {
264 + log.info("Sending flow to serfice-function-forwarder. Selector {}, Treatment {}",
265 + selector.toString(), treatment.toString());
293 ForwardingObjective.Builder objective = DefaultForwardingObjective.builder().withTreatment(treatment.build()) 266 ForwardingObjective.Builder objective = DefaultForwardingObjective.builder().withTreatment(treatment.build())
294 .withSelector(selector.build()).fromApp(appId).makePermanent().withFlag(Flag.VERSATILE); 267 .withSelector(selector.build()).fromApp(appId).makePermanent().withFlag(Flag.VERSATILE);
295 if (type.equals(Objective.Operation.ADD)) { 268 if (type.equals(Objective.Operation.ADD)) {
......
1 +/*
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 + */
16 +package org.onosproject.sfc.forwarder.impl;
17 +
18 +import static org.easymock.EasyMock.createMock;
19 +import static org.easymock.EasyMock.expect;
20 +import static org.easymock.EasyMock.replay;
21 +import static org.hamcrest.Matchers.instanceOf;
22 +import static org.hamcrest.Matchers.is;
23 +import static org.junit.Assert.assertThat;
24 +
25 +import java.util.List;
26 +import java.util.Map;
27 +import java.util.Set;
28 +
29 +import org.junit.Test;
30 +import org.onlab.packet.IpAddress;
31 +import org.onlab.packet.MacAddress;
32 +import org.onosproject.core.ApplicationId;
33 +import org.onosproject.core.DefaultApplicationId;
34 +import org.onosproject.net.DeviceId;
35 +import org.onosproject.net.NshServicePathId;
36 +import org.onosproject.net.PortNumber;
37 +import org.onosproject.net.device.DeviceService;
38 +import org.onosproject.net.device.DeviceServiceAdapter;
39 +import org.onosproject.net.driver.DriverHandler;
40 +import org.onosproject.net.driver.DriverService;
41 +import org.onosproject.net.flow.criteria.Criterion;
42 +import org.onosproject.net.flow.criteria.PortCriterion;
43 +import org.onosproject.net.flow.instructions.Instruction;
44 +import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
45 +import org.onosproject.net.flowobjective.FlowObjectiveService;
46 +import org.onosproject.net.flowobjective.ForwardingObjective;
47 +import org.onosproject.net.host.HostService;
48 +import org.onosproject.net.host.HostServiceAdapter;
49 +import org.onosproject.sfc.util.FlowClassifierAdapter;
50 +import org.onosproject.sfc.util.FlowObjectiveAdapter;
51 +import org.onosproject.sfc.util.MockDriverHandler;
52 +import org.onosproject.sfc.util.PortPairAdapter;
53 +import org.onosproject.sfc.util.PortPairGroupAdapter;
54 +import org.onosproject.sfc.util.VirtualPortAdapter;
55 +import org.onosproject.sfc.util.VtnRscAdapter;
56 +import org.onosproject.vtnrsc.AllowedAddressPair;
57 +import org.onosproject.vtnrsc.BindingHostId;
58 +import org.onosproject.vtnrsc.DefaultPortPair;
59 +import org.onosproject.vtnrsc.DefaultVirtualPort;
60 +import org.onosproject.vtnrsc.FixedIp;
61 +import org.onosproject.vtnrsc.PortPair;
62 +import org.onosproject.vtnrsc.PortPairId;
63 +import org.onosproject.vtnrsc.SecurityGroup;
64 +import org.onosproject.vtnrsc.SubnetId;
65 +import org.onosproject.vtnrsc.TenantId;
66 +import org.onosproject.vtnrsc.TenantNetworkId;
67 +import org.onosproject.vtnrsc.VirtualPort;
68 +import org.onosproject.vtnrsc.VirtualPortId;
69 +import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService;
70 +import org.onosproject.vtnrsc.portpair.PortPairService;
71 +import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService;
72 +import org.onosproject.vtnrsc.service.VtnRscService;
73 +import org.onosproject.vtnrsc.virtualport.VirtualPortService;
74 +
75 +import com.google.common.collect.Lists;
76 +import com.google.common.collect.Maps;
77 +import com.google.common.collect.Sets;
78 +
79 +public 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 +}
...\ No newline at end of file ...\ No newline at end of file