7d47714169b1a90da15d710fd1f714823521db27
[groupbasedpolicy.git] / neutron-mapper / src / main / java / org / opendaylight / groupbasedpolicy / neutron / mapper / infrastructure / Router.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.util.IidFactory;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierName;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClauseName;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContractId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Description;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Name;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ParameterName;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.RuleName;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SelectorName;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.HasDirection.Direction;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.classifier.refs.ClassifierRef;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.classifier.refs.ClassifierRefBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.EndpointIdentificationConstraintsBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.endpoint.identification.constraints.L3EndpointIdentificationConstraints;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.endpoint.identification.constraints.L3EndpointIdentificationConstraintsBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.endpoint.identification.constraints.l3.endpoint.identification.constraints.PrefixConstraintBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValueBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.Contract;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.ContractBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.EndpointGroup;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.EndpointGroup.IntraGroupPolicy;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.EndpointGroupBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.Clause;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.ClauseBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.Subject;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.SubjectBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.ConsumerMatchers;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.ConsumerMatchersBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.ProviderMatchers;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.ProviderMatchersBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.subject.Rule;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.subject.RuleBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ConsumerNamedSelector;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ConsumerNamedSelectorBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ProviderNamedSelector;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ProviderNamedSelectorBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.subject.feature.instances.ClassifierInstance;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.subject.feature.instances.ClassifierInstanceBuilder;
66
67 import com.google.common.collect.ImmutableList;
68
69 public class Router {
70
71     private static final ClassifierName IPV4_NAME = new ClassifierName("IPv4");
72     private static final ClassifierName IPV6_NAME = new ClassifierName("IPv6");
73     private static final SubjectName ROUTER_SUBJECT_NAME = new SubjectName("ALLOW_IPv4_IPv6");
74     private static final Description ROUTER_CONTRACT_DESC =
75             new Description("Allow IPv4 and IPv6 communication between router interfaces and endpoints.");
76     /**
77      * ID of {@link Contract}
78      */
79     public static final ContractId CONTRACT_ID = new ContractId("111bc60e-1110-11e5-885d-feff819cdc9f");
80     /**
81      * Contains rules with action {@link MappingUtils#ACTION_REF_ALLOW} matching IPv4 and IPv6
82      * communication in both directions
83      */
84     public static final Contract CONTRACT;
85
86     private static final Name ROUTER_EPG_NAME = new Name("ROUTER_PORTS");
87     private static final Description ROUTER_EPG_DESC = new Description("Represents router's interfaces.");
88     /**
89      * ID of {@link #EPG}
90      */
91     public static final EndpointGroupId EPG_ID = new EndpointGroupId("1118172e-cd84-4933-a35f-749f9a651de9");
92     /**
93      * Router endpoint-group providing {@link #CONTRACT}
94      */
95     public static final EndpointGroup EPG;
96     /**
97      * {@link ConsumerNamedSelector} pointing to {@link #CONTRACT}
98      */
99     public static final ConsumerNamedSelector CONTRACT_CONSUMER_SELECTOR;
100
101     static {
102         CONTRACT = createContractRouter();
103         CONTRACT_CONSUMER_SELECTOR = createConsumerSelector(CONTRACT);
104         EPG = createRouterEpg();
105     }
106
107     private static EndpointGroup createRouterEpg() {
108         ProviderNamedSelector routerProviderSelector = createProviderSelector(CONTRACT);
109         return new EndpointGroupBuilder().setId(EPG_ID)
110             .setName(ROUTER_EPG_NAME)
111             .setProviderNamedSelector(ImmutableList.of(routerProviderSelector))
112             .setIntraGroupPolicy(IntraGroupPolicy.RequireContract)
113             .setDescription(ROUTER_EPG_DESC)
114             .build();
115     }
116
117     private static ProviderNamedSelector createProviderSelector(Contract contract) {
118         SelectorName selectorName = new SelectorName(contract.getSubject().get(0).getName().getValue());
119         return new ProviderNamedSelectorBuilder().setName(selectorName)
120             .setContract(ImmutableList.of(contract.getId()))
121             .build();
122     }
123
124     private static ConsumerNamedSelector createConsumerSelector(Contract contract) {
125         SelectorName selectorName = new SelectorName(contract.getSubject().get(0).getName().getValue());
126         return new ConsumerNamedSelectorBuilder().setName(selectorName)
127             .setContract(ImmutableList.of(contract.getId()))
128             .build();
129     }
130
131     private static Contract createContractRouter() {
132         Rule endpointRouterIpv4Rule = createRuleAllow(IPV4_NAME, Direction.In);
133         Rule routerEndpointIpv4Rule = createRuleAllow(IPV4_NAME, Direction.Out);
134         Rule endpointRouterIpv6Rule = createRuleAllow(IPV6_NAME, Direction.In);
135         Rule routerEndpointIpv6Rule = createRuleAllow(IPV6_NAME, Direction.Out);
136         Subject subject = new SubjectBuilder().setName(ROUTER_SUBJECT_NAME)
137             .setOrder(0)
138             .setRule(ImmutableList.of(endpointRouterIpv4Rule, routerEndpointIpv4Rule, endpointRouterIpv6Rule,
139                     routerEndpointIpv6Rule))
140             .build();
141         return new ContractBuilder().setId(CONTRACT_ID)
142             .setSubject(ImmutableList.of(subject))
143             .setDescription(ROUTER_CONTRACT_DESC)
144             .build();
145     }
146
147     private static Rule createRuleAllow(ClassifierName classifierName, Direction direction) {
148         ClassifierName name =
149                 new ClassifierName(direction.name() + MappingUtils.NAME_DOUBLE_DELIMETER + classifierName.getValue());
150         ClassifierRef classifierRef = new ClassifierRefBuilder().setName(name)
151             .setInstanceName(classifierName)
152             .setDirection(direction)
153             .build();
154         return new RuleBuilder().setName(new RuleName(name))
155             .setActionRef(MappingUtils.ACTION_REF_ALLOW)
156             .setClassifierRef(ImmutableList.of(classifierRef))
157             .build();
158     }
159
160     /**
161      * puts clause with {@link L3EndpointIdentificationConstraints} in {@link ConsumerMatchers}
162      * and {@link ProviderMatchers}. This clause points to subject in {@link #CONTRACT}.
163      *
164      * @param tenantId location of {@link #CONTRACT}
165      * @param ipPrefix used in {@link L3EndpointIdentificationConstraints}
166      * @param wTx transaction where entities are written
167      */
168     public static void writeRouterClauseWithConsProvEic(TenantId tenantId, @Nullable IpPrefix ipPrefix,
169             WriteTransaction wTx) {
170         Clause clause = createClauseWithConsProvEic(ipPrefix, ROUTER_SUBJECT_NAME);
171         wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.clauseIid(tenantId, CONTRACT_ID, clause.getName()),
172                 clause, true);
173     }
174
175     private static Clause createClauseWithConsProvEic(@Nullable IpPrefix ipPrefix, SubjectName subjectName) {
176         ConsumerMatchers consumerMatchers = null;
177         ProviderMatchers providerMatchers = null;
178         StringBuilder clauseName = new StringBuilder();
179         clauseName.append(subjectName.getValue());
180         if (ipPrefix != null) {
181             clauseName.append(MappingUtils.NAME_DOUBLE_DELIMETER).append(Utils.getStringIpPrefix(ipPrefix));
182             consumerMatchers =
183                     new ConsumerMatchersBuilder()
184                         .setEndpointIdentificationConstraints(new EndpointIdentificationConstraintsBuilder()
185                             .setL3EndpointIdentificationConstraints(new L3EndpointIdentificationConstraintsBuilder()
186                                 .setPrefixConstraint(
187                                         ImmutableList.of(new PrefixConstraintBuilder().setIpPrefix(ipPrefix).build()))
188                                 .build())
189                             .build())
190                         .build();
191             providerMatchers =
192                     new ProviderMatchersBuilder()
193                         .setEndpointIdentificationConstraints(new EndpointIdentificationConstraintsBuilder()
194                             .setL3EndpointIdentificationConstraints(new L3EndpointIdentificationConstraintsBuilder()
195                                 .setPrefixConstraint(
196                                         ImmutableList.of(new PrefixConstraintBuilder().setIpPrefix(ipPrefix).build()))
197                                 .build())
198                             .build())
199                         .build();
200         }
201         return new ClauseBuilder().setName(new ClauseName(clauseName.toString()))
202             .setSubjectRefs(ImmutableList.of(subjectName))
203             .setConsumerMatchers(consumerMatchers)
204             .setProviderMatchers(providerMatchers)
205             .build();
206     }
207
208     /**
209      * Puts router entities (classifier-instances, {@link #CONTRACT} and {@link #EPG}) to
210      * {@link LogicalDatastoreType#CONFIGURATION}
211      *
212      * @param tenantId location of router entities
213      * @param wTx transaction where router entities are written
214      */
215     public static void writeRouterEntitiesToTenant(TenantId tenantId, WriteTransaction wTx) {
216         Set<ClassifierInstance> classifierInstances = getAllClassifierInstances();
217         for (ClassifierInstance ci : classifierInstances) {
218             wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.classifierInstanceIid(tenantId, ci.getName()), ci,
219                     true);
220         }
221         wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.contractIid(tenantId, CONTRACT_ID), CONTRACT, true);
222         wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.endpointGroupIid(tenantId, EPG_ID), EPG, true);
223     }
224
225     /**
226      * @return All classifier-instances used in {@link CONTRACT}
227      */
228     public static Set<ClassifierInstance> getAllClassifierInstances() {
229         HashSet<ClassifierInstance> cis = new HashSet<>();
230         cis.add(createIpv4());
231         cis.add(createIpv6());
232         return cis;
233     }
234
235     private static ClassifierInstance createIpv4() {
236         return new ClassifierInstanceBuilder().setName(IPV4_NAME)
237             .setClassifierDefinitionId(EtherTypeClassifier.DEFINITION.getId())
238             .setParameterValue(createParams(EtherTypeClassifier.IPv4_VALUE))
239             .build();
240     }
241
242     private static ClassifierInstance createIpv6() {
243         return new ClassifierInstanceBuilder().setName(IPV6_NAME)
244             .setClassifierDefinitionId(EtherTypeClassifier.DEFINITION.getId())
245             .setParameterValue(createParams(EtherTypeClassifier.IPv6_VALUE))
246             .build();
247     }
248
249     private static List<ParameterValue> createParams(long etherType) {
250         List<ParameterValue> params = new ArrayList<>();
251         params.add(new ParameterValueBuilder().setName(new ParameterName(EtherTypeClassifier.ETHERTYPE_PARAM))
252             .setIntValue(etherType)
253             .build());
254         return params;
255     }
256
257 }