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