ios-xe renderer bugfix
[groupbasedpolicy.git] / renderers / ios-xe / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ios_xe_provider / impl / util / ServiceChainingUtilTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.util;
10
11 import static org.powermock.api.support.membermodification.MemberMatcher.method;
12 import static org.powermock.api.support.membermodification.MemberModifier.stub;
13
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;
86
87 import java.util.Collections;
88 import java.util.HashMap;
89 import java.util.List;
90 import java.util.Map;
91
92 /**
93  * Test for {@link ServiceChainingUtil}.
94  */
95 @RunWith(PowerMockRunner.class)
96 @PrepareForTest({
97         ServiceChainingUtil.class,
98         SfcProviderServicePathAPI.class,
99         SfcProviderRenderedPathAPI.class,
100         SfcProviderServiceForwarderAPI.class,
101         NetconfTransactionCreator.class
102 })
103 public class ServiceChainingUtilTest {
104
105     @Captor
106     private ArgumentCaptor<RspName> rspNameCaptor;
107     @Captor
108     private ArgumentCaptor<SffName> sffNameCaptor;
109     @Captor
110     private ArgumentCaptor<ServiceFunctionPath> sfpCaptor;
111     @Captor
112     private ArgumentCaptor<CreateRenderedPathInput> createRspCaptor;
113     @Captor
114     private ArgumentCaptor<RenderedServicePath> rspCaptor;
115     @Captor
116     private ArgumentCaptor<List<Class>> listClassCaptor;
117     @Captor
118     private ArgumentCaptor<ClassMap> classMapCaptor;
119     @Mock
120     private PolicyWriter policyWriter;
121     @Mock
122     private DataBroker dataBroker;
123     @Mock
124     private ReadWriteTransaction rwTx;
125
126     private PolicyConfigurationContext policyConfigurationContext;
127
128     @Before
129     public void setUp() throws Exception {
130         final NodeId currentNodeId = new NodeId("unit-node-01");
131         Mockito.when(policyWriter.getCurrentNodeId()).thenReturn(currentNodeId);
132
133         final String managementIpAddress = "1.2.3.5";
134         Mockito.when(policyWriter.getManagementIpAddress()).thenReturn(managementIpAddress);
135
136         Mockito.when(policyWriter.getCurrentMountpoint()).thenReturn(dataBroker);
137         Mockito.when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
138
139         policyConfigurationContext = new PolicyConfigurationContext();
140         policyConfigurationContext.setPolicyWriter(policyWriter);
141     }
142
143     @Test
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))
149                 .build();
150
151         stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
152
153         final ServiceFunctionPath servicePath = ServiceChainingUtil.getServicePath(Lists.newArrayList(
154                 createParameterValue("sfc-chain-name", sfcNameValue)
155         ));
156         Assert.assertEquals(sfcPath, servicePath);
157     }
158
159     private ParameterValue createParameterValue(final String name, final String value) {
160         return new ParameterValueBuilder().setName(new ParameterName(name)).setStringValue(value).build();
161     }
162
163     @Test
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";
170
171         final String sfcNameValue = "123";
172         final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, false);
173         final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
174                 .setServiceFunctionPath(Collections.singletonList(sfcPath))
175                 .build();
176
177         stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
178
179         final RenderedServicePath rsp = createRsp("unit-rsp-02");
180         stub(method(SfcProviderRenderedPathAPI.class, "readRenderedServicePath")).toReturn(rsp);
181         stub(method(ServiceChainingUtil.class, "setSfcPart")).toReturn(true);
182
183         ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap, policyConfigurationContext, dataBroker);
184
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());
189     }
190
191     @Test
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";
198
199         final String sfcNameValue = "123";
200         final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, true);
201         final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
202                 .setServiceFunctionPath(Collections.singletonList(sfcPath))
203                 .build();
204
205         stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
206
207         final RenderedServicePath rsp = createRsp("unit-rsp-02");
208         stub(method(SfcProviderRenderedPathAPI.class, "readRenderedServicePath")).toReturn(rsp);
209         stub(method(ServiceChainingUtil.class, "setSfcPart")).toReturn(true);
210
211         ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap, policyConfigurationContext, dataBroker);
212
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());
217     }
218
219     @Test
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";
226
227         final String sfcNameValue = "123";
228         final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, true);
229         final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
230                 .setServiceFunctionPath(Collections.singletonList(sfcPath))
231                 .build();
232
233         stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
234
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);
239
240         policyConfigurationContext.setCurrentRendererEP(createRendererEP(
241                 "unit-address", new ContextId("unit-conext-1"), Collections.emptyList())
242         );
243
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);
249     }
250
251     @Test
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";
258
259         final String sfcNameValue = "123";
260         final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, false);
261         final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
262                 .setServiceFunctionPath(Collections.singletonList(sfcPath))
263                 .build();
264
265         stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
266
267         final RenderedServicePath rsp = createRsp("unit-rsp-02");
268         stub(method(SfcProviderRenderedPathAPI.class, "readRenderedServicePath")).toReturn(rsp);
269         stub(method(ServiceChainingUtil.class, "setSfcPart")).toReturn(false);
270
271         ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap, policyConfigurationContext, dataBroker);
272
273         Mockito.verifyNoMoreInteractions(policyWriter);
274     }
275
276     @Test
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";
283
284         final String sfcNameValue = "123";
285         final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, false);
286         final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
287                 .setServiceFunctionPath(Collections.singletonList(sfcPath))
288                 .build();
289
290         stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
291
292         final RenderedServicePath rsp = createRsp("unit-rsp-02");
293         stub(method(SfcProviderRenderedPathAPI.class, "readRenderedServicePath")).toReturn(rsp);
294
295         policyConfigurationContext.setCurrentRendererEP(createRendererEP(
296                 "unit-address", new ContextId("unit-conext-1"), Collections.emptyList())
297         );
298
299         ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap, policyConfigurationContext, dataBroker);
300
301         Mockito.verifyNoMoreInteractions(policyWriter);
302     }
303
304     @Test
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";
311
312         policyConfigurationContext.setCurrentRendererEP(createRendererEP(
313                 "unit-address", new ContextId("unit-conext-1"), Collections.emptyList())
314         );
315
316         ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap, policyConfigurationContext, dataBroker);
317
318         Mockito.verifyNoMoreInteractions(policyWriter);
319     }
320
321     private Map<PolicyManagerImpl.ActionCase, Action> createActionMap() {
322         return createActionMap(true);
323     }
324
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")
332                     .build()));
333         }
334         actionMap.put(PolicyManagerImpl.ActionCase.CHAIN, actionValue.build());
335         return actionMap;
336     }
337
338     private RendererEndpoint createRendererEP(final String address, final ContextId contextId, final List<PeerEndpoint> peerEndpoints) {
339         return new RendererEndpointBuilder()
340                 .setAddress(address)
341                 .setAddressType(IpPrefixType.class)
342                 .setContextId(contextId)
343                 .setContextType(L3Context.class)
344                 .setPeerEndpoint(peerEndpoints)
345                 .build();
346     }
347
348     private PeerEndpoint createPeerEndpoint() {
349         return createPeerEndpoint(new TenantId("unit-tenant-06"));
350     }
351
352     private PeerEndpoint createPeerEndpoint(final TenantId tenantId) {
353         return new PeerEndpointBuilder()
354                 .setRuleGroupWithRendererEndpointParticipation(Collections.singletonList(
355                         new RuleGroupWithRendererEndpointParticipationBuilder()
356                                 .setTenantId(tenantId)
357                                 .build()
358                 ))
359                 .build();
360     }
361
362     @Test
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");
367
368         final RenderedServicePath rsp = createRsp("unit-rsp-01");
369
370         PowerMockito.mockStatic(SfcProviderRenderedPathAPI.class);
371         final SfcProviderRenderedPathAPI api = PowerMockito.mock(SfcProviderRenderedPathAPI.class);
372         PowerMockito.when(api.readRenderedServicePath(rspNameCaptor.capture())).thenReturn(rsp);
373
374         final RenderedServicePath renderedPath = ServiceChainingUtil.createRenderedPath(sfp, tenantId, dataBroker);
375
376         Assert.assertEquals("123_plainunit-tennant-01-gbp-rsp", rspNameCaptor.getValue().getValue());
377         Assert.assertEquals(rsp, renderedPath);
378     }
379
380     @Test
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");
385
386         final RenderedServicePath rsp = createRsp("unit-rsp-01");
387
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()
393         )).thenReturn(rsp);
394         PowerMockito.mockStatic(NetconfTransactionCreator.class);
395         final NetconfTransactionCreator creator = PowerMockito.mock(NetconfTransactionCreator.class);
396         PowerMockito.when(creator.netconfReadWriteTransaction(dataBroker)).thenReturn(java.util.Optional.empty());
397
398         final RenderedServicePath renderedPath = ServiceChainingUtil.createRenderedPath(sfp, tenantId, dataBroker);
399
400         Assert.assertEquals("123_plainunit-tennant-01-gbp-rsp", rspNameCaptor.getValue().getValue());
401
402         final ServiceFunctionPath serviceFunctionPath = sfpCaptor.getValue();
403         Assert.assertEquals("123_plain", serviceFunctionPath.getName().getValue());
404         Assert.assertFalse(serviceFunctionPath.isSymmetric());
405
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());
410
411         Assert.assertEquals(rsp, renderedPath);
412     }
413
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")))
418                 .build();
419     }
420
421     private RenderedServicePathHop createRspHop(final String sfNameValue) {
422         return new RenderedServicePathHopBuilder()
423                 .setServiceFunctionName(new SfName(sfNameValue))
424                 .setServiceFunctionForwarder(new SffName(sfNameValue + "+sff"))
425                 .build();
426     }
427
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)
433                 .build();
434     }
435
436     @Test
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");
441
442         PowerMockito.mockStatic(SfcProviderRenderedPathAPI.class);
443         final SfcProviderRenderedPathAPI api = PowerMockito.mock(SfcProviderRenderedPathAPI.class);
444         PowerMockito.when(api.readRenderedServicePath(rspNameCaptor.capture())).thenReturn(rsp);
445
446         final RenderedServicePath symmetricRenderedPath = ServiceChainingUtil.createSymmetricRenderedPath(sfp, rsp, tennantId, dataBroker);
447
448         Assert.assertEquals("unit-sfp-02_plaintenant-02-gbp-rsp-Reverse", rspNameCaptor.getValue().getValue());
449         Assert.assertEquals(rsp, symmetricRenderedPath);
450     }
451
452     @Test
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");
457
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());
465
466         final RenderedServicePath symmetricRenderedPath = ServiceChainingUtil.createSymmetricRenderedPath(sfp, rsp, tennantId, dataBroker);
467
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);
471     }
472
473     @Test
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))
479                 .build();
480
481         stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
482
483         final ServiceFunctionPath servicePath = ServiceChainingUtil.findServiceFunctionPath(new SfcName(sfcNameValue));
484         Assert.assertEquals(sfcPath, servicePath);
485     }
486
487     @Test
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()))
501                 .build();
502         final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
503         sfpBuilder.setSymmetric(false);
504         final ServiceFunctionPath sfp = sfpBuilder.build();
505
506         PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
507         final SfcProviderServiceForwarderAPI api = PowerMockito.mock(SfcProviderServiceForwarderAPI.class);
508         PowerMockito.when(api.readServiceFunctionForwarder(sffNameCaptor.capture())).thenReturn(sff);
509
510         final boolean outcome = ServiceChainingUtil.setSfcPart(sfp, rsp, null, policyWriter);
511
512         Assert.assertEquals("rsp-hop-01-sf+sff", sffNameCaptor.getValue().getValue());
513         Assert.assertTrue(outcome);
514
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);
519     }
520
521     @Test
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()))
535                 .build();
536         final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
537         sfpBuilder.setSymmetric(false);
538         final ServiceFunctionPath sfp = sfpBuilder.build();
539
540         PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
541         final SfcProviderServiceForwarderAPI api = PowerMockito.mock(SfcProviderServiceForwarderAPI.class);
542         PowerMockito.when(api.readServiceFunctionForwarder(sffNameCaptor.capture())).thenReturn(sff);
543
544         final boolean outcome = ServiceChainingUtil.setSfcPart(sfp, rsp, null, policyWriter);
545
546         Assert.assertEquals("rsp-hop-01-sf+sff", sffNameCaptor.getValue().getValue());
547         Assert.assertTrue(outcome);
548
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);
553     }
554
555     @Test
556     public void testSetSfcPart_fail01() throws Exception {
557         final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
558         sfpBuilder.setSymmetric(false);
559         final ServiceFunctionPath sfp = sfpBuilder.build();
560
561         Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, null, null, policyWriter));
562
563         final RenderedServicePathBuilder rspBuilder = new RenderedServicePathBuilder().setName(new RspName("unit-rsp-05"));
564         Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, rspBuilder.build(), null, policyWriter));
565
566         rspBuilder.setRenderedServicePathHop(Collections.emptyList());
567         Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, rspBuilder.build(), null, policyWriter));
568
569         rspBuilder.setRenderedServicePathHop(Collections.singletonList(null));
570         Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, rspBuilder.build(), null, policyWriter));
571     }
572
573     @Test
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();
579
580         Mockito.doReturn(Futures.immediateCheckedFuture(Optional.absent()))
581                 .when(rwTx).read(Mockito.eq(LogicalDatastoreType.CONFIGURATION), Mockito.<InstanceIdentifier<Local>>any());
582
583         PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
584         final SfcProviderServiceForwarderAPI api = PowerMockito.mock(SfcProviderServiceForwarderAPI.class);
585         PowerMockito.when(api.readServiceFunctionForwarder(sffNameCaptor.capture())).thenReturn(null);
586
587         final boolean outcome = ServiceChainingUtil.setSfcPart(sfp, rsp, null, policyWriter);
588
589         Assert.assertEquals("rsp-hop-01-sf+sff", sffNameCaptor.getValue().getValue());
590         Assert.assertFalse(outcome);
591
592         Mockito.verifyNoMoreInteractions(policyWriter);
593     }
594
595     @Test
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();
604
605         PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
606         final SfcProviderServiceForwarderAPI api = PowerMockito.mock(SfcProviderServiceForwarderAPI.class);
607         PowerMockito.when(api.readServiceFunctionForwarder(sffNameCaptor.capture())).thenReturn(
608                 sffBuilder.build());
609
610         Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, rsp, null, policyWriter));
611
612         Assert.assertEquals("rsp-hop-01-sf+sff", sffNameCaptor.getValue().getValue());
613
614         Mockito.verifyNoMoreInteractions(policyWriter);
615     }
616
617     @Test
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();
626
627         PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
628         final SfcProviderServiceForwarderAPI api = PowerMockito.mock(SfcProviderServiceForwarderAPI.class);
629         PowerMockito.when(api.readServiceFunctionForwarder(sffNameCaptor.capture())).thenReturn(
630                 sffBuilder.build());
631
632         Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, rsp, null, policyWriter));
633
634         Assert.assertEquals("rsp-hop-01-sf+sff", sffNameCaptor.getValue().getValue());
635
636         Mockito.verifyNoMoreInteractions(policyWriter);
637     }
638
639     @Test
640     public void testForwarderTypeChoice() throws Exception {
641         final String sffValue = "unit-xx";
642         final ServiceTypeChoice fwChoice = ServiceChainingUtil.forwarderTypeChoice(sffValue);
643
644         Assert.assertTrue(fwChoice instanceof ServiceFunctionForwarder);
645         final ServiceFunctionForwarder sff = (ServiceFunctionForwarder) fwChoice;
646         Assert.assertEquals(sffValue, sff.getServiceFunctionForwarder());
647     }
648
649     @Test
650     public void testFunctionTypeChoice() throws Exception {
651         final String stcValue = "unit-xx";
652         final ServiceTypeChoice srvTypeChoice = ServiceChainingUtil.functionTypeChoice(stcValue);
653
654         Assert.assertTrue(srvTypeChoice instanceof ServiceFunction);
655         final ServiceFunction stc = (ServiceFunction) srvTypeChoice;
656         Assert.assertEquals(stcValue, stc.getServiceFunction());
657     }
658 }