Bug 5409 - setting orders in single rule contract
[groupbasedpolicy.git] / neutron-mapper / src / main / java / org / opendaylight / groupbasedpolicy / neutron / mapper / util / MappingUtils.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.groupbasedpolicy.neutron.mapper.util;
10
11 import java.util.List;
12
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;
37
38 import com.google.common.base.Optional;
39
40 public final class MappingUtils {
41
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())
48         .build();
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");
54
55     public static final String NAME_VALUE_DELIMETER = "-";
56     public static final String NAME_DELIMETER = "_";
57     public static final String NAME_DOUBLE_DELIMETER = "__";
58
59     private MappingUtils() {
60         throw new UnsupportedOperationException("Cannot create an instance.");
61     }
62
63     public static ActionRef createSfcActionRef(String sfcChainName) {
64         return new ActionRefBuilder().setName(new ActionName(sfcChainName)).setOrder(0).build();
65     }
66
67     public static ActionChoice createSfcActionChoice(String chainName) {
68         return new SfcActionCaseBuilder().setSfcChainName(chainName).build();
69     }
70
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);
76         }
77         L2BridgeDomainId l2BdId = potentialL2Fd.get().getParent();
78         if (l2BdId == null) {
79             return new ForwardingCtx(potentialL2Fd.get(), null, null);
80         }
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);
85         }
86         L3ContextId l3ContextId = potentialL2Bd.get().getParent();
87         if (l3ContextId == null) {
88             return new ForwardingCtx(potentialL2Fd.get(), potentialL2Bd.get(), null);
89         }
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);
94         }
95         return new ForwardingCtx(potentialL2Fd.get(), potentialL2Bd.get(), potentialL3Context.get());
96     }
97
98     public static final class ForwardingCtx {
99
100         private final L2FloodDomain l2FloodDomain;
101         private final L2BridgeDomain l2BridgeDomain;
102         private final L3Context l3Context;
103
104         private ForwardingCtx(L2FloodDomain l2Fd, L2BridgeDomain l2Bd, L3Context l3Context) {
105             this.l2FloodDomain = l2Fd;
106             this.l2BridgeDomain = l2Bd;
107             this.l3Context = l3Context;
108         }
109
110         public L2FloodDomain getL2FloodDomain() {
111             return l2FloodDomain;
112         }
113
114         public L2BridgeDomain getL2BridgeDomain() {
115             return l2BridgeDomain;
116         }
117
118         public L3Context getL3Context() {
119             return l3Context;
120         }
121
122     }
123
124     public static Neutron_IPs getFirstIp(List<Neutron_IPs> fixedIPs) {
125         if (fixedIPs == null || fixedIPs.isEmpty()) {
126             return null;
127         }
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 {}",
131                     neutron_Ip);
132         }
133         return neutron_Ip;
134     }
135 }