72b9618c9c7c6d816b2294f1fb05edc73d05f7a8
[groupbasedpolicy.git] / neutron-mapper / src / main / java / org / opendaylight / groupbasedpolicy / neutron / mapper / util / MappingUtils.java
1 package org.opendaylight.groupbasedpolicy.neutron.mapper.util;
2
3 import java.util.List;
4
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;
25
26 import com.google.common.base.Optional;
27 import com.google.common.collect.ImmutableList;
28
29 public final class MappingUtils {
30
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())
37         .build();
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");
41
42     public static final String NAME_VALUE_DELIMETER = "-";
43     public static final String NAME_DELIMETER = "_";
44     public static final String NAME_DOUBLE_DELIMETER = "__";
45
46     private MappingUtils() {
47         throw new UnsupportedOperationException("Cannot create an instance.");
48     }
49
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);
55         }
56         L2BridgeDomainId l2BdId = potentialL2Fd.get().getParent();
57         if (l2BdId == null) {
58             return new ForwardingCtx(potentialL2Fd.get(), null, null);
59         }
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);
64         }
65         L3ContextId l3ContextId = potentialL2Bd.get().getParent();
66         if (l3ContextId == null) {
67             return new ForwardingCtx(potentialL2Fd.get(), potentialL2Bd.get(), null);
68         }
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);
73         }
74         return new ForwardingCtx(potentialL2Fd.get(), potentialL2Bd.get(), potentialL3Context.get());
75     }
76
77     public static final class ForwardingCtx {
78
79         private final L2FloodDomain l2FloodDomain;
80         private final L2BridgeDomain l2BridgeDomain;
81         private final L3Context l3Context;
82
83         private ForwardingCtx(L2FloodDomain l2Fd, L2BridgeDomain l2Bd, L3Context l3Context) {
84             this.l2FloodDomain = l2Fd;
85             this.l2BridgeDomain = l2Bd;
86             this.l3Context = l3Context;
87         }
88
89         public L2FloodDomain getL2FloodDomain() {
90             return l2FloodDomain;
91         }
92
93         public L2BridgeDomain getL2BridgeDomain() {
94             return l2BridgeDomain;
95         }
96
97         public L3Context getL3Context() {
98             return l3Context;
99         }
100
101     }
102
103     public static Neutron_IPs getFirstIp(List<Neutron_IPs> fixedIPs) {
104         if (fixedIPs == null || fixedIPs.isEmpty()) {
105             return null;
106         }
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 {}",
110                     neutron_Ip);
111         }
112         return neutron_Ip;
113     }
114 }