Add yang generated packages in .gitignore
[groupbasedpolicy.git] / neutron-mapper / src / main / java / org / opendaylight / groupbasedpolicy / neutron / mapper / infrastructure / NetworkService.java
1 package org.opendaylight.groupbasedpolicy.neutron.mapper.infrastructure;
2
3 import java.util.ArrayList;
4 import java.util.HashSet;
5 import java.util.List;
6 import java.util.Set;
7
8 import javax.annotation.Nullable;
9
10 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
11 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
12 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.MappingUtils;
13 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.Utils;
14 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf.EtherTypeClassifier;
15 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf.IpProtoClassifier;
16 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf.L4Classifier;
17 import org.opendaylight.groupbasedpolicy.util.IidFactory;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierName;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClauseName;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContractId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Description;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Name;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ParameterName;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.RuleName;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SelectorName;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.HasDirection.Direction;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.classifier.refs.ClassifierRef;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.classifier.refs.ClassifierRefBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.EndpointIdentificationConstraintsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.endpoint.identification.constraints.L3EndpointIdentificationConstraints;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.endpoint.identification.constraints.L3EndpointIdentificationConstraintsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.endpoint.identification.constraints.l3.endpoint.identification.constraints.PrefixConstraintBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValueBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.Contract;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.ContractBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.EndpointGroup;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.EndpointGroup.IntraGroupPolicy;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.EndpointGroupBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.Clause;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.ClauseBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.Subject;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.SubjectBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.ConsumerMatchers;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.ConsumerMatchersBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.ProviderMatchers;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.ProviderMatchersBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.subject.Rule;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.subject.RuleBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ConsumerNamedSelector;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ConsumerNamedSelectorBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ProviderNamedSelector;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ProviderNamedSelectorBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.subject.feature.instances.ClassifierInstance;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.subject.feature.instances.ClassifierInstanceBuilder;
60
61 import com.google.common.collect.ImmutableList;
62
63 public class NetworkService {
64
65     // ########### DHCP
66     private static final long DHCP_IPV4_SERVER_PORT = 67;
67     private static final long DHCP_IPV4_CLIENT_PORT = 68;
68     private static final long DHCP_IPV6_SERVER_PORT = 547;
69     private static final long DHCP_IPV6_CLIENT_PORT = 546;
70     private static final ClassifierName DHCP_IPV4_CLIENT_SERVER_NAME =
71             new ClassifierName("DHCP_IPv4_FROM_CLIENT_TO_SERVER");
72     private static final ClassifierName DHCP_IPV4_SERVER_CLIENT_NAME =
73             new ClassifierName("DHCP_IPv4_FROM_SERVER_TO_CLIENT");
74     private static final ClassifierName DHCP_IPV6_CLIENT_SERVER_NAME =
75             new ClassifierName("DHCP_IPv6_FROM_CLIENT_TO_SERVER");
76     private static final ClassifierName DHCP_IPV6_SERVER_CLIENT_NAME =
77             new ClassifierName("DHCP_IPv6_FROM_SERVER_TO_CLIENT");
78     private static final SubjectName DHCP_SUBJECT_NAME = new SubjectName("ALLOW_DHCP");
79     private static final Description DHCP_CONTRACT_DESC =
80             new Description("Allow DHCP communication between client and server.");
81     /**
82      * Id of {@link #DHCP_CONTRACT}
83      */
84     public static final ContractId DHCP_CONTRACT_ID = new ContractId("11118d2e-dddd-11e5-885d-feff819cdc9f");
85     /**
86      * Contains rules with action {@link MappingUtils#ACTION_REF_ALLOW} matching DHCP communication
87      * between Client and Server.
88      */
89     public static final Contract DHCP_CONTRACT;
90     /**
91      * {@link ConsumerNamedSelector} pointing to {@link #DHCP_CONTRACT}
92      */
93     public static final ConsumerNamedSelector DHCP_CONTRACT_CONSUMER_SELECTOR;
94
95     // ########### DNS
96     private static final long DNS_SERVER_PORT = 53;
97     private static final ClassifierName DNS_UDP_IPV4_CLIENT_SERVER_NAME =
98             new ClassifierName("DNS_UDP_IPv4_FROM_CLIENT_TO_SERVER");
99     private static final ClassifierName DNS_UDP_IPV4_SERVER_CLIENT_NAME =
100             new ClassifierName("DNS_UDP_IPv4_FROM_SERVER_TO_CLIENT");
101     private static final ClassifierName DNS_UDP_IPV6_CLIENT_SERVER_NAME =
102             new ClassifierName("DNS_UDP_IPv6_FROM_CLIENT_TO_SERVER");
103     private static final ClassifierName DNS_UDP_IPV6_SERVER_CLIENT_NAME =
104             new ClassifierName("DNS_UDP_IPv6_FROM_SERVER_TO_CLIENT");
105     private static final ClassifierName DNS_TCP_IPV4_CLIENT_SERVER_NAME =
106             new ClassifierName("DNS_TCP_IPv4_FROM_CLIENT_TO_SERVER");
107     private static final ClassifierName DNS_TCP_IPV4_SERVER_CLIENT_NAME =
108             new ClassifierName("DNS_TCP_IPv4_FROM_SERVER_TO_CLIENT");
109     private static final ClassifierName DNS_TCP_IPV6_CLIENT_SERVER_NAME =
110             new ClassifierName("DNS_TCP_IPv6_FROM_CLIENT_TO_SERVER");
111     private static final ClassifierName DNS_TCP_IPV6_SERVER_CLIENT_NAME =
112             new ClassifierName("DNS_TCP_IPv6_FROM_SERVER_TO_CLIENT");
113     private static final SubjectName DNS_SUBJECT_NAME = new SubjectName("ALLOW_DNS");
114     private static final Description DNS_CONTRACT_DESC =
115             new Description("Allow DNS communication between client and server.");
116     /**
117      * ID of {@link #DNS_CONTRACT}
118      */
119     public static final ContractId DNS_CONTRACT_ID = new ContractId("22218d2e-dddd-11e5-885d-feff819cdc9f");
120     /**
121      * Contains rules with action {@link MappingUtils#ACTION_REF_ALLOW} matching DNS communication
122      * between Client and Server.
123      */
124     public static final Contract DNS_CONTRACT;
125     /**
126      * {@link ConsumerNamedSelector} pointing to {@link #DNS_CONTRACT}
127      */
128     public static final ConsumerNamedSelector DNS_CONTRACT_CONSUMER_SELECTOR;
129
130     // ########### NETWORK-SERVICE ENDPOINT-GROUP
131     private static final Name NETWORK_SERVICE_EPG_NAME = new Name("NETWORK_SERVICE");
132     private static final Description NETWORK_SERVICE_EPG_DESC = new Description("Represents DHCP and DNS servers.");
133     /**
134      * ID of {@link #EPG}
135      */
136     public static final EndpointGroupId EPG_ID = new EndpointGroupId("ddd6cfe6-dfe5-11e4-8a00-1681e6b88ec1");
137     /**
138      * Network-service endpoint-group providing {@link #DHCP_CONTRACT} and {@link #DNS_CONTRACT}
139      */
140     public static final EndpointGroup EPG;
141
142     static {
143         DHCP_CONTRACT = createContractDhcp();
144         DHCP_CONTRACT_CONSUMER_SELECTOR = createConsumerSelector(DHCP_CONTRACT);
145         DNS_CONTRACT = createContractDns();
146         DNS_CONTRACT_CONSUMER_SELECTOR = createConsumerSelector(DNS_CONTRACT);
147         EPG = createNetworkServiceEpg();
148     }
149
150     private static EndpointGroup createNetworkServiceEpg() {
151         ProviderNamedSelector dhcpProviderSelector = createProviderSelector(DHCP_CONTRACT);
152         ProviderNamedSelector dnsProviderSelector = createProviderSelector(DNS_CONTRACT);
153         return new EndpointGroupBuilder().setId(EPG_ID)
154             .setName(NETWORK_SERVICE_EPG_NAME)
155             .setProviderNamedSelector(ImmutableList.of(dhcpProviderSelector, dnsProviderSelector))
156             .setIntraGroupPolicy(IntraGroupPolicy.RequireContract)
157             .setDescription(NETWORK_SERVICE_EPG_DESC)
158             .build();
159     }
160
161     private static ProviderNamedSelector createProviderSelector(Contract contract) {
162         SelectorName selectorName = new SelectorName(contract.getSubject().get(0).getName().getValue());
163         return new ProviderNamedSelectorBuilder().setName(selectorName)
164             .setContract(ImmutableList.of(contract.getId()))
165             .build();
166     }
167
168     private static ConsumerNamedSelector createConsumerSelector(Contract contract) {
169         SelectorName selectorName = new SelectorName(contract.getSubject().get(0).getName().getValue());
170         return new ConsumerNamedSelectorBuilder().setName(selectorName)
171             .setContract(ImmutableList.of(contract.getId()))
172             .build();
173     }
174
175     private static Contract createContractDhcp() {
176         Rule clientServerIpv4Rule = createRuleAllow(DHCP_IPV4_CLIENT_SERVER_NAME, Direction.In);
177         Rule serverClientIpv4Rule = createRuleAllow(DHCP_IPV4_SERVER_CLIENT_NAME, Direction.Out);
178         Rule clientServerIpv6Rule = createRuleAllow(DHCP_IPV6_CLIENT_SERVER_NAME, Direction.In);
179         Rule serverClientIpv6Rule = createRuleAllow(DHCP_IPV6_SERVER_CLIENT_NAME, Direction.Out);
180         Subject subject = new SubjectBuilder().setName(DHCP_SUBJECT_NAME)
181             .setOrder(0)
182             .setRule(ImmutableList.of(clientServerIpv4Rule, serverClientIpv4Rule, clientServerIpv6Rule,
183                     serverClientIpv6Rule))
184             .build();
185         return new ContractBuilder().setId(DHCP_CONTRACT_ID)
186             .setSubject(ImmutableList.of(subject))
187             .setDescription(DHCP_CONTRACT_DESC)
188             .build();
189     }
190
191     private static Contract createContractDns() {
192         Rule clientServerUdpIpv4Rule = createRuleAllow(DNS_UDP_IPV4_CLIENT_SERVER_NAME, Direction.In);
193         Rule serverClientUdpIpv4Rule = createRuleAllow(DNS_UDP_IPV4_SERVER_CLIENT_NAME, Direction.Out);
194         Rule clientServerUdpIpv6Rule = createRuleAllow(DNS_UDP_IPV6_CLIENT_SERVER_NAME, Direction.In);
195         Rule serverClientUdpIpv6Rule = createRuleAllow(DNS_UDP_IPV6_SERVER_CLIENT_NAME, Direction.Out);
196         Rule clientServerTcpIpv4Rule = createRuleAllow(DNS_TCP_IPV4_CLIENT_SERVER_NAME, Direction.In);
197         Rule serverClientTcpIpv4Rule = createRuleAllow(DNS_TCP_IPV4_SERVER_CLIENT_NAME, Direction.Out);
198         Rule clientServerTcpIpv6Rule = createRuleAllow(DNS_TCP_IPV6_CLIENT_SERVER_NAME, Direction.In);
199         Rule serverClientTcpIpv6Rule = createRuleAllow(DNS_TCP_IPV6_SERVER_CLIENT_NAME, Direction.Out);
200         Subject subject = new SubjectBuilder().setName(DNS_SUBJECT_NAME)
201             .setOrder(0)
202             .setRule(ImmutableList.of(clientServerUdpIpv4Rule, serverClientUdpIpv4Rule, clientServerUdpIpv6Rule,
203                     serverClientUdpIpv6Rule, clientServerTcpIpv4Rule, serverClientTcpIpv4Rule, clientServerTcpIpv6Rule,
204                     serverClientTcpIpv6Rule))
205             .build();
206         return new ContractBuilder().setId(DNS_CONTRACT_ID)
207             .setSubject(ImmutableList.of(subject))
208             .setDescription(DNS_CONTRACT_DESC)
209             .build();
210     }
211
212     private static Rule createRuleAllow(ClassifierName classifierName, Direction direction) {
213         ClassifierName name =
214                 new ClassifierName(direction.name() + MappingUtils.NAME_DOUBLE_DELIMETER + classifierName.getValue());
215         ClassifierRef classifierRef = new ClassifierRefBuilder().setName(name)
216             .setInstanceName(classifierName)
217             .setDirection(direction)
218             .build();
219         return new RuleBuilder().setName(new RuleName(name))
220             .setActionRef(MappingUtils.ACTION_REF_ALLOW)
221             .setClassifierRef(ImmutableList.of(classifierRef))
222             .build();
223     }
224
225     /**
226      * puts clause with {@link L3EndpointIdentificationConstraints} in {@link ConsumerMatchers}
227      * and {@link ProviderMatchers}. This clause points to subject in {@link #DHCP_CONTRACT}.
228      * 
229      * @param tenantId location of {@link #DHCP_CONTRACT}
230      * @param ipPrefix used in {@link L3EndpointIdentificationConstraints}
231      * @param wTx transaction where entities are written
232      */
233     public static void writeDhcpClauseWithConsProvEic(TenantId tenantId, @Nullable IpPrefix ipPrefix,
234             WriteTransaction wTx) {
235         Clause clause = createClauseWithConsProvEic(ipPrefix, DHCP_SUBJECT_NAME);
236         wTx.put(LogicalDatastoreType.CONFIGURATION,
237                 IidFactory.clauseIid(tenantId, DHCP_CONTRACT_ID, clause.getName()), clause, true);
238     }
239
240     /**
241      * puts clause with {@link L3EndpointIdentificationConstraints} in {@link ConsumerMatchers}
242      * and {@link ProviderMatchers}. This clause points to subject in {@link #DNS_CONTRACT}.
243      * 
244      * @param tenantId location of {@link #DNS_CONTRACT}
245      * @param ipPrefix used in {@link L3EndpointIdentificationConstraints}
246      * @param wTx transaction where entities are written
247      */
248     public static void writeDnsClauseWithConsProvEic(TenantId tenantId, @Nullable IpPrefix ipPrefix,
249             WriteTransaction wTx) {
250         Clause clause = createClauseWithConsProvEic(ipPrefix, DNS_SUBJECT_NAME);
251         wTx.put(LogicalDatastoreType.CONFIGURATION,
252                 IidFactory.clauseIid(tenantId, DNS_CONTRACT_ID, clause.getName()), clause, true);
253     }
254
255     private static Clause createClauseWithConsProvEic(@Nullable IpPrefix ipPrefix, SubjectName subjectName) {
256         ConsumerMatchers consumerMatchers = null;
257         ProviderMatchers providerMatchers = null;
258         StringBuilder clauseName = new StringBuilder();
259         clauseName.append(subjectName.getValue());
260         if (ipPrefix != null) {
261             clauseName.append(MappingUtils.NAME_DOUBLE_DELIMETER).append(Utils.getStringIpPrefix(ipPrefix));
262             consumerMatchers =
263                     new ConsumerMatchersBuilder()
264                         .setEndpointIdentificationConstraints(new EndpointIdentificationConstraintsBuilder()
265                             .setL3EndpointIdentificationConstraints(new L3EndpointIdentificationConstraintsBuilder()
266                                 .setPrefixConstraint(
267                                         ImmutableList.of(new PrefixConstraintBuilder().setIpPrefix(ipPrefix).build()))
268                                 .build())
269                             .build())
270                         .build();
271             providerMatchers =
272                     new ProviderMatchersBuilder()
273                         .setEndpointIdentificationConstraints(new EndpointIdentificationConstraintsBuilder()
274                             .setL3EndpointIdentificationConstraints(new L3EndpointIdentificationConstraintsBuilder()
275                                 .setPrefixConstraint(
276                                         ImmutableList.of(new PrefixConstraintBuilder().setIpPrefix(ipPrefix).build()))
277                                 .build())
278                             .build())
279                         .build();
280         }
281         return new ClauseBuilder().setName(new ClauseName(clauseName.toString()))
282             .setSubjectRefs(ImmutableList.of(subjectName))
283             .setConsumerMatchers(consumerMatchers)
284             .setProviderMatchers(providerMatchers)
285             .build();
286     }
287
288     /**
289      * Puts network service entities (classifier-instances, {@link #DHCP_CONTRACT},
290      * {@link #DNS_CONTRACT}, and {@link #EPG}) to
291      * {@link LogicalDatastoreType#CONFIGURATION}
292      * 
293      * @param tenantId location of network-service entities
294      * @param wTx transaction where network-service entities are written
295      */
296     public static void writeNetworkServiceEntitiesToTenant(TenantId tenantId, WriteTransaction wTx) {
297         Set<ClassifierInstance> classifierInstances = getAllClassifierInstances();
298         for (ClassifierInstance ci : classifierInstances) {
299             wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.classifierInstanceIid(tenantId, ci.getName()), ci,
300                     true);
301         }
302         wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.contractIid(tenantId, DHCP_CONTRACT_ID), DHCP_CONTRACT,
303                 true);
304         wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.contractIid(tenantId, DNS_CONTRACT_ID), DNS_CONTRACT,
305                 true);
306         wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.endpointGroupIid(tenantId, EPG_ID), EPG, true);
307     }
308
309     /**
310      * @return All classifier-instances used in {@link #DHCP_CONTRACT} and {@link #DNS_CONTRACT}
311      */
312     public static Set<ClassifierInstance> getAllClassifierInstances() {
313         HashSet<ClassifierInstance> cis = new HashSet<>();
314         cis.add(createDhcpIpv4ClientServer());
315         cis.add(createDhcpIpv4ServerClient());
316         cis.add(createDhcpIpv6ClientServer());
317         cis.add(createDhcpIpv6ServerClient());
318         cis.add(createDnsUdpIpv4ClientServer());
319         cis.add(createDnsUdpIpv4ServerClient());
320         cis.add(createDnsUdpIpv6ClientServer());
321         cis.add(createDnsUdpIpv6ServerClient());
322         cis.add(createDnsTcpIpv4ClientServer());
323         cis.add(createDnsTcpIpv4ServerClient());
324         cis.add(createDnsTcpIpv6ClientServer());
325         cis.add(createDnsTcpIpv6ServerClient());
326         return cis;
327     }
328
329     // ###################### DHCP
330     private static ClassifierInstance createDhcpIpv4ClientServer() {
331         return new ClassifierInstanceBuilder().setName(DHCP_IPV4_CLIENT_SERVER_NAME)
332             .setClassifierDefinitionId(L4Classifier.DEFINITION.getId())
333             .setParameterValue(createParams(EtherTypeClassifier.IPv4_VALUE, IpProtoClassifier.UDP_VALUE,
334                     DHCP_IPV4_CLIENT_PORT, DHCP_IPV4_SERVER_PORT))
335             .build();
336     }
337
338     private static ClassifierInstance createDhcpIpv4ServerClient() {
339         return new ClassifierInstanceBuilder().setName(DHCP_IPV4_SERVER_CLIENT_NAME)
340             .setClassifierDefinitionId(L4Classifier.DEFINITION.getId())
341             .setParameterValue(createParams(EtherTypeClassifier.IPv4_VALUE, IpProtoClassifier.UDP_VALUE,
342                     DHCP_IPV4_SERVER_PORT, DHCP_IPV4_CLIENT_PORT))
343             .build();
344     }
345
346     private static ClassifierInstance createDhcpIpv6ClientServer() {
347         return new ClassifierInstanceBuilder().setName(DHCP_IPV6_CLIENT_SERVER_NAME)
348             .setClassifierDefinitionId(L4Classifier.DEFINITION.getId())
349             .setParameterValue(createParams(EtherTypeClassifier.IPv6_VALUE, IpProtoClassifier.UDP_VALUE,
350                     DHCP_IPV6_CLIENT_PORT, DHCP_IPV6_SERVER_PORT))
351             .build();
352     }
353
354     private static ClassifierInstance createDhcpIpv6ServerClient() {
355         return new ClassifierInstanceBuilder().setName(DHCP_IPV6_SERVER_CLIENT_NAME)
356             .setClassifierDefinitionId(L4Classifier.DEFINITION.getId())
357             .setParameterValue(createParams(EtherTypeClassifier.IPv6_VALUE, IpProtoClassifier.UDP_VALUE,
358                     DHCP_IPV6_SERVER_PORT, DHCP_IPV6_CLIENT_PORT))
359             .build();
360     }
361
362     // ###################### DNS UDP
363     private static ClassifierInstance createDnsUdpIpv4ClientServer() {
364         return new ClassifierInstanceBuilder().setName(DNS_UDP_IPV4_CLIENT_SERVER_NAME)
365             .setClassifierDefinitionId(L4Classifier.DEFINITION.getId())
366             .setParameterValue(
367                     createParams(EtherTypeClassifier.IPv4_VALUE, IpProtoClassifier.UDP_VALUE, null, DNS_SERVER_PORT))
368             .build();
369     }
370
371     private static ClassifierInstance createDnsUdpIpv4ServerClient() {
372         return new ClassifierInstanceBuilder().setName(DNS_UDP_IPV4_SERVER_CLIENT_NAME)
373             .setClassifierDefinitionId(L4Classifier.DEFINITION.getId())
374             .setParameterValue(
375                     createParams(EtherTypeClassifier.IPv4_VALUE, IpProtoClassifier.UDP_VALUE, DNS_SERVER_PORT, null))
376             .build();
377     }
378
379     private static ClassifierInstance createDnsUdpIpv6ClientServer() {
380         return new ClassifierInstanceBuilder().setName(DNS_UDP_IPV6_CLIENT_SERVER_NAME)
381             .setClassifierDefinitionId(L4Classifier.DEFINITION.getId())
382             .setParameterValue(
383                     createParams(EtherTypeClassifier.IPv6_VALUE, IpProtoClassifier.UDP_VALUE, null, DNS_SERVER_PORT))
384             .build();
385     }
386
387     private static ClassifierInstance createDnsUdpIpv6ServerClient() {
388         return new ClassifierInstanceBuilder().setName(DNS_UDP_IPV6_SERVER_CLIENT_NAME)
389             .setClassifierDefinitionId(L4Classifier.DEFINITION.getId())
390             .setParameterValue(
391                     createParams(EtherTypeClassifier.IPv6_VALUE, IpProtoClassifier.UDP_VALUE, DNS_SERVER_PORT, null))
392             .build();
393     }
394
395     // ###################### DNS TCP
396     private static ClassifierInstance createDnsTcpIpv4ClientServer() {
397         return new ClassifierInstanceBuilder().setName(DNS_TCP_IPV4_CLIENT_SERVER_NAME)
398             .setClassifierDefinitionId(L4Classifier.DEFINITION.getId())
399             .setParameterValue(
400                     createParams(EtherTypeClassifier.IPv4_VALUE, IpProtoClassifier.TCP_VALUE, null, DNS_SERVER_PORT))
401             .build();
402     }
403
404     private static ClassifierInstance createDnsTcpIpv4ServerClient() {
405         return new ClassifierInstanceBuilder().setName(DNS_TCP_IPV4_SERVER_CLIENT_NAME)
406             .setClassifierDefinitionId(L4Classifier.DEFINITION.getId())
407             .setParameterValue(
408                     createParams(EtherTypeClassifier.IPv4_VALUE, IpProtoClassifier.TCP_VALUE, DNS_SERVER_PORT, null))
409             .build();
410     }
411
412     private static ClassifierInstance createDnsTcpIpv6ClientServer() {
413         return new ClassifierInstanceBuilder().setName(DNS_TCP_IPV6_CLIENT_SERVER_NAME)
414             .setClassifierDefinitionId(L4Classifier.DEFINITION.getId())
415             .setParameterValue(
416                     createParams(EtherTypeClassifier.IPv6_VALUE, IpProtoClassifier.TCP_VALUE, null, DNS_SERVER_PORT))
417             .build();
418     }
419
420     private static ClassifierInstance createDnsTcpIpv6ServerClient() {
421         return new ClassifierInstanceBuilder().setName(DNS_TCP_IPV6_SERVER_CLIENT_NAME)
422             .setClassifierDefinitionId(L4Classifier.DEFINITION.getId())
423             .setParameterValue(
424                     createParams(EtherTypeClassifier.IPv6_VALUE, IpProtoClassifier.TCP_VALUE, DNS_SERVER_PORT, null))
425             .build();
426     }
427
428     private static List<ParameterValue> createParams(long etherType, long proto, @Nullable Long srcPort,
429             @Nullable Long dstPort) {
430         List<ParameterValue> params = new ArrayList<>();
431         if (srcPort != null) {
432             params.add(new ParameterValueBuilder().setName(new ParameterName(L4Classifier.SRC_PORT_PARAM))
433                 .setIntValue(srcPort)
434                 .build());
435         }
436         if (dstPort != null) {
437             params.add(new ParameterValueBuilder().setName(new ParameterName(L4Classifier.DST_PORT_PARAM))
438                 .setIntValue(dstPort)
439                 .build());
440         }
441         params.add(new ParameterValueBuilder().setName(new ParameterName(IpProtoClassifier.PROTO_PARAM))
442             .setIntValue(proto)
443             .build());
444         params.add(new ParameterValueBuilder().setName(new ParameterName(EtherTypeClassifier.ETHERTYPE_PARAM))
445             .setIntValue(etherType)
446             .build());
447         return params;
448     }
449
450 }