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 org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.ArgumentCaptor;
22 import org.mockito.Captor;
23 import org.mockito.Matchers;
24 import org.mockito.Mock;
25 import org.mockito.Mockito;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.groupbasedpolicy.api.sf.ChainActionDefinition;
30 import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.manager.PolicyConfigurationContext;
31 import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.manager.PolicyManagerImpl;
32 import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.writer.NetconfTransactionCreator;
33 import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.writer.PolicyWriter;
34 import org.opendaylight.groupbasedpolicy.util.IetfModelCodec;
35 import org.opendaylight.sfc.provider.api.SfcProviderRenderedPathAPI;
36 import org.opendaylight.sfc.provider.api.SfcProviderServiceForwarderAPI;
37 import org.opendaylight.sfc.provider.api.SfcProviderServicePathAPI;
38 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.RspName;
39 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfName;
40 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfcName;
41 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SffName;
42 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfpName;
43 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.CreateRenderedPathInput;
44 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.RenderedServicePath;
45 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.RenderedServicePathBuilder;
46 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.rendered.service.path.RenderedServicePathHop;
47 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.rendered.service.path.RenderedServicePathHopBuilder;
48 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarder.base.SffDataPlaneLocatorBuilder;
49 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarder.base.sff.data.plane.locator.DataPlaneLocatorBuilder;
50 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarders.ServiceFunctionForwarderBuilder;
51 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.ServiceFunctionPaths;
52 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.ServiceFunctionPathsBuilder;
53 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPath;
54 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPathBuilder;
55 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sl.rev140701.data.plane.locator.locator.type.IpBuilder;
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;
87 import java.util.Collections;
88 import java.util.HashMap;
89 import java.util.List;
93 * Test for {@link ServiceChainingUtil}.
95 @RunWith(PowerMockRunner.class)
97 ServiceChainingUtil.class,
98 SfcProviderServicePathAPI.class,
99 SfcProviderRenderedPathAPI.class,
100 SfcProviderServiceForwarderAPI.class,
101 NetconfTransactionCreator.class
103 public class ServiceChainingUtilTest {
106 private ArgumentCaptor<RspName> rspNameCaptor;
108 private ArgumentCaptor<SffName> sffNameCaptor;
110 private ArgumentCaptor<ServiceFunctionPath> sfpCaptor;
112 private ArgumentCaptor<CreateRenderedPathInput> createRspCaptor;
114 private ArgumentCaptor<RenderedServicePath> rspCaptor;
116 private ArgumentCaptor<List<Class>> listClassCaptor;
118 private ArgumentCaptor<ClassMap> classMapCaptor;
120 private PolicyWriter policyWriter;
122 private DataBroker dataBroker;
124 private ReadWriteTransaction rwTx;
126 private PolicyConfigurationContext policyConfigurationContext;
129 public void setUp() throws Exception {
130 final NodeId currentNodeId = new NodeId("unit-node-01");
131 Mockito.when(policyWriter.getCurrentNodeId()).thenReturn(currentNodeId);
133 final String managementIpAddress = "1.2.3.5";
134 Mockito.when(policyWriter.getManagementIpAddress()).thenReturn(managementIpAddress);
136 Mockito.when(policyWriter.getCurrentMountpoint()).thenReturn(dataBroker);
137 Mockito.when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
139 policyConfigurationContext = new PolicyConfigurationContext();
140 policyConfigurationContext.setPolicyWriter(policyWriter);
144 public void testGetServicePath() throws Exception {
145 final String sfcNameValue = "123";
146 final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, false);
147 final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
148 .setServiceFunctionPath(Collections.singletonList(sfcPath))
151 stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
153 final ServiceFunctionPath servicePath = ServiceChainingUtil.getServicePath(Lists.newArrayList(
154 createParameterValue("sfc-chain-name", sfcNameValue)
156 Assert.assertEquals(sfcPath, servicePath);
159 private ParameterValue createParameterValue(final String name, final String value) {
160 return new ParameterValueBuilder().setName(new ParameterName(name)).setStringValue(value).build();
164 public void testResolveChainAction_full() throws Exception {
165 final PeerEndpoint peerEndpoint = createPeerEndpoint();
166 final Sgt sourceSgt = new Sgt(1);
167 final Sgt destinationSgt = new Sgt(2);
168 final Map<PolicyManagerImpl.ActionCase, Action> actionMap = createActionMap();
169 final String classMapName = "unit-class-map-name-01";
171 final String sfcNameValue = "123";
172 final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, false);
173 final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
174 .setServiceFunctionPath(Collections.singletonList(sfcPath))
177 stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
179 final RenderedServicePath rsp = createRsp("unit-rsp-02");
180 stub(method(SfcProviderRenderedPathAPI.class, "readRenderedServicePath")).toReturn(rsp);
181 stub(method(ServiceChainingUtil.class, "setSfcPart")).toReturn(true);
183 ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap, policyConfigurationContext, dataBroker);
185 Mockito.verify(policyWriter).cache(classMapCaptor.capture());
186 Mockito.verify(policyWriter).cache(listClassCaptor.capture());
187 Mockito.verifyNoMoreInteractions(policyWriter);
188 Assert.assertEquals(1, listClassCaptor.getValue().size());
192 public void testResolveChainAction_fullSymmetric() throws Exception {
193 final PeerEndpoint peerEndpoint = createPeerEndpoint();
194 final Sgt sourceSgt = new Sgt(1);
195 final Sgt destinationSgt = new Sgt(2);
196 final Map<PolicyManagerImpl.ActionCase, Action> actionMap = createActionMap();
197 final String classMapName = "unit-class-map-name-01";
199 final String sfcNameValue = "123";
200 final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, true);
201 final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
202 .setServiceFunctionPath(Collections.singletonList(sfcPath))
205 stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
207 final RenderedServicePath rsp = createRsp("unit-rsp-02");
208 stub(method(SfcProviderRenderedPathAPI.class, "readRenderedServicePath")).toReturn(rsp);
209 stub(method(ServiceChainingUtil.class, "setSfcPart")).toReturn(true);
211 ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap, policyConfigurationContext, dataBroker);
213 Mockito.verify(policyWriter, Mockito.times(2)).cache(classMapCaptor.capture());
214 Mockito.verify(policyWriter).cache(listClassCaptor.capture());
215 Mockito.verifyNoMoreInteractions(policyWriter);
216 Assert.assertEquals(2, listClassCaptor.getValue().size());
220 public void testResolveChainAction_partial01() throws Exception {
221 final PeerEndpoint peerEndpoint = createPeerEndpoint();
222 final Sgt sourceSgt = new Sgt(1);
223 final Sgt destinationSgt = new Sgt(2);
224 final Map<PolicyManagerImpl.ActionCase, Action> actionMap = createActionMap();
225 final String classMapName = "unit-class-map-name-01";
227 final String sfcNameValue = "123";
228 final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, true);
229 final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
230 .setServiceFunctionPath(Collections.singletonList(sfcPath))
233 stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
235 final RenderedServicePath rsp = createRsp("unit-rsp-02");
236 stub(method(SfcProviderRenderedPathAPI.class, "readRenderedServicePath")).toReturn(rsp);
237 stub(method(ServiceChainingUtil.class, "setSfcPart")).toReturn(true);
238 stub(method(ServiceChainingUtil.class, "createSymmetricRenderedPath")).toReturn(null);
240 policyConfigurationContext.setCurrentRendererEP(createRendererEP(
241 "unit-address", new ContextId("unit-conext-1"), Collections.emptyList())
244 ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap,
245 policyConfigurationContext, dataBroker);
246 Mockito.verify(policyWriter, Mockito.times(2)).cache(classMapCaptor.capture());
247 Mockito.verify(policyWriter).cache(listClassCaptor.capture());
248 Mockito.verifyNoMoreInteractions(policyWriter);
252 public void testResolveChainAction_partial02() throws Exception {
253 final PeerEndpoint peerEndpoint = createPeerEndpoint();
254 final Sgt sourceSgt = new Sgt(1);
255 final Sgt destinationSgt = new Sgt(2);
256 final Map<PolicyManagerImpl.ActionCase, Action> actionMap = createActionMap();
257 final String classMapName = "unit-class-map-name-01";
259 final String sfcNameValue = "123";
260 final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, false);
261 final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
262 .setServiceFunctionPath(Collections.singletonList(sfcPath))
265 stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
267 final RenderedServicePath rsp = createRsp("unit-rsp-02");
268 stub(method(SfcProviderRenderedPathAPI.class, "readRenderedServicePath")).toReturn(rsp);
269 stub(method(ServiceChainingUtil.class, "setSfcPart")).toReturn(false);
271 ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap, policyConfigurationContext, dataBroker);
273 Mockito.verifyNoMoreInteractions(policyWriter);
277 public void testResolveChainAction_partial03() throws Exception {
278 final PeerEndpoint peerEndpoint = createPeerEndpoint(null);
279 final Sgt sourceSgt = new Sgt(1);
280 final Sgt destinationSgt = new Sgt(2);
281 final Map<PolicyManagerImpl.ActionCase, Action> actionMap = createActionMap();
282 final String classMapName = "unit-class-map-name-01";
284 final String sfcNameValue = "123";
285 final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, false);
286 final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
287 .setServiceFunctionPath(Collections.singletonList(sfcPath))
290 stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
292 final RenderedServicePath rsp = createRsp("unit-rsp-02");
293 stub(method(SfcProviderRenderedPathAPI.class, "readRenderedServicePath")).toReturn(rsp);
295 policyConfigurationContext.setCurrentRendererEP(createRendererEP(
296 "unit-address", new ContextId("unit-conext-1"), Collections.emptyList())
299 ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap, policyConfigurationContext, dataBroker);
301 Mockito.verifyNoMoreInteractions(policyWriter);
305 public void testResolveChainAction_partial04() throws Exception {
306 final PeerEndpoint peerEndpoint = createPeerEndpoint(null);
307 final Sgt sourceSgt = new Sgt(1);
308 final Sgt destinationSgt = new Sgt(2);
309 final Map<PolicyManagerImpl.ActionCase, Action> actionMap = createActionMap(false);
310 final String classMapName = "unit-class-map-name-01";
312 policyConfigurationContext.setCurrentRendererEP(createRendererEP(
313 "unit-address", new ContextId("unit-conext-1"), Collections.emptyList())
316 ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap, policyConfigurationContext, dataBroker);
318 Mockito.verifyNoMoreInteractions(policyWriter);
321 private Map<PolicyManagerImpl.ActionCase, Action> createActionMap() {
322 return createActionMap(true);
325 private Map<PolicyManagerImpl.ActionCase, Action> createActionMap(final boolean fillParamValue) {
326 final Map<PolicyManagerImpl.ActionCase,Action> actionMap = new HashMap<>();
327 final ActionBuilder actionValue = new ActionBuilder();
328 if (fillParamValue) {
329 actionValue.setParameterValue(Collections.singletonList(new ParameterValueBuilder()
330 .setName(new ParameterName(ChainActionDefinition.SFC_CHAIN_NAME))
331 .setStringValue("123")
334 actionMap.put(PolicyManagerImpl.ActionCase.CHAIN, actionValue.build());
338 private RendererEndpoint createRendererEP(final String address, final ContextId contextId, final List<PeerEndpoint> peerEndpoints) {
339 return new RendererEndpointBuilder()
341 .setAddressType(IpPrefixType.class)
342 .setContextId(contextId)
343 .setContextType(L3Context.class)
344 .setPeerEndpoint(peerEndpoints)
348 private PeerEndpoint createPeerEndpoint() {
349 return createPeerEndpoint(new TenantId("unit-tenant-06"));
352 private PeerEndpoint createPeerEndpoint(final TenantId tenantId) {
353 return new PeerEndpointBuilder()
354 .setRuleGroupWithRendererEndpointParticipation(Collections.singletonList(
355 new RuleGroupWithRendererEndpointParticipationBuilder()
356 .setTenantId(tenantId)
363 public void testCreateRenderedPath() throws Exception {
364 final String sfcNameValue = "123";
365 final ServiceFunctionPath sfp = createSfp(sfcNameValue, false);
366 final TenantId tenantId = new TenantId("unit-tennant-01");
368 final RenderedServicePath rsp = createRsp("unit-rsp-01");
370 PowerMockito.mockStatic(SfcProviderRenderedPathAPI.class);
371 final SfcProviderRenderedPathAPI api = PowerMockito.mock(SfcProviderRenderedPathAPI.class);
372 PowerMockito.when(api.readRenderedServicePath(rspNameCaptor.capture())).thenReturn(rsp);
374 final RenderedServicePath renderedPath = ServiceChainingUtil.createRenderedPath(sfp, tenantId, dataBroker);
376 Assert.assertEquals("123_plainunit-tennant-01-gbp-rsp", rspNameCaptor.getValue().getValue());
377 Assert.assertEquals(rsp, renderedPath);
381 public void testCreateRenderedPath_notExisting() throws Exception {
382 final String sfcNameValue = "123";
383 final ServiceFunctionPath sfp = createSfp(sfcNameValue, false);
384 final TenantId tenantId = new TenantId("unit-tennant-01");
386 final RenderedServicePath rsp = createRsp("unit-rsp-01");
388 PowerMockito.mockStatic(SfcProviderRenderedPathAPI.class);
389 final SfcProviderRenderedPathAPI api = PowerMockito.mock(SfcProviderRenderedPathAPI.class);
390 PowerMockito.when(api.readRenderedServicePath(rspNameCaptor.capture())).thenReturn(null);
391 PowerMockito.when(api.createRenderedServicePathAndState(
392 sfpCaptor.capture(), createRspCaptor.capture()
394 PowerMockito.mockStatic(NetconfTransactionCreator.class);
395 final NetconfTransactionCreator creator = PowerMockito.mock(NetconfTransactionCreator.class);
396 PowerMockito.when(creator.netconfReadWriteTransaction(dataBroker)).thenReturn(java.util.Optional.empty());
398 final RenderedServicePath renderedPath = ServiceChainingUtil.createRenderedPath(sfp, tenantId, dataBroker);
400 Assert.assertEquals("123_plainunit-tennant-01-gbp-rsp", rspNameCaptor.getValue().getValue());
402 final ServiceFunctionPath serviceFunctionPath = sfpCaptor.getValue();
403 Assert.assertEquals("123_plain", serviceFunctionPath.getName().getValue());
404 Assert.assertFalse(serviceFunctionPath.isSymmetric());
406 final CreateRenderedPathInput createRPInput = createRspCaptor.getValue();
407 Assert.assertFalse(createRPInput.isSymmetric());
408 Assert.assertEquals("123_plain", createRPInput.getParentServiceFunctionPath());
409 Assert.assertEquals("123_plainunit-tennant-01-gbp-rsp", createRPInput.getName());
411 Assert.assertEquals(rsp, renderedPath);
414 private RenderedServicePath createRsp(final String rspNameValue) {
415 return new RenderedServicePathBuilder()
416 .setName(new RspName(rspNameValue))
417 .setRenderedServicePathHop(Lists.newArrayList(createRspHop("rsp-hop-01-sf")))
421 private RenderedServicePathHop createRspHop(final String sfNameValue) {
422 return new RenderedServicePathHopBuilder()
423 .setServiceFunctionName(new SfName(sfNameValue))
424 .setServiceFunctionForwarder(new SffName(sfNameValue + "+sff"))
428 private ServiceFunctionPath createSfp(final String sfcNameValue, final boolean symmetric) {
429 return new ServiceFunctionPathBuilder()
430 .setServiceChainName(new SfcName(sfcNameValue))
431 .setName(new SfpName(sfcNameValue + "_plain"))
432 .setSymmetric(symmetric)
437 public void testCreateSymmetricRenderedPath() throws Exception {
438 final ServiceFunctionPath sfp = createSfp("unit-sfp-02", false);
439 final RenderedServicePath rsp = createRsp("unit-rsp-02");
440 final TenantId tennantId = new TenantId("tenant-02");
442 PowerMockito.mockStatic(SfcProviderRenderedPathAPI.class);
443 final SfcProviderRenderedPathAPI api = PowerMockito.mock(SfcProviderRenderedPathAPI.class);
444 PowerMockito.when(api.readRenderedServicePath(rspNameCaptor.capture())).thenReturn(rsp);
446 final RenderedServicePath symmetricRenderedPath = ServiceChainingUtil.createSymmetricRenderedPath(sfp, rsp, tennantId, dataBroker);
448 Assert.assertEquals("unit-sfp-02_plaintenant-02-gbp-rsp-Reverse", rspNameCaptor.getValue().getValue());
449 Assert.assertEquals(rsp, symmetricRenderedPath);
453 public void testCreateSymmetricRenderedPath_notExisting() throws Exception {
454 final ServiceFunctionPath sfp = createSfp("unit-sfp-02", false);
455 final RenderedServicePath rsp = createRsp("unit-rsp-02");
456 final TenantId tennantId = new TenantId("tenant-02");
458 PowerMockito.mockStatic(SfcProviderRenderedPathAPI.class);
459 final SfcProviderRenderedPathAPI api = PowerMockito.mock(SfcProviderRenderedPathAPI.class);
460 PowerMockito.when(api.readRenderedServicePath(rspNameCaptor.capture())).thenReturn(null);
461 PowerMockito.when(api.createReverseRenderedServicePathEntry(rspCaptor.capture())).thenReturn(rsp);
462 PowerMockito.mockStatic(NetconfTransactionCreator.class);
463 final NetconfTransactionCreator creator = PowerMockito.mock(NetconfTransactionCreator.class);
464 PowerMockito.when(creator.netconfReadWriteTransaction(dataBroker)).thenReturn(java.util.Optional.empty());
466 final RenderedServicePath symmetricRenderedPath = ServiceChainingUtil.createSymmetricRenderedPath(sfp, rsp, tennantId, dataBroker);
468 Assert.assertEquals("unit-sfp-02_plaintenant-02-gbp-rsp-Reverse", rspNameCaptor.getValue().getValue());
469 Assert.assertEquals(rsp, rspCaptor.getValue());
470 Assert.assertEquals(rsp, symmetricRenderedPath);
474 public void testFindServiceFunctionPath() throws Exception {
475 final String sfcNameValue = "123";
476 final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, false);
477 final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
478 .setServiceFunctionPath(Collections.singletonList(sfcPath))
481 stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
483 final ServiceFunctionPath servicePath = ServiceChainingUtil.findServiceFunctionPath(new SfcName(sfcNameValue));
484 Assert.assertEquals(sfcPath, servicePath);
488 public void testSetSfcPart_success() throws Exception {
489 final RenderedServicePath rsp = createRsp("unit-rsp-03");
490 final DataPlaneLocatorBuilder dataPlaneLocatorBuilder = new DataPlaneLocatorBuilder();
491 final IpBuilder ipBuilderLocatorType = new IpBuilder();
492 ipBuilderLocatorType.setIp(IetfModelCodec.ipAddress2013(new IpAddress(new Ipv4Address("1.2.3.4"))));
493 dataPlaneLocatorBuilder.setLocatorType(ipBuilderLocatorType.build());
494 final SffDataPlaneLocatorBuilder sffDataPlaneLocatorBuilder = new SffDataPlaneLocatorBuilder();
495 sffDataPlaneLocatorBuilder.setDataPlaneLocator(dataPlaneLocatorBuilder.build());
496 final org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarders.ServiceFunctionForwarder
497 sff = new ServiceFunctionForwarderBuilder()
498 .setName(new SffName("unit-sff-03"))
499 .setIpMgmtAddress(IetfModelCodec.ipAddress2013(new IpAddress(new Ipv4Address("1.2.3.4"))))
500 .setSffDataPlaneLocator(Collections.singletonList(sffDataPlaneLocatorBuilder.build()))
502 final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
503 sfpBuilder.setSymmetric(false);
504 final ServiceFunctionPath sfp = sfpBuilder.build();
506 PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
507 final SfcProviderServiceForwarderAPI api = PowerMockito.mock(SfcProviderServiceForwarderAPI.class);
508 PowerMockito.when(api.readServiceFunctionForwarder(sffNameCaptor.capture())).thenReturn(sff);
510 final boolean outcome = ServiceChainingUtil.setSfcPart(sfp, rsp, null, policyWriter);
512 Assert.assertEquals("rsp-hop-01-sf+sff", sffNameCaptor.getValue().getValue());
513 Assert.assertTrue(outcome);
515 Mockito.verify(policyWriter).cache(Matchers.<ServiceFfName>any());
516 Mockito.verify(policyWriter).cache(Matchers.<ServiceChain>any());
517 Mockito.verify(policyWriter).getManagementIpAddress();
518 Mockito.verifyNoMoreInteractions(policyWriter);
522 public void testSetSfcPart_success_newRsp() throws Exception {
523 final RenderedServicePath rsp = createRsp("unit-rsp-03");
524 final DataPlaneLocatorBuilder dataPlaneLocatorBuilder = new DataPlaneLocatorBuilder();
525 final IpBuilder ipBuilderLocatorType = new IpBuilder();
526 ipBuilderLocatorType.setIp(IetfModelCodec.ipAddress2013(new IpAddress(new Ipv4Address("1.2.3.4"))));
527 dataPlaneLocatorBuilder.setLocatorType(ipBuilderLocatorType.build());
528 final SffDataPlaneLocatorBuilder sffDataPlaneLocatorBuilder = new SffDataPlaneLocatorBuilder();
529 sffDataPlaneLocatorBuilder.setDataPlaneLocator(dataPlaneLocatorBuilder.build());
530 final org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarders.ServiceFunctionForwarder
531 sff = new ServiceFunctionForwarderBuilder()
532 .setName(new SffName("unit-sff-03"))
533 .setIpMgmtAddress(IetfModelCodec.ipAddress2013(new IpAddress(new Ipv4Address("1.2.3.4"))))
534 .setSffDataPlaneLocator(Collections.singletonList(sffDataPlaneLocatorBuilder.build()))
536 final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
537 sfpBuilder.setSymmetric(false);
538 final ServiceFunctionPath sfp = sfpBuilder.build();
540 PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
541 final SfcProviderServiceForwarderAPI api = PowerMockito.mock(SfcProviderServiceForwarderAPI.class);
542 PowerMockito.when(api.readServiceFunctionForwarder(sffNameCaptor.capture())).thenReturn(sff);
544 final boolean outcome = ServiceChainingUtil.setSfcPart(sfp, rsp, null, policyWriter);
546 Assert.assertEquals("rsp-hop-01-sf+sff", sffNameCaptor.getValue().getValue());
547 Assert.assertTrue(outcome);
549 Mockito.verify(policyWriter).cache(Matchers.<ServiceFfName>any());
550 Mockito.verify(policyWriter).cache(Matchers.<ServiceChain>any());
551 Mockito.verify(policyWriter).getManagementIpAddress();
552 Mockito.verifyNoMoreInteractions(policyWriter);
556 public void testSetSfcPart_fail01() throws Exception {
557 final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
558 sfpBuilder.setSymmetric(false);
559 final ServiceFunctionPath sfp = sfpBuilder.build();
561 Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, null, null, policyWriter));
563 final RenderedServicePathBuilder rspBuilder = new RenderedServicePathBuilder().setName(new RspName("unit-rsp-05"));
564 Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, rspBuilder.build(), null, policyWriter));
566 rspBuilder.setRenderedServicePathHop(Collections.emptyList());
567 Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, rspBuilder.build(), null, policyWriter));
569 rspBuilder.setRenderedServicePathHop(Collections.singletonList(null));
570 Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, rspBuilder.build(), null, policyWriter));
574 public void testSetSfcPart_fail02() throws Exception {
575 final RenderedServicePath rsp = createRsp("unit-rsp-03");
576 final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
577 sfpBuilder.setSymmetric(false);
578 final ServiceFunctionPath sfp = sfpBuilder.build();
580 Mockito.doReturn(Futures.immediateCheckedFuture(Optional.absent()))
581 .when(rwTx).read(Mockito.eq(LogicalDatastoreType.CONFIGURATION), Mockito.<InstanceIdentifier<Local>>any());
583 PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
584 final SfcProviderServiceForwarderAPI api = PowerMockito.mock(SfcProviderServiceForwarderAPI.class);
585 PowerMockito.when(api.readServiceFunctionForwarder(sffNameCaptor.capture())).thenReturn(null);
587 final boolean outcome = ServiceChainingUtil.setSfcPart(sfp, rsp, null, policyWriter);
589 Assert.assertEquals("rsp-hop-01-sf+sff", sffNameCaptor.getValue().getValue());
590 Assert.assertFalse(outcome);
592 Mockito.verifyNoMoreInteractions(policyWriter);
596 public void testSetSfcPart_fail03() throws Exception {
597 final RenderedServicePath rsp = createRsp("unit-rsp-03");
598 final ServiceFunctionForwarderBuilder sffBuilder = new ServiceFunctionForwarderBuilder()
599 .setName(new SffName("unit-sff-03"))
600 .setIpMgmtAddress(null);
601 final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
602 sfpBuilder.setSymmetric(false);
603 final ServiceFunctionPath sfp = sfpBuilder.build();
605 PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
606 final SfcProviderServiceForwarderAPI api = PowerMockito.mock(SfcProviderServiceForwarderAPI.class);
607 PowerMockito.when(api.readServiceFunctionForwarder(sffNameCaptor.capture())).thenReturn(
610 Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, rsp, null, policyWriter));
612 Assert.assertEquals("rsp-hop-01-sf+sff", sffNameCaptor.getValue().getValue());
614 Mockito.verifyNoMoreInteractions(policyWriter);
618 public void testSetSfcPart_fail04() throws Exception {
619 final RenderedServicePath rsp = createRsp("unit-rsp-03");
620 final ServiceFunctionForwarderBuilder sffBuilder = new ServiceFunctionForwarderBuilder()
621 .setName(new SffName("unit-sff-03"))
622 .setIpMgmtAddress(null);
623 final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
624 sfpBuilder.setSymmetric(false);
625 final ServiceFunctionPath sfp = sfpBuilder.build();
627 PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
628 final SfcProviderServiceForwarderAPI api = PowerMockito.mock(SfcProviderServiceForwarderAPI.class);
629 PowerMockito.when(api.readServiceFunctionForwarder(sffNameCaptor.capture())).thenReturn(
632 Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, rsp, null, policyWriter));
634 Assert.assertEquals("rsp-hop-01-sf+sff", sffNameCaptor.getValue().getValue());
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());