Fix fileEncoding violations for checkstyle
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / statistics / flowcache / FlowCacheFactoryTest.java
1 /*
2  * Copyright (c) 2016 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.renderer.ofoverlay.statistics.flowcache;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
14 import java.util.Arrays;
15 import java.util.List;
16
17 import com.google.common.collect.ImmutableList;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.groupbasedpolicy.api.sf.EtherTypeClassifierDefinition;
21 import org.opendaylight.groupbasedpolicy.api.sf.IpProtoClassifierDefinition;
22 import org.opendaylight.groupbasedpolicy.api.sf.L4ClassifierDefinition;
23 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.statistics.ResolvedPolicyClassifierListener;
24 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.statistics.util.FlowCacheCons;
25 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.statistics.util.IidSflowNameUtil;
26 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.test.ParameterValueList;
27 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.test.TestUtils;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierDefinitionId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierName;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContractId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.RuleName;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.has.classifiers.Classifier;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.has.classifiers.ClassifierBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.ResolvedPolicy;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39
40 public class FlowCacheFactoryTest {
41
42     private final EndpointGroupId consumerEpgId = new EndpointGroupId("consumerEpg1");
43     private final EndpointGroupId providerEpgId = new EndpointGroupId("providerEpg1");
44     private final ContractId contractId = new ContractId("contract1");
45     private final TenantId tenantId = new TenantId("tenant1");
46     private final ClassifierName classifierName = ClassifierName.getDefaultInstance("classifier1");
47     private final SubjectName subjectName = SubjectName.getDefaultInstance("subject1");
48     private final RuleName ruleName = new RuleName("rule1");
49
50     private InstanceIdentifier<ResolvedPolicy> rpIid;
51     private String expectedName;
52
53     @Before
54     public void init() {
55         rpIid = InstanceIdentifier.create(ResolvedPolicy.class);
56         expectedName = tenantId.getValue() + IidSflowNameUtil.KEY_DELIMETER + contractId.getValue()
57                 + IidSflowNameUtil.KEY_DELIMETER + subjectName.getValue() + IidSflowNameUtil.DELIMETER
58                 + ruleName.getValue() + IidSflowNameUtil.DELIMETER + classifierName.getValue()
59                 + IidSflowNameUtil.DELIMETER + FlowCacheCons.Value.BYTES.get();
60     }
61
62     @Test
63     public void testCreateFlowCache_EtherTypeClassifier_IPv4() {
64         ParameterValueList parameterValues = new ParameterValueList();
65         parameterValues.addEthertype(EtherTypeClassifierDefinition.IPv4_VALUE);
66
67         Classifier classifier = newEtherTypeClassifier(parameterValues);
68         ResolvedPolicy rp = TestUtils.newResolvedPolicy(tenantId, contractId, subjectName, ruleName, consumerEpgId,
69                 providerEpgId, classifier);
70
71         FlowCache flowCache = callCreateFlowCache(rp, classifier);
72
73         assertNotNull(flowCache);
74
75         List<String> keys = Arrays.asList(flowCache.getKeyNames());
76         List<String> expectedKeys = ImmutableList.of(FlowCacheCons.Key.ETH_PROTOCOL.get(),
77                 FlowCacheCons.Key.IP_SOURCE.get(), FlowCacheCons.Key.IP_DESTINATION.get());
78
79         assertEquals(expectedName, flowCache.getName());
80         assertEquals(expectedKeys.size(), keys.size());
81         assertTrue(keys.containsAll(expectedKeys));
82     }
83
84     @Test
85     public void testCreateFlowCache_IpProtoClassifier_TCP_IPv4() {
86
87         ParameterValueList parameterValues = new ParameterValueList();
88         parameterValues.addEthertype(EtherTypeClassifierDefinition.IPv4_VALUE)
89             .addProto(IpProtoClassifierDefinition.TCP_VALUE);
90
91         Classifier classifier = newIpProtoClassifier(parameterValues);
92         ResolvedPolicy rp = TestUtils.newResolvedPolicy(tenantId, contractId, subjectName, ruleName, consumerEpgId,
93                 providerEpgId, classifier);
94
95         FlowCache flowCache = callCreateFlowCache(rp, classifier);
96
97         assertNotNull(flowCache);
98
99         List<String> keys = Arrays.asList(flowCache.getKeyNames());
100         List<String> expectedKeys =
101                 ImmutableList.of(FlowCacheCons.Key.ETH_PROTOCOL.get(), FlowCacheCons.Key.IP_PROTOCOL.get(),
102                         FlowCacheCons.Key.IP_SOURCE.get(), FlowCacheCons.Key.IP_DESTINATION.get());
103
104         assertEquals(expectedName, flowCache.getName());
105         assertEquals(expectedKeys.size(), keys.size());
106         assertTrue(keys.containsAll(expectedKeys));
107     }
108
109     @Test
110     public void testCreateFlowCache_IpProtoClassifier_UDP_noEthertype() {
111         ParameterValueList parameterValues = new ParameterValueList();
112         parameterValues.addProto(IpProtoClassifierDefinition.UDP_VALUE);
113
114         Classifier classifier = newIpProtoClassifier(parameterValues);
115         ResolvedPolicy rp = TestUtils.newResolvedPolicy(tenantId, contractId, subjectName, ruleName, consumerEpgId,
116                 providerEpgId, classifier);
117
118         FlowCache flowCache = callCreateFlowCache(rp, classifier);
119
120         assertNotNull(flowCache);
121
122         List<String> keys = Arrays.asList(flowCache.getKeyNames());
123         List<String> expectedKeys =
124                 ImmutableList.of(FlowCacheCons.Key.ETH_PROTOCOL.get(), FlowCacheCons.Key.IP_PROTOCOL.get(),
125                         FlowCacheCons.Key.IP_SOURCE.get(), FlowCacheCons.Key.IP_DESTINATION.get());
126
127         assertEquals(expectedName, flowCache.getName());
128         assertEquals(expectedKeys.size(), keys.size());
129         assertTrue(keys.containsAll(expectedKeys));
130     }
131
132     @Test
133     public void testCreateFlowCache_L4Classifier_dstPort() {
134         ParameterValueList parameterValues = new ParameterValueList();
135         parameterValues.addEthertype(EtherTypeClassifierDefinition.IPv4_VALUE)
136             .addProto(IpProtoClassifierDefinition.TCP_VALUE)
137             .addDstPort((long) 80);
138         Classifier classifier = newL4Classifier(parameterValues);
139         ResolvedPolicy rp = TestUtils.newResolvedPolicy(tenantId, contractId, subjectName, ruleName, consumerEpgId,
140                 providerEpgId, classifier);
141
142         FlowCache flowCache = callCreateFlowCache(rp, classifier);
143
144         assertNotNull(flowCache);
145
146         List<String> keys = Arrays.asList(flowCache.getKeyNames());
147         List<String> expectedKeys = ImmutableList.of(FlowCacheCons.Key.ETH_PROTOCOL.get(),
148                 FlowCacheCons.Key.IP_PROTOCOL.get(), FlowCacheCons.Key.TCP_DST_PORT.get(),
149                 FlowCacheCons.Key.IP_SOURCE.get(), FlowCacheCons.Key.IP_DESTINATION.get());
150
151         assertEquals(expectedName, flowCache.getName());
152         assertEquals(expectedKeys.size(), keys.size());
153         assertTrue(keys.containsAll(expectedKeys));
154
155         ParameterValueList parameterValuesUDP = new ParameterValueList();
156         parameterValuesUDP.addEthertype(EtherTypeClassifierDefinition.IPv4_VALUE)
157             .addProto(IpProtoClassifierDefinition.UDP_VALUE)
158             .addDstPort((long) 80);
159         Classifier classifierUDP = newL4Classifier(parameterValuesUDP);
160         ResolvedPolicy rpUDP = TestUtils.newResolvedPolicy(tenantId, contractId, subjectName, ruleName, consumerEpgId,
161                 providerEpgId, classifierUDP);
162
163         FlowCache flowCacheUDP = callCreateFlowCache(rpUDP, classifierUDP);
164
165         assertNotNull(flowCacheUDP);
166
167         List<String> keysUDP = Arrays.asList(flowCacheUDP.getKeyNames());
168         List<String> expectedKeysUDP = ImmutableList.of(FlowCacheCons.Key.ETH_PROTOCOL.get(),
169                 FlowCacheCons.Key.IP_PROTOCOL.get(), FlowCacheCons.Key.UDP_DST_PORT.get(),
170                 FlowCacheCons.Key.IP_SOURCE.get(), FlowCacheCons.Key.IP_DESTINATION.get());
171
172         assertEquals(expectedName, flowCacheUDP.getName());
173         assertEquals(expectedKeysUDP.size(), keysUDP.size());
174         assertTrue(keysUDP.containsAll(expectedKeysUDP));
175     }
176
177     @Test
178     public void testCreateFlowCache_L4Classifier_srcPort() {
179         ParameterValueList parameterValues = new ParameterValueList();
180         parameterValues.addEthertype(EtherTypeClassifierDefinition.IPv4_VALUE)
181             .addProto(IpProtoClassifierDefinition.TCP_VALUE)
182             .addSrcPort((long) 80);
183         Classifier classifier = newL4Classifier(parameterValues);
184         ResolvedPolicy rp = TestUtils.newResolvedPolicy(tenantId, contractId, subjectName, ruleName, consumerEpgId,
185                 providerEpgId, classifier);
186
187         FlowCache flowCache = callCreateFlowCache(rp, classifier);
188
189         assertNotNull(flowCache);
190
191         List<String> keys = Arrays.asList(flowCache.getKeyNames());
192         List<String> expectedKeys = ImmutableList.of(FlowCacheCons.Key.ETH_PROTOCOL.get(),
193                 FlowCacheCons.Key.IP_PROTOCOL.get(), FlowCacheCons.Key.TCP_SRC_PORT.get(),
194                 FlowCacheCons.Key.IP_SOURCE.get(), FlowCacheCons.Key.IP_DESTINATION.get());
195
196         assertEquals(expectedName, flowCache.getName());
197         assertEquals(expectedKeys.size(), keys.size());
198         assertTrue(keys.containsAll(expectedKeys));
199
200         ParameterValueList parameterValuesUDP = new ParameterValueList();
201         parameterValuesUDP.addEthertype(EtherTypeClassifierDefinition.IPv4_VALUE)
202             .addProto(IpProtoClassifierDefinition.UDP_VALUE)
203             .addSrcPort((long) 80);
204         Classifier classifierUDP = newL4Classifier(parameterValuesUDP);
205         ResolvedPolicy rpUDP = TestUtils.newResolvedPolicy(tenantId, contractId, subjectName, ruleName, consumerEpgId,
206                 providerEpgId, classifierUDP);
207
208         FlowCache flowCacheUDP = callCreateFlowCache(rpUDP, classifierUDP);
209
210         assertNotNull(flowCacheUDP);
211
212         List<String> keysUDP = Arrays.asList(flowCacheUDP.getKeyNames());
213         List<String> expectedKeysUDP = ImmutableList.of(FlowCacheCons.Key.ETH_PROTOCOL.get(),
214                 FlowCacheCons.Key.IP_PROTOCOL.get(), FlowCacheCons.Key.UDP_SRC_PORT.get(),
215                 FlowCacheCons.Key.IP_SOURCE.get(), FlowCacheCons.Key.IP_DESTINATION.get());
216
217         assertEquals(expectedName, flowCacheUDP.getName());
218         assertEquals(expectedKeysUDP.size(), keysUDP.size());
219         assertTrue(keysUDP.containsAll(expectedKeysUDP));
220     }
221
222     private Classifier newClassifier(ClassifierDefinitionId classifierDefinitionId,
223             ParameterValueList parameterValues) {
224         return new ClassifierBuilder().setName(classifierName)
225             .setClassifierDefinitionId(classifierDefinitionId)
226             .setParameterValue(parameterValues)
227             .build();
228     }
229
230     private Classifier newEtherTypeClassifier(ParameterValueList parameterValues) {
231         return newClassifier(EtherTypeClassifierDefinition.ID, parameterValues);
232     }
233
234     private Classifier newIpProtoClassifier(ParameterValueList parameterValues) {
235         return newClassifier(IpProtoClassifierDefinition.ID, parameterValues);
236     }
237
238     private Classifier newL4Classifier(ParameterValueList parameterValues) {
239         return newClassifier(L4ClassifierDefinition.ID, parameterValues);
240     }
241
242     private FlowCache callCreateFlowCache(ResolvedPolicy rp, Classifier classifier) {
243         return FlowCacheFactory.createFlowCache(
244                 TestUtils.getClassifierIid(ResolvedPolicyClassifierListener.resolveClassifiers(rp, rpIid)), classifier,
245                 FlowCacheCons.Value.BYTES);
246     }
247
248 }