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.ios.rev160308._native._class.map.Match;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ParameterName;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValueBuilder;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import static org.junit.Assert.assertNotNull;
48 * Test for {@link PolicyManagerUtil}.
50 @RunWith(MockitoJUnitRunner.class)
51 public class PolicyManagerUtilTest {
54 private DataBroker dataBroker;
56 private ReadOnlyTransaction roTx;
58 private ArgumentCaptor<InstanceIdentifier<RenderedServicePath>> rendererServicePathIICaptor;
60 private ServiceFunctionPath serviceFunctionPath;
63 public void setUp() throws Exception {
64 serviceFunctionPath = new ServiceFunctionPathBuilder()
65 .setServiceChainName(new SfcName("sfc-chain-name-42"))
66 .setName(new SfpName("sfp-name-01"))
69 new OpendaylightSfc().setDataProvider(dataBroker);
73 public void testCreateClassMap() {
74 ClassMap cm = PolicyManagerUtil.createClassMap("testName", null);
79 public void testGetServicePath() throws Exception {
80 final ParameterValue paramValueSfc = new ParameterValueBuilder()
81 .setName(new ParameterName(ChainActionDefinition.SFC_CHAIN_NAME))
82 .setStringValue("sfc-chain-name-42")
85 final ServiceFunctionPaths sfPaths = new ServiceFunctionPathsBuilder()
86 .setServiceFunctionPath(Collections.singletonList(serviceFunctionPath))
89 Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(roTx);
90 Mockito.when(roTx.read(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(ServiceFunctionPaths.class)))
91 .thenReturn(Futures.immediateCheckedFuture(Optional.of(sfPaths)));
93 final ServiceFunctionPath servicePath = ServiceChainingUtil.findServicePathFromParameterValues(Collections.singletonList(paramValueSfc));
94 Assert.assertEquals(serviceFunctionPath, servicePath);
98 public void testCreateRenderedPath() throws Exception {
99 final TenantId tenantId = new TenantId("tenant-id-01");
100 final RenderedServicePath renderedSP = new RenderedServicePathBuilder().build();
102 Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(roTx);
103 Mockito.when(roTx.read(Matchers.eq(LogicalDatastoreType.OPERATIONAL), rendererServicePathIICaptor.capture()))
104 .thenReturn(Futures.immediateCheckedFuture(Optional.of(renderedSP)));
106 final RenderedServicePath renderedPath = ServiceChainingUtil.createRenderedPath(serviceFunctionPath, tenantId,
108 Assert.assertEquals(renderedSP, renderedPath);
109 final InstanceIdentifier<RenderedServicePath> ii = rendererServicePathIICaptor.getValue();
110 Assert.assertEquals("sfp-name-01-tenant-id-01-gbp-rsp", ii.firstKeyOf(RenderedServicePath.class).getName().getValue());
114 public void testCreateSymmetricRenderedPath() throws Exception {
115 final RenderedServicePath renderedServicePath = new RenderedServicePathBuilder().build();
116 final TenantId tenantId = new TenantId("tenant-id-02");
118 Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(roTx);
119 Mockito.when(roTx.read(Matchers.eq(LogicalDatastoreType.OPERATIONAL), rendererServicePathIICaptor.capture()))
120 .thenReturn(Futures.immediateCheckedFuture(Optional.of(renderedServicePath)));
123 final RenderedServicePath symmetricRenderedPath = ServiceChainingUtil.createReversedRenderedPath(
124 serviceFunctionPath, renderedServicePath, tenantId, dataBroker);
125 Assert.assertEquals(renderedServicePath, symmetricRenderedPath);
126 final InstanceIdentifier<RenderedServicePath> ii = rendererServicePathIICaptor.getValue();
127 Assert.assertEquals("sfp-name-01-tenant-id-02-gbp-rsp-Reverse", ii.firstKeyOf(RenderedServicePath.class).getName().getValue());
131 public void testMatch() {
132 Match result = PolicyManagerUtil.createSecurityGroupMatch(10, 20);
133 assertNotNull(result);