2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.util;
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.Futures;
13 import java.util.Collections;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.ArgumentCaptor;
19 import org.mockito.Captor;
20 import org.mockito.Matchers;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.runners.MockitoJUnitRunner;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.groupbasedpolicy.api.sf.ChainActionDefinition;
28 import org.opendaylight.sfc.provider.OpendaylightSfc;
29 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfcName;
30 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfpName;
31 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.RenderedServicePath;
32 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.RenderedServicePathBuilder;
33 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.ServiceFunctionPaths;
34 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.ServiceFunctionPathsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPath;
36 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPathBuilder;
37 import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.ClassMap;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ParameterName;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValueBuilder;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import static org.junit.Assert.assertNotNull;
47 * Test for {@link PolicyManagerUtil}.
49 @RunWith(MockitoJUnitRunner.class)
50 public class PolicyManagerUtilTest {
53 private DataBroker dataBroker;
55 private ReadOnlyTransaction roTx;
57 private ArgumentCaptor<InstanceIdentifier<RenderedServicePath>> rendererServicePathIICaptor;
59 private ServiceFunctionPath serviceFunctionPath;
62 public void setUp() throws Exception {
63 serviceFunctionPath = new ServiceFunctionPathBuilder()
64 .setServiceChainName(new SfcName("sfc-chain-name-42"))
65 .setName(new SfpName("sfp-name-01"))
68 new OpendaylightSfc().setDataProvider(dataBroker);
72 public void testCreateClassMap() {
73 ClassMap cm = PolicyManagerUtil.createClassMap("testName", null);
78 public void testGetServicePath() throws Exception {
79 final ParameterValue paramValueSfc = new ParameterValueBuilder()
80 .setName(new ParameterName(ChainActionDefinition.SFC_CHAIN_NAME))
81 .setStringValue("sfc-chain-name-42")
84 final ServiceFunctionPaths sfPaths = new ServiceFunctionPathsBuilder()
85 .setServiceFunctionPath(Collections.singletonList(serviceFunctionPath))
88 Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(roTx);
89 Mockito.when(roTx.read(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(ServiceFunctionPaths.class)))
90 .thenReturn(Futures.immediateCheckedFuture(Optional.of(sfPaths)));
92 final ServiceFunctionPath servicePath = ServiceChainingUtil.getServicePath(Collections.singletonList(paramValueSfc));
93 Assert.assertEquals(serviceFunctionPath, servicePath);
97 public void testCreateRenderedPath() throws Exception {
98 final TenantId tenantId = new TenantId("tenant-id-01");
99 final RenderedServicePath renderedSP = new RenderedServicePathBuilder().build();
101 Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(roTx);
102 Mockito.when(roTx.read(Matchers.eq(LogicalDatastoreType.OPERATIONAL), rendererServicePathIICaptor.capture()))
103 .thenReturn(Futures.immediateCheckedFuture(Optional.of(renderedSP)));
105 final RenderedServicePath renderedPath = ServiceChainingUtil.createRenderedPath(serviceFunctionPath, tenantId);
106 Assert.assertEquals(renderedSP, renderedPath);
107 final InstanceIdentifier<RenderedServicePath> ii = rendererServicePathIICaptor.getValue();
108 Assert.assertEquals("sfp-name-01tenant-id-01-gbp-rsp", ii.firstKeyOf(RenderedServicePath.class).getName().getValue());
112 public void testCreateSymmetricRenderedPath() throws Exception {
113 final RenderedServicePath renderedServicePath = new RenderedServicePathBuilder().build();
114 final TenantId tenantId = new TenantId("tenant-id-02");
116 Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(roTx);
117 Mockito.when(roTx.read(Matchers.eq(LogicalDatastoreType.OPERATIONAL), rendererServicePathIICaptor.capture()))
118 .thenReturn(Futures.immediateCheckedFuture(Optional.of(renderedServicePath)));
121 final RenderedServicePath symmetricRenderedPath = ServiceChainingUtil.createSymmetricRenderedPath(
122 serviceFunctionPath, renderedServicePath, tenantId);
123 Assert.assertEquals(renderedServicePath, symmetricRenderedPath);
124 final InstanceIdentifier<RenderedServicePath> ii = rendererServicePathIICaptor.getValue();
125 Assert.assertEquals("sfp-name-01tenant-id-02-gbp-rsp-Reverse", ii.firstKeyOf(RenderedServicePath.class).getName().getValue());