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