1 package org.opendaylight.groupbasedpolicy.neutron.mapper.util;
5 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
6 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
7 import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.NeutronPortAware;
8 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf.AllowAction;
9 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
10 import org.opendaylight.groupbasedpolicy.util.IidFactory;
11 import org.opendaylight.neutron.spi.Neutron_IPs;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ActionName;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2FloodDomainId;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.action.refs.ActionRef;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.action.refs.ActionRefBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L2BridgeDomain;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L2FloodDomain;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L3Context;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.subject.feature.instances.ActionInstance;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.subject.feature.instances.ActionInstanceBuilder;
26 import com.google.common.base.Optional;
27 import com.google.common.collect.ImmutableList;
29 public final class MappingUtils {
31 public static final String NEUTRON_ROUTER = "neutron_router-";
32 public static final String NEUTRON_EXTERNAL = "neutron_external_network-";
33 public static final String NEUTRON_GROUP = "neutron_group-";
34 public static final ActionInstance ACTION_ALLOW = new ActionInstanceBuilder().setName(
35 new ActionName("Allow"))
36 .setActionDefinitionId(AllowAction.DEFINITION.getId())
38 public static final List<ActionRef> ACTION_REF_ALLOW =
39 ImmutableList.of(new ActionRefBuilder().setName(ACTION_ALLOW.getName()).setOrder(0).build());
40 public static final EndpointGroupId EPG_EXTERNAL_ID = new EndpointGroupId("eeeaa3a2-e9ba-44e0-a462-bea923d30e38");
42 public static final String NAME_VALUE_DELIMETER = "-";
43 public static final String NAME_DELIMETER = "_";
44 public static final String NAME_DOUBLE_DELIMETER = "__";
46 private MappingUtils() {
47 throw new UnsupportedOperationException("Cannot create an instance.");
50 public static ForwardingCtx createForwardingContext(TenantId tenantId, L2FloodDomainId l2FdId, ReadTransaction rTx) {
51 Optional<L2FloodDomain> potentialL2Fd = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
52 IidFactory.l2FloodDomainIid(tenantId, l2FdId), rTx);
53 if (!potentialL2Fd.isPresent()) {
54 return new ForwardingCtx(null, null, null);
56 L2BridgeDomainId l2BdId = potentialL2Fd.get().getParent();
58 return new ForwardingCtx(potentialL2Fd.get(), null, null);
60 Optional<L2BridgeDomain> potentialL2Bd = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
61 IidFactory.l2BridgeDomainIid(tenantId, l2BdId), rTx);
62 if (!potentialL2Bd.isPresent()) {
63 return new ForwardingCtx(potentialL2Fd.get(), null, null);
65 L3ContextId l3ContextId = potentialL2Bd.get().getParent();
66 if (l3ContextId == null) {
67 return new ForwardingCtx(potentialL2Fd.get(), potentialL2Bd.get(), null);
69 Optional<L3Context> potentialL3Context = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
70 IidFactory.l3ContextIid(tenantId, l3ContextId), rTx);
71 if (!potentialL3Context.isPresent()) {
72 return new ForwardingCtx(potentialL2Fd.get(), potentialL2Bd.get(), null);
74 return new ForwardingCtx(potentialL2Fd.get(), potentialL2Bd.get(), potentialL3Context.get());
77 public static final class ForwardingCtx {
79 private final L2FloodDomain l2FloodDomain;
80 private final L2BridgeDomain l2BridgeDomain;
81 private final L3Context l3Context;
83 private ForwardingCtx(L2FloodDomain l2Fd, L2BridgeDomain l2Bd, L3Context l3Context) {
84 this.l2FloodDomain = l2Fd;
85 this.l2BridgeDomain = l2Bd;
86 this.l3Context = l3Context;
89 public L2FloodDomain getL2FloodDomain() {
93 public L2BridgeDomain getL2BridgeDomain() {
94 return l2BridgeDomain;
97 public L3Context getL3Context() {
103 public static Neutron_IPs getFirstIp(List<Neutron_IPs> fixedIPs) {
104 if (fixedIPs == null || fixedIPs.isEmpty()) {
107 Neutron_IPs neutron_Ip = fixedIPs.get(0);
108 if (fixedIPs.size() > 1) {
109 NeutronPortAware.LOG.warn("Neutron mapper does not support multiple IPs on the same port. Only first IP is selected {}",