Merge "Parameter of classifiers changed in POC"
[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.neutron.spi.Neutron_IPs;
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ActionName;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2FloodDomainId;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L2BridgeDomain;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L2FloodDomain;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L3Context;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.subject.feature.instances.ActionInstance;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.subject.feature.instances.ActionInstanceBuilder;
21
22 import com.google.common.base.Optional;
23
24 public final class MappingUtils {
25
26     public static final String NEUTRON_RULE__ = "neutron_rule__";
27     public static final String NEUTRON_NETWORK__ = "neutron_network__";
28     public static final String NEUTRON_ROUTER__ = "neutron_router__";
29     public static final String NEUTRON_GROUP__ = "neutron_group__";
30     public static final ActionInstance ACTION_ALLOW = new ActionInstanceBuilder().setName(
31             new ActionName(NEUTRON_RULE__ + "allow"))
32         .setActionDefinitionId(AllowAction.DEFINITION.getId())
33         .build();
34     public static final EndpointGroupId EPG_ANY_ID = new EndpointGroupId("aaaec0ce-dd5a-11e4-b9d6-1681e6b88ec1");
35     public static final EndpointGroupId EPG_DHCP_ID = new EndpointGroupId("ddd6cfe6-dfe5-11e4-8a00-1681e6b88ec1");
36     public static final EndpointGroupId EPG_ROUTER_ID = new EndpointGroupId("1118172e-cd84-4933-a35f-749f9a651de9");
37
38     private MappingUtils() {
39         throw new UnsupportedOperationException("Cannot create an instance.");
40     }
41
42     public static ForwardingCtx createForwardingContext(TenantId tenantId, L2FloodDomainId l2FdId, ReadTransaction rTx) {
43         Optional<L2FloodDomain> potentialL2Fd = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
44                 IidFactory.l2FloodDomainIid(tenantId, l2FdId), rTx);
45         if (!potentialL2Fd.isPresent()) {
46             return new ForwardingCtx(null, null, null);
47         }
48         L2BridgeDomainId l2BdId = potentialL2Fd.get().getParent();
49         if (l2BdId == null) {
50             return new ForwardingCtx(potentialL2Fd.get(), null, null);
51         }
52         Optional<L2BridgeDomain> potentialL2Bd = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
53                 IidFactory.l2BridgeDomainIid(tenantId, l2BdId), rTx);
54         if (!potentialL2Bd.isPresent()) {
55             return new ForwardingCtx(potentialL2Fd.get(), null, null);
56         }
57         L3ContextId l3ContextId = potentialL2Bd.get().getParent();
58         if (l3ContextId == null) {
59             return new ForwardingCtx(potentialL2Fd.get(), potentialL2Bd.get(), null);
60         }
61         Optional<L3Context> potentialL3Context = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
62                 IidFactory.l3ContextIid(tenantId, l3ContextId), rTx);
63         if (!potentialL3Context.isPresent()) {
64             return new ForwardingCtx(potentialL2Fd.get(), potentialL2Bd.get(), null);
65         }
66         return new ForwardingCtx(potentialL2Fd.get(), potentialL2Bd.get(), potentialL3Context.get());
67     }
68
69     public static final class ForwardingCtx {
70
71         private final L2FloodDomain l2FloodDomain;
72         private final L2BridgeDomain l2BridgeDomain;
73         private final L3Context l3Context;
74
75         private ForwardingCtx(L2FloodDomain l2Fd, L2BridgeDomain l2Bd, L3Context l3Context) {
76             this.l2FloodDomain = l2Fd;
77             this.l2BridgeDomain = l2Bd;
78             this.l3Context = l3Context;
79         }
80
81         public L2FloodDomain getL2FloodDomain() {
82             return l2FloodDomain;
83         }
84
85         public L2BridgeDomain getL2BridgeDomain() {
86             return l2BridgeDomain;
87         }
88
89         public L3Context getL3Context() {
90             return l3Context;
91         }
92
93     }
94
95     public static Neutron_IPs getFirstIp(List<Neutron_IPs> fixedIPs) {
96         if (fixedIPs == null || fixedIPs.isEmpty()) {
97             return null;
98         }
99         Neutron_IPs neutron_Ip = fixedIPs.get(0);
100         if (fixedIPs.size() > 1) {
101             NeutronPortAware.LOG.warn("Neutron mapper does not support multiple IPs on the same port. Only first IP is selected {}",
102                     neutron_Ip);
103         }
104         return neutron_Ip;
105     }
106 }