apply checkstyle check during build for neutron-mapper
[groupbasedpolicy.git] / neutron-mapper / src / main / java / org / opendaylight / groupbasedpolicy / neutron / mapper / infrastructure / MetadataService.java
1 /*
2  * Copyright (c) 2017 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 com.google.common.collect.ImmutableList;
12
13 import java.util.Collections;
14 import java.util.HashSet;
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.util.IidFactory;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.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.ContractId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Description;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Name;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.HasDirection.Direction;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.endpoint.identification.constraints.L3EndpointIdentificationConstraints;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.Contract;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.Clause;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.Subject;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.SubjectBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.ConsumerMatchers;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.ProviderMatchers;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.subject.Rule;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ConsumerNamedSelector;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ProviderNamedSelector;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ActionInstance;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ClassifierInstance;
48
49 public class MetadataService extends ServiceUtil {
50
51     private static final ClassifierName METADATA_SERVER_TO_CLIENT_NAME =
52         new ClassifierName("METADATA_FROM_SERVER_TO_CLIENT");
53     private static final ClassifierName METADATA_CLIENT_TO_SERVER_NAME =
54         new ClassifierName("METADATA_FROM_CLIENT_TO_SERVER");
55     private static final SubjectName METADATA_SUBJECT_NAME = new SubjectName("ALLOW_METADATA");
56     private static final Description METADATA_CONTRACT_DESC =
57         new Description("Allow METADATA management communication between server and client.");
58
59     /**
60      * Id of {@link #METADATA_CONTRACT}.
61      */
62     public static final ContractId METADATA_CONTRACT_ID = new ContractId("be0675b7-b0d6-46cc-acf1-247ed31cf572");
63     /**
64      * Contains rules with action {@link MappingUtils#ACTION_REF_ALLOW} matching ICMP and SSH communication
65      * between Client and Server.
66      */
67     public static final Contract METADATA_CONTRACT;
68     /**
69      * {@link ConsumerNamedSelector} pointing to {@link #METADATA_CONTRACT}.
70      */
71     public static final ConsumerNamedSelector METADATA_CONTRACT_CONSUMER_SELECTOR;
72
73     // ########### NETWORK-SERVICE ENDPOINT-GROUP
74     private static final Name METADATA_SERVICE_EPG_NAME = new Name("NETWORK_SERVICE");
75     private static final Description METADATA_SERVICE_EPG_DESC = new Description("Represents DHCP and DNS servers.");
76     /**
77      * ID of {@link #EPG}.
78      */
79     public static final EndpointGroupId EPG_ID = new EndpointGroupId("ffff1111-dfe5-11e4-8a00-1681e6b88ec1");
80     /**
81      * Network-service endpoint-group providing {@link #METADATA_CONTRACT}.
82      */
83     public static final EndpointGroup EPG;
84
85     static {
86         METADATA_CONTRACT = createContractMetadata();
87         METADATA_CONTRACT_CONSUMER_SELECTOR = createConsumerSelector(METADATA_CONTRACT);
88         EPG = createNetworkServiceEpg();
89     }
90
91     private static EndpointGroup createNetworkServiceEpg() {
92         ProviderNamedSelector metadataProviderSelector = createProviderSelector(METADATA_CONTRACT);
93         return createEpgBuilder(EPG_ID, METADATA_SERVICE_EPG_NAME, METADATA_SERVICE_EPG_DESC)
94             .setProviderNamedSelector(ImmutableList.of(metadataProviderSelector))
95             .build();
96     }
97
98     private static Contract createContractMetadata() {
99         Rule serverClientMetadataIpv4Rule = createRuleAllow(METADATA_SERVER_TO_CLIENT_NAME, Direction.Out);
100         Rule clientServerMetadataIpv4Rule = createRuleAllow(METADATA_CLIENT_TO_SERVER_NAME, Direction.In);
101         Subject subject = new SubjectBuilder().setName(METADATA_SUBJECT_NAME)
102             .setOrder(0)
103             .setRule(ImmutableList.of(serverClientMetadataIpv4Rule, clientServerMetadataIpv4Rule))
104             .build();
105         return createContract(METADATA_CONTRACT_ID, ImmutableList.of(subject), METADATA_CONTRACT_DESC);
106     }
107
108     /**
109      * puts clause with {@link L3EndpointIdentificationConstraints} in {@link ConsumerMatchers}
110      * and {@link ProviderMatchers}. This clause points to subject in {@link #METADATA_CONTRACT}.
111      *
112      * @param tenantId location of {@link #METADATA_CONTRACT}
113      * @param ipPrefix used in {@link L3EndpointIdentificationConstraints}
114      * @param writeTx transaction where entities are written
115      */
116     public static void writeMetadataClauseWithConsProvEic(TenantId tenantId, @Nullable IpPrefix ipPrefix,
117         WriteTransaction writeTx) {
118         Clause clause = createClauseWithConsProvEic(ipPrefix, METADATA_SUBJECT_NAME);
119         writeTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.clauseIid(tenantId, METADATA_CONTRACT_ID,
120             clause.getName()), clause, true);
121     }
122
123     /**
124      * Puts network service entities (classifier-instances, {@link #METADATA_CONTRACT},
125      * and {@link #EPG}) to {@link LogicalDatastoreType#CONFIGURATION}.
126      *
127      * @param tenantId location of network-service entities
128      * @param writeTx transaction where network-service entities are written
129      * @param metadataPort port for metadata
130      */
131     public static void writeNetworkServiceEntitiesToTenant(TenantId tenantId, WriteTransaction writeTx,
132         long metadataPort) {
133
134         Set<ClassifierInstance> classifierInstances = getAllClassifierInstances(metadataPort);
135         for (ClassifierInstance ci : classifierInstances) {
136             writeTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.classifierInstanceIid(tenantId, ci.getName()),
137                 ci, true);
138         }
139         for (ActionInstance ai : Collections.singleton(MappingUtils.ACTION_ALLOW)) {
140             writeTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.actionInstanceIid(tenantId, ai.getName()), ai,
141                 true);
142         }
143         writeTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.contractIid(tenantId, METADATA_CONTRACT_ID),
144             METADATA_CONTRACT, true);
145         writeTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.endpointGroupIid(tenantId, EPG_ID), EPG, true);
146     }
147
148     /**
149      * Used to get all classifier-instances used in {@link #METADATA_CONTRACT} for specified port.
150      *
151      * @param metadataPort port for metadata
152      * @return All classifier-instances used in {@link #METADATA_CONTRACT}
153      */
154     public static Set<ClassifierInstance> getAllClassifierInstances(long metadataPort) {
155         HashSet<ClassifierInstance> cis = new HashSet<>();
156         // METADATA
157         cis.add(createMetadataIpv4ClientServer(metadataPort));
158         cis.add(createMetadataIpv4ServerClient(metadataPort));
159         return cis;
160     }
161
162     private static ClassifierInstance createMetadataIpv4ClientServer(long dstPort) {
163         return createClassifInstance(METADATA_CLIENT_TO_SERVER_NAME,
164                 L4ClassifierDefinition.DEFINITION.getId(),
165                 createParams(EtherTypeClassifierDefinition.IPv4_VALUE,
166                         IpProtoClassifierDefinition.TCP_VALUE, null, dstPort));
167     }
168
169     private static ClassifierInstance createMetadataIpv4ServerClient(long srcPort) {
170         return createClassifInstance(METADATA_SERVER_TO_CLIENT_NAME,
171                 L4ClassifierDefinition.DEFINITION.getId(),
172                 createParams(EtherTypeClassifierDefinition.IPv4_VALUE,
173                         IpProtoClassifierDefinition.TCP_VALUE, srcPort, null));
174     }
175 }