2 * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.neutron.mapper.infrastructure;
11 import java.util.Collections;
12 import java.util.HashSet;
15 import javax.annotation.Nullable;
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;
47 import com.google.common.collect.ImmutableList;
49 public class MetadataService extends ServiceUtil {
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.");
60 * Id of {@link #METADATA_CONTRACT}
62 public static final ContractId METADATA_CONTRACT_ID = new ContractId("be0675b7-b0d6-46cc-acf1-247ed31cf572");
64 * Contains rules with action {@link MappingUtils#ACTION_REF_ALLOW} matching ICMP and SSH
66 * between Client and Server.
68 public static final Contract METADATA_CONTRACT;
70 * {@link ConsumerNamedSelector} pointing to {@link #METADATA_CONTRACT}
72 public static final ConsumerNamedSelector METADATA_CONTRACT_CONSUMER_SELECTOR;
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.");
80 public static final EndpointGroupId EPG_ID = new EndpointGroupId("ffff1111-dfe5-11e4-8a00-1681e6b88ec1");
82 * Network-service endpoint-group providing {@link #METADATA_CONTRACT}
84 public static final EndpointGroup EPG;
87 METADATA_CONTRACT = createContractMetadata();
88 METADATA_CONTRACT_CONSUMER_SELECTOR = createConsumerSelector(METADATA_CONTRACT);
89 EPG = createNetworkServiceEpg();
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))
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)
104 .setRule(ImmutableList.of(serverClientMetadataIpv4Rule, clientServerMetadataIpv4Rule))
106 return createContract(METADATA_CONTRACT_ID, ImmutableList.of(subject), METADATA_CONTRACT_DESC);
110 * puts clause with {@link L3EndpointIdentificationConstraints} in {@link ConsumerMatchers}
111 * and {@link ProviderMatchers}. This clause points to subject in {@link #METADATA_CONTRACT}.
113 * @param tenantId location of {@link #METADATA_CONTRACT}
114 * @param ipPrefix used in {@link L3EndpointIdentificationConstraints}
115 * @param wTx transaction where entities are written
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()),
125 * Puts network service entities (classifier-instances, {@link #METADATA_CONTRACT},
126 * and {@link #EPG}) to {@link LogicalDatastoreType#CONFIGURATION}
128 * @param tenantId location of network-service entities
129 * @param wTx transaction where network-service entities are written
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,
137 for (ActionInstance ai : Collections.singleton(MappingUtils.ACTION_ALLOW)) {
138 wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.actionInstanceIid(tenantId, ai.getName()), ai, true);
140 wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.contractIid(tenantId, METADATA_CONTRACT_ID), METADATA_CONTRACT,
142 wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.endpointGroupIid(tenantId, EPG_ID), EPG, true);
146 * @return All classifier-instances used in {@link #METADATA_CONTRACT}
148 public static Set<ClassifierInstance> getAllClassifierInstances(long metadataPort) {
149 HashSet<ClassifierInstance> cis = new HashSet<>();
151 cis.add(createMetadataIpv4ClientServer(metadataPort));
152 cis.add(createMetadataIpv4ServerClient(metadataPort));
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));
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));