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