5fe122044fc18d89614a3b91cebcfb4908ba7eb3
[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 java.util.Collections;
12 import java.util.HashSet;
13 import java.util.Set;
14
15 import javax.annotation.Nullable;
16
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.groupbasedpolicy.api.sf.EtherTypeClassifierDefinition;
20 import org.opendaylight.groupbasedpolicy.api.sf.IpProtoClassifierDefinition;
21 import org.opendaylight.groupbasedpolicy.api.sf.L4ClassifierDefinition;
22 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.MappingUtils;
23 import org.opendaylight.groupbasedpolicy.util.IidFactory;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.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.ContractId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Description;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Name;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.HasDirection.Direction;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.endpoint.identification.constraints.L3EndpointIdentificationConstraints;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.Contract;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.Clause;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.Subject;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.SubjectBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.ConsumerMatchers;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.ProviderMatchers;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.subject.Rule;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ConsumerNamedSelector;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ProviderNamedSelector;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ActionInstance;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ClassifierInstance;
46
47 import com.google.common.collect.ImmutableList;
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
65      * communication
66      * between Client and Server.
67      */
68     public static final Contract METADATA_CONTRACT;
69     /**
70      * {@link ConsumerNamedSelector} pointing to {@link #METADATA_CONTRACT}
71      */
72     public static final ConsumerNamedSelector METADATA_CONTRACT_CONSUMER_SELECTOR;
73
74     // ########### NETWORK-SERVICE ENDPOINT-GROUP
75     private static final Name METADATA_SERVICE_EPG_NAME = new Name("NETWORK_SERVICE");
76     private static final Description METADATA_SERVICE_EPG_DESC = new Description("Represents DHCP and DNS servers.");
77     /**
78      * ID of {@link #EPG}
79      */
80     public static final EndpointGroupId EPG_ID = new EndpointGroupId("ffff1111-dfe5-11e4-8a00-1681e6b88ec1");
81     /**
82      * Network-service endpoint-group providing {@link #METADATA_CONTRACT}
83      */
84     public static final EndpointGroup EPG;
85
86     static {
87         METADATA_CONTRACT = createContractMetadata();
88         METADATA_CONTRACT_CONSUMER_SELECTOR = createConsumerSelector(METADATA_CONTRACT);
89         EPG = createNetworkServiceEpg();
90     }
91
92     private static EndpointGroup createNetworkServiceEpg() {
93         ProviderNamedSelector metadataProviderSelector = createProviderSelector(METADATA_CONTRACT);
94         return createEpgBuilder(EPG_ID, METADATA_SERVICE_EPG_NAME, METADATA_SERVICE_EPG_DESC)
95             .setProviderNamedSelector(ImmutableList.of(metadataProviderSelector))
96             .build();
97     }
98
99     private static Contract createContractMetadata() {
100         Rule serverClientMetadataIpv4Rule = createRuleAllow(METADATA_SERVER_TO_CLIENT_NAME, Direction.Out);
101         Rule clientServerMetadataIpv4Rule = createRuleAllow(METADATA_CLIENT_TO_SERVER_NAME, Direction.In);
102         Subject subject = new SubjectBuilder().setName(METADATA_SUBJECT_NAME)
103             .setOrder(0)
104             .setRule(ImmutableList.of(serverClientMetadataIpv4Rule, clientServerMetadataIpv4Rule))
105             .build();
106         return createContract(METADATA_CONTRACT_ID, ImmutableList.of(subject), METADATA_CONTRACT_DESC);
107     }
108
109     /**
110      * puts clause with {@link L3EndpointIdentificationConstraints} in {@link ConsumerMatchers}
111      * and {@link ProviderMatchers}. This clause points to subject in {@link #METADATA_CONTRACT}.
112      *
113      * @param tenantId location of {@link #METADATA_CONTRACT}
114      * @param ipPrefix used in {@link L3EndpointIdentificationConstraints}
115      * @param wTx transaction where entities are written
116      */
117     public static void writeMetadataClauseWithConsProvEic(TenantId tenantId, @Nullable IpPrefix ipPrefix,
118         WriteTransaction wTx) {
119         Clause clause = createClauseWithConsProvEic(ipPrefix, METADATA_SUBJECT_NAME);
120         wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.clauseIid(tenantId, METADATA_CONTRACT_ID, clause.getName()),
121             clause, true);
122     }
123
124     /**
125      * Puts network service entities (classifier-instances, {@link #METADATA_CONTRACT},
126      * and {@link #EPG}) to {@link LogicalDatastoreType#CONFIGURATION}
127      *
128      * @param tenantId location of network-service entities
129      * @param wTx transaction where network-service entities are written
130      */
131     public static void writeNetworkServiceEntitiesToTenant(TenantId tenantId, WriteTransaction wTx, long metadataPort) {
132         Set<ClassifierInstance> classifierInstances = getAllClassifierInstances(metadataPort);
133         for (ClassifierInstance ci : classifierInstances) {
134             wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.classifierInstanceIid(tenantId, ci.getName()), ci,
135                     true);
136         }
137         for (ActionInstance ai : Collections.singleton(MappingUtils.ACTION_ALLOW)) {
138             wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.actionInstanceIid(tenantId, ai.getName()), ai, true);
139         }
140         wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.contractIid(tenantId, METADATA_CONTRACT_ID), METADATA_CONTRACT,
141                 true);
142         wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.endpointGroupIid(tenantId, EPG_ID), EPG, true);
143     }
144
145     /**
146      * @return All classifier-instances used in {@link #METADATA_CONTRACT}
147      */
148     public static Set<ClassifierInstance> getAllClassifierInstances(long metadataPort) {
149         HashSet<ClassifierInstance> cis = new HashSet<>();
150         // METADATA
151         cis.add(createMetadataIpv4ClientServer(metadataPort));
152         cis.add(createMetadataIpv4ServerClient(metadataPort));
153         return cis;
154     }
155
156     private static ClassifierInstance createMetadataIpv4ClientServer(long dstPort) {
157         return createClassifInstance(METADATA_CLIENT_TO_SERVER_NAME,
158                 L4ClassifierDefinition.DEFINITION.getId(),
159                 createParams(EtherTypeClassifierDefinition.IPv4_VALUE,
160                         IpProtoClassifierDefinition.TCP_VALUE, null, dstPort));
161     }
162
163     private static ClassifierInstance createMetadataIpv4ServerClient(long srcPort) {
164         return createClassifInstance(METADATA_SERVER_TO_CLIENT_NAME,
165                 L4ClassifierDefinition.DEFINITION.getId(),
166                 createParams(EtherTypeClassifierDefinition.IPv4_VALUE,
167                         IpProtoClassifierDefinition.TCP_VALUE, srcPort, null));
168     }
169 }