Bug 5483 - added support for num values in rules
[groupbasedpolicy.git] / neutron-mapper / src / test / java / org / opendaylight / groupbasedpolicy / neutron / mapper / mapping / rule / SecRuleEntityDecoderTest.java
1 package org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.rule;
2
3 import static org.junit.Assert.assertFalse;
4 import static org.junit.Assert.assertTrue;
5 import static org.junit.Assert.fail;
6
7 import java.util.List;
8
9 import org.junit.Assert;
10 import org.junit.Before;
11 import org.junit.Rule;
12 import org.junit.Test;
13 import org.junit.rules.ExpectedException;
14 import org.opendaylight.groupbasedpolicy.api.sf.EtherTypeClassifierDefinition;
15 import org.opendaylight.groupbasedpolicy.api.sf.IpProtoClassifierDefinition;
16 import org.opendaylight.groupbasedpolicy.api.sf.L4ClassifierDefinition;
17 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.MappingUtils;
18 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.NeutronUtils;
19 import org.opendaylight.neutron.spi.NeutronSecurityRule;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
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.ClauseName;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ParameterName;
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.HasDirection.Direction;
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.has.endpoint.identification.constraints.EndpointIdentificationConstraints;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.endpoint.identification.constraints.L3EndpointIdentificationConstraints;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.endpoint.identification.constraints.l3.endpoint.identification.constraints.PrefixConstraint;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.parameter.value.RangeValue;
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.clause.ConsumerMatchers;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ClassifierInstance;
35
36 public class SecRuleEntityDecoderTest {
37
38     @Rule
39     public ExpectedException thrown = ExpectedException.none();
40
41     private NeutronSecurityRule secRule;
42
43     @Before
44     public void setUp() throws Exception {
45         secRule = new NeutronSecurityRule();
46     }
47
48     @Test
49     public final void testGetTenantId_lowercaseUuidTenantID() {
50         secRule.setSecurityRuleTenantID("01234567-abcd-ef01-0123-0123456789ab");
51         Assert.assertEquals("01234567-abcd-ef01-0123-0123456789ab",
52                 SecRuleEntityDecoder.getTenantId(secRule).getValue());
53     }
54
55     @Test
56     public final void testGetTenantId_uppercaseUuidTenantID() {
57         secRule.setSecurityRuleTenantID("01234567-ABCD-EF01-0123-0123456789AB");
58         Assert.assertEquals("01234567-ABCD-EF01-0123-0123456789AB",
59                 SecRuleEntityDecoder.getTenantId(secRule).getValue());
60     }
61
62     @Test
63     public final void testGetTenantId_mixUuidTenantID() {
64         secRule.setSecurityRuleTenantID("01234567-ABCD-ef01-0123-0123456789Ab");
65         Assert.assertEquals("01234567-ABCD-ef01-0123-0123456789Ab",
66                 SecRuleEntityDecoder.getTenantId(secRule).getValue());
67     }
68
69     @Test
70     public final void testGetTenantId_noSlashLowercaseUuidTenantID() {
71         secRule.setSecurityRuleTenantID("01234567abcdef0101230123456789ab");
72         Assert.assertEquals("01234567-abcd-ef01-0123-0123456789ab",
73                 SecRuleEntityDecoder.getTenantId(secRule).getValue());
74     }
75
76     @Test
77     public final void testGetTenantId_noSlashUppercaseUuidTenantID() {
78         secRule.setSecurityRuleTenantID("01234567ABCDEF0101230123456789AB");
79         Assert.assertEquals("01234567-ABCD-EF01-0123-0123456789AB",
80                 SecRuleEntityDecoder.getTenantId(secRule).getValue());
81     }
82
83     @Test
84     public final void testGetTenantId_noSlashMixUuidTenantID() {
85         secRule.setSecurityRuleTenantID("01234567ABCDef0101230123456789Ab");
86         Assert.assertEquals("01234567-ABCD-ef01-0123-0123456789Ab",
87                 SecRuleEntityDecoder.getTenantId(secRule).getValue());
88     }
89
90     @Test
91     public final void testGetTenantId_emptyUuidTenantID() {
92         secRule.setSecurityRuleTenantID("");
93         thrown.expect(NullPointerException.class);
94         SecRuleEntityDecoder.getTenantId(secRule);
95     }
96
97     @Test
98     public final void testGetTenantId_nullUuidTenantID() {
99         secRule.setSecurityRuleTenantID(null);
100         thrown.expect(NullPointerException.class);
101         SecRuleEntityDecoder.getTenantId(secRule);
102     }
103
104     @Test
105     public final void testGetProviderEpgId_lowercaseUuidGroupID() {
106         secRule.setSecurityRuleGroupID("01234567-abcd-ef01-0123-0123456789ab");
107         Assert.assertEquals("01234567-abcd-ef01-0123-0123456789ab",
108                 SecRuleEntityDecoder.getProviderEpgId(secRule).getValue());
109     }
110
111     @Test
112     public final void testGetProviderEpgId_uppercaseUuidGroupID() {
113         secRule.setSecurityRuleGroupID("01234567-ABCD-EF01-0123-0123456789AB");
114         Assert.assertEquals("01234567-ABCD-EF01-0123-0123456789AB",
115                 SecRuleEntityDecoder.getProviderEpgId(secRule).getValue());
116     }
117
118     @Test
119     public final void testGetProviderEpgId_mixUuidGroupID() {
120         secRule.setSecurityRuleGroupID("01234567-ABCD-ef01-0123-0123456789Ab");
121         Assert.assertEquals("01234567-ABCD-ef01-0123-0123456789Ab",
122                 SecRuleEntityDecoder.getProviderEpgId(secRule).getValue());
123     }
124
125     @Test
126     public final void testGetProviderEpgId_noSlashLowercaseUuidGroupID() {
127         secRule.setSecurityRuleGroupID("01234567abcdef0101230123456789ab");
128         Assert.assertEquals("01234567-abcd-ef01-0123-0123456789ab",
129                 SecRuleEntityDecoder.getProviderEpgId(secRule).getValue());
130     }
131
132     @Test
133     public final void testGetProviderEpgId_noSlashUppercaseUuidGroupID() {
134         secRule.setSecurityRuleGroupID("01234567ABCDEF0101230123456789AB");
135         Assert.assertEquals("01234567-ABCD-EF01-0123-0123456789AB",
136                 SecRuleEntityDecoder.getProviderEpgId(secRule).getValue());
137     }
138
139     @Test
140     public final void testGetProviderEpgId_noSlashMixUuidGroupID() {
141         secRule.setSecurityRuleGroupID("01234567ABCDef0101230123456789Ab");
142         Assert.assertEquals("01234567-ABCD-ef01-0123-0123456789Ab",
143                 SecRuleEntityDecoder.getProviderEpgId(secRule).getValue());
144     }
145
146     @Test
147     public final void testGetProviderEpgId_emptyUuidGroupID() {
148         secRule.setSecurityRuleGroupID("");
149         thrown.expect(IllegalArgumentException.class);
150         SecRuleEntityDecoder.getProviderEpgId(secRule);
151     }
152
153     @Test
154     public final void testGetProviderEpgId_nullUuidGroupID() {
155         secRule.setSecurityRuleGroupID(null);
156         thrown.expect(NullPointerException.class);
157         SecRuleEntityDecoder.getProviderEpgId(secRule);
158     }
159
160     @Test
161     public final void testGetConsumerEpgId_lowercaseUuidRemoteGroupID() {
162         secRule.setSecurityRemoteGroupID("01234567-abcd-ef01-0123-0123456789ab");
163         Assert.assertEquals("01234567-abcd-ef01-0123-0123456789ab",
164                 SecRuleEntityDecoder.getConsumerEpgId(secRule).getValue());
165     }
166
167     @Test
168     public final void testGetConsumerEpgId_uppercaseUuidRemoteGroupID() {
169         secRule.setSecurityRemoteGroupID("01234567-ABCD-EF01-0123-0123456789AB");
170         Assert.assertEquals("01234567-ABCD-EF01-0123-0123456789AB",
171                 SecRuleEntityDecoder.getConsumerEpgId(secRule).getValue());
172     }
173
174     @Test
175     public final void testGetConsumerEpgId_mixUuidRemoteGroupID() {
176         secRule.setSecurityRemoteGroupID("01234567-ABCD-ef01-0123-0123456789Ab");
177         Assert.assertEquals("01234567-ABCD-ef01-0123-0123456789Ab",
178                 SecRuleEntityDecoder.getConsumerEpgId(secRule).getValue());
179     }
180
181     @Test
182     public final void testGetConsumerEpgId_noSlashLowercaseUuidRemoteGroupID() {
183         secRule.setSecurityRemoteGroupID("01234567abcdef0101230123456789ab");
184         Assert.assertEquals("01234567-abcd-ef01-0123-0123456789ab",
185                 SecRuleEntityDecoder.getConsumerEpgId(secRule).getValue());
186     }
187
188     @Test
189     public final void testGetConsumerEpgId_noSlashUppercaseUuidRemoteGroupID() {
190         secRule.setSecurityRemoteGroupID("01234567ABCDEF0101230123456789AB");
191         Assert.assertEquals("01234567-ABCD-EF01-0123-0123456789AB",
192                 SecRuleEntityDecoder.getConsumerEpgId(secRule).getValue());
193     }
194
195     @Test
196     public final void testGetConsumerEpgId_noSlashMixUuidRemoteGroupID() {
197         secRule.setSecurityRemoteGroupID("01234567ABCDef0101230123456789Ab");
198         Assert.assertEquals("01234567-ABCD-ef01-0123-0123456789Ab",
199                 SecRuleEntityDecoder.getConsumerEpgId(secRule).getValue());
200     }
201
202     @Test
203     public final void testGetConsumerEpgId_emptyUuidRemoteGroupID() {
204         secRule.setSecurityRemoteGroupID("");
205         Assert.assertSame(null, SecRuleEntityDecoder.getConsumerEpgId(secRule));
206     }
207
208     @Test
209     public final void testGetConsumerEpgId_nullUuidRemoteGroupID() {
210         secRule.setSecurityRemoteGroupID(null);
211         Assert.assertSame(null, SecRuleEntityDecoder.getConsumerEpgId(secRule));
212     }
213
214     @Test
215     public final void testGetContractId_lowercaseUuidID() {
216         secRule.setSecurityRuleUUID("01234567-abcd-ef01-0123-0123456789ab");
217         Assert.assertEquals("01234567-abcd-ef01-0123-0123456789ab",
218                 SecRuleEntityDecoder.getContractId(secRule).getValue());
219     }
220
221     @Test
222     public final void testGetContractId_uppercaseUuidID() {
223         secRule.setSecurityRuleUUID("01234567-ABCD-EF01-0123-0123456789AB");
224         Assert.assertEquals("01234567-ABCD-EF01-0123-0123456789AB",
225                 SecRuleEntityDecoder.getContractId(secRule).getValue());
226     }
227
228     @Test
229     public final void testGetContractId_mixUuidID() {
230         secRule.setSecurityRuleUUID("01234567-ABCD-ef01-0123-0123456789Ab");
231         Assert.assertEquals("01234567-ABCD-ef01-0123-0123456789Ab",
232                 SecRuleEntityDecoder.getContractId(secRule).getValue());
233     }
234
235     @Test
236     public final void testGetClassifierInstance_onlyEthertype() {
237         secRule.setSecurityRuleEthertype(NeutronUtils.IPv4);
238         ClassifierInstance ci = SecRuleEntityDecoder.getClassifierInstance(secRule);
239         Assert.assertEquals(EtherTypeClassifierDefinition.DEFINITION.getId(), ci.getClassifierDefinitionId());
240         // name is ether_type_IPv4
241         String expectedName = new StringBuilder().append(EtherTypeClassifierDefinition.DEFINITION.getName().getValue())
242             .append(MappingUtils.NAME_VALUE_DELIMETER)
243             .append(NeutronUtils.IPv4)
244             .toString();
245         Assert.assertEquals(expectedName, ci.getName().getValue());
246         Assert.assertEquals(expectedName, ci.getName().getValue());
247         List<ParameterValue> parameterValues = ci.getParameterValue();
248         Assert.assertNotNull(parameterValues);
249         Assert.assertEquals(1, parameterValues.size());
250         ParameterValue parameter = parameterValues.get(0);
251         Assert.assertEquals(EtherTypeClassifierDefinition.ETHERTYPE_PARAM, parameter.getName().getValue());
252         assertClassifierParameterValue(parameter, EtherTypeClassifierDefinition.IPv4_VALUE, null, null);
253     }
254
255     @Test
256     public final void testGetClassifierInstance_EthertypeAndProtocol() {
257         secRule.setSecurityRuleEthertype(NeutronUtils.IPv4);
258         secRule.setSecurityRuleProtocol(NeutronUtils.TCP);
259         ClassifierInstance ci = SecRuleEntityDecoder.getClassifierInstance(secRule);
260         Assert.assertEquals(IpProtoClassifierDefinition.DEFINITION.getId(), ci.getClassifierDefinitionId());
261         // name is ip_proto_tcp__ether_type_IPv4
262         String expectedName = new StringBuilder().append(IpProtoClassifierDefinition.DEFINITION.getName().getValue())
263             .append(MappingUtils.NAME_VALUE_DELIMETER)
264             .append(NeutronUtils.TCP)
265             .append(MappingUtils.NAME_DOUBLE_DELIMETER)
266             .append(EtherTypeClassifierDefinition.DEFINITION.getName().getValue())
267             .append(MappingUtils.NAME_VALUE_DELIMETER)
268             .append(NeutronUtils.IPv4)
269             .toString();
270         Assert.assertEquals(expectedName, ci.getName().getValue());
271         List<ParameterValue> parameterValues = ci.getParameterValue();
272         Assert.assertNotNull(parameterValues);
273         Assert.assertEquals(2, parameterValues.size());
274         boolean containsEthertypeParam = false;
275         boolean containsProtoParam = false;
276         for (ParameterValue parameter : parameterValues) {
277             ParameterName parameterName = parameter.getName();
278             Assert.assertNotNull(parameterName);
279             if (EtherTypeClassifierDefinition.ETHERTYPE_PARAM.equals(parameterName.getValue())) {
280                 containsEthertypeParam = true;
281                 assertClassifierParameterValue(parameter, EtherTypeClassifierDefinition.IPv4_VALUE, null, null);
282             } else if (IpProtoClassifierDefinition.PROTO_PARAM.equals(parameterName.getValue())) {
283                 containsProtoParam = true;
284                 assertClassifierParameterValue(parameter, IpProtoClassifierDefinition.TCP_VALUE, null, null);
285             } else {
286                 fail("This parameter is not expected: " + parameter);
287             }
288         }
289         Assert.assertTrue("Classifier-instance does not contain ethertype parameter", containsEthertypeParam);
290         Assert.assertTrue("Classifier-instance does not contain protocol parameter", containsProtoParam);
291     }
292
293     @Test
294     public final void testGetClassifierInstance_EthertypeAndProtocolAndPort() {
295         secRule.setSecurityRuleEthertype(NeutronUtils.IPv4);
296         secRule.setSecurityRuleProtocol(NeutronUtils.TCP);
297         secRule.setSecurityRulePortMin(5);
298         secRule.setSecurityRulePortMax(5);
299         ClassifierInstance ci = SecRuleEntityDecoder.getClassifierInstance(secRule);
300         Assert.assertEquals(L4ClassifierDefinition.DEFINITION.getId(), ci.getClassifierDefinitionId());
301         // name is l4_destport-5__ip_proto-tcp__ether_type-IPv4
302         String expectedName = new StringBuilder().append(L4ClassifierDefinition.DEFINITION.getName().getValue())
303             .append(MappingUtils.NAME_DELIMETER)
304             .append(L4ClassifierDefinition.DST_PORT_PARAM)
305             .append(MappingUtils.NAME_VALUE_DELIMETER)
306             .append(secRule.getSecurityRulePortMin())
307             .append(MappingUtils.NAME_DOUBLE_DELIMETER)
308             .append(IpProtoClassifierDefinition.DEFINITION.getName().getValue())
309             .append(MappingUtils.NAME_VALUE_DELIMETER)
310             .append(NeutronUtils.TCP)
311             .append(MappingUtils.NAME_DOUBLE_DELIMETER)
312             .append(EtherTypeClassifierDefinition.DEFINITION.getName().getValue())
313             .append(MappingUtils.NAME_VALUE_DELIMETER)
314             .append(NeutronUtils.IPv4)
315             .toString();
316         Assert.assertEquals(expectedName, ci.getName().getValue());
317         List<ParameterValue> parameterValues = ci.getParameterValue();
318         Assert.assertNotNull(parameterValues);
319         Assert.assertEquals(3, parameterValues.size());
320         boolean containsEthertypeParam = false;
321         boolean containsProtoParam = false;
322         boolean containsDstPortParam = false;
323         for (ParameterValue parameter : parameterValues) {
324             ParameterName parameterName = parameter.getName();
325             Assert.assertNotNull(parameterName);
326             if (EtherTypeClassifierDefinition.ETHERTYPE_PARAM.equals(parameterName.getValue())) {
327                 containsEthertypeParam = true;
328                 assertClassifierParameterValue(parameter, EtherTypeClassifierDefinition.IPv4_VALUE, null, null);
329             } else if (IpProtoClassifierDefinition.PROTO_PARAM.equals(parameterName.getValue())) {
330                 containsProtoParam = true;
331                 assertClassifierParameterValue(parameter, IpProtoClassifierDefinition.TCP_VALUE, null, null);
332             } else if (L4ClassifierDefinition.DST_PORT_PARAM.equals(parameterName.getValue())) {
333                 containsDstPortParam = true;
334                 assertClassifierParameterValue(parameter, 5L, null, null);
335             } else {
336                 fail("This parameter is not expected: " + parameter);
337             }
338         }
339         Assert.assertTrue("Classifier-instance does not contain ethertype parameter", containsEthertypeParam);
340         Assert.assertTrue("Classifier-instance does not contain protocol parameter", containsProtoParam);
341         Assert.assertTrue("Classifier-instance does not contain destination port parameter", containsDstPortParam);
342     }
343
344     private final void assertClassifierParameterValue(ParameterValue parameter, Long expectedIntValue,
345             String expectedStringValue, RangeValue expectedRangeValue) {
346         Assert.assertEquals(expectedIntValue, parameter.getIntValue());
347         Assert.assertEquals(expectedStringValue, parameter.getStringValue());
348         Assert.assertEquals(expectedRangeValue, parameter.getRangeValue());
349     }
350
351     @Test
352     public final void testGetClassifierInstance_EthertypeAndProtocolAndPorts() {
353         secRule.setSecurityRuleEthertype(NeutronUtils.IPv4);
354         secRule.setSecurityRuleProtocol(NeutronUtils.TCP);
355         secRule.setSecurityRulePortMin(5);
356         secRule.setSecurityRulePortMax(10);
357         ClassifierInstance ci = SecRuleEntityDecoder.getClassifierInstance(secRule);
358         Assert.assertEquals(L4ClassifierDefinition.DEFINITION.getId(), ci.getClassifierDefinitionId());
359         // name is l4_destport_range_min-5_max-10__ip_proto-tcp__ether_type-IPv4
360         String expectedName = new StringBuilder().append(L4ClassifierDefinition.DEFINITION.getName().getValue())
361             .append(MappingUtils.NAME_DELIMETER)
362             .append(L4ClassifierDefinition.DST_PORT_RANGE_PARAM)
363             .append(SecRuleNameDecoder.MIN_PORT)
364             .append(MappingUtils.NAME_VALUE_DELIMETER)
365             .append(secRule.getSecurityRulePortMin())
366             .append(SecRuleNameDecoder.MAX_PORT)
367             .append(MappingUtils.NAME_VALUE_DELIMETER)
368             .append(secRule.getSecurityRulePortMax())
369             .append(MappingUtils.NAME_DOUBLE_DELIMETER)
370             .append(IpProtoClassifierDefinition.DEFINITION.getName().getValue())
371             .append(MappingUtils.NAME_VALUE_DELIMETER)
372             .append(NeutronUtils.TCP)
373             .append(MappingUtils.NAME_DOUBLE_DELIMETER)
374             .append(EtherTypeClassifierDefinition.DEFINITION.getName().getValue())
375             .append(MappingUtils.NAME_VALUE_DELIMETER)
376             .append(NeutronUtils.IPv4)
377             .toString();
378         Assert.assertEquals(expectedName, ci.getName().getValue());
379     }
380
381     @Test
382     public final void testGetClassifierRef() {
383         secRule.setSecurityRuleDirection(NeutronUtils.INGRESS);
384         secRule.setSecurityRuleEthertype(NeutronUtils.IPv4);
385         ClassifierName expectedName = SecRuleNameDecoder.getClassifierRefName(secRule);
386         ClassifierRef cr = SecRuleEntityDecoder.getClassifierRef(secRule);
387         Assert.assertEquals(expectedName, cr.getName());
388     }
389
390     @Test
391     public final void testGetDirection_directionIngress() {
392         secRule.setSecurityRuleDirection(NeutronUtils.INGRESS);
393         Assert.assertEquals(Direction.In, SecRuleEntityDecoder.getDirection(secRule));
394     }
395
396     @Test
397     public final void testGetDirection_directionEgress() {
398         secRule.setSecurityRuleDirection(NeutronUtils.EGRESS);
399         Assert.assertEquals(Direction.Out, SecRuleEntityDecoder.getDirection(secRule));
400     }
401
402     @Test
403     public final void testGetDirection_directionNull() {
404         secRule.setSecurityRuleDirection(null);
405         thrown.expect(IllegalArgumentException.class);
406         SecRuleEntityDecoder.getDirection(secRule);
407     }
408
409     @Test
410     public final void testGetDirection_directionUnknown() {
411         secRule.setSecurityRuleDirection("foo");
412         thrown.expect(IllegalArgumentException.class);
413         SecRuleEntityDecoder.getDirection(secRule);
414     }
415
416     @Test
417     public final void testGetClause_noRemoteIpPrefix() {
418         secRule.setSecurityRuleDirection(NeutronUtils.INGRESS);
419         secRule.setSecurityRuleEthertype(NeutronUtils.IPv4);
420         ClauseName expectedClauseName = SecRuleNameDecoder.getClauseName(secRule);
421         Clause clause = SecRuleEntityDecoder.getClause(secRule);
422         Assert.assertEquals(expectedClauseName, clause.getName());
423         List<SubjectName> subjectRefs = clause.getSubjectRefs();
424         Assert.assertNotNull(subjectRefs);
425         Assert.assertEquals(1, subjectRefs.size());
426         SubjectName subjectNameFromClause = subjectRefs.get(0);
427         SubjectName expectedSubjectName = SecRuleNameDecoder.getSubjectName(secRule);
428         Assert.assertEquals(expectedSubjectName, subjectNameFromClause);
429         Assert.assertNull(clause.getConsumerMatchers());
430         Assert.assertNull(clause.getProviderMatchers());
431     }
432
433     @Test
434     public final void testGetClause_remoteIpPrefix() {
435         secRule.setSecurityRuleDirection(NeutronUtils.INGRESS);
436         secRule.setSecurityRuleEthertype(NeutronUtils.IPv4);
437         secRule.setSecurityRuleRemoteIpPrefix("10.0.0.0/8");
438         Clause clause = SecRuleEntityDecoder.getClause(secRule);
439         ClauseName expectedClauseName = SecRuleNameDecoder.getClauseName(secRule);
440         Assert.assertEquals(expectedClauseName, clause.getName());
441         List<SubjectName> subjectRefs = clause.getSubjectRefs();
442         Assert.assertNotNull(subjectRefs);
443         Assert.assertEquals(1, subjectRefs.size());
444         SubjectName subjectNameFromClause = subjectRefs.get(0);
445         SubjectName expectedSubjectName = SecRuleNameDecoder.getSubjectName(secRule);
446         Assert.assertEquals(expectedSubjectName, subjectNameFromClause);
447         Assert.assertNull(clause.getProviderMatchers());
448         ConsumerMatchers consumerMatchers = clause.getConsumerMatchers();
449         Assert.assertNotNull(consumerMatchers);
450         Assert.assertNull(consumerMatchers.getConditionMatcher());
451         Assert.assertNull(consumerMatchers.getGroupIdentificationConstraints());
452         EndpointIdentificationConstraints endpointIdentificationConstraints =
453                 consumerMatchers.getEndpointIdentificationConstraints();
454         Assert.assertNotNull(endpointIdentificationConstraints);
455         L3EndpointIdentificationConstraints l3EndpointIdentificationConstraints =
456                 endpointIdentificationConstraints.getL3EndpointIdentificationConstraints();
457         Assert.assertNotNull(l3EndpointIdentificationConstraints);
458         List<PrefixConstraint> prefixConstraints = l3EndpointIdentificationConstraints.getPrefixConstraint();
459         Assert.assertNotNull(prefixConstraints);
460         Assert.assertEquals(1, prefixConstraints.size());
461         PrefixConstraint prefixConstraint = prefixConstraints.get(0);
462         Assert.assertEquals(new Ipv4Prefix("10.0.0.0/8"), prefixConstraint.getIpPrefix().getIpv4Prefix());
463     }
464
465     @Test
466     public final void testIsEtherTypeOfOneWithinTwo_oneEthTypeIPv4TwoEthTypeIPv4() {
467         NeutronSecurityRule one = createSecRuleWithEtherType(NeutronUtils.IPv4);
468         NeutronSecurityRule two = createSecRuleWithEtherType(NeutronUtils.IPv4);
469         assertTrue(SecRuleEntityDecoder.isEtherTypeOfOneWithinTwo(one, two));
470     }
471
472     @Test
473     public final void testIsEtherTypeOfOneWithinTwo_oneEthTypeIPv4TwoEthTypeNull() {
474         NeutronSecurityRule one = createSecRuleWithEtherType(NeutronUtils.IPv4);
475         NeutronSecurityRule two = createSecRuleWithEtherType(null);
476         assertTrue(SecRuleEntityDecoder.isEtherTypeOfOneWithinTwo(one, two));
477     }
478
479     @Test
480     public final void testIsEtherTypeOfOneWithinTwo_oneEthTypeNullTwoEthTypeNull() {
481         NeutronSecurityRule one = createSecRuleWithEtherType(null);
482         NeutronSecurityRule two = createSecRuleWithEtherType(null);
483         assertTrue(SecRuleEntityDecoder.isEtherTypeOfOneWithinTwo(one, two));
484     }
485
486     @Test
487     public final void testIsEtherTypeOfOneWithinTwo_oneEthTypeIPv4TwoEthTypeIPv6() {
488         NeutronSecurityRule one = createSecRuleWithEtherType(NeutronUtils.IPv4);
489         NeutronSecurityRule two = createSecRuleWithEtherType(NeutronUtils.IPv6);
490         assertFalse(SecRuleEntityDecoder.isEtherTypeOfOneWithinTwo(one, two));
491     }
492
493     @Test
494     public final void testIsEtherTypeOfOneWithinTwo_oneEthTypeNullTwoEthTypeIPv4() {
495         NeutronSecurityRule one = createSecRuleWithEtherType(null);
496         NeutronSecurityRule two = createSecRuleWithEtherType(NeutronUtils.IPv4);
497         assertFalse(SecRuleEntityDecoder.isEtherTypeOfOneWithinTwo(one, two));
498     }
499
500     private final NeutronSecurityRule createSecRuleWithEtherType(String etherType) {
501         NeutronSecurityRule secRule = new NeutronSecurityRule();
502         secRule.setSecurityRuleEthertype(etherType);
503         return secRule;
504     }
505
506     @Test
507     public void testIsProtocolOfOneWithinTwo_oneProtocolTcpTwoProtocolTcp() {
508         NeutronSecurityRule one = createSecRuleWithProtocol(NeutronUtils.TCP);
509         NeutronSecurityRule two = createSecRuleWithProtocol(NeutronUtils.TCP);
510         assertTrue(SecRuleEntityDecoder.isProtocolOfOneWithinTwo(one, two));
511     }
512
513     @Test
514     public void testIsProtocolOfOneWithinTwo_oneProtocolTcpTwoProtocolNull() {
515         NeutronSecurityRule one = createSecRuleWithProtocol(NeutronUtils.TCP);
516         NeutronSecurityRule two = createSecRuleWithProtocol(null);
517         assertTrue(SecRuleEntityDecoder.isProtocolOfOneWithinTwo(one, two));
518     }
519
520     @Test
521     public void testIsProtocolOfOneWithinTwo_oneProtocolNullTwoProtocolNull() {
522         NeutronSecurityRule one = createSecRuleWithProtocol(null);
523         NeutronSecurityRule two = createSecRuleWithProtocol(null);
524         assertTrue(SecRuleEntityDecoder.isProtocolOfOneWithinTwo(one, two));
525     }
526
527     @Test
528     public void testIsProtocolOfOneWithinTwo_oneProtocolTcpTwoProtocolUdp() {
529         NeutronSecurityRule one = createSecRuleWithProtocol(NeutronUtils.TCP);
530         NeutronSecurityRule two = createSecRuleWithProtocol(NeutronUtils.UDP);
531         assertFalse(SecRuleEntityDecoder.isProtocolOfOneWithinTwo(one, two));
532     }
533
534     @Test
535     public void testIsProtocolOfOneWithinTwo_oneProtocolNullTwoProtocolTcp() {
536         NeutronSecurityRule one = createSecRuleWithProtocol(null);
537         NeutronSecurityRule two = createSecRuleWithProtocol(NeutronUtils.TCP);
538         assertFalse(SecRuleEntityDecoder.isProtocolOfOneWithinTwo(one, two));
539     }
540
541     private NeutronSecurityRule createSecRuleWithProtocol(String protocol) {
542         NeutronSecurityRule secRule = new NeutronSecurityRule();
543         secRule.setSecurityRuleProtocol(protocol);
544         return secRule;
545     }
546
547     @Test
548     public final void testIsPortsOfOneWithinTwo_onePortMinGtTwoPortMinOnePortMaxLtTwoPortMax() {
549         NeutronSecurityRule one = createSecRuleWithMinMaxPort(6, 9);
550         NeutronSecurityRule two = createSecRuleWithMinMaxPort(5, 10);
551         assertTrue(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
552     }
553
554     @Test
555     public final void testIsPortsOfOneWithinTwo_onePortMinEqTwoPortMinOnePortMaxEqTwoPortMax() {
556         NeutronSecurityRule one = createSecRuleWithMinMaxPort(5, 10);
557         NeutronSecurityRule two = createSecRuleWithMinMaxPort(5, 10);
558         assertTrue(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
559     }
560
561     @Test
562     public final void testIsPortsOfOneWithinTwo_onePortMinTwoPortMinNullOnePortMaxTwoPortMaxNull() {
563         NeutronSecurityRule one = createSecRuleWithMinMaxPort(4, 9);
564         NeutronSecurityRule two = createSecRuleWithMinMaxPort(null, null);
565         assertTrue(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
566     }
567
568     @Test
569     public final void testIsPortsOfOneWithinTwo_onePortMinNullTwoPortMinNullOnePortMaxNullTwoPortMaxNull() {
570         NeutronSecurityRule one = createSecRuleWithMinMaxPort(null, null);
571         NeutronSecurityRule two = createSecRuleWithMinMaxPort(null, null);
572         assertTrue(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
573     }
574
575     @Test
576     public final void testIsPortsOfOneWithinTwo_onePortMinNullTwoPortMinOnePortMaxNullTwoPortMax() {
577         NeutronSecurityRule one = createSecRuleWithMinMaxPort(null, null);
578         NeutronSecurityRule two = createSecRuleWithMinMaxPort(5, 10);
579         assertFalse(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
580     }
581
582     @Test
583     public final void testIsPortsOfOneWithinTwo_onePortMinLtTwoPortMinOnePortMaxLtTwoPortMax() {
584         NeutronSecurityRule one = createSecRuleWithMinMaxPort(4, 9);
585         NeutronSecurityRule two = createSecRuleWithMinMaxPort(5, 10);
586         assertFalse(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
587     }
588
589     @Test
590     public final void testIsPortsOfOneWithinTwo_onePortMinGtTwoPortMinOnePortMaxGtTwoPortMax() {
591         NeutronSecurityRule one = createSecRuleWithMinMaxPort(6, 11);
592         NeutronSecurityRule two = createSecRuleWithMinMaxPort(5, 10);
593         assertFalse(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
594     }
595
596     @Test
597     public final void testIsPortsOfOneWithinTwo_onePortMinLtTwoPortMinOnePortMaxGtTwoPortMax() {
598         NeutronSecurityRule one = createSecRuleWithMinMaxPort(4, 11);
599         NeutronSecurityRule two = createSecRuleWithMinMaxPort(5, 10);
600         assertFalse(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
601     }
602
603     private NeutronSecurityRule createSecRuleWithMinMaxPort(Integer portMin, Integer portMax) {
604         NeutronSecurityRule secRule = new NeutronSecurityRule();
605         secRule.setSecurityRulePortMin(portMin);
606         secRule.setSecurityRulePortMax(portMax);
607         return secRule;
608     }
609
610     @Test
611     public final void testGetEtherType_ethertypeIPv4() {
612         secRule.setSecurityRuleEthertype("IPv4");
613         Assert.assertEquals(EtherTypeClassifierDefinition.IPv4_VALUE, SecRuleEntityDecoder.getEtherType(secRule));
614     }
615
616     @Test
617     public final void testGetEtherType_ethertypeIPv6() {
618         secRule.setSecurityRuleEthertype("IPv6");
619         Assert.assertEquals(EtherTypeClassifierDefinition.IPv6_VALUE, SecRuleEntityDecoder.getEtherType(secRule));
620     }
621
622     @Test
623     public final void testGetEtherType_ethertypeNull() {
624         secRule.setSecurityRuleEthertype(null);
625         Assert.assertNull(SecRuleEntityDecoder.getEtherType(secRule));
626     }
627
628     @Test
629     public final void testGetEtherType_ethertypeEmptyString() {
630         secRule.setSecurityRuleEthertype("");
631         Assert.assertNull(SecRuleEntityDecoder.getEtherType(secRule));
632     }
633
634     @Test
635     public final void testGetEtherType_ethertypeUnknown() {
636         secRule.setSecurityRuleEthertype("foo");
637         thrown.expect(IllegalArgumentException.class);
638         SecRuleEntityDecoder.getEtherType(secRule);
639     }
640
641     @Test
642     public final void testGetProtocol_protoTcp() {
643         secRule.setSecurityRuleProtocol("tcp");
644         Assert.assertEquals(IpProtoClassifierDefinition.TCP_VALUE, SecRuleEntityDecoder.getProtocol(secRule));
645     }
646
647     @Test
648     public final void testGetProtocol_protoUdp() {
649         secRule.setSecurityRuleProtocol("udp");
650         Assert.assertEquals(IpProtoClassifierDefinition.UDP_VALUE, SecRuleEntityDecoder.getProtocol(secRule));
651     }
652
653     @Test
654     public final void testGetProtocol_protoIcmp() {
655         secRule.setSecurityRuleProtocol("icmp");
656         Assert.assertEquals(IpProtoClassifierDefinition.ICMP_VALUE, SecRuleEntityDecoder.getProtocol(secRule));
657     }
658
659     @Test
660     public final void testGetProtocol_numValue() {
661         secRule.setSecurityRuleProtocol("199");
662         Assert.assertEquals(Long.valueOf(199) , SecRuleEntityDecoder.getProtocol(secRule));
663     }
664
665     @Test
666     public final void testGetProtocol_protoNull() {
667         secRule.setSecurityRuleProtocol(null);
668         Assert.assertNull(SecRuleEntityDecoder.getProtocol(secRule));
669     }
670
671     @Test
672     public final void testGetProtocol_protoEmptyString() {
673         secRule.setSecurityRuleProtocol("");
674         Assert.assertNull(SecRuleEntityDecoder.getProtocol(secRule));
675     }
676
677     @Test
678     public final void testGetProtocol_protoUnknown() {
679         secRule.setSecurityRuleProtocol("foo");
680         thrown.expect(IllegalArgumentException.class);
681         SecRuleEntityDecoder.getProtocol(secRule);
682     }
683 }