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
8 package org.opendaylight.groupbasedpolicy.renderer.faas;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.mockito.Matchers.any;
16 import static org.mockito.Matchers.eq;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.when;
20 import java.util.List;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.Executors;
23 import java.util.concurrent.ScheduledExecutorService;
25 import com.google.common.base.Optional;
26 import com.google.common.collect.ImmutableList;
27 import com.google.common.util.concurrent.CheckedFuture;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mockito;
32 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
33 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
34 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
35 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
36 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
37 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
38 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
39 import org.opendaylight.faas.uln.datastore.api.UlnDatastoreApi;
40 import org.opendaylight.groupbasedpolicy.renderer.faas.test.DataChangeListenerTester;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.common.rev151013.Uuid;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.security.rules.rev151013.security.rule.groups.attributes.security.rule.groups.container.SecurityRuleGroups;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.security.rules.rev151013.security.rule.groups.attributes.security.rule.groups.container.security.rule.groups.SecurityRuleGroup;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.security.rules.rev151013.security.rule.groups.attributes.security.rule.groups.container.security.rule.groups.security.rule.group.SecurityRule;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierName;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClauseName;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContractId;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.RuleName;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.mapped.entity.MappedContract;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.classifier.refs.ClassifierRef;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.classifier.refs.ClassifierRefBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.Contract;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.ContractBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.Clause;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.ClauseBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.SubjectBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.subject.Rule;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.subject.RuleBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ClassifierInstance;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ClassifierInstanceBuilder;
63 import org.opendaylight.yangtools.yang.binding.DataObject;
64 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
65 import org.powermock.api.mockito.PowerMockito;
66 import org.powermock.core.classloader.annotations.PrepareForTest;
67 import org.powermock.modules.junit4.PowerMockRunner;
69 @RunWith(PowerMockRunner.class)
70 @PrepareForTest(UlnDatastoreApi.class)
71 public class FaasContractManagerListenerCovrgTest {
73 private static final ClauseName CLAUSE_NAME = new ClauseName("clause-1");
74 public static final SubjectName SUBJECT_NAME = new SubjectName("subject-name");
75 private InstanceIdentifier<Contract> contractIid;
76 private ContractId contractId = new ContractId("contractId");
77 private FaasContractManagerListener listener;
78 private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
79 private TenantId gbpTenantId = new TenantId("b4511aac-ae43-11e5-bf7f-feff819cdc9f");
80 private Uuid faasTenantId = new Uuid("b4511aac-ae43-11e5-bf7f-feff819cdc9f");
81 private DataChangeListenerTester tester;
82 private DataBroker dataProvider;
84 @SuppressWarnings("unchecked")
87 contractIid = mock(InstanceIdentifier.class);
88 dataProvider = mock(DataBroker.class);
90 listener = new FaasContractManagerListener(dataProvider, gbpTenantId, faasTenantId, executor);
91 tester = new DataChangeListenerTester(listener);
92 tester.setRemovedPath(contractIid);
95 @SuppressWarnings("unchecked")
97 public void testExecuteEvent() throws ReadFailedException {
98 PowerMockito.mockStatic(UlnDatastoreApi.class);
99 PowerMockito.doNothing().when(UlnDatastoreApi.class);
100 UlnDatastoreApi.submitSecurityGroupsToDs(any(SecurityRuleGroups.class));
102 ReadWriteTransaction rwTx = mock(ReadWriteTransaction.class);
103 WriteTransaction woTx = mock(WriteTransaction.class);
104 CheckedFuture<Void, TransactionCommitFailedException> futureVoid = mock(CheckedFuture.class);
105 when(rwTx.submit()).thenReturn(futureVoid);
106 when(woTx.submit()).thenReturn(futureVoid);
107 when(dataProvider.newReadWriteTransaction()).thenReturn(rwTx);
108 when(dataProvider.newWriteOnlyTransaction()).thenReturn(woTx);
110 CheckedFuture<Optional<MappedContract>, ReadFailedException> futureMappedContract = mock(CheckedFuture.class);
111 when(rwTx.read(LogicalDatastoreType.OPERATIONAL, FaasIidFactory.mappedContractIid(gbpTenantId, contractId)))
112 .thenReturn(futureMappedContract);
113 Optional<MappedContract> optMappedContract = mock(Optional.class);
114 when(optMappedContract.isPresent()).thenReturn(true);
115 when(futureMappedContract.checkedGet()).thenReturn(optMappedContract);
117 Contract contract = new ContractBuilder().setId(contractId).build();
118 tester.setDataObject(contractIid, contract);
119 listener.executeEvent(tester.getChangeMock());
123 public void testBuildSecurityRuleGroup(){
124 List<SecurityRuleGroup> securityRuleGroups;
126 Clause clause = new ClauseBuilder()
127 .setName(CLAUSE_NAME)
128 .setSubjectRefs(ImmutableList.of(SUBJECT_NAME))
131 Contract contractNoClause = new ContractBuilder()
135 securityRuleGroups = listener.buildSecurityRuleGroup(contractNoClause);
136 assertNull(securityRuleGroups);
138 Clause clauseNoSubjectRefs = new ClauseBuilder()
139 .setName(CLAUSE_NAME)
141 Contract contractClauseNoSubjectRefs = new ContractBuilder()
143 .setClause(ImmutableList.of(clauseNoSubjectRefs))
146 securityRuleGroups = listener.buildSecurityRuleGroup(contractClauseNoSubjectRefs);
147 assertNotNull(securityRuleGroups);
148 assertTrue(securityRuleGroups.isEmpty());
150 Contract contractNoSubject = new ContractBuilder()
152 .setClause(ImmutableList.of(clause))
155 securityRuleGroups = listener.buildSecurityRuleGroup(contractNoSubject);
156 assertNotNull(securityRuleGroups);
157 assertTrue(securityRuleGroups.isEmpty());
159 Contract contract = new ContractBuilder()
161 .setClause(ImmutableList.of(clause))
162 .setSubject(ImmutableList.of(new SubjectBuilder().setName(SUBJECT_NAME).build()))
165 securityRuleGroups = listener.buildSecurityRuleGroup(contract);
166 assertNotNull(securityRuleGroups);
167 assertFalse(securityRuleGroups.isEmpty());
171 @SuppressWarnings("unchecked")
173 public void testGetSecurityRules() throws ReadFailedException, ExecutionException, InterruptedException {
175 List<SecurityRule> securityRules;
177 Clause clause = new ClauseBuilder()
178 .setName(CLAUSE_NAME)
179 .setSubjectRefs(ImmutableList.of(SUBJECT_NAME))
182 Contract contract = new ContractBuilder()
184 .setClause(ImmutableList.of(clause))
185 .setSubject(ImmutableList.of(new SubjectBuilder().setName(SUBJECT_NAME).build()))
188 Rule ruleNoRefs = new RuleBuilder()
189 .setName(new RuleName("rule-no-refs"))
193 Rule ruleEmptyRefs = new RuleBuilder()
194 .setClassifierRef(ImmutableList.of())
195 .setActionRef(ImmutableList.of())
196 .setName(new RuleName("rule-empty-refs"))
200 rules = ImmutableList.of(ruleNoRefs, ruleEmptyRefs);
202 securityRules = listener.getSecurityRules(contract, SUBJECT_NAME, rules);
204 assertEquals(rules.size(), securityRules.size());
206 ClassifierName CLASSIFIER_NAME = new ClassifierName("classifier-1");
207 ClassifierRef classifierRef = new ClassifierRefBuilder()
208 .setName(CLASSIFIER_NAME)
210 Rule rule = new RuleBuilder()
211 .setClassifierRef(ImmutableList.of(classifierRef))
212 .setActionRef(ImmutableList.of())
213 .setName(new RuleName("rule-1"))
217 ReadOnlyTransaction roTx1 = mock(ReadOnlyTransaction.class);
219 CheckedFuture<Optional<ClassifierInstance>, ReadFailedException> futureClassifierInstance = mock(CheckedFuture.class);
220 Optional<ClassifierInstance> optClassifierInstance = mock(Optional.class);
221 when(optClassifierInstance.isPresent()).thenReturn(false);
222 when(futureClassifierInstance.checkedGet()).thenReturn(optClassifierInstance);
223 when(roTx1.read(eq(LogicalDatastoreType.CONFIGURATION),
224 Mockito.<InstanceIdentifier>any())).thenReturn(futureClassifierInstance);
226 rules = ImmutableList.of(rule);
227 securityRules = listener.getSecurityRules(contract, SUBJECT_NAME, rules);
229 assertEquals(rules.size(), securityRules.size());