GBP UI build fix
[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 java.util.Collections;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.ArgumentCaptor;
26 import org.mockito.Captor;
27 import org.mockito.Matchers;
28 import org.mockito.Mock;
29 import org.mockito.Mockito;
30 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
31 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
32 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
33 import org.opendaylight.groupbasedpolicy.api.sf.ChainActionDefinition;
34 import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.manager.PolicyConfigurationContext;
35 import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.manager.PolicyManagerImpl;
36 import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.writer.NetconfTransactionCreator;
37 import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.writer.PolicyWriter;
38 import org.opendaylight.sfc.provider.api.SfcProviderRenderedPathAPI;
39 import org.opendaylight.sfc.provider.api.SfcProviderServiceForwarderAPI;
40 import org.opendaylight.sfc.provider.api.SfcProviderServicePathAPI;
41 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.RspName;
42 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfName;
43 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfcName;
44 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SffName;
45 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfpName;
46 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.CreateRenderedPathInput;
47 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.RenderedServicePath;
48 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.RenderedServicePathBuilder;
49 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.rendered.service.path.RenderedServicePathHop;
50 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.rendered.service.path.RenderedServicePathHopBuilder;
51 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarders.ServiceFunctionForwarderBuilder;
52 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.ServiceFunctionPaths;
53 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.ServiceFunctionPathsBuilder;
54 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPath;
55 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPathBuilder;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
58 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.ClassMap;
59 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.ServiceChain;
60 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.policy.map.Class;
61 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.service.chain.service.function.forwarder.Local;
62 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.service.chain.service.function.forwarder.ServiceFfName;
63 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.service.chain.service.path.config.service.chain.path.mode.service.index.services.ServiceTypeChoice;
64 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.service.chain.service.path.config.service.chain.path.mode.service.index.services.service.type.choice.ServiceFunction;
65 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.service.chain.service.path.config.service.chain.path.mode.service.index.services.service.type.choice.ServiceFunctionForwarder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContextId;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ParameterName;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev160427.IpPrefixType;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev160427.L3Context;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValueBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.has.rule.group.with.renderer.endpoint.participation.RuleGroupWithRendererEndpointParticipationBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.renderer.endpoints.RendererEndpoint;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.renderer.endpoints.RendererEndpointBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.renderer.endpoints.renderer.endpoint.PeerEndpoint;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.renderer.endpoints.renderer.endpoint.PeerEndpointBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.has.actions.Action;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.has.actions.ActionBuilder;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.database.rev160308.Sgt;
81 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
82 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
83 import org.powermock.api.mockito.PowerMockito;
84 import org.powermock.core.classloader.annotations.PrepareForTest;
85 import org.powermock.modules.junit4.PowerMockRunner;
86
87 /**
88  * Test for {@link ServiceChainingUtil}.
89  */
90 @RunWith(PowerMockRunner.class)
91 @PrepareForTest({
92         ServiceChainingUtil.class,
93         SfcProviderServicePathAPI.class,
94         SfcProviderRenderedPathAPI.class,
95         SfcProviderServiceForwarderAPI.class,
96         NetconfTransactionCreator.class
97 })
98 public class ServiceChainingUtilTest {
99
100     @Captor
101     private ArgumentCaptor<RspName> rspNameCaptor;
102     @Captor
103     private ArgumentCaptor<SffName> sffNameCaptor;
104     @Captor
105     private ArgumentCaptor<ServiceFunctionPath> sfpCaptor;
106     @Captor
107     private ArgumentCaptor<CreateRenderedPathInput> createRspCaptor;
108     @Captor
109     private ArgumentCaptor<RenderedServicePath> rspCaptor;
110     @Captor
111     private ArgumentCaptor<List<Class>> listClassCaptor;
112     @Captor
113     private ArgumentCaptor<ClassMap> classMapCaptor;
114     @Mock
115     private PolicyWriter policyWriter;
116     @Mock
117     private DataBroker dataBroker;
118     @Mock
119     private ReadWriteTransaction rwTx;
120
121     private PolicyConfigurationContext policyConfigurationContext;
122
123     @Before
124     public void setUp() throws Exception {
125         final NodeId currentNodeId = new NodeId("unit-node-01");
126         Mockito.when(policyWriter.getCurrentNodeId()).thenReturn(currentNodeId);
127
128         final String managementIpAddress = "1.2.3.5";
129         Mockito.when(policyWriter.getManagementIpAddress()).thenReturn(managementIpAddress);
130
131         Mockito.when(policyWriter.getCurrentMountpoint()).thenReturn(dataBroker);
132         Mockito.when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
133
134         policyConfigurationContext = new PolicyConfigurationContext();
135         policyConfigurationContext.setPolicyWriter(policyWriter);
136     }
137
138     @Test
139     public void testGetServicePath() throws Exception {
140         final String sfcNameValue = "123";
141         final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, false);
142         final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
143                 .setServiceFunctionPath(Collections.singletonList(sfcPath))
144                 .build();
145
146         stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
147
148         final ServiceFunctionPath servicePath = ServiceChainingUtil.getServicePath(Lists.newArrayList(
149                 createParameterValue("sfc-chain-name", sfcNameValue)
150         ));
151         Assert.assertEquals(sfcPath, servicePath);
152     }
153
154     private ParameterValue createParameterValue(final String name, final String value) {
155         return new ParameterValueBuilder().setName(new ParameterName(name)).setStringValue(value).build();
156     }
157
158     @Test
159     public void testResolveChainAction_full() throws Exception {
160         final PeerEndpoint peerEndpoint = createPeerEndpoint();
161         final Sgt sourceSgt = new Sgt(1);
162         final Sgt destinationSgt = new Sgt(2);
163         final Map<PolicyManagerImpl.ActionCase, Action> actionMap = createActionMap();
164         final String classMapName = "unit-class-map-name-01";
165
166         final String sfcNameValue = "123";
167         final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, false);
168         final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
169                 .setServiceFunctionPath(Collections.singletonList(sfcPath))
170                 .build();
171
172         stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
173
174         final RenderedServicePath rsp = createRsp("unit-rsp-02");
175         stub(method(SfcProviderRenderedPathAPI.class, "readRenderedServicePath")).toReturn(rsp);
176         stub(method(ServiceChainingUtil.class, "setSfcPart")).toReturn(true);
177
178         ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap, policyConfigurationContext, dataBroker);
179
180         Mockito.verify(policyWriter).cache(classMapCaptor.capture());
181         Mockito.verify(policyWriter).cache(listClassCaptor.capture());
182         Mockito.verifyNoMoreInteractions(policyWriter);
183         Assert.assertEquals(1, listClassCaptor.getValue().size());
184     }
185
186     @Test
187     public void testResolveChainAction_fullSymmetric() throws Exception {
188         final PeerEndpoint peerEndpoint = createPeerEndpoint();
189         final Sgt sourceSgt = new Sgt(1);
190         final Sgt destinationSgt = new Sgt(2);
191         final Map<PolicyManagerImpl.ActionCase, Action> actionMap = createActionMap();
192         final String classMapName = "unit-class-map-name-01";
193
194         final String sfcNameValue = "123";
195         final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, true);
196         final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
197                 .setServiceFunctionPath(Collections.singletonList(sfcPath))
198                 .build();
199
200         stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
201
202         final RenderedServicePath rsp = createRsp("unit-rsp-02");
203         stub(method(SfcProviderRenderedPathAPI.class, "readRenderedServicePath")).toReturn(rsp);
204         stub(method(ServiceChainingUtil.class, "setSfcPart")).toReturn(true);
205
206         ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap, policyConfigurationContext, dataBroker);
207
208         Mockito.verify(policyWriter, Mockito.times(2)).cache(classMapCaptor.capture());
209         Mockito.verify(policyWriter).cache(listClassCaptor.capture());
210         Mockito.verifyNoMoreInteractions(policyWriter);
211         Assert.assertEquals(2, listClassCaptor.getValue().size());
212     }
213
214     @Test
215     public void testResolveChainAction_partial01() throws Exception {
216         final PeerEndpoint peerEndpoint = createPeerEndpoint();
217         final Sgt sourceSgt = new Sgt(1);
218         final Sgt destinationSgt = new Sgt(2);
219         final Map<PolicyManagerImpl.ActionCase, Action> actionMap = createActionMap();
220         final String classMapName = "unit-class-map-name-01";
221
222         final String sfcNameValue = "123";
223         final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, true);
224         final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
225                 .setServiceFunctionPath(Collections.singletonList(sfcPath))
226                 .build();
227
228         stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
229
230         final RenderedServicePath rsp = createRsp("unit-rsp-02");
231         stub(method(SfcProviderRenderedPathAPI.class, "readRenderedServicePath")).toReturn(rsp);
232         stub(method(ServiceChainingUtil.class, "setSfcPart")).toReturn(true);
233         stub(method(ServiceChainingUtil.class, "createSymmetricRenderedPath")).toReturn(null);
234
235         policyConfigurationContext.setCurrentRendererEP(createRendererEP(
236                 "unit-address", new ContextId("unit-conext-1"), Collections.emptyList())
237         );
238
239         ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap,
240                 policyConfigurationContext, dataBroker);
241         Mockito.verify(policyWriter, Mockito.times(2)).cache(classMapCaptor.capture());
242         Mockito.verify(policyWriter).cache(listClassCaptor.capture());
243         Mockito.verifyNoMoreInteractions(policyWriter);
244     }
245
246     @Test
247     public void testResolveChainAction_partial02() throws Exception {
248         final PeerEndpoint peerEndpoint = createPeerEndpoint();
249         final Sgt sourceSgt = new Sgt(1);
250         final Sgt destinationSgt = new Sgt(2);
251         final Map<PolicyManagerImpl.ActionCase, Action> actionMap = createActionMap();
252         final String classMapName = "unit-class-map-name-01";
253
254         final String sfcNameValue = "123";
255         final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, false);
256         final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
257                 .setServiceFunctionPath(Collections.singletonList(sfcPath))
258                 .build();
259
260         stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
261
262         final RenderedServicePath rsp = createRsp("unit-rsp-02");
263         stub(method(SfcProviderRenderedPathAPI.class, "readRenderedServicePath")).toReturn(rsp);
264         stub(method(ServiceChainingUtil.class, "setSfcPart")).toReturn(false);
265
266         ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap, policyConfigurationContext, dataBroker);
267
268         Mockito.verifyNoMoreInteractions(policyWriter);
269     }
270
271     @Test
272     public void testResolveChainAction_partial03() throws Exception {
273         final PeerEndpoint peerEndpoint = createPeerEndpoint(null);
274         final Sgt sourceSgt = new Sgt(1);
275         final Sgt destinationSgt = new Sgt(2);
276         final Map<PolicyManagerImpl.ActionCase, Action> actionMap = createActionMap();
277         final String classMapName = "unit-class-map-name-01";
278
279         final String sfcNameValue = "123";
280         final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, false);
281         final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
282                 .setServiceFunctionPath(Collections.singletonList(sfcPath))
283                 .build();
284
285         stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
286
287         final RenderedServicePath rsp = createRsp("unit-rsp-02");
288         stub(method(SfcProviderRenderedPathAPI.class, "readRenderedServicePath")).toReturn(rsp);
289
290         policyConfigurationContext.setCurrentRendererEP(createRendererEP(
291                 "unit-address", new ContextId("unit-conext-1"), Collections.emptyList())
292         );
293
294         ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap, policyConfigurationContext, dataBroker);
295
296         Mockito.verifyNoMoreInteractions(policyWriter);
297     }
298
299     @Test
300     public void testResolveChainAction_partial04() throws Exception {
301         final PeerEndpoint peerEndpoint = createPeerEndpoint(null);
302         final Sgt sourceSgt = new Sgt(1);
303         final Sgt destinationSgt = new Sgt(2);
304         final Map<PolicyManagerImpl.ActionCase, Action> actionMap = createActionMap(false);
305         final String classMapName = "unit-class-map-name-01";
306
307         policyConfigurationContext.setCurrentRendererEP(createRendererEP(
308                 "unit-address", new ContextId("unit-conext-1"), Collections.emptyList())
309         );
310
311         ServiceChainingUtil.resolveNewChainAction(peerEndpoint, sourceSgt, destinationSgt, actionMap, policyConfigurationContext, dataBroker);
312
313         Mockito.verifyNoMoreInteractions(policyWriter);
314     }
315
316     private Map<PolicyManagerImpl.ActionCase, Action> createActionMap() {
317         return createActionMap(true);
318     }
319
320     private Map<PolicyManagerImpl.ActionCase, Action> createActionMap(final boolean fillParamValue) {
321         final Map<PolicyManagerImpl.ActionCase,Action> actionMap = new HashMap<>();
322         final ActionBuilder actionValue = new ActionBuilder();
323         if (fillParamValue) {
324             actionValue.setParameterValue(Collections.singletonList(new ParameterValueBuilder()
325                     .setName(new ParameterName(ChainActionDefinition.SFC_CHAIN_NAME))
326                     .setStringValue("123")
327                     .build()));
328         }
329         actionMap.put(PolicyManagerImpl.ActionCase.CHAIN, actionValue.build());
330         return actionMap;
331     }
332
333     private RendererEndpoint createRendererEP(final String address, final ContextId contextId, final List<PeerEndpoint> peerEndpoints) {
334         return new RendererEndpointBuilder()
335                 .setAddress(address)
336                 .setAddressType(IpPrefixType.class)
337                 .setContextId(contextId)
338                 .setContextType(L3Context.class)
339                 .setPeerEndpoint(peerEndpoints)
340                 .build();
341     }
342
343     private PeerEndpoint createPeerEndpoint() {
344         return createPeerEndpoint(new TenantId("unit-tenant-06"));
345     }
346
347     private PeerEndpoint createPeerEndpoint(final TenantId tenantId) {
348         return new PeerEndpointBuilder()
349                 .setRuleGroupWithRendererEndpointParticipation(Collections.singletonList(
350                         new RuleGroupWithRendererEndpointParticipationBuilder()
351                                 .setTenantId(tenantId)
352                                 .build()
353                 ))
354                 .build();
355     }
356
357     @Test
358     public void testCreateRenderedPath() throws Exception {
359         final String sfcNameValue = "123";
360         final ServiceFunctionPath sfp = createSfp(sfcNameValue, false);
361         final TenantId tenantId = new TenantId("unit-tennant-01");
362
363         final RenderedServicePath rsp = createRsp("unit-rsp-01");
364
365         PowerMockito.mockStatic(SfcProviderRenderedPathAPI.class);
366         final SfcProviderRenderedPathAPI api = PowerMockito.mock(SfcProviderRenderedPathAPI.class);
367         PowerMockito.when(api.readRenderedServicePath(rspNameCaptor.capture())).thenReturn(rsp);
368
369         final RenderedServicePath renderedPath = ServiceChainingUtil.createRenderedPath(sfp, tenantId, dataBroker);
370
371         Assert.assertEquals("123_plainunit-tennant-01-gbp-rsp", rspNameCaptor.getValue().getValue());
372         Assert.assertEquals(rsp, renderedPath);
373     }
374
375     @Test
376     public void testCreateRenderedPath_notExisting() throws Exception {
377         final String sfcNameValue = "123";
378         final ServiceFunctionPath sfp = createSfp(sfcNameValue, false);
379         final TenantId tenantId = new TenantId("unit-tennant-01");
380
381         final RenderedServicePath rsp = createRsp("unit-rsp-01");
382
383         PowerMockito.mockStatic(SfcProviderRenderedPathAPI.class);
384         final SfcProviderRenderedPathAPI api = PowerMockito.mock(SfcProviderRenderedPathAPI.class);
385         PowerMockito.when(api.readRenderedServicePath(rspNameCaptor.capture())).thenReturn(null);
386         PowerMockito.when(api.createRenderedServicePathAndState(
387                 sfpCaptor.capture(), createRspCaptor.capture()
388         )).thenReturn(rsp);
389         PowerMockito.mockStatic(NetconfTransactionCreator.class);
390         final NetconfTransactionCreator creator = PowerMockito.mock(NetconfTransactionCreator.class);
391         PowerMockito.when(creator.netconfReadWriteTransaction(dataBroker)).thenReturn(java.util.Optional.empty());
392
393         final RenderedServicePath renderedPath = ServiceChainingUtil.createRenderedPath(sfp, tenantId, dataBroker);
394
395         Assert.assertEquals("123_plainunit-tennant-01-gbp-rsp", rspNameCaptor.getValue().getValue());
396
397         final ServiceFunctionPath serviceFunctionPath = sfpCaptor.getValue();
398         Assert.assertEquals("123_plain", serviceFunctionPath.getName().getValue());
399         Assert.assertFalse(serviceFunctionPath.isSymmetric());
400
401         final CreateRenderedPathInput createRPInput = createRspCaptor.getValue();
402         Assert.assertFalse(createRPInput.isSymmetric());
403         Assert.assertEquals("123_plain", createRPInput.getParentServiceFunctionPath());
404         Assert.assertEquals("123_plainunit-tennant-01-gbp-rsp", createRPInput.getName());
405
406         Assert.assertEquals(rsp, renderedPath);
407     }
408
409     private RenderedServicePath createRsp(final String rspNameValue) {
410         return new RenderedServicePathBuilder()
411                 .setName(new RspName(rspNameValue))
412                 .setRenderedServicePathHop(Lists.newArrayList(createRspHop("rsp-hop-01-sf")))
413                 .build();
414     }
415
416     private RenderedServicePathHop createRspHop(final String sfNameValue) {
417         return new RenderedServicePathHopBuilder()
418                 .setServiceFunctionName(new SfName(sfNameValue))
419                 .setServiceFunctionForwarder(new SffName(sfNameValue + "+sff"))
420                 .build();
421     }
422
423     private ServiceFunctionPath createSfp(final String sfcNameValue, final boolean symmetric) {
424         return new ServiceFunctionPathBuilder()
425                 .setServiceChainName(new SfcName(sfcNameValue))
426                 .setName(new SfpName(sfcNameValue + "_plain"))
427                 .setSymmetric(symmetric)
428                 .build();
429     }
430
431     @Test
432     public void testCreateSymmetricRenderedPath() throws Exception {
433         final ServiceFunctionPath sfp = createSfp("unit-sfp-02", false);
434         final RenderedServicePath rsp = createRsp("unit-rsp-02");
435         final TenantId tennantId = new TenantId("tenant-02");
436
437         PowerMockito.mockStatic(SfcProviderRenderedPathAPI.class);
438         final SfcProviderRenderedPathAPI api = PowerMockito.mock(SfcProviderRenderedPathAPI.class);
439         PowerMockito.when(api.readRenderedServicePath(rspNameCaptor.capture())).thenReturn(rsp);
440
441         final RenderedServicePath symmetricRenderedPath = ServiceChainingUtil.createSymmetricRenderedPath(sfp, rsp, tennantId, dataBroker);
442
443         Assert.assertEquals("unit-sfp-02_plaintenant-02-gbp-rsp-Reverse", rspNameCaptor.getValue().getValue());
444         Assert.assertEquals(rsp, symmetricRenderedPath);
445     }
446
447     @Test
448     public void testCreateSymmetricRenderedPath_notExisting() throws Exception {
449         final ServiceFunctionPath sfp = createSfp("unit-sfp-02", false);
450         final RenderedServicePath rsp = createRsp("unit-rsp-02");
451         final TenantId tennantId = new TenantId("tenant-02");
452
453         PowerMockito.mockStatic(SfcProviderRenderedPathAPI.class);
454         final SfcProviderRenderedPathAPI api = PowerMockito.mock(SfcProviderRenderedPathAPI.class);
455         PowerMockito.when(api.readRenderedServicePath(rspNameCaptor.capture())).thenReturn(null);
456         PowerMockito.when(api.createReverseRenderedServicePathEntry(rspCaptor.capture())).thenReturn(rsp);
457         PowerMockito.mockStatic(NetconfTransactionCreator.class);
458         final NetconfTransactionCreator creator = PowerMockito.mock(NetconfTransactionCreator.class);
459         PowerMockito.when(creator.netconfReadWriteTransaction(dataBroker)).thenReturn(java.util.Optional.empty());
460
461         final RenderedServicePath symmetricRenderedPath = ServiceChainingUtil.createSymmetricRenderedPath(sfp, rsp, tennantId, dataBroker);
462
463         Assert.assertEquals("unit-sfp-02_plaintenant-02-gbp-rsp-Reverse", rspNameCaptor.getValue().getValue());
464         Assert.assertEquals(rsp, rspCaptor.getValue());
465         Assert.assertEquals(rsp, symmetricRenderedPath);
466     }
467
468     @Test
469     public void testFindServiceFunctionPath() throws Exception {
470         final String sfcNameValue = "123";
471         final ServiceFunctionPath sfcPath = createSfp(sfcNameValue, false);
472         final ServiceFunctionPaths sfcPaths = new ServiceFunctionPathsBuilder()
473                 .setServiceFunctionPath(Collections.singletonList(sfcPath))
474                 .build();
475
476         stub(method(SfcProviderServicePathAPI.class, "readAllServiceFunctionPaths")).toReturn(sfcPaths);
477
478         final ServiceFunctionPath servicePath = ServiceChainingUtil.findServiceFunctionPath(new SfcName(sfcNameValue));
479         Assert.assertEquals(sfcPath, servicePath);
480     }
481
482     @Test
483     public void testSetSfcPart_success() throws Exception {
484         final RenderedServicePath rsp = createRsp("unit-rsp-03");
485         final org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarders.ServiceFunctionForwarder
486                 sff = new ServiceFunctionForwarderBuilder()
487                 .setName(new SffName("unit-sff-03"))
488                 .setIpMgmtAddress(new IpAddress(new Ipv4Address("1.2.3.4")))
489                 .build();
490         final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
491         sfpBuilder.setSymmetric(false);
492         final ServiceFunctionPath sfp = sfpBuilder.build();
493
494         stub(method(ServiceChainingUtil.class, "checkLocalForwarderPresence")).toReturn(true);
495
496         PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
497         final SfcProviderServiceForwarderAPI api = PowerMockito.mock(SfcProviderServiceForwarderAPI.class);
498         PowerMockito.when(api.readServiceFunctionForwarder(sffNameCaptor.capture())).thenReturn(sff);
499
500         final boolean outcome = ServiceChainingUtil.setSfcPart(sfp, rsp, null, policyWriter);
501
502         Assert.assertEquals("rsp-hop-01-sf+sff", sffNameCaptor.getValue().getValue());
503         Assert.assertTrue(outcome);
504
505         Mockito.verify(policyWriter).cache(Matchers.<ServiceFfName>any());
506         Mockito.verify(policyWriter).cache(Matchers.<ServiceChain>any());
507         Mockito.verify(policyWriter).getCurrentNodeId();
508         Mockito.verify(policyWriter).getCurrentMountpoint();
509         Mockito.verify(policyWriter).getManagementIpAddress();
510         Mockito.verifyNoMoreInteractions(policyWriter);
511     }
512
513     @Test
514     public void testSetSfcPart_success_newRsp() throws Exception {
515         final RenderedServicePath rsp = createRsp("unit-rsp-03");
516         final org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarders.ServiceFunctionForwarder
517                 sff = new ServiceFunctionForwarderBuilder()
518                 .setName(new SffName("unit-sff-03"))
519                 .setIpMgmtAddress(new IpAddress(new Ipv4Address("1.2.3.4")))
520                 .build();
521         final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
522         sfpBuilder.setSymmetric(false);
523         final ServiceFunctionPath sfp = sfpBuilder.build();
524
525         stub(method(ServiceChainingUtil.class, "checkLocalForwarderPresence")).toReturn(false);
526
527         PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
528         final SfcProviderServiceForwarderAPI api = PowerMockito.mock(SfcProviderServiceForwarderAPI.class);
529         PowerMockito.when(api.readServiceFunctionForwarder(sffNameCaptor.capture())).thenReturn(sff);
530
531         final boolean outcome = ServiceChainingUtil.setSfcPart(sfp, rsp, null, policyWriter);
532
533         Assert.assertEquals("rsp-hop-01-sf+sff", sffNameCaptor.getValue().getValue());
534         Assert.assertTrue(outcome);
535
536         Mockito.verify(policyWriter).cache(Matchers.<Local>any());
537         Mockito.verify(policyWriter).cache(Matchers.<ServiceFfName>any());
538         Mockito.verify(policyWriter).cache(Matchers.<ServiceChain>any());
539         Mockito.verify(policyWriter).getCurrentMountpoint();
540         Mockito.verify(policyWriter, Mockito.times(2)).getManagementIpAddress();
541         Mockito.verifyNoMoreInteractions(policyWriter);
542     }
543
544     @Test
545     public void testSetSfcPart_fail01() throws Exception {
546         final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
547         sfpBuilder.setSymmetric(false);
548         final ServiceFunctionPath sfp = sfpBuilder.build();
549
550         Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, null, null, policyWriter));
551
552         final RenderedServicePathBuilder rspBuilder = new RenderedServicePathBuilder().setName(new RspName("unit-rsp-05"));
553         Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, rspBuilder.build(), null, policyWriter));
554
555         rspBuilder.setRenderedServicePathHop(Collections.emptyList());
556         Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, rspBuilder.build(), null, policyWriter));
557
558         rspBuilder.setRenderedServicePathHop(Collections.singletonList(null));
559         Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, rspBuilder.build(), null, policyWriter));
560     }
561
562     @Test
563     public void testSetSfcPart_fail02() throws Exception {
564         final RenderedServicePath rsp = createRsp("unit-rsp-03");
565         final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
566         sfpBuilder.setSymmetric(false);
567         final ServiceFunctionPath sfp = sfpBuilder.build();
568
569         Mockito.doReturn(Futures.immediateCheckedFuture(Optional.absent()))
570                 .when(rwTx).read(Mockito.eq(LogicalDatastoreType.CONFIGURATION), Mockito.<InstanceIdentifier<Local>>any());
571
572         PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
573         final SfcProviderServiceForwarderAPI api = PowerMockito.mock(SfcProviderServiceForwarderAPI.class);
574         PowerMockito.when(api.readServiceFunctionForwarder(sffNameCaptor.capture())).thenReturn(null);
575
576         final boolean outcome = ServiceChainingUtil.setSfcPart(sfp, rsp, null, policyWriter);
577
578         Assert.assertEquals("rsp-hop-01-sf+sff", sffNameCaptor.getValue().getValue());
579         Assert.assertFalse(outcome);
580
581         Mockito.verify(policyWriter).getCurrentMountpoint();
582         Mockito.verify(policyWriter).getManagementIpAddress();
583         Mockito.verify(policyWriter).cache(Matchers.<Local>any());
584         Mockito.verifyNoMoreInteractions(policyWriter);
585     }
586
587     @Test
588     public void testSetSfcPart_fail03() throws Exception {
589         final RenderedServicePath rsp = createRsp("unit-rsp-03");
590         final ServiceFunctionForwarderBuilder sffBuilder = new ServiceFunctionForwarderBuilder()
591                 .setName(new SffName("unit-sff-03"))
592                 .setIpMgmtAddress(null);
593         final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
594         sfpBuilder.setSymmetric(false);
595         final ServiceFunctionPath sfp = sfpBuilder.build();
596
597         stub(method(ServiceChainingUtil.class, "checkLocalForwarderPresence")).toReturn(true);
598
599         PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
600         final SfcProviderServiceForwarderAPI api = PowerMockito.mock(SfcProviderServiceForwarderAPI.class);
601         PowerMockito.when(api.readServiceFunctionForwarder(sffNameCaptor.capture())).thenReturn(
602                 sffBuilder.build());
603
604         Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, rsp, null, policyWriter));
605
606         Assert.assertEquals("rsp-hop-01-sf+sff", sffNameCaptor.getValue().getValue());
607
608         Mockito.verify(policyWriter).getCurrentMountpoint();
609         Mockito.verify(policyWriter).getCurrentNodeId();
610         Mockito.verifyNoMoreInteractions(policyWriter);
611     }
612
613     @Test
614     public void testSetSfcPart_fail04() throws Exception {
615         final RenderedServicePath rsp = createRsp("unit-rsp-03");
616         final ServiceFunctionForwarderBuilder sffBuilder = new ServiceFunctionForwarderBuilder()
617                 .setName(new SffName("unit-sff-03"))
618                 .setIpMgmtAddress(new IpAddress((Ipv4Address) null));
619         final ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
620         sfpBuilder.setSymmetric(false);
621         final ServiceFunctionPath sfp = sfpBuilder.build();
622
623         stub(method(ServiceChainingUtil.class, "checkLocalForwarderPresence")).toReturn(true);
624
625         PowerMockito.mockStatic(SfcProviderServiceForwarderAPI.class);
626         final SfcProviderServiceForwarderAPI api = PowerMockito.mock(SfcProviderServiceForwarderAPI.class);
627         PowerMockito.when(api.readServiceFunctionForwarder(sffNameCaptor.capture())).thenReturn(
628                 sffBuilder.build());
629
630         Assert.assertFalse(ServiceChainingUtil.setSfcPart(sfp, rsp, null, policyWriter));
631
632         Assert.assertEquals("rsp-hop-01-sf+sff", sffNameCaptor.getValue().getValue());
633
634         Mockito.verify(policyWriter).getCurrentMountpoint();
635         Mockito.verify(policyWriter).getCurrentNodeId();
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 }