Security group in VPP renderer
[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.Collections;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Set;
16
17 import javax.annotation.Nullable;
18
19 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.groupbasedpolicy.api.sf.EtherTypeClassifierDefinition;
22 import org.opendaylight.groupbasedpolicy.api.sf.IpProtoClassifierDefinition;
23 import org.opendaylight.groupbasedpolicy.api.sf.L4ClassifierDefinition;
24 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.MappingUtils;
25 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.Utils;
26 import org.opendaylight.groupbasedpolicy.util.IidFactory;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierName;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClauseName;
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.Description;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Name;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ParameterName;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.RuleName;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SelectorName;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.HasDirection.Direction;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.action.refs.ActionRef;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.classifier.refs.ClassifierRef;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.classifier.refs.ClassifierRefBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.EndpointIdentificationConstraintsBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.endpoint.identification.constraints.L3EndpointIdentificationConstraints;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.endpoint.identification.constraints.L3EndpointIdentificationConstraintsBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.endpoint.identification.constraints.l3.endpoint.identification.constraints.PrefixConstraintBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValueBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.Contract;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.ContractBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup.IntraGroupPolicy;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroupBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.Clause;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.ClauseBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.Subject;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.SubjectBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.ConsumerMatchers;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.ConsumerMatchersBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.ProviderMatchers;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.ProviderMatchersBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.subject.Rule;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.subject.RuleBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ConsumerNamedSelector;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ConsumerNamedSelectorBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ProviderNamedSelector;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ProviderNamedSelectorBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ActionInstance;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ClassifierInstance;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ClassifierInstanceBuilder;
71
72 import com.google.common.collect.ImmutableList;
73
74 public class NetworkService {
75
76     /**
77      * Unit tests {@link NetworkServiceTest}
78      */
79     // ########### DHCP
80     private static final long DHCP_IPV4_SERVER_PORT = 67;
81     private static final long DHCP_IPV4_CLIENT_PORT = 68;
82     private static final long DHCP_IPV6_SERVER_PORT = 547;
83     private static final long DHCP_IPV6_CLIENT_PORT = 546;
84     private static final ClassifierName DHCP_IPV4_CLIENT_SERVER_NAME =
85             new ClassifierName("DHCP_IPv4_FROM_CLIENT_TO_SERVER");
86     private static final ClassifierName DHCP_IPV4_SERVER_CLIENT_NAME =
87             new ClassifierName("DHCP_IPv4_FROM_SERVER_TO_CLIENT");
88     private static final ClassifierName DHCP_IPV6_CLIENT_SERVER_NAME =
89             new ClassifierName("DHCP_IPv6_FROM_CLIENT_TO_SERVER");
90     private static final ClassifierName DHCP_IPV6_SERVER_CLIENT_NAME =
91             new ClassifierName("DHCP_IPv6_FROM_SERVER_TO_CLIENT");
92     private static final SubjectName DHCP_SUBJECT_NAME = new SubjectName("ALLOW_DHCP");
93     private static final Description DHCP_CONTRACT_DESC =
94             new Description("Allow DHCP communication between client and server.");
95
96     /**
97      * Id of {@link #DHCP_CONTRACT}
98      */
99     public static final ContractId DHCP_CONTRACT_ID = new ContractId("11118d2e-dddd-11e5-885d-feff819cdc9f");
100     /**
101      * Contains rules with action {@link MappingUtils#ACTION_REF_ALLOW} matching DHCP communication
102      * between Client and Server.
103      */
104     public static final Contract DHCP_CONTRACT;
105     /**
106      * {@link ConsumerNamedSelector} pointing to {@link #DHCP_CONTRACT}
107      */
108     public static final ConsumerNamedSelector DHCP_CONTRACT_CONSUMER_SELECTOR;
109
110     // ########### DNS
111     private static final long DNS_SERVER_PORT = 53;
112     private static final ClassifierName DNS_UDP_IPV4_CLIENT_SERVER_NAME =
113             new ClassifierName("DNS_UDP_IPv4_FROM_CLIENT_TO_SERVER");
114     private static final ClassifierName DNS_UDP_IPV4_SERVER_CLIENT_NAME =
115             new ClassifierName("DNS_UDP_IPv4_FROM_SERVER_TO_CLIENT");
116     private static final ClassifierName DNS_UDP_IPV6_CLIENT_SERVER_NAME =
117             new ClassifierName("DNS_UDP_IPv6_FROM_CLIENT_TO_SERVER");
118     private static final ClassifierName DNS_UDP_IPV6_SERVER_CLIENT_NAME =
119             new ClassifierName("DNS_UDP_IPv6_FROM_SERVER_TO_CLIENT");
120     private static final ClassifierName DNS_TCP_IPV4_CLIENT_SERVER_NAME =
121             new ClassifierName("DNS_TCP_IPv4_FROM_CLIENT_TO_SERVER");
122     private static final ClassifierName DNS_TCP_IPV4_SERVER_CLIENT_NAME =
123             new ClassifierName("DNS_TCP_IPv4_FROM_SERVER_TO_CLIENT");
124     private static final ClassifierName DNS_TCP_IPV6_CLIENT_SERVER_NAME =
125             new ClassifierName("DNS_TCP_IPv6_FROM_CLIENT_TO_SERVER");
126     private static final ClassifierName DNS_TCP_IPV6_SERVER_CLIENT_NAME =
127             new ClassifierName("DNS_TCP_IPv6_FROM_SERVER_TO_CLIENT");
128     private static final SubjectName DNS_SUBJECT_NAME = new SubjectName("ALLOW_DNS");
129     private static final Description DNS_CONTRACT_DESC =
130             new Description("Allow DNS communication between client and server.");
131     /**
132      * ID of {@link #DNS_CONTRACT}
133      */
134     public static final ContractId DNS_CONTRACT_ID = new ContractId("22218d2e-dddd-11e5-885d-feff819cdc9f");
135     /**
136      * Contains rules with action {@link MappingUtils#ACTION_REF_ALLOW} matching DNS communication
137      * between Client and Server.
138      */
139     public static final Contract DNS_CONTRACT;
140     /**
141      * {@link ConsumerNamedSelector} pointing to {@link #DNS_CONTRACT}
142      */
143     public static final ConsumerNamedSelector DNS_CONTRACT_CONSUMER_SELECTOR;
144
145     // ########### SSH and ICMP management
146     private static final long SSH_TCP_PORT = 22;
147     private static final ClassifierName SSH_IPV4_SERVER_TO_CLIENT_NAME =
148             new ClassifierName("SSH_IPV4_FROM_SERVER_TO_CLIENT");
149     private static final ClassifierName SSH_IPV6_SERVER_TO_CLIENT_NAME =
150             new ClassifierName("SSH_IPV6_FROM_SERVER_TO_CLIENT");
151     private static final ClassifierName SSH_IPV4_CLIENT_TO_SERVER_NAME =
152             new ClassifierName("SSH_IPV4_FROM_CLIENT_TO_SERVER");
153     private static final ClassifierName SSH_IPV6_CLIENT_TO_SERVER_NAME =
154             new ClassifierName("SSH_IPV6_FROM_CLIENT_TO_SERVER");
155     private static final ClassifierName ICMP_IPV4_BETWEEN_SERVER_CLIENT_NAME =
156             new ClassifierName("ICMP_IPV4_BETWEEN_SERVER_CLIENT");
157     private static final ClassifierName ICMP_IPV6_BETWEEN_SERVER_CLIENT_NAME =
158             new ClassifierName("ICMP_IPV6_BETWEEN_SERVER_CLIENT");
159     private static final SubjectName MGMT_SUBJECT_NAME = new SubjectName("ALLOW_MGMT");
160     private static final Description MGMT_CONTRACT_DESC =
161             new Description("Allow ICMP and SSH management communication between server and client.");
162
163     /**
164      * Id of {@link #MGMT_CONTRACT}
165      */
166     public static final ContractId MGMT_CONTRACT_ID = new ContractId("33318d2e-dddd-11e5-885d-feff819cdc9f");
167     /**
168      * Contains rules with action {@link MappingUtils#ACTION_REF_ALLOW} matching ICMP and SSH
169      * communication
170      * between Client and Server.
171      */
172     public static final Contract MGMT_CONTRACT;
173     /**
174      * {@link ConsumerNamedSelector} pointing to {@link #MGMT_CONTRACT}
175      */
176     public static final ConsumerNamedSelector MGMT_CONTRACT_CONSUMER_SELECTOR;
177
178     // ########### NETWORK-SERVICE ENDPOINT-GROUP
179     private static final Name NETWORK_SERVICE_EPG_NAME = new Name("NETWORK_SERVICE");
180     private static final Description NETWORK_SERVICE_EPG_DESC = new Description("Represents DHCP and DNS servers.");
181     /**
182      * ID of {@link #EPG}
183      */
184     public static final EndpointGroupId EPG_ID = new EndpointGroupId("ddd6cfe6-dfe5-11e4-8a00-1681e6b88ec1");
185     /**
186      * Network-service endpoint-group providing {@link #DHCP_CONTRACT} and {@link #DNS_CONTRACT}
187      */
188     public static final EndpointGroup EPG;
189
190     static {
191         DHCP_CONTRACT = createContractDhcp();
192         DHCP_CONTRACT_CONSUMER_SELECTOR = createConsumerSelector(DHCP_CONTRACT);
193         DNS_CONTRACT = createContractDns();
194         DNS_CONTRACT_CONSUMER_SELECTOR = createConsumerSelector(DNS_CONTRACT);
195         MGMT_CONTRACT = createContractMgmt();
196         MGMT_CONTRACT_CONSUMER_SELECTOR = createConsumerSelector(MGMT_CONTRACT);
197         EPG = createNetworkServiceEpg();
198     }
199
200     private static EndpointGroup createNetworkServiceEpg() {
201         ProviderNamedSelector dhcpProviderSelector = createProviderSelector(DHCP_CONTRACT);
202         ProviderNamedSelector dnsProviderSelector = createProviderSelector(DNS_CONTRACT);
203         ProviderNamedSelector mgmtProviderSelector = createProviderSelector(MGMT_CONTRACT);
204         return new EndpointGroupBuilder().setId(EPG_ID)
205             .setName(NETWORK_SERVICE_EPG_NAME)
206             .setProviderNamedSelector(ImmutableList.of(dhcpProviderSelector, dnsProviderSelector, mgmtProviderSelector))
207             .setIntraGroupPolicy(IntraGroupPolicy.RequireContract)
208             .setDescription(NETWORK_SERVICE_EPG_DESC)
209             .build();
210     }
211
212     private static ProviderNamedSelector createProviderSelector(Contract contract) {
213         SelectorName selectorName = new SelectorName(contract.getSubject().get(0).getName().getValue());
214         return new ProviderNamedSelectorBuilder().setName(selectorName)
215             .setContract(ImmutableList.of(contract.getId()))
216             .build();
217     }
218
219     private static ConsumerNamedSelector createConsumerSelector(Contract contract) {
220         SelectorName selectorName = new SelectorName(contract.getSubject().get(0).getName().getValue());
221         return new ConsumerNamedSelectorBuilder().setName(selectorName)
222             .setContract(ImmutableList.of(contract.getId()))
223             .build();
224     }
225
226     private static Contract createContractDhcp() {
227         Rule clientServerIpv4Rule = createRuleAllow(DHCP_IPV4_CLIENT_SERVER_NAME, Direction.In);
228         Rule serverClientIpv4Rule = createRuleAllow(DHCP_IPV4_SERVER_CLIENT_NAME, Direction.Out);
229         Rule clientServerIpv6Rule = createRuleAllow(DHCP_IPV6_CLIENT_SERVER_NAME, Direction.In);
230         Rule serverClientIpv6Rule = createRuleAllow(DHCP_IPV6_SERVER_CLIENT_NAME, Direction.Out);
231         Subject subject = new SubjectBuilder().setName(DHCP_SUBJECT_NAME)
232             .setOrder(0)
233             .setRule(ImmutableList.of(clientServerIpv4Rule, serverClientIpv4Rule, clientServerIpv6Rule,
234                     serverClientIpv6Rule))
235             .build();
236         return new ContractBuilder().setId(DHCP_CONTRACT_ID)
237             .setSubject(ImmutableList.of(subject))
238             .setDescription(DHCP_CONTRACT_DESC)
239             .build();
240     }
241
242     private static Contract createContractDns() {
243         Rule clientServerUdpIpv4Rule = createRuleAllow(DNS_UDP_IPV4_CLIENT_SERVER_NAME, Direction.In);
244         Rule serverClientUdpIpv4Rule = createRuleAllow(DNS_UDP_IPV4_SERVER_CLIENT_NAME, Direction.Out);
245         Rule clientServerUdpIpv6Rule = createRuleAllow(DNS_UDP_IPV6_CLIENT_SERVER_NAME, Direction.In);
246         Rule serverClientUdpIpv6Rule = createRuleAllow(DNS_UDP_IPV6_SERVER_CLIENT_NAME, Direction.Out);
247         Rule clientServerTcpIpv4Rule = createRuleAllow(DNS_TCP_IPV4_CLIENT_SERVER_NAME, Direction.In);
248         Rule serverClientTcpIpv4Rule = createRuleAllow(DNS_TCP_IPV4_SERVER_CLIENT_NAME, Direction.Out);
249         Rule clientServerTcpIpv6Rule = createRuleAllow(DNS_TCP_IPV6_CLIENT_SERVER_NAME, Direction.In);
250         Rule serverClientTcpIpv6Rule = createRuleAllow(DNS_TCP_IPV6_SERVER_CLIENT_NAME, Direction.Out);
251         Subject subject = new SubjectBuilder().setName(DNS_SUBJECT_NAME)
252             .setOrder(0)
253             .setRule(ImmutableList.of(clientServerUdpIpv4Rule, serverClientUdpIpv4Rule, clientServerUdpIpv6Rule,
254                     serverClientUdpIpv6Rule, clientServerTcpIpv4Rule, serverClientTcpIpv4Rule, clientServerTcpIpv6Rule,
255                     serverClientTcpIpv6Rule))
256             .build();
257         return new ContractBuilder().setId(DNS_CONTRACT_ID)
258             .setSubject(ImmutableList.of(subject))
259             .setDescription(DNS_CONTRACT_DESC)
260             .build();
261     }
262
263     private static Contract createContractMgmt() {
264         Rule serverClientSshIpv4Rule = createRuleAllow(SSH_IPV4_SERVER_TO_CLIENT_NAME, Direction.Out);
265         Rule serverClientSshIpv6Rule = createRuleAllow(SSH_IPV6_SERVER_TO_CLIENT_NAME, Direction.Out);
266         Rule clientServerSshIpv4Rule = createRuleAllow(SSH_IPV4_CLIENT_TO_SERVER_NAME, Direction.In);
267         Rule clientServerSshIpv6Rule = createRuleAllow(SSH_IPV6_CLIENT_TO_SERVER_NAME, Direction.In);
268         Rule serverClientIcmpIpv4Rule = createRuleAllow(ICMP_IPV4_BETWEEN_SERVER_CLIENT_NAME, Direction.Out);
269         Rule serverClientIcmpIpv6Rule = createRuleAllow(ICMP_IPV6_BETWEEN_SERVER_CLIENT_NAME, Direction.Out);
270         Rule clientServerIcmpIpv4Rule = createRuleAllow(ICMP_IPV4_BETWEEN_SERVER_CLIENT_NAME, Direction.In);
271         Rule clientServerIcmpIpv6Rule = createRuleAllow(ICMP_IPV6_BETWEEN_SERVER_CLIENT_NAME, Direction.In);
272
273         Subject subject = new SubjectBuilder().setName(MGMT_SUBJECT_NAME)
274             .setOrder(0)
275             .setRule(ImmutableList.of(serverClientSshIpv4Rule, serverClientSshIpv6Rule, clientServerSshIpv4Rule,
276                     clientServerSshIpv6Rule, clientServerIcmpIpv4Rule, clientServerIcmpIpv6Rule,
277                     serverClientIcmpIpv4Rule, serverClientIcmpIpv6Rule))
278             .build();
279         return new ContractBuilder().setId(MGMT_CONTRACT_ID)
280             .setSubject(ImmutableList.of(subject))
281             .setDescription(MGMT_CONTRACT_DESC)
282             .build();
283     }
284
285     private static Rule createRuleAllow(ClassifierName classifierName, Direction direction) {
286         ClassifierName name =
287                 new ClassifierName(direction.name() + MappingUtils.NAME_DOUBLE_DELIMETER + classifierName.getValue());
288         ClassifierRef classifierRef = new ClassifierRefBuilder().setName(name)
289             .setInstanceName(classifierName)
290             .setDirection(direction)
291             .build();
292         return new RuleBuilder().setName(new RuleName(name))
293             .setActionRef(ImmutableList.<ActionRef>of(MappingUtils.ACTION_REF_ALLOW))
294             .setClassifierRef(ImmutableList.of(classifierRef))
295             .build();
296     }
297
298     /**
299      * puts clause with {@link L3EndpointIdentificationConstraints} in {@link ConsumerMatchers}
300      * and {@link ProviderMatchers}. This clause points to subject in {@link #DHCP_CONTRACT}.
301      *
302      * @param tenantId location of {@link #DHCP_CONTRACT}
303      * @param ipPrefix used in {@link L3EndpointIdentificationConstraints}
304      * @param wTx transaction where entities are written
305      */
306     public static void writeDhcpClauseWithConsProvEic(TenantId tenantId, @Nullable IpPrefix ipPrefix,
307             WriteTransaction wTx) {
308         Clause clause = createClauseWithConsProvEic(ipPrefix, DHCP_SUBJECT_NAME);
309         wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.clauseIid(tenantId, DHCP_CONTRACT_ID, clause.getName()),
310                 clause, true);
311     }
312
313     /**
314      * puts clause with {@link L3EndpointIdentificationConstraints} in {@link ConsumerMatchers}
315      * and {@link ProviderMatchers}. This clause points to subject in {@link #DNS_CONTRACT}.
316      *
317      * @param tenantId location of {@link #DNS_CONTRACT}
318      * @param ipPrefix used in {@link L3EndpointIdentificationConstraints}
319      * @param wTx transaction where entities are written
320      */
321     public static void writeDnsClauseWithConsProvEic(TenantId tenantId, @Nullable IpPrefix ipPrefix,
322             WriteTransaction wTx) {
323         Clause clause = createClauseWithConsProvEic(ipPrefix, DNS_SUBJECT_NAME);
324         wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.clauseIid(tenantId, DNS_CONTRACT_ID, clause.getName()),
325                 clause, true);
326     }
327
328     /**
329      * puts clause with {@link L3EndpointIdentificationConstraints} in {@link ConsumerMatchers}
330      * and {@link ProviderMatchers}. This clause points to subject in {@link #MGMT_CONTRACT}.
331      *
332      * @param tenantId location of {@link #MGMT_CONTRACT}
333      * @param ipPrefix used in {@link L3EndpointIdentificationConstraints}
334      * @param wTx transaction where entities are written
335      */
336     public static void writeMgmtClauseWithConsProvEic(TenantId tenantId, @Nullable IpPrefix ipPrefix,
337             WriteTransaction wTx) {
338         Clause clause = createClauseWithConsProvEic(ipPrefix, MGMT_SUBJECT_NAME);
339         wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.clauseIid(tenantId, MGMT_CONTRACT_ID, clause.getName()),
340                 clause, true);
341     }
342
343     private static Clause createClauseWithConsProvEic(@Nullable IpPrefix ipPrefix, SubjectName subjectName) {
344         ConsumerMatchers consumerMatchers = null;
345         ProviderMatchers providerMatchers = null;
346         StringBuilder clauseName = new StringBuilder();
347         clauseName.append(subjectName.getValue());
348         if (ipPrefix != null) {
349             clauseName.append(MappingUtils.NAME_DOUBLE_DELIMETER).append(Utils.getStringIpPrefix(ipPrefix));
350             consumerMatchers =
351                     new ConsumerMatchersBuilder()
352                         .setEndpointIdentificationConstraints(new EndpointIdentificationConstraintsBuilder()
353                             .setL3EndpointIdentificationConstraints(new L3EndpointIdentificationConstraintsBuilder()
354                                 .setPrefixConstraint(
355                                         ImmutableList.of(new PrefixConstraintBuilder().setIpPrefix(ipPrefix).build()))
356                                 .build())
357                             .build())
358                         .build();
359             providerMatchers =
360                     new ProviderMatchersBuilder()
361                         .setEndpointIdentificationConstraints(new EndpointIdentificationConstraintsBuilder()
362                             .setL3EndpointIdentificationConstraints(new L3EndpointIdentificationConstraintsBuilder()
363                                 .setPrefixConstraint(
364                                         ImmutableList.of(new PrefixConstraintBuilder().setIpPrefix(ipPrefix).build()))
365                                 .build())
366                             .build())
367                         .build();
368         }
369         return new ClauseBuilder().setName(new ClauseName(clauseName.toString()))
370             .setSubjectRefs(ImmutableList.of(subjectName))
371             .setConsumerMatchers(consumerMatchers)
372             .setProviderMatchers(providerMatchers)
373             .build();
374     }
375
376     /**
377      * Puts network service entities (classifier-instances, {@link #DHCP_CONTRACT},
378      * {@link #DNS_CONTRACT}, {@link #MGMT_CONTRACT} and {@link #EPG}) to
379      * {@link LogicalDatastoreType#CONFIGURATION}
380      *
381      * @param tenantId location of network-service entities
382      * @param wTx transaction where network-service entities are written
383      */
384     public static void writeNetworkServiceEntitiesToTenant(TenantId tenantId, WriteTransaction wTx) {
385         Set<ClassifierInstance> classifierInstances = getAllClassifierInstances();
386         for (ClassifierInstance ci : classifierInstances) {
387             wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.classifierInstanceIid(tenantId, ci.getName()), ci,
388                     true);
389         }
390         for (ActionInstance ai : Collections.singleton(MappingUtils.ACTION_ALLOW)) {
391             wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.actionInstanceIid(tenantId, ai.getName()), ai, true);
392         }
393         wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.contractIid(tenantId, DHCP_CONTRACT_ID), DHCP_CONTRACT,
394                 true);
395         wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.contractIid(tenantId, DNS_CONTRACT_ID), DNS_CONTRACT,
396                 true);
397         wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.contractIid(tenantId, MGMT_CONTRACT_ID), MGMT_CONTRACT,
398                 true);
399         wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.endpointGroupIid(tenantId, EPG_ID), EPG, true);
400     }
401
402     /**
403      * @return All classifier-instances used in {@link #DHCP_CONTRACT}, {@link #DNS_CONTRACT} and
404      *         {@link #MGMT_CONTRACT}
405      */
406     public static Set<ClassifierInstance> getAllClassifierInstances() {
407         HashSet<ClassifierInstance> cis = new HashSet<>();
408         cis.add(createDhcpIpv4ClientServer());
409         cis.add(createDhcpIpv4ServerClient());
410         cis.add(createDhcpIpv6ClientServer());
411         cis.add(createDhcpIpv6ServerClient());
412         cis.add(createDnsUdpIpv4ClientServer());
413         cis.add(createDnsUdpIpv4ServerClient());
414         cis.add(createDnsUdpIpv6ClientServer());
415         cis.add(createDnsUdpIpv6ServerClient());
416         cis.add(createDnsTcpIpv4ClientServer());
417         cis.add(createDnsTcpIpv4ServerClient());
418         cis.add(createDnsTcpIpv6ClientServer());
419         cis.add(createDnsTcpIpv6ServerClient());
420         // MGMT
421         cis.add(createSshTcpIpv4ServerClient());
422         cis.add(createSshTcpIpv6ServerClient());
423         cis.add(createSshTcpIpv4ClientServer());
424         cis.add(createSshTcpIpv6ClientServer());
425         cis.add(createIcmpIpv4());
426         cis.add(createIcmpIpv6());
427
428         return cis;
429     }
430
431     // ###################### DHCP
432     private static ClassifierInstance createDhcpIpv4ClientServer() {
433         return new ClassifierInstanceBuilder().setName(DHCP_IPV4_CLIENT_SERVER_NAME)
434             .setClassifierDefinitionId(L4ClassifierDefinition.DEFINITION.getId())
435             .setParameterValue(createParams(EtherTypeClassifierDefinition.IPv4_VALUE, IpProtoClassifierDefinition.UDP_VALUE,
436                     DHCP_IPV4_CLIENT_PORT, DHCP_IPV4_SERVER_PORT))
437             .build();
438     }
439
440     private static ClassifierInstance createDhcpIpv4ServerClient() {
441         return new ClassifierInstanceBuilder().setName(DHCP_IPV4_SERVER_CLIENT_NAME)
442             .setClassifierDefinitionId(L4ClassifierDefinition.DEFINITION.getId())
443             .setParameterValue(createParams(EtherTypeClassifierDefinition.IPv4_VALUE, IpProtoClassifierDefinition.UDP_VALUE,
444                     DHCP_IPV4_SERVER_PORT, DHCP_IPV4_CLIENT_PORT))
445             .build();
446     }
447
448     private static ClassifierInstance createDhcpIpv6ClientServer() {
449         return new ClassifierInstanceBuilder().setName(DHCP_IPV6_CLIENT_SERVER_NAME)
450             .setClassifierDefinitionId(L4ClassifierDefinition.DEFINITION.getId())
451             .setParameterValue(createParams(EtherTypeClassifierDefinition.IPv6_VALUE, IpProtoClassifierDefinition.UDP_VALUE,
452                     DHCP_IPV6_CLIENT_PORT, DHCP_IPV6_SERVER_PORT))
453             .build();
454     }
455
456     private static ClassifierInstance createDhcpIpv6ServerClient() {
457         return new ClassifierInstanceBuilder().setName(DHCP_IPV6_SERVER_CLIENT_NAME)
458             .setClassifierDefinitionId(L4ClassifierDefinition.DEFINITION.getId())
459             .setParameterValue(createParams(EtherTypeClassifierDefinition.IPv6_VALUE, IpProtoClassifierDefinition.UDP_VALUE,
460                     DHCP_IPV6_SERVER_PORT, DHCP_IPV6_CLIENT_PORT))
461             .build();
462     }
463
464     // ###################### DNS UDP
465     private static ClassifierInstance createDnsUdpIpv4ClientServer() {
466         return new ClassifierInstanceBuilder().setName(DNS_UDP_IPV4_CLIENT_SERVER_NAME)
467             .setClassifierDefinitionId(L4ClassifierDefinition.DEFINITION.getId())
468             .setParameterValue(
469                     createParams(EtherTypeClassifierDefinition.IPv4_VALUE, IpProtoClassifierDefinition.UDP_VALUE, null, DNS_SERVER_PORT))
470             .build();
471     }
472
473     private static ClassifierInstance createDnsUdpIpv4ServerClient() {
474         return new ClassifierInstanceBuilder().setName(DNS_UDP_IPV4_SERVER_CLIENT_NAME)
475             .setClassifierDefinitionId(L4ClassifierDefinition.DEFINITION.getId())
476             .setParameterValue(
477                     createParams(EtherTypeClassifierDefinition.IPv4_VALUE, IpProtoClassifierDefinition.UDP_VALUE, DNS_SERVER_PORT, null))
478             .build();
479     }
480
481     private static ClassifierInstance createDnsUdpIpv6ClientServer() {
482         return new ClassifierInstanceBuilder().setName(DNS_UDP_IPV6_CLIENT_SERVER_NAME)
483             .setClassifierDefinitionId(L4ClassifierDefinition.DEFINITION.getId())
484             .setParameterValue(
485                     createParams(EtherTypeClassifierDefinition.IPv6_VALUE, IpProtoClassifierDefinition.UDP_VALUE, null, DNS_SERVER_PORT))
486             .build();
487     }
488
489     private static ClassifierInstance createDnsUdpIpv6ServerClient() {
490         return new ClassifierInstanceBuilder().setName(DNS_UDP_IPV6_SERVER_CLIENT_NAME)
491             .setClassifierDefinitionId(L4ClassifierDefinition.DEFINITION.getId())
492             .setParameterValue(
493                     createParams(EtherTypeClassifierDefinition.IPv6_VALUE, IpProtoClassifierDefinition.UDP_VALUE, DNS_SERVER_PORT, null))
494             .build();
495     }
496
497     // ###################### DNS TCP
498     private static ClassifierInstance createDnsTcpIpv4ClientServer() {
499         return new ClassifierInstanceBuilder().setName(DNS_TCP_IPV4_CLIENT_SERVER_NAME)
500             .setClassifierDefinitionId(L4ClassifierDefinition.DEFINITION.getId())
501             .setParameterValue(
502                     createParams(EtherTypeClassifierDefinition.IPv4_VALUE, IpProtoClassifierDefinition.TCP_VALUE, null, DNS_SERVER_PORT))
503             .build();
504     }
505
506     private static ClassifierInstance createDnsTcpIpv4ServerClient() {
507         return new ClassifierInstanceBuilder().setName(DNS_TCP_IPV4_SERVER_CLIENT_NAME)
508             .setClassifierDefinitionId(L4ClassifierDefinition.DEFINITION.getId())
509             .setParameterValue(
510                     createParams(EtherTypeClassifierDefinition.IPv4_VALUE, IpProtoClassifierDefinition.TCP_VALUE, DNS_SERVER_PORT, null))
511             .build();
512     }
513
514     private static ClassifierInstance createDnsTcpIpv6ClientServer() {
515         return new ClassifierInstanceBuilder().setName(DNS_TCP_IPV6_CLIENT_SERVER_NAME)
516             .setClassifierDefinitionId(L4ClassifierDefinition.DEFINITION.getId())
517             .setParameterValue(
518                     createParams(EtherTypeClassifierDefinition.IPv6_VALUE, IpProtoClassifierDefinition.TCP_VALUE, null, DNS_SERVER_PORT))
519             .build();
520     }
521
522     private static ClassifierInstance createDnsTcpIpv6ServerClient() {
523         return new ClassifierInstanceBuilder().setName(DNS_TCP_IPV6_SERVER_CLIENT_NAME)
524             .setClassifierDefinitionId(L4ClassifierDefinition.DEFINITION.getId())
525             .setParameterValue(
526                     createParams(EtherTypeClassifierDefinition.IPv6_VALUE, IpProtoClassifierDefinition.TCP_VALUE, DNS_SERVER_PORT, null))
527             .build();
528     }
529
530     // ###################### SSH TCP
531     private static ClassifierInstance createSshTcpIpv4ClientServer() {
532         return new ClassifierInstanceBuilder().setName(SSH_IPV4_CLIENT_TO_SERVER_NAME)
533             .setClassifierDefinitionId(L4ClassifierDefinition.DEFINITION.getId())
534             .setParameterValue(
535                     createParams(EtherTypeClassifierDefinition.IPv4_VALUE, IpProtoClassifierDefinition.TCP_VALUE, SSH_TCP_PORT, null))
536             .build();
537     }
538
539     private static ClassifierInstance createSshTcpIpv4ServerClient() {
540         return new ClassifierInstanceBuilder().setName(SSH_IPV4_SERVER_TO_CLIENT_NAME)
541             .setClassifierDefinitionId(L4ClassifierDefinition.DEFINITION.getId())
542             .setParameterValue(
543                     createParams(EtherTypeClassifierDefinition.IPv4_VALUE, IpProtoClassifierDefinition.TCP_VALUE, null, SSH_TCP_PORT))
544             .build();
545     }
546
547     private static ClassifierInstance createSshTcpIpv6ClientServer() {
548         return new ClassifierInstanceBuilder().setName(SSH_IPV6_CLIENT_TO_SERVER_NAME)
549             .setClassifierDefinitionId(L4ClassifierDefinition.DEFINITION.getId())
550             .setParameterValue(
551                     createParams(EtherTypeClassifierDefinition.IPv6_VALUE, IpProtoClassifierDefinition.TCP_VALUE, SSH_TCP_PORT, null))
552             .build();
553     }
554
555     private static ClassifierInstance createSshTcpIpv6ServerClient() {
556         return new ClassifierInstanceBuilder().setName(SSH_IPV6_SERVER_TO_CLIENT_NAME)
557             .setClassifierDefinitionId(L4ClassifierDefinition.DEFINITION.getId())
558             .setParameterValue(
559                     createParams(EtherTypeClassifierDefinition.IPv6_VALUE, IpProtoClassifierDefinition.TCP_VALUE, null, SSH_TCP_PORT))
560             .build();
561     }
562
563     // ###################### ICMP
564     private static ClassifierInstance createIcmpIpv4() {
565         return new ClassifierInstanceBuilder().setName(ICMP_IPV4_BETWEEN_SERVER_CLIENT_NAME)
566             .setClassifierDefinitionId(IpProtoClassifierDefinition.DEFINITION.getId())
567             .setParameterValue(createParams(EtherTypeClassifierDefinition.IPv4_VALUE, IpProtoClassifierDefinition.ICMP_VALUE, null, null))
568             .build();
569     }
570
571     private static ClassifierInstance createIcmpIpv6() {
572         return new ClassifierInstanceBuilder().setName(ICMP_IPV6_BETWEEN_SERVER_CLIENT_NAME)
573             .setClassifierDefinitionId(IpProtoClassifierDefinition.DEFINITION.getId())
574             .setParameterValue(createParams(EtherTypeClassifierDefinition.IPv6_VALUE, IpProtoClassifierDefinition.ICMP_VALUE, null, null))
575             .build();
576     }
577
578     private static List<ParameterValue> createParams(long etherType, long proto, @Nullable Long srcPort,
579             @Nullable Long dstPort) {
580         List<ParameterValue> params = new ArrayList<>();
581         if (srcPort != null) {
582             params.add(new ParameterValueBuilder().setName(new ParameterName(L4ClassifierDefinition.SRC_PORT_PARAM))
583                 .setIntValue(srcPort)
584                 .build());
585         }
586         if (dstPort != null) {
587             params.add(new ParameterValueBuilder().setName(new ParameterName(L4ClassifierDefinition.DST_PORT_PARAM))
588                 .setIntValue(dstPort)
589                 .build());
590         }
591         params.add(new ParameterValueBuilder().setName(new ParameterName(IpProtoClassifierDefinition.PROTO_PARAM))
592             .setIntValue(proto)
593             .build());
594         params.add(new ParameterValueBuilder().setName(new ParameterName(EtherTypeClassifierDefinition.ETHERTYPE_PARAM))
595             .setIntValue(etherType)
596             .build());
597         return params;
598     }
599 }