2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.util;
11 import static org.powermock.api.support.membermodification.MemberMatcher.method;
12 import static org.powermock.api.support.membermodification.MemberModifier.stub;
14 import com.google.common.base.Optional;
15 import com.google.common.collect.Lists;
16 import com.google.common.util.concurrent.Futures;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.List;
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.ArgumentCaptor;
26 import org.mockito.Captor;
27 import org.mockito.Matchers;
28 import org.mockito.Mock;
29 import org.mockito.Mockito;
30 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
31 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
32 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
33 import org.opendaylight.groupbasedpolicy.api.sf.ChainActionDefinition;
34 import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.manager.PolicyConfigurationContext;
35 import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.manager.PolicyManagerImpl;
36 import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.writer.NetconfTransactionCreator;
37 import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.writer.PolicyWriter;
38 import org.opendaylight.sfc.provider.api.SfcProviderRenderedPathAPI;
39 import org.opendaylight.sfc.provider.api.SfcProviderServiceForwarderAPI;
40 import org.opendaylight.sfc.provider.api.SfcProviderServicePathAPI;
41 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.RspName;
42 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfName;
43 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfcName;
44 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SffName;
45 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfpName;
46 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.CreateRenderedPathInput;
47 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.RenderedServicePath;
48 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.RenderedServicePathBuilder;
49 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.rendered.service.path.RenderedServicePathHop;
50 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.rendered.service.path.RenderedServicePathHopBuilder;
51 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarders.ServiceFunctionForwarderBuilder;
52 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.ServiceFunctionPaths;
53 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.ServiceFunctionPathsBuilder;
54 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPath;
55 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPathBuilder;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
58 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.ClassMap;
59 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.ServiceChain;
60 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.policy.map.Class;
61 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.service.chain.service.function.forwarder.Local;
62 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.service.chain.service.function.forwarder.ServiceFfName;
63 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.service.chain.service.path.config.service.chain.path.mode.service.index.services.ServiceTypeChoice;
64 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.service.chain.service.path.config.service.chain.path.mode.service.index.services.service.type.choice.ServiceFunction;
65 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.service.chain.service.path.config.service.chain.path.mode.service.index.services.service.type.choice.ServiceFunctionForwarder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContextId;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ParameterName;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev160427.IpPrefixType;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev160427.L3Context;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValueBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.has.rule.group.with.renderer.endpoint.participation.RuleGroupWithRendererEndpointParticipationBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.renderer.endpoints.RendererEndpoint;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.renderer.endpoints.RendererEndpointBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.renderer.endpoints.renderer.endpoint.PeerEndpoint;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.renderer.endpoints.renderer.endpoint.PeerEndpointBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.has.actions.Action;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.has.actions.ActionBuilder;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.database.rev160308.Sgt;
81 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
82 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
83 import org.powermock.api.mockito.PowerMockito;
84 import org.powermock.core.classloader.annotations.PrepareForTest;
85 import org.powermock.modules.junit4.PowerMockRunner;
88 * Test for {@link ServiceChainingUtil}.
90 @RunWith(PowerMockRunner.class)
92 ServiceChainingUtil.class,
93 SfcProviderServicePathAPI.class,
94 SfcProviderRenderedPathAPI.class,
95 SfcProviderServiceForwarderAPI.class,
96 NetconfTransactionCreator.class
98 public class ServiceChainingUtilTest {
101 private ArgumentCaptor<RspName> rspNameCaptor;
103 private ArgumentCaptor<SffName> sffNameCaptor;
105 private ArgumentCaptor<ServiceFunctionPath> sfpCaptor;
107 private ArgumentCaptor<CreateRenderedPathInput> createRspCaptor;
109 private ArgumentCaptor<RenderedServicePath> rspCaptor;
111 private ArgumentCaptor<List<Class>> listClassCaptor;
113 private ArgumentCaptor<ClassMap> classMapCaptor;
115 private PolicyWriter policyWriter;
117 private DataBroker dataBroker;
119 private ReadWriteTransaction rwTx;
121 private PolicyConfigurationContext policyConfigurationContext;
124 public void setUp() throws Exception {
125 final NodeId currentNodeId = new NodeId("unit-node-01");
126 Mockito.when(policyWriter.getCurrentNodeId()).thenReturn(currentNodeId);
128 final String managementIpAddress = "1.2.3.5";
129 Mockito.when(policyWriter.getManagementIpAddress()).thenReturn(managementIpAddress);
131 Mockito.when(policyWriter.getCurrentMountpoint()).thenReturn(dataBroker);
132 Mockito.when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
134 policyConfigurationContext = new PolicyConfigurationContext();
135 policyConfigurationContext.setPolicyWriter(policyWriter);
139 public void testGetServicePath() throws Exception {
140 final String sfcNameValue = "123";
141 final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, false);
142 final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
143 .setServiceFunctionPath(Collections.singletonList(sfcPath))
146 stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
148 final ServiceFunctionPath servicePath = ServiceChainingUtil.getServicePath(Lists.newArrayList(
149 createParameterValue("sfc-chain-name", sfcNameValue)
151 Assert.assertEquals(sfcPath, servicePath);
154 private ParameterValue createParameterValue(final String name, final String value) {
155 return new ParameterValueBuilder().setName(new ParameterName(name)).setStringValue(value).build();
159 public void testResolveChainAction_full() throws Exception {
160 final PeerEndpoint peerEndpoint = createPeerEndpoint();
161 final Sgt sourceSgt = new Sgt(1);
162 final Sgt destinationSgt = new Sgt(2);
163 final Map<PolicyManagerImpl.ActionCase, Action> actionMap = createActionMap();
164 final String classMapName = "unit-class-map-name-01";
166 final String sfcNameValue = "123";
167 final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, false);
168 final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
169 .setServiceFunctionPath(Collections.singletonList(sfcPath))
172 stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
174 final RenderedServicePath rsp = createRsp("unit-rsp-02");
175 stub(method(SfcProviderRenderedPathAPI.class, "readRenderedServicePath")).toReturn(rsp);
176 stub(method(ServiceChainingUtil.class, "setSfcPart")).toReturn(true);
178 ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap, policyConfigurationContext, dataBroker);
180 Mockito.verify(policyWriter).cache(classMapCaptor.capture());
181 Mockito.verify(policyWriter).cache(listClassCaptor.capture());
182 Mockito.verifyNoMoreInteractions(policyWriter);
183 Assert.assertEquals(1, listClassCaptor.getValue().size());
187 public void testResolveChainAction_fullSymmetric() throws Exception {
188 final PeerEndpoint peerEndpoint = createPeerEndpoint();
189 final Sgt sourceSgt = new Sgt(1);
190 final Sgt destinationSgt = new Sgt(2);
191 final Map<PolicyManagerImpl.ActionCase, Action> actionMap = createActionMap();
192 final String classMapName = "unit-class-map-name-01";
194 final String sfcNameValue = "123";
195 final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, true);
196 final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
197 .setServiceFunctionPath(Collections.singletonList(sfcPath))
200 stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
202 final RenderedServicePath rsp = createRsp("unit-rsp-02");
203 stub(method(SfcProviderRenderedPathAPI.class, "readRenderedServicePath")).toReturn(rsp);
204 stub(method(ServiceChainingUtil.class, "setSfcPart")).toReturn(true);
206 ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap, policyConfigurationContext, dataBroker);
208 Mockito.verify(policyWriter, Mockito.times(2)).cache(classMapCaptor.capture());
209 Mockito.verify(policyWriter).cache(listClassCaptor.capture());
210 Mockito.verifyNoMoreInteractions(policyWriter);
211 Assert.assertEquals(2, listClassCaptor.getValue().size());
215 public void testResolveChainAction_partial01() throws Exception {
216 final PeerEndpoint peerEndpoint = createPeerEndpoint();
217 final Sgt sourceSgt = new Sgt(1);
218 final Sgt destinationSgt = new Sgt(2);
219 final Map<PolicyManagerImpl.ActionCase, Action> actionMap = createActionMap();
220 final String classMapName = "unit-class-map-name-01";
222 final String sfcNameValue = "123";
223 final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, true);
224 final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
225 .setServiceFunctionPath(Collections.singletonList(sfcPath))
228 stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
230 final RenderedServicePath rsp = createRsp("unit-rsp-02");
231 stub(method(SfcProviderRenderedPathAPI.class, "readRenderedServicePath")).toReturn(rsp);
232 stub(method(ServiceChainingUtil.class, "setSfcPart")).toReturn(true);
233 stub(method(ServiceChainingUtil.class, "createSymmetricRenderedPath")).toReturn(null);
235 policyConfigurationContext.setCurrentRendererEP(createRendererEP(
236 "unit-address", new ContextId("unit-conext-1"), Collections.emptyList())
239 ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap,
240 policyConfigurationContext, dataBroker);
241 Mockito.verify(policyWriter, Mockito.times(2)).cache(classMapCaptor.capture());
242 Mockito.verify(policyWriter).cache(listClassCaptor.capture());
243 Mockito.verifyNoMoreInteractions(policyWriter);
247 public void testResolveChainAction_partial02() throws Exception {
248 final PeerEndpoint peerEndpoint = createPeerEndpoint();
249 final Sgt sourceSgt = new Sgt(1);
250 final Sgt destinationSgt = new Sgt(2);
251 final Map<PolicyManagerImpl.ActionCase, Action> actionMap = createActionMap();
252 final String classMapName = "unit-class-map-name-01";
254 final String sfcNameValue = "123";
255 final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, false);
256 final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
257 .setServiceFunctionPath(Collections.singletonList(sfcPath))
260 stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
262 final RenderedServicePath rsp = createRsp("unit-rsp-02");
263 stub(method(SfcProviderRenderedPathAPI.class, "readRenderedServicePath")).toReturn(rsp);
264 stub(method(ServiceChainingUtil.class, "setSfcPart")).toReturn(false);
266 ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap, policyConfigurationContext, dataBroker);
268 Mockito.verifyNoMoreInteractions(policyWriter);
272 public void testResolveChainAction_partial03() throws Exception {
273 final PeerEndpoint peerEndpoint = createPeerEndpoint(null);
274 final Sgt sourceSgt = new Sgt(1);
275 final Sgt destinationSgt = new Sgt(2);
276 final Map<PolicyManagerImpl.ActionCase, Action> actionMap = createActionMap();
277 final String classMapName = "unit-class-map-name-01";
279 final String sfcNameValue = "123";
280 final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, false);
281 final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
282 .setServiceFunctionPath(Collections.singletonList(sfcPath))
285 stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
287 final RenderedServicePath rsp = createRsp("unit-rsp-02");
288 stub(method(SfcProviderRenderedPathAPI.class, "readRenderedServicePath")).toReturn(rsp);
290 policyConfigurationContext.setCurrentRendererEP(createRendererEP(
291 "unit-address", new ContextId("unit-conext-1"), Collections.emptyList())
294 ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap, policyConfigurationContext, dataBroker);
296 Mockito.verifyNoMoreInteractions(policyWriter);
300 public void testResolveChainAction_partial04() throws Exception {
301 final PeerEndpoint peerEndpoint = createPeerEndpoint(null);
302 final Sgt sourceSgt = new Sgt(1);
303 final Sgt destinationSgt = new Sgt(2);
304 final Map<PolicyManagerImpl.ActionCase, Action> actionMap = createActionMap(false);
305 final String classMapName = "unit-class-map-name-01";
307 policyConfigurationContext.setCurrentRendererEP(createRendererEP(
308 "unit-address", new ContextId("unit-conext-1"), Collections.emptyList())
311 ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap, policyConfigurationContext, dataBroker);
313 Mockito.verifyNoMoreInteractions(policyWriter);
316 private Map<PolicyManagerImpl.ActionCase, Action> createActionMap() {
317 return createActionMap(true);
320 private Map<PolicyManagerImpl.ActionCase, Action> createActionMap(final boolean fillParamValue) {
321 final Map<PolicyManagerImpl.ActionCase,Action> actionMap = new HashMap<>();
322 final ActionBuilder actionValue = new ActionBuilder();
323 if (fillParamValue) {
324 actionValue.setParameterValue(Collections.singletonList(new ParameterValueBuilder()
325 .setName(new ParameterName(ChainActionDefinition.SFC_CHAIN_NAME))
326 .setStringValue("123")
329 actionMap.put(PolicyManagerImpl.ActionCase.CHAIN, actionValue.build());
333 private RendererEndpoint createRendererEP(final String address, final ContextId contextId, final List<PeerEndpoint> peerEndpoints) {
334 return new RendererEndpointBuilder()
336 .setAddressType(IpPrefixType.class)
337 .setContextId(contextId)
338 .setContextType(L3Context.class)
339 .setPeerEndpoint(peerEndpoints)
343 private PeerEndpoint createPeerEndpoint() {
344 return createPeerEndpoint(new TenantId("unit-tenant-06"));
347 private PeerEndpoint createPeerEndpoint(final TenantId tenantId) {
348 return new PeerEndpointBuilder()
349 .setRuleGroupWithRendererEndpointParticipation(Collections.singletonList(
350 new RuleGroupWithRendererEndpointParticipationBuilder()
351 .setTenantId(tenantId)
358 public void testCreateRenderedPath() throws Exception {
359 final String sfcNameValue = "123";
360 final ServiceFunctionPath sfp = createSfp(sfcNameValue, false);
361 final TenantId tenantId = new TenantId("unit-tennant-01");
363 final RenderedServicePath rsp = createRsp("unit-rsp-01");
365 PowerMockito.mockStatic(SfcProviderRenderedPathAPI.class);
366 final SfcProviderRenderedPathAPI api = PowerMockito.mock(SfcProviderRenderedPathAPI.class);
367 PowerMockito.when(api.readRenderedServicePath(rspNameCaptor.capture())).thenReturn(rsp);
369 final RenderedServicePath renderedPath = ServiceChainingUtil.createRenderedPath(sfp, tenantId, dataBroker);
371 Assert.assertEquals("123_plainunit-tennant-01-gbp-rsp", rspNameCaptor.getValue().getValue());
372 Assert.assertEquals(rsp, renderedPath);
376 public void testCreateRenderedPath_notExisting() throws Exception {
377 final String sfcNameValue = "123";
378 final ServiceFunctionPath sfp = createSfp(sfcNameValue, false);
379 final TenantId tenantId = new TenantId("unit-tennant-01");
381 final RenderedServicePath rsp = createRsp("unit-rsp-01");
383 PowerMockito.mockStatic(SfcProviderRenderedPathAPI.class);
384 final SfcProviderRenderedPathAPI api = PowerMockito.mock(SfcProviderRenderedPathAPI.class);
385 PowerMockito.when(api.readRenderedServicePath(rspNameCaptor.capture())).thenReturn(null);
386 PowerMockito.when(api.createRenderedServicePathAndState(
387 sfpCaptor.capture(), createRspCaptor.capture()
389 PowerMockito.mockStatic(NetconfTransactionCreator.class);
390 final NetconfTransactionCreator creator = PowerMockito.mock(NetconfTransactionCreator.class);
391 PowerMockito.when(creator.netconfReadWriteTransaction(dataBroker)).thenReturn(java.util.Optional.empty());
393 final RenderedServicePath renderedPath = ServiceChainingUtil.createRenderedPath(sfp, tenantId, dataBroker);
395 Assert.assertEquals("123_plainunit-tennant-01-gbp-rsp", rspNameCaptor.getValue().getValue());
397 final ServiceFunctionPath serviceFunctionPath = sfpCaptor.getValue();
398 Assert.assertEquals("123_plain", serviceFunctionPath.getName().getValue());
399 Assert.assertFalse(serviceFunctionPath.isSymmetric());
401 final CreateRenderedPathInput createRPInput = createRspCaptor.getValue();
402 Assert.assertFalse(createRPInput.isSymmetric());
403 Assert.assertEquals("123_plain", createRPInput.getParentServiceFunctionPath());
404 Assert.assertEquals("123_plainunit-tennant-01-gbp-rsp", createRPInput.getName());
406 Assert.assertEquals(rsp, renderedPath);
409 private RenderedServicePath createRsp(final String rspNameValue) {
410 return new RenderedServicePathBuilder()
411 .setName(new RspName(rspNameValue))
412 .setRenderedServicePathHop(Lists.newArrayList(createRspHop("rsp-hop-01-sf")))
416 private RenderedServicePathHop createRspHop(final String sfNameValue) {
417 return new RenderedServicePathHopBuilder()
418 .setServiceFunctionName(new SfName(sfNameValue))
419 .setServiceFunctionForwarder(new SffName(sfNameValue + "+sff"))
423 private ServiceFunctionPath createSfp(final String sfcNameValue, final boolean symmetric) {
424 return new ServiceFunctionPathBuilder()
425 .setServiceChainName(new SfcName(sfcNameValue))
426 .setName(new SfpName(sfcNameValue + "_plain"))
427 .setSymmetric(symmetric)
432 public void testCreateSymmetricRenderedPath() throws Exception {
433 final ServiceFunctionPath sfp = createSfp("unit-sfp-02", false);
434 final RenderedServicePath rsp = createRsp("unit-rsp-02");
435 final TenantId tennantId = new TenantId("tenant-02");
437 PowerMockito.mockStatic(SfcProviderRenderedPathAPI.class);
438 final SfcProviderRenderedPathAPI api = PowerMockito.mock(SfcProviderRenderedPathAPI.class);
439 PowerMockito.when(api.readRenderedServicePath(rspNameCaptor.capture())).thenReturn(rsp);
441 final RenderedServicePath symmetricRenderedPath = ServiceChainingUtil.createSymmetricRenderedPath(sfp, rsp, tennantId, dataBroker);
443 Assert.assertEquals("unit-sfp-02_plaintenant-02-gbp-rsp-Reverse", rspNameCaptor.getValue().getValue());
444 Assert.assertEquals(rsp, symmetricRenderedPath);
448 public void testCreateSymmetricRenderedPath_notExisting() throws Exception {
449 final ServiceFunctionPath sfp = createSfp("unit-sfp-02", false);
450 final RenderedServicePath rsp = createRsp("unit-rsp-02");
451 final TenantId tennantId = new TenantId("tenant-02");
453 PowerMockito.mockStatic(SfcProviderRenderedPathAPI.class);
454 final SfcProviderRenderedPathAPI api = PowerMockito.mock(SfcProviderRenderedPathAPI.class);
455 PowerMockito.when(api.readRenderedServicePath(rspNameCaptor.capture())).thenReturn(null);
456 PowerMockito.when(api.createReverseRenderedServicePathEntry(rspCaptor.capture())).thenReturn(rsp);
457 PowerMockito.mockStatic(NetconfTransactionCreator.class);
458 final NetconfTransactionCreator creator = PowerMockito.mock(NetconfTransactionCreator.class);
459 PowerMockito.when(creator.netconfReadWriteTransaction(dataBroker)).thenReturn(java.util.Optional.empty());
461 final RenderedServicePath symmetricRenderedPath = ServiceChainingUtil.createSymmetricRenderedPath(sfp, rsp, tennantId, dataBroker);
463 Assert.assertEquals("unit-sfp-02_plaintenant-02-gbp-rsp-Reverse", rspNameCaptor.getValue().getValue());
464 Assert.assertEquals(rsp, rspCaptor.getValue());
465 Assert.assertEquals(rsp, symmetricRenderedPath);
469 public void testFindServiceFunctionPath() throws Exception {
470 final String sfcNameValue = "123";
471 final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, false);
472 final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
473 .setServiceFunctionPath(Collections.singletonList(sfcPath))
476 stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
478 final ServiceFunctionPath servicePath = ServiceChainingUtil.findServiceFunctionPath(new SfcName(sfcNameValue));
479 Assert.assertEquals(sfcPath, servicePath);
483 public void testSetSfcPart_success() throws Exception {
484 final RenderedServicePath rsp = createRsp("unit-rsp-03");
485 final org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarders.ServiceFunctionForwarder
486 sff = new ServiceFunctionForwarderBuilder()
487 .setName(new SffName("unit-sff-03"))
488 .setIpMgmtAddress(new IpAddress(new Ipv4Address("1.2.3.4")))
490 final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
491 sfpBuilder.setSymmetric(false);
492 final ServiceFunctionPath sfp = sfpBuilder.build();
494 stub(method(ServiceChainingUtil.class, "checkLocalForwarderPresence")).toReturn(true);
496 PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
497 final SfcProviderServiceForwarderAPI api = PowerMockito.mock(SfcProviderServiceForwarderAPI.class);
498 PowerMockito.when(api.readServiceFunctionForwarder(sffNameCaptor.capture())).thenReturn(sff);
500 final boolean outcome = ServiceChainingUtil.setSfcPart(sfp, rsp, null, policyWriter);
502 Assert.assertEquals("rsp-hop-01-sf+sff", sffNameCaptor.getValue().getValue());
503 Assert.assertTrue(outcome);
505 Mockito.verify(policyWriter).cache(Matchers.<ServiceFfName>any());
506 Mockito.verify(policyWriter).cache(Matchers.<ServiceChain>any());
507 Mockito.verify(policyWriter).getCurrentNodeId();
508 Mockito.verify(policyWriter).getCurrentMountpoint();
509 Mockito.verify(policyWriter).getManagementIpAddress();
510 Mockito.verifyNoMoreInteractions(policyWriter);
514 public void testSetSfcPart_success_newRsp() throws Exception {
515 final RenderedServicePath rsp = createRsp("unit-rsp-03");
516 final org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarders.ServiceFunctionForwarder
517 sff = new ServiceFunctionForwarderBuilder()
518 .setName(new SffName("unit-sff-03"))
519 .setIpMgmtAddress(new IpAddress(new Ipv4Address("1.2.3.4")))
521 final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
522 sfpBuilder.setSymmetric(false);
523 final ServiceFunctionPath sfp = sfpBuilder.build();
525 stub(method(ServiceChainingUtil.class, "checkLocalForwarderPresence")).toReturn(false);
527 PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
528 final SfcProviderServiceForwarderAPI api = PowerMockito.mock(SfcProviderServiceForwarderAPI.class);
529 PowerMockito.when(api.readServiceFunctionForwarder(sffNameCaptor.capture())).thenReturn(sff);
531 final boolean outcome = ServiceChainingUtil.setSfcPart(sfp, rsp, null, policyWriter);
533 Assert.assertEquals("rsp-hop-01-sf+sff", sffNameCaptor.getValue().getValue());
534 Assert.assertTrue(outcome);
536 Mockito.verify(policyWriter).cache(Matchers.<Local>any());
537 Mockito.verify(policyWriter).cache(Matchers.<ServiceFfName>any());
538 Mockito.verify(policyWriter).cache(Matchers.<ServiceChain>any());
539 Mockito.verify(policyWriter).getCurrentMountpoint();
540 Mockito.verify(policyWriter, Mockito.times(2)).getManagementIpAddress();
541 Mockito.verifyNoMoreInteractions(policyWriter);
545 public void testSetSfcPart_fail01() throws Exception {
546 final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
547 sfpBuilder.setSymmetric(false);
548 final ServiceFunctionPath sfp = sfpBuilder.build();
550 Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, null, null, policyWriter));
552 final RenderedServicePathBuilder rspBuilder = new RenderedServicePathBuilder().setName(new RspName("unit-rsp-05"));
553 Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, rspBuilder.build(), null, policyWriter));
555 rspBuilder.setRenderedServicePathHop(Collections.emptyList());
556 Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, rspBuilder.build(), null, policyWriter));
558 rspBuilder.setRenderedServicePathHop(Collections.singletonList(null));
559 Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, rspBuilder.build(), null, policyWriter));
563 public void testSetSfcPart_fail02() throws Exception {
564 final RenderedServicePath rsp = createRsp("unit-rsp-03");
565 final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
566 sfpBuilder.setSymmetric(false);
567 final ServiceFunctionPath sfp = sfpBuilder.build();
569 Mockito.doReturn(Futures.immediateCheckedFuture(Optional.absent()))
570 .when(rwTx).read(Mockito.eq(LogicalDatastoreType.CONFIGURATION), Mockito.<InstanceIdentifier<Local>>any());
572 PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
573 final SfcProviderServiceForwarderAPI api = PowerMockito.mock(SfcProviderServiceForwarderAPI.class);
574 PowerMockito.when(api.readServiceFunctionForwarder(sffNameCaptor.capture())).thenReturn(null);
576 final boolean outcome = ServiceChainingUtil.setSfcPart(sfp, rsp, null, policyWriter);
578 Assert.assertEquals("rsp-hop-01-sf+sff", sffNameCaptor.getValue().getValue());
579 Assert.assertFalse(outcome);
581 Mockito.verify(policyWriter).getCurrentMountpoint();
582 Mockito.verify(policyWriter).getManagementIpAddress();
583 Mockito.verify(policyWriter).cache(Matchers.<Local>any());
584 Mockito.verifyNoMoreInteractions(policyWriter);
588 public void testSetSfcPart_fail03() throws Exception {
589 final RenderedServicePath rsp = createRsp("unit-rsp-03");
590 final ServiceFunctionForwarderBuilder sffBuilder = new ServiceFunctionForwarderBuilder()
591 .setName(new SffName("unit-sff-03"))
592 .setIpMgmtAddress(null);
593 final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
594 sfpBuilder.setSymmetric(false);
595 final ServiceFunctionPath sfp = sfpBuilder.build();
597 stub(method(ServiceChainingUtil.class, "checkLocalForwarderPresence")).toReturn(true);
599 PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
600 final SfcProviderServiceForwarderAPI api = PowerMockito.mock(SfcProviderServiceForwarderAPI.class);
601 PowerMockito.when(api.readServiceFunctionForwarder(sffNameCaptor.capture())).thenReturn(
604 Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, rsp, null, policyWriter));
606 Assert.assertEquals("rsp-hop-01-sf+sff", sffNameCaptor.getValue().getValue());
608 Mockito.verify(policyWriter).getCurrentMountpoint();
609 Mockito.verify(policyWriter).getCurrentNodeId();
610 Mockito.verifyNoMoreInteractions(policyWriter);
614 public void testSetSfcPart_fail04() throws Exception {
615 final RenderedServicePath rsp = createRsp("unit-rsp-03");
616 final ServiceFunctionForwarderBuilder sffBuilder = new ServiceFunctionForwarderBuilder()
617 .setName(new SffName("unit-sff-03"))
618 .setIpMgmtAddress(new IpAddress((Ipv4Address) null));
619 final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
620 sfpBuilder.setSymmetric(false);
621 final ServiceFunctionPath sfp = sfpBuilder.build();
623 stub(method(ServiceChainingUtil.class, "checkLocalForwarderPresence")).toReturn(true);
625 PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
626 final SfcProviderServiceForwarderAPI api = PowerMockito.mock(SfcProviderServiceForwarderAPI.class);
627 PowerMockito.when(api.readServiceFunctionForwarder(sffNameCaptor.capture())).thenReturn(
630 Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, rsp, null, policyWriter));
632 Assert.assertEquals("rsp-hop-01-sf+sff", sffNameCaptor.getValue().getValue());
634 Mockito.verify(policyWriter).getCurrentMountpoint();
635 Mockito.verify(policyWriter).getCurrentNodeId();
636 Mockito.verifyNoMoreInteractions(policyWriter);
640 public void testForwarderTypeChoice() throws Exception {
641 final String sffValue = "unit-xx";
642 final ServiceTypeChoice fwChoice = ServiceChainingUtil.forwarderTypeChoice(sffValue);
644 Assert.assertTrue(fwChoice instanceof ServiceFunctionForwarder);
645 final ServiceFunctionForwarder sff = (ServiceFunctionForwarder) fwChoice;
646 Assert.assertEquals(sffValue, sff.getServiceFunctionForwarder());
650 public void testFunctionTypeChoice() throws Exception {
651 final String stcValue = "unit-xx";
652 final ServiceTypeChoice srvTypeChoice = ServiceChainingUtil.functionTypeChoice(stcValue);
654 Assert.assertTrue(srvTypeChoice instanceof ServiceFunction);
655 final ServiceFunction stc = (ServiceFunction) srvTypeChoice;
656 Assert.assertEquals(stcValue, stc.getServiceFunction());