Add missing license headers
[groupbasedpolicy.git] / neutron-mapper / src / test / java / org / opendaylight / groupbasedpolicy / neutron / mapper / test / PolicyAssert.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.groupbasedpolicy.neutron.mapper.test;
9
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
16 import java.util.Collection;
17 import java.util.List;
18 import java.util.Set;
19
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.rule.SecRuleEntityDecoder;
22 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.rule.SecRuleNameDecoder;
23 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.MappingUtils;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ActionName;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierName;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContractId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.action.refs.ActionRef;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.classifier.refs.ClassifierRef;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.Tenant;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.Policy;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.Contract;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup.IntraGroupPolicy;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.Clause;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.Subject;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.subject.Rule;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ConsumerNamedSelector;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ProviderNamedSelector;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ActionInstance;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ClassifierInstance;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRule;
44
45 import com.google.common.base.Optional;
46 import com.google.common.base.Preconditions;
47
48 public final class PolicyAssert {
49
50     private PolicyAssert() {
51         throw new UnsupportedOperationException("Cannot create an instance");
52     }
53
54     // asserts for tenant
55
56     public static void assertTenantExists(DataBroker dataBroker, String tenantId) throws Exception {
57         Optional<Tenant> tenant = ConfigDataStoreReader.readTenant(dataBroker, tenantId);
58         assertTrue(tenant.isPresent());
59     }
60
61     public static void assertTenantNotExists(DataBroker dataBroker, String tenantId) throws Exception {
62         Optional<Tenant> tenant = ConfigDataStoreReader.readTenant(dataBroker, tenantId);
63         assertFalse(tenant.isPresent());
64     }
65
66     // asserts for contract
67
68     public static void assertContractExists(DataBroker dataBroker, String tenantId, String contractId) throws Exception {
69         Optional<Contract> contract = ConfigDataStoreReader.readContract(dataBroker, tenantId, contractId);
70         assertTrue(contract.isPresent());
71     }
72
73     public static void assertContractNotExists(DataBroker dataBroker, String tenantId, String contractId)
74             throws Exception {
75         Optional<Contract> contract = ConfigDataStoreReader.readContract(dataBroker, tenantId, contractId);
76         assertFalse(contract.isPresent());
77     }
78
79     public static void assertContractCount(DataBroker dataBroker, String tenantId, int expectedCount) throws Exception {
80         Optional<Tenant> tenant = ConfigDataStoreReader.readTenant(dataBroker, tenantId);
81         assertTrue(tenant.isPresent());
82         Policy policy = tenant.get().getPolicy();
83         if (policy == null) {
84             assertEquals(expectedCount, 0);
85         } else {
86             List<Contract> contracts = policy.getContract();
87             if (contracts != null) {
88                 assertEquals(expectedCount, policy.getContract().size());
89             } else {
90                 assertEquals(expectedCount, 0);
91             }
92         }
93     }
94
95     public static void assertContractWithEic(Contract contract, SecurityRule secRule) {
96         assertEquals(new ContractId(secRule.getUuid().getValue()), contract.getId());
97         assertNull(contract.getQuality());
98         assertNull(contract.getTarget());
99         assertOneClauseWithEicWithOneSubject(contract, secRule);
100         PolicyAssert.assertOneSubjectWithOneRule(contract, secRule);
101     }
102
103     private static void assertOneClauseWithEicWithOneSubject(Contract contract, SecurityRule secRule) {
104         Clause clause = assertOneItem(contract.getClause());
105         assertNull(clause.getAnyMatchers());
106         IpPrefix expectedIpPrefix = secRule.getRemoteIpPrefix();
107         assertNotNull(clause.getConsumerMatchers());
108         IpPrefix ipPrefix = clause.getConsumerMatchers()
109             .getEndpointIdentificationConstraints()
110             .getL3EndpointIdentificationConstraints()
111             .getPrefixConstraint()
112             .get(0)
113             .getIpPrefix();
114         assertEquals(expectedIpPrefix, ipPrefix);
115         SubjectName subjectRef = assertOneItem(clause.getSubjectRefs());
116         assertEquals(SecRuleNameDecoder.getSubjectName(secRule), subjectRef);
117     }
118
119     public static void assertContract(Contract contract, SecurityRule secRule) {
120         assertEquals(new ContractId(secRule.getUuid().getValue()), contract.getId());
121         assertNull(contract.getQuality());
122         assertNull(contract.getTarget());
123         assertOneClauseWithOneSubject(contract, secRule);
124         assertOneSubjectWithOneRule(contract, secRule);
125     }
126
127     private static void assertOneClauseWithOneSubject(Contract contract, SecurityRule secRule) {
128         Clause clause = assertOneItem(contract.getClause());
129         assertClauseWithOneSubject(clause, secRule);
130     }
131
132     private static void assertOneSubjectWithOneRule(Contract contract, SecurityRule secRule) {
133         Subject subject = assertOneItem(contract.getSubject());
134         assertSubjectWithOneRule(subject, secRule);
135     }
136
137     public static void assertEndpointGroupExists(DataBroker dataBroker, String tenantId, String endpointGroupId)
138             throws Exception {
139         Optional<EndpointGroup> epg = ConfigDataStoreReader.readEndpointGroup(dataBroker, tenantId, endpointGroupId);
140         assertTrue(epg.isPresent());
141     }
142
143     public static void assertEndpointGroupNotExists(DataBroker dataBroker, String tenantId, String endpointGroupId)
144             throws Exception {
145         Optional<EndpointGroup> epg = ConfigDataStoreReader.readEndpointGroup(dataBroker, tenantId, endpointGroupId);
146         assertFalse(epg.isPresent());
147     }
148
149     public static void assertEndpointGroupCount(DataBroker dataBroker, String tenantId, int expectedCount)
150             throws Exception {
151         Optional<Tenant> tenant = ConfigDataStoreReader.readTenant(dataBroker, tenantId);
152         assertTrue(tenant.isPresent());
153         Policy policy = tenant.get().getPolicy();
154         if (policy == null) {
155             assertEquals(expectedCount, 0);
156         } else {
157             List<EndpointGroup> endpointGroups = policy.getEndpointGroup();
158             if (endpointGroups != null) {
159                 assertEquals(expectedCount, endpointGroups.size());
160             } else {
161                 assertEquals(expectedCount, 0);
162             }
163         }
164     }
165
166     public static void assertIntraGroupPolicy(DataBroker dataBroker, String tenantId, String endpointGroupId,
167             IntraGroupPolicy intraGroupPolicy) throws Exception {
168         Optional<EndpointGroup> epg = ConfigDataStoreReader.readEndpointGroup(dataBroker, tenantId, endpointGroupId);
169         assertTrue(epg.isPresent());
170         assertEquals(intraGroupPolicy, epg.get().getIntraGroupPolicy());
171     }
172
173     // asserts for endpoint group selectors
174
175     public static void assertNoProviderNamedSelectors(DataBroker dataBroker, String tenantId, String secGroupId)
176             throws Exception {
177         Optional<EndpointGroup> epg = ConfigDataStoreReader.readEndpointGroup(dataBroker, tenantId, secGroupId);
178         assertTrue(epg.isPresent());
179         List<ProviderNamedSelector> selectors = epg.get().getProviderNamedSelector();
180         assertTrue(selectors == null || selectors.isEmpty());
181     }
182
183     public static void assertNoConsumerNamedSelectors(DataBroker dataBroker, String tenantId, String secGroupId)
184             throws Exception {
185         Optional<EndpointGroup> epg = ConfigDataStoreReader.readEndpointGroup(dataBroker, tenantId, secGroupId);
186         assertTrue(epg.isPresent());
187         List<ConsumerNamedSelector> selectors = epg.get().getConsumerNamedSelector();
188         assertTrue(selectors == null || selectors.isEmpty());
189     }
190
191     public static void assertProviderNamedSelectors(EndpointGroup epg, Set<ContractId> expectedContracts) {
192         Preconditions.checkNotNull(expectedContracts);
193         assertNotNull(epg.getProviderNamedSelector());
194         int numberOfContracts = 0;
195         for (ProviderNamedSelector pns : epg.getProviderNamedSelector()) {
196             assertNotNull(pns.getContract());
197             numberOfContracts += pns.getContract().size();
198             for (ContractId contractId : pns.getContract()) {
199                 assertTrue(expectedContracts.contains(contractId));
200             }
201         }
202         assertEquals(expectedContracts.size(), numberOfContracts);
203     }
204
205     public static void assertConsumerNamedSelectors(EndpointGroup epg, Set<ContractId> expectedContracts) {
206         Preconditions.checkNotNull(expectedContracts);
207         assertNotNull(epg.getConsumerNamedSelector());
208         int numberOfContracts = 0;
209         for (ConsumerNamedSelector cns : epg.getConsumerNamedSelector()) {
210             assertNotNull(cns.getContract());
211             numberOfContracts += cns.getContract().size();
212             for (ContractId contractId : cns.getContract()) {
213                 assertTrue(expectedContracts.contains(contractId));
214             }
215         }
216         assertEquals(expectedContracts.size(), numberOfContracts);
217     }
218
219     // asserts for classifier
220
221     public static void assertClassifierInstanceExists(DataBroker dataBroker, SecurityRule secRule)
222             throws Exception {
223         ClassifierInstance clsfInstance = SecRuleEntityDecoder.getClassifierInstance(secRule);
224         Optional<ClassifierInstance> readClsfInstance = ConfigDataStoreReader.readClassifierInstance(dataBroker,
225                 secRule.getTenantId().getValue(), clsfInstance.getName());
226         assertTrue(readClsfInstance.isPresent());
227     }
228
229     public static void assertClassifierInstanceExists(DataBroker dataBroker, String tenantId, String classifierName)
230             throws Exception {
231         Optional<ClassifierInstance> classifierInstance = ConfigDataStoreReader.readClassifierInstance(dataBroker,
232                 tenantId, new ClassifierName(classifierName));
233         assertTrue(classifierInstance.isPresent());
234     }
235
236     public static void assertClassifierInstanceNotExists(DataBroker dataBroker, SecurityRule secRule)
237             throws Exception {
238         ClassifierInstance clsfInstance = SecRuleEntityDecoder.getClassifierInstance(secRule);
239         Optional<ClassifierInstance> readClsfInstance = ConfigDataStoreReader.readClassifierInstance(dataBroker,
240                 secRule.getTenantId().getValue(), clsfInstance.getName());
241         assertFalse(readClsfInstance.isPresent());
242     }
243
244     // asserts for action
245
246     public static void assertActionInstanceExists(DataBroker dataBroker, String tenantId, ActionName actionName)
247             throws Exception {
248         Optional<ActionInstance> actionInstance = ConfigDataStoreReader.readActionInstance(dataBroker, tenantId,
249                 actionName);
250         assertTrue(actionInstance.isPresent());
251     }
252
253     public static void assertActionInstanceNotExists(DataBroker dataBroker, String tenantId, ActionName actionName)
254             throws Exception {
255         Optional<ActionInstance> actionInstance = ConfigDataStoreReader.readActionInstance(dataBroker, tenantId,
256                 actionName);
257         assertFalse(actionInstance.isPresent());
258     }
259
260     // asserts for clause
261
262     public static void assertClauseWithOneSubject(Clause clause, SecurityRule secRule) {
263         assertNull(clause.getAnyMatchers());
264         assertNull(clause.getConsumerMatchers());
265         assertNull(clause.getProviderMatchers());
266         SubjectName subjectRef = assertOneItem(clause.getSubjectRefs());
267         assertEquals(SecRuleNameDecoder.getSubjectName(secRule), subjectRef);
268     }
269
270     public static void assertClauseExists(DataBroker dataBroker, String tenantId, String contractId, String clauseName) {
271         Optional<Clause> clause = ConfigDataStoreReader.readClause(dataBroker, tenantId, contractId, clauseName);
272         assertTrue(clause.isPresent());
273     }
274
275     // asserts for subject
276
277     public static void assertSubjectWithOneRule(Subject subject, SecurityRule secRule) {
278         assertEquals(SecRuleNameDecoder.getSubjectName(secRule), subject.getName());
279         Rule rule = assertOneItem(subject.getRule());
280         assertRule(rule, secRule);
281     }
282
283     // asserts for rule
284
285     public static void assertRule(Rule rule, SecurityRule secRule) {
286         assertEquals(SecRuleNameDecoder.getRuleName(secRule), rule.getName());
287         ActionRef actionRef = assertOneItem(rule.getActionRef());
288         assertEquals(MappingUtils.ACTION_ALLOW.getName(), actionRef.getName());
289         ClassifierRef classifierRef = assertOneItem(rule.getClassifierRef());
290         assertEquals(SecRuleNameDecoder.getClassifierRefName(secRule), classifierRef.getName());
291         assertEquals(SecRuleNameDecoder.getClassifierInstanceName(secRule), classifierRef.getInstanceName());
292         assertEquals(SecRuleEntityDecoder.getDirection(secRule), classifierRef.getDirection());
293     }
294
295     public static void assertRule(Rule rule, SecurityRule secRule, int order) {
296         assertRule(rule, secRule);
297         assertEquals(order, rule.getOrder().intValue());
298     }
299
300     private static <T> T assertOneItem(Collection<T> c) {
301         assertNotNull(c);
302         assertTrue(c.size() == 1);
303         return c.iterator().next();
304     }
305
306     // asserts for selector
307
308     public static void assertConsumerNamedSelectorExists(DataBroker dataBroker, String tenantId, String egId,
309                                                          String selectorName) {
310         Optional<ConsumerNamedSelector> potentialCns = ConfigDataStoreReader.readConsumerNamedSelector(dataBroker,
311                 tenantId, egId, selectorName);
312         assertTrue(potentialCns.isPresent());
313     }
314
315 }