2 * Copyright (c) 2014 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.util;
11 import java.util.List;
13 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.groupbasedpolicy.api.sf.AllowActionDefinition;
16 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.NeutronPortAware;
17 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
18 import org.opendaylight.groupbasedpolicy.util.IidFactory;
19 import org.opendaylight.neutron.spi.Neutron_IPs;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ActionName;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2FloodDomainId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.change.action.of.security.group.rules.input.action.ActionChoice;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.change.action.of.security.group.rules.input.action.action.choice.AllowActionCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.change.action.of.security.group.rules.input.action.action.choice.SfcActionCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.change.action.of.security.group.rules.input.action.action.choice.allow.action._case.AllowBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.action.refs.ActionRef;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.action.refs.ActionRefBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2BridgeDomain;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2FloodDomain;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L3Context;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ActionInstance;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ActionInstanceBuilder;
38 import com.google.common.base.Optional;
40 public final class MappingUtils {
42 public static final String NEUTRON_ROUTER = "neutron_router-";
43 public static final String NEUTRON_EXTERNAL = "neutron_external_network-";
44 public static final String NEUTRON_GROUP = "neutron_group-";
45 public static final ActionInstance ACTION_ALLOW = new ActionInstanceBuilder().setName(
46 new ActionName("Allow"))
47 .setActionDefinitionId(AllowActionDefinition.DEFINITION.getId())
49 public static final ActionChoice ALLOW_ACTION_CHOICE = new AllowActionCaseBuilder().setAllow(
50 new AllowBuilder().build()).build();
51 public static final ActionRef ACTION_REF_ALLOW =
52 new ActionRefBuilder().setName(ACTION_ALLOW.getName()).setOrder(0).build();
53 public static final EndpointGroupId EPG_EXTERNAL_ID = new EndpointGroupId("eeeaa3a2-e9ba-44e0-a462-bea923d30e38");
55 public static final String NAME_VALUE_DELIMETER = "-";
56 public static final String NAME_DELIMETER = "_";
57 public static final String NAME_DOUBLE_DELIMETER = "__";
59 private MappingUtils() {
60 throw new UnsupportedOperationException("Cannot create an instance.");
63 public static ActionRef createSfcActionRef(String sfcChainName) {
64 return new ActionRefBuilder().setName(new ActionName(sfcChainName)).setOrder(0).build();
67 public static ActionChoice createSfcActionChoice(String chainName) {
68 return new SfcActionCaseBuilder().setSfcChainName(chainName).build();
71 public static ForwardingCtx createForwardingContext(TenantId tenantId, L2FloodDomainId l2FdId, ReadTransaction rTx) {
72 Optional<L2FloodDomain> potentialL2Fd = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
73 IidFactory.l2FloodDomainIid(tenantId, l2FdId), rTx);
74 if (!potentialL2Fd.isPresent()) {
75 return new ForwardingCtx(null, null, null);
77 L2BridgeDomainId l2BdId = potentialL2Fd.get().getParent();
79 return new ForwardingCtx(potentialL2Fd.get(), null, null);
81 Optional<L2BridgeDomain> potentialL2Bd = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
82 IidFactory.l2BridgeDomainIid(tenantId, l2BdId), rTx);
83 if (!potentialL2Bd.isPresent()) {
84 return new ForwardingCtx(potentialL2Fd.get(), null, null);
86 L3ContextId l3ContextId = potentialL2Bd.get().getParent();
87 if (l3ContextId == null) {
88 return new ForwardingCtx(potentialL2Fd.get(), potentialL2Bd.get(), null);
90 Optional<L3Context> potentialL3Context = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
91 IidFactory.l3ContextIid(tenantId, l3ContextId), rTx);
92 if (!potentialL3Context.isPresent()) {
93 return new ForwardingCtx(potentialL2Fd.get(), potentialL2Bd.get(), null);
95 return new ForwardingCtx(potentialL2Fd.get(), potentialL2Bd.get(), potentialL3Context.get());
98 public static final class ForwardingCtx {
100 private final L2FloodDomain l2FloodDomain;
101 private final L2BridgeDomain l2BridgeDomain;
102 private final L3Context l3Context;
104 private ForwardingCtx(L2FloodDomain l2Fd, L2BridgeDomain l2Bd, L3Context l3Context) {
105 this.l2FloodDomain = l2Fd;
106 this.l2BridgeDomain = l2Bd;
107 this.l3Context = l3Context;
110 public L2FloodDomain getL2FloodDomain() {
111 return l2FloodDomain;
114 public L2BridgeDomain getL2BridgeDomain() {
115 return l2BridgeDomain;
118 public L3Context getL3Context() {
124 public static Neutron_IPs getFirstIp(List<Neutron_IPs> fixedIPs) {
125 if (fixedIPs == null || fixedIPs.isEmpty()) {
128 Neutron_IPs neutron_Ip = fixedIPs.get(0);
129 if (fixedIPs.size() > 1) {
130 NeutronPortAware.LOG.warn("Neutron mapper does not support multiple IPs on the same port. Only first IP is selected {}",