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.groupbasedpolicy.util.IetfModelCodec;
39 import org.opendaylight.sfc.provider.api.SfcProviderRenderedPathAPI;
40 import org.opendaylight.sfc.provider.api.SfcProviderServiceForwarderAPI;
41 import org.opendaylight.sfc.provider.api.SfcProviderServicePathAPI;
42 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.RspName;
43 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfName;
44 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfcName;
45 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SffName;
46 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfpName;
47 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.CreateRenderedPathInput;
48 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.RenderedServicePath;
49 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.RenderedServicePathBuilder;
50 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.rendered.service.path.RenderedServicePathHop;
51 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.rendered.service.path.RenderedServicePathHopBuilder;
52 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarders.ServiceFunctionForwarderBuilder;
53 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.ServiceFunctionPaths;
54 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.ServiceFunctionPathsBuilder;
55 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPath;
56 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPathBuilder;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
58 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
59 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.ClassMap;
60 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.ServiceChain;
61 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.policy.map.Class;
62 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.service.chain.service.function.forwarder.Local;
63 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.service.chain.service.function.forwarder.ServiceFfName;
64 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.service.chain.service.path.config.service.chain.path.mode.service.index.services.ServiceTypeChoice;
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.ServiceFunction;
66 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;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContextId;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ParameterName;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev160427.IpPrefixType;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev160427.L3Context;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValueBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.has.rule.group.with.renderer.endpoint.participation.RuleGroupWithRendererEndpointParticipationBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.renderer.endpoints.RendererEndpoint;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.renderer.endpoints.RendererEndpointBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.renderer.endpoints.renderer.endpoint.PeerEndpoint;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.renderer.endpoints.renderer.endpoint.PeerEndpointBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.has.actions.Action;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.has.actions.ActionBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.database.rev160308.Sgt;
82 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
83 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
84 import org.powermock.api.mockito.PowerMockito;
85 import org.powermock.core.classloader.annotations.PrepareForTest;
86 import org.powermock.modules.junit4.PowerMockRunner;
89 * Test for {@link ServiceChainingUtil}.
91 @RunWith(PowerMockRunner.class)
93 ServiceChainingUtil.class,
94 SfcProviderServicePathAPI.class,
95 SfcProviderRenderedPathAPI.class,
96 SfcProviderServiceForwarderAPI.class,
97 NetconfTransactionCreator.class
99 public class ServiceChainingUtilTest {
102 private ArgumentCaptor<RspName> rspNameCaptor;
104 private ArgumentCaptor<SffName> sffNameCaptor;
106 private ArgumentCaptor<ServiceFunctionPath> sfpCaptor;
108 private ArgumentCaptor<CreateRenderedPathInput> createRspCaptor;
110 private ArgumentCaptor<RenderedServicePath> rspCaptor;
112 private ArgumentCaptor<List<Class>> listClassCaptor;
114 private ArgumentCaptor<ClassMap> classMapCaptor;
116 private PolicyWriter policyWriter;
118 private DataBroker dataBroker;
120 private ReadWriteTransaction rwTx;
122 private PolicyConfigurationContext policyConfigurationContext;
125 public void setUp() throws Exception {
126 final NodeId currentNodeId = new NodeId("unit-node-01");
127 Mockito.when(policyWriter.getCurrentNodeId()).thenReturn(currentNodeId);
129 final String managementIpAddress = "1.2.3.5";
130 Mockito.when(policyWriter.getManagementIpAddress()).thenReturn(managementIpAddress);
132 Mockito.when(policyWriter.getCurrentMountpoint()).thenReturn(dataBroker);
133 Mockito.when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
135 policyConfigurationContext = new PolicyConfigurationContext();
136 policyConfigurationContext.setPolicyWriter(policyWriter);
140 public void testGetServicePath() throws Exception {
141 final String sfcNameValue = "123";
142 final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, false);
143 final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
144 .setServiceFunctionPath(Collections.singletonList(sfcPath))
147 stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
149 final ServiceFunctionPath servicePath = ServiceChainingUtil.getServicePath(Lists.newArrayList(
150 createParameterValue("sfc-chain-name", sfcNameValue)
152 Assert.assertEquals(sfcPath, servicePath);
155 private ParameterValue createParameterValue(final String name, final String value) {
156 return new ParameterValueBuilder().setName(new ParameterName(name)).setStringValue(value).build();
160 public void testResolveChainAction_full() throws Exception {
161 final PeerEndpoint peerEndpoint = createPeerEndpoint();
162 final Sgt sourceSgt = new Sgt(1);
163 final Sgt destinationSgt = new Sgt(2);
164 final Map<PolicyManagerImpl.ActionCase, Action> actionMap = createActionMap();
165 final String classMapName = "unit-class-map-name-01";
167 final String sfcNameValue = "123";
168 final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, false);
169 final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
170 .setServiceFunctionPath(Collections.singletonList(sfcPath))
173 stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
175 final RenderedServicePath rsp = createRsp("unit-rsp-02");
176 stub(method(SfcProviderRenderedPathAPI.class, "readRenderedServicePath")).toReturn(rsp);
177 stub(method(ServiceChainingUtil.class, "setSfcPart")).toReturn(true);
179 ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap, policyConfigurationContext, dataBroker);
181 Mockito.verify(policyWriter).cache(classMapCaptor.capture());
182 Mockito.verify(policyWriter).cache(listClassCaptor.capture());
183 Mockito.verifyNoMoreInteractions(policyWriter);
184 Assert.assertEquals(1, listClassCaptor.getValue().size());
188 public void testResolveChainAction_fullSymmetric() throws Exception {
189 final PeerEndpoint peerEndpoint = createPeerEndpoint();
190 final Sgt sourceSgt = new Sgt(1);
191 final Sgt destinationSgt = new Sgt(2);
192 final Map<PolicyManagerImpl.ActionCase, Action> actionMap = createActionMap();
193 final String classMapName = "unit-class-map-name-01";
195 final String sfcNameValue = "123";
196 final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, true);
197 final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
198 .setServiceFunctionPath(Collections.singletonList(sfcPath))
201 stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
203 final RenderedServicePath rsp = createRsp("unit-rsp-02");
204 stub(method(SfcProviderRenderedPathAPI.class, "readRenderedServicePath")).toReturn(rsp);
205 stub(method(ServiceChainingUtil.class, "setSfcPart")).toReturn(true);
207 ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap, policyConfigurationContext, dataBroker);
209 Mockito.verify(policyWriter, Mockito.times(2)).cache(classMapCaptor.capture());
210 Mockito.verify(policyWriter).cache(listClassCaptor.capture());
211 Mockito.verifyNoMoreInteractions(policyWriter);
212 Assert.assertEquals(2, listClassCaptor.getValue().size());
216 public void testResolveChainAction_partial01() throws Exception {
217 final PeerEndpoint peerEndpoint = createPeerEndpoint();
218 final Sgt sourceSgt = new Sgt(1);
219 final Sgt destinationSgt = new Sgt(2);
220 final Map<PolicyManagerImpl.ActionCase, Action> actionMap = createActionMap();
221 final String classMapName = "unit-class-map-name-01";
223 final String sfcNameValue = "123";
224 final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, true);
225 final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
226 .setServiceFunctionPath(Collections.singletonList(sfcPath))
229 stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
231 final RenderedServicePath rsp = createRsp("unit-rsp-02");
232 stub(method(SfcProviderRenderedPathAPI.class, "readRenderedServicePath")).toReturn(rsp);
233 stub(method(ServiceChainingUtil.class, "setSfcPart")).toReturn(true);
234 stub(method(ServiceChainingUtil.class, "createSymmetricRenderedPath")).toReturn(null);
236 policyConfigurationContext.setCurrentRendererEP(createRendererEP(
237 "unit-address", new ContextId("unit-conext-1"), Collections.emptyList())
240 ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap,
241 policyConfigurationContext, dataBroker);
242 Mockito.verify(policyWriter, Mockito.times(2)).cache(classMapCaptor.capture());
243 Mockito.verify(policyWriter).cache(listClassCaptor.capture());
244 Mockito.verifyNoMoreInteractions(policyWriter);
248 public void testResolveChainAction_partial02() throws Exception {
249 final PeerEndpoint peerEndpoint = createPeerEndpoint();
250 final Sgt sourceSgt = new Sgt(1);
251 final Sgt destinationSgt = new Sgt(2);
252 final Map<PolicyManagerImpl.ActionCase, Action> actionMap = createActionMap();
253 final String classMapName = "unit-class-map-name-01";
255 final String sfcNameValue = "123";
256 final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, false);
257 final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
258 .setServiceFunctionPath(Collections.singletonList(sfcPath))
261 stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
263 final RenderedServicePath rsp = createRsp("unit-rsp-02");
264 stub(method(SfcProviderRenderedPathAPI.class, "readRenderedServicePath")).toReturn(rsp);
265 stub(method(ServiceChainingUtil.class, "setSfcPart")).toReturn(false);
267 ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap, policyConfigurationContext, dataBroker);
269 Mockito.verifyNoMoreInteractions(policyWriter);
273 public void testResolveChainAction_partial03() throws Exception {
274 final PeerEndpoint peerEndpoint = createPeerEndpoint(null);
275 final Sgt sourceSgt = new Sgt(1);
276 final Sgt destinationSgt = new Sgt(2);
277 final Map<PolicyManagerImpl.ActionCase, Action> actionMap = createActionMap();
278 final String classMapName = "unit-class-map-name-01";
280 final String sfcNameValue = "123";
281 final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, false);
282 final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
283 .setServiceFunctionPath(Collections.singletonList(sfcPath))
286 stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
288 final RenderedServicePath rsp = createRsp("unit-rsp-02");
289 stub(method(SfcProviderRenderedPathAPI.class, "readRenderedServicePath")).toReturn(rsp);
291 policyConfigurationContext.setCurrentRendererEP(createRendererEP(
292 "unit-address", new ContextId("unit-conext-1"), Collections.emptyList())
295 ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap, policyConfigurationContext, dataBroker);
297 Mockito.verifyNoMoreInteractions(policyWriter);
301 public void testResolveChainAction_partial04() throws Exception {
302 final PeerEndpoint peerEndpoint = createPeerEndpoint(null);
303 final Sgt sourceSgt = new Sgt(1);
304 final Sgt destinationSgt = new Sgt(2);
305 final Map<PolicyManagerImpl.ActionCase, Action> actionMap = createActionMap(false);
306 final String classMapName = "unit-class-map-name-01";
308 policyConfigurationContext.setCurrentRendererEP(createRendererEP(
309 "unit-address", new ContextId("unit-conext-1"), Collections.emptyList())
312 ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap, policyConfigurationContext, dataBroker);
314 Mockito.verifyNoMoreInteractions(policyWriter);
317 private Map<PolicyManagerImpl.ActionCase, Action> createActionMap() {
318 return createActionMap(true);
321 private Map<PolicyManagerImpl.ActionCase, Action> createActionMap(final boolean fillParamValue) {
322 final Map<PolicyManagerImpl.ActionCase,Action> actionMap = new HashMap<>();
323 final ActionBuilder actionValue = new ActionBuilder();
324 if (fillParamValue) {
325 actionValue.setParameterValue(Collections.singletonList(new ParameterValueBuilder()
326 .setName(new ParameterName(ChainActionDefinition.SFC_CHAIN_NAME))
327 .setStringValue("123")
330 actionMap.put(PolicyManagerImpl.ActionCase.CHAIN, actionValue.build());
334 private RendererEndpoint createRendererEP(final String address, final ContextId contextId, final List<PeerEndpoint> peerEndpoints) {
335 return new RendererEndpointBuilder()
337 .setAddressType(IpPrefixType.class)
338 .setContextId(contextId)
339 .setContextType(L3Context.class)
340 .setPeerEndpoint(peerEndpoints)
344 private PeerEndpoint createPeerEndpoint() {
345 return createPeerEndpoint(new TenantId("unit-tenant-06"));
348 private PeerEndpoint createPeerEndpoint(final TenantId tenantId) {
349 return new PeerEndpointBuilder()
350 .setRuleGroupWithRendererEndpointParticipation(Collections.singletonList(
351 new RuleGroupWithRendererEndpointParticipationBuilder()
352 .setTenantId(tenantId)
359 public void testCreateRenderedPath() throws Exception {
360 final String sfcNameValue = "123";
361 final ServiceFunctionPath sfp = createSfp(sfcNameValue, false);
362 final TenantId tenantId = new TenantId("unit-tennant-01");
364 final RenderedServicePath rsp = createRsp("unit-rsp-01");
366 PowerMockito.mockStatic(SfcProviderRenderedPathAPI.class);
367 final SfcProviderRenderedPathAPI api = PowerMockito.mock(SfcProviderRenderedPathAPI.class);
368 PowerMockito.when(api.readRenderedServicePath(rspNameCaptor.capture())).thenReturn(rsp);
370 final RenderedServicePath renderedPath = ServiceChainingUtil.createRenderedPath(sfp, tenantId, dataBroker);
372 Assert.assertEquals("123_plainunit-tennant-01-gbp-rsp", rspNameCaptor.getValue().getValue());
373 Assert.assertEquals(rsp, renderedPath);
377 public void testCreateRenderedPath_notExisting() throws Exception {
378 final String sfcNameValue = "123";
379 final ServiceFunctionPath sfp = createSfp(sfcNameValue, false);
380 final TenantId tenantId = new TenantId("unit-tennant-01");
382 final RenderedServicePath rsp = createRsp("unit-rsp-01");
384 PowerMockito.mockStatic(SfcProviderRenderedPathAPI.class);
385 final SfcProviderRenderedPathAPI api = PowerMockito.mock(SfcProviderRenderedPathAPI.class);
386 PowerMockito.when(api.readRenderedServicePath(rspNameCaptor.capture())).thenReturn(null);
387 PowerMockito.when(api.createRenderedServicePathAndState(
388 sfpCaptor.capture(), createRspCaptor.capture()
390 PowerMockito.mockStatic(NetconfTransactionCreator.class);
391 final NetconfTransactionCreator creator = PowerMockito.mock(NetconfTransactionCreator.class);
392 PowerMockito.when(creator.netconfReadWriteTransaction(dataBroker)).thenReturn(java.util.Optional.empty());
394 final RenderedServicePath renderedPath = ServiceChainingUtil.createRenderedPath(sfp, tenantId, dataBroker);
396 Assert.assertEquals("123_plainunit-tennant-01-gbp-rsp", rspNameCaptor.getValue().getValue());
398 final ServiceFunctionPath serviceFunctionPath = sfpCaptor.getValue();
399 Assert.assertEquals("123_plain", serviceFunctionPath.getName().getValue());
400 Assert.assertFalse(serviceFunctionPath.isSymmetric());
402 final CreateRenderedPathInput createRPInput = createRspCaptor.getValue();
403 Assert.assertFalse(createRPInput.isSymmetric());
404 Assert.assertEquals("123_plain", createRPInput.getParentServiceFunctionPath());
405 Assert.assertEquals("123_plainunit-tennant-01-gbp-rsp", createRPInput.getName());
407 Assert.assertEquals(rsp, renderedPath);
410 private RenderedServicePath createRsp(final String rspNameValue) {
411 return new RenderedServicePathBuilder()
412 .setName(new RspName(rspNameValue))
413 .setRenderedServicePathHop(Lists.newArrayList(createRspHop("rsp-hop-01-sf")))
417 private RenderedServicePathHop createRspHop(final String sfNameValue) {
418 return new RenderedServicePathHopBuilder()
419 .setServiceFunctionName(new SfName(sfNameValue))
420 .setServiceFunctionForwarder(new SffName(sfNameValue + "+sff"))
424 private ServiceFunctionPath createSfp(final String sfcNameValue, final boolean symmetric) {
425 return new ServiceFunctionPathBuilder()
426 .setServiceChainName(new SfcName(sfcNameValue))
427 .setName(new SfpName(sfcNameValue + "_plain"))
428 .setSymmetric(symmetric)
433 public void testCreateSymmetricRenderedPath() throws Exception {
434 final ServiceFunctionPath sfp = createSfp("unit-sfp-02", false);
435 final RenderedServicePath rsp = createRsp("unit-rsp-02");
436 final TenantId tennantId = new TenantId("tenant-02");
438 PowerMockito.mockStatic(SfcProviderRenderedPathAPI.class);
439 final SfcProviderRenderedPathAPI api = PowerMockito.mock(SfcProviderRenderedPathAPI.class);
440 PowerMockito.when(api.readRenderedServicePath(rspNameCaptor.capture())).thenReturn(rsp);
442 final RenderedServicePath symmetricRenderedPath = ServiceChainingUtil.createSymmetricRenderedPath(sfp, rsp, tennantId, dataBroker);
444 Assert.assertEquals("unit-sfp-02_plaintenant-02-gbp-rsp-Reverse", rspNameCaptor.getValue().getValue());
445 Assert.assertEquals(rsp, symmetricRenderedPath);
449 public void testCreateSymmetricRenderedPath_notExisting() throws Exception {
450 final ServiceFunctionPath sfp = createSfp("unit-sfp-02", false);
451 final RenderedServicePath rsp = createRsp("unit-rsp-02");
452 final TenantId tennantId = new TenantId("tenant-02");
454 PowerMockito.mockStatic(SfcProviderRenderedPathAPI.class);
455 final SfcProviderRenderedPathAPI api = PowerMockito.mock(SfcProviderRenderedPathAPI.class);
456 PowerMockito.when(api.readRenderedServicePath(rspNameCaptor.capture())).thenReturn(null);
457 PowerMockito.when(api.createReverseRenderedServicePathEntry(rspCaptor.capture())).thenReturn(rsp);
458 PowerMockito.mockStatic(NetconfTransactionCreator.class);
459 final NetconfTransactionCreator creator = PowerMockito.mock(NetconfTransactionCreator.class);
460 PowerMockito.when(creator.netconfReadWriteTransaction(dataBroker)).thenReturn(java.util.Optional.empty());
462 final RenderedServicePath symmetricRenderedPath = ServiceChainingUtil.createSymmetricRenderedPath(sfp, rsp, tennantId, dataBroker);
464 Assert.assertEquals("unit-sfp-02_plaintenant-02-gbp-rsp-Reverse", rspNameCaptor.getValue().getValue());
465 Assert.assertEquals(rsp, rspCaptor.getValue());
466 Assert.assertEquals(rsp, symmetricRenderedPath);
470 public void testFindServiceFunctionPath() throws Exception {
471 final String sfcNameValue = "123";
472 final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, false);
473 final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
474 .setServiceFunctionPath(Collections.singletonList(sfcPath))
477 stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
479 final ServiceFunctionPath servicePath = ServiceChainingUtil.findServiceFunctionPath(new SfcName(sfcNameValue));
480 Assert.assertEquals(sfcPath, servicePath);
484 public void testSetSfcPart_success() throws Exception {
485 final RenderedServicePath rsp = createRsp("unit-rsp-03");
486 final org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarders.ServiceFunctionForwarder
487 sff = new ServiceFunctionForwarderBuilder()
488 .setName(new SffName("unit-sff-03"))
489 .setIpMgmtAddress(IetfModelCodec.ipAddress2013(new IpAddress(new Ipv4Address("1.2.3.4"))))
491 final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
492 sfpBuilder.setSymmetric(false);
493 final ServiceFunctionPath sfp = sfpBuilder.build();
495 stub(method(ServiceChainingUtil.class, "checkLocalForwarderPresence")).toReturn(true);
497 PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
498 final SfcProviderServiceForwarderAPI api = PowerMockito.mock(SfcProviderServiceForwarderAPI.class);
499 PowerMockito.when(api.readServiceFunctionForwarder(sffNameCaptor.capture())).thenReturn(sff);
501 final boolean outcome = ServiceChainingUtil.setSfcPart(sfp, rsp, null, policyWriter);
503 Assert.assertEquals("rsp-hop-01-sf+sff", sffNameCaptor.getValue().getValue());
504 Assert.assertTrue(outcome);
506 Mockito.verify(policyWriter).cache(Matchers.<ServiceFfName>any());
507 Mockito.verify(policyWriter).cache(Matchers.<ServiceChain>any());
508 Mockito.verify(policyWriter).getCurrentNodeId();
509 Mockito.verify(policyWriter).getCurrentMountpoint();
510 Mockito.verify(policyWriter).getManagementIpAddress();
511 Mockito.verifyNoMoreInteractions(policyWriter);
515 public void testSetSfcPart_success_newRsp() throws Exception {
516 final RenderedServicePath rsp = createRsp("unit-rsp-03");
517 final org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarders.ServiceFunctionForwarder
518 sff = new ServiceFunctionForwarderBuilder()
519 .setName(new SffName("unit-sff-03"))
520 .setIpMgmtAddress(IetfModelCodec.ipAddress2013(new IpAddress(new Ipv4Address("1.2.3.4"))))
522 final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
523 sfpBuilder.setSymmetric(false);
524 final ServiceFunctionPath sfp = sfpBuilder.build();
526 stub(method(ServiceChainingUtil.class, "checkLocalForwarderPresence")).toReturn(false);
528 PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
529 final SfcProviderServiceForwarderAPI api = PowerMockito.mock(SfcProviderServiceForwarderAPI.class);
530 PowerMockito.when(api.readServiceFunctionForwarder(sffNameCaptor.capture())).thenReturn(sff);
532 final boolean outcome = ServiceChainingUtil.setSfcPart(sfp, rsp, null, policyWriter);
534 Assert.assertEquals("rsp-hop-01-sf+sff", sffNameCaptor.getValue().getValue());
535 Assert.assertTrue(outcome);
537 Mockito.verify(policyWriter).cache(Matchers.<Local>any());
538 Mockito.verify(policyWriter).cache(Matchers.<ServiceFfName>any());
539 Mockito.verify(policyWriter).cache(Matchers.<ServiceChain>any());
540 Mockito.verify(policyWriter).getCurrentMountpoint();
541 Mockito.verify(policyWriter, Mockito.times(2)).getManagementIpAddress();
542 Mockito.verifyNoMoreInteractions(policyWriter);
546 public void testSetSfcPart_fail01() throws Exception {
547 final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
548 sfpBuilder.setSymmetric(false);
549 final ServiceFunctionPath sfp = sfpBuilder.build();
551 Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, null, null, policyWriter));
553 final RenderedServicePathBuilder rspBuilder = new RenderedServicePathBuilder().setName(new RspName("unit-rsp-05"));
554 Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, rspBuilder.build(), null, policyWriter));
556 rspBuilder.setRenderedServicePathHop(Collections.emptyList());
557 Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, rspBuilder.build(), null, policyWriter));
559 rspBuilder.setRenderedServicePathHop(Collections.singletonList(null));
560 Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, rspBuilder.build(), null, policyWriter));
564 public void testSetSfcPart_fail02() throws Exception {
565 final RenderedServicePath rsp = createRsp("unit-rsp-03");
566 final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
567 sfpBuilder.setSymmetric(false);
568 final ServiceFunctionPath sfp = sfpBuilder.build();
570 Mockito.doReturn(Futures.immediateCheckedFuture(Optional.absent()))
571 .when(rwTx).read(Mockito.eq(LogicalDatastoreType.CONFIGURATION), Mockito.<InstanceIdentifier<Local>>any());
573 PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
574 final SfcProviderServiceForwarderAPI api = PowerMockito.mock(SfcProviderServiceForwarderAPI.class);
575 PowerMockito.when(api.readServiceFunctionForwarder(sffNameCaptor.capture())).thenReturn(null);
577 final boolean outcome = ServiceChainingUtil.setSfcPart(sfp, rsp, null, policyWriter);
579 Assert.assertEquals("rsp-hop-01-sf+sff", sffNameCaptor.getValue().getValue());
580 Assert.assertFalse(outcome);
582 Mockito.verify(policyWriter).getCurrentMountpoint();
583 Mockito.verify(policyWriter).getManagementIpAddress();
584 Mockito.verify(policyWriter).cache(Matchers.<Local>any());
585 Mockito.verifyNoMoreInteractions(policyWriter);
589 public void testSetSfcPart_fail03() throws Exception {
590 final RenderedServicePath rsp = createRsp("unit-rsp-03");
591 final ServiceFunctionForwarderBuilder sffBuilder = new ServiceFunctionForwarderBuilder()
592 .setName(new SffName("unit-sff-03"))
593 .setIpMgmtAddress(null);
594 final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
595 sfpBuilder.setSymmetric(false);
596 final ServiceFunctionPath sfp = sfpBuilder.build();
598 stub(method(ServiceChainingUtil.class, "checkLocalForwarderPresence")).toReturn(true);
600 PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
601 final SfcProviderServiceForwarderAPI api = PowerMockito.mock(SfcProviderServiceForwarderAPI.class);
602 PowerMockito.when(api.readServiceFunctionForwarder(sffNameCaptor.capture())).thenReturn(
605 Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, rsp, null, policyWriter));
607 Assert.assertEquals("rsp-hop-01-sf+sff", sffNameCaptor.getValue().getValue());
609 Mockito.verify(policyWriter).getCurrentMountpoint();
610 Mockito.verify(policyWriter).getCurrentNodeId();
611 Mockito.verifyNoMoreInteractions(policyWriter);
615 public void testSetSfcPart_fail04() throws Exception {
616 final RenderedServicePath rsp = createRsp("unit-rsp-03");
617 final ServiceFunctionForwarderBuilder sffBuilder = new ServiceFunctionForwarderBuilder()
618 .setName(new SffName("unit-sff-03"))
619 .setIpMgmtAddress(null);
620 final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
621 sfpBuilder.setSymmetric(false);
622 final ServiceFunctionPath sfp = sfpBuilder.build();
624 stub(method(ServiceChainingUtil.class, "checkLocalForwarderPresence")).toReturn(true);
626 PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
627 final SfcProviderServiceForwarderAPI api = PowerMockito.mock(SfcProviderServiceForwarderAPI.class);
628 PowerMockito.when(api.readServiceFunctionForwarder(sffNameCaptor.capture())).thenReturn(
631 Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, rsp, null, policyWriter));
633 Assert.assertEquals("rsp-hop-01-sf+sff", sffNameCaptor.getValue().getValue());
635 Mockito.verify(policyWriter).getCurrentMountpoint();
636 Mockito.verify(policyWriter).getCurrentNodeId();
637 Mockito.verifyNoMoreInteractions(policyWriter);
641 public void testForwarderTypeChoice() throws Exception {
642 final String sffValue = "unit-xx";
643 final ServiceTypeChoice fwChoice = ServiceChainingUtil.forwarderTypeChoice(sffValue);
645 Assert.assertTrue(fwChoice instanceof ServiceFunctionForwarder);
646 final ServiceFunctionForwarder sff = (ServiceFunctionForwarder) fwChoice;
647 Assert.assertEquals(sffValue, sff.getServiceFunctionForwarder());
651 public void testFunctionTypeChoice() throws Exception {
652 final String stcValue = "unit-xx";
653 final ServiceTypeChoice srvTypeChoice = ServiceChainingUtil.functionTypeChoice(stcValue);
655 Assert.assertTrue(srvTypeChoice instanceof ServiceFunction);
656 final ServiceFunction stc = (ServiceFunction) srvTypeChoice;
657 Assert.assertEquals(stcValue, stc.getServiceFunction());