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.neutron.mapper.mapping.NeutronPortAware;
16 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf.AllowAction;
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.policy.rev140421.has.action.refs.ActionRef;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.action.refs.ActionRefBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L2BridgeDomain;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L2FloodDomain;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L3Context;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.subject.feature.instances.ActionInstance;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.subject.feature.instances.ActionInstanceBuilder;
34 import com.google.common.base.Optional;
35 import com.google.common.collect.ImmutableList;
37 public final class MappingUtils {
39 public static final String NEUTRON_ROUTER = "neutron_router-";
40 public static final String NEUTRON_EXTERNAL = "neutron_external_network-";
41 public static final String NEUTRON_GROUP = "neutron_group-";
42 public static final ActionInstance ACTION_ALLOW = new ActionInstanceBuilder().setName(
43 new ActionName("Allow"))
44 .setActionDefinitionId(AllowAction.DEFINITION.getId())
46 public static final List<ActionRef> ACTION_REF_ALLOW =
47 ImmutableList.of(new ActionRefBuilder().setName(ACTION_ALLOW.getName()).setOrder(0).build());
48 public static final EndpointGroupId EPG_EXTERNAL_ID = new EndpointGroupId("eeeaa3a2-e9ba-44e0-a462-bea923d30e38");
50 public static final String NAME_VALUE_DELIMETER = "-";
51 public static final String NAME_DELIMETER = "_";
52 public static final String NAME_DOUBLE_DELIMETER = "__";
54 private MappingUtils() {
55 throw new UnsupportedOperationException("Cannot create an instance.");
58 public static ForwardingCtx createForwardingContext(TenantId tenantId, L2FloodDomainId l2FdId, ReadTransaction rTx) {
59 Optional<L2FloodDomain> potentialL2Fd = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
60 IidFactory.l2FloodDomainIid(tenantId, l2FdId), rTx);
61 if (!potentialL2Fd.isPresent()) {
62 return new ForwardingCtx(null, null, null);
64 L2BridgeDomainId l2BdId = potentialL2Fd.get().getParent();
66 return new ForwardingCtx(potentialL2Fd.get(), null, null);
68 Optional<L2BridgeDomain> potentialL2Bd = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
69 IidFactory.l2BridgeDomainIid(tenantId, l2BdId), rTx);
70 if (!potentialL2Bd.isPresent()) {
71 return new ForwardingCtx(potentialL2Fd.get(), null, null);
73 L3ContextId l3ContextId = potentialL2Bd.get().getParent();
74 if (l3ContextId == null) {
75 return new ForwardingCtx(potentialL2Fd.get(), potentialL2Bd.get(), null);
77 Optional<L3Context> potentialL3Context = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
78 IidFactory.l3ContextIid(tenantId, l3ContextId), rTx);
79 if (!potentialL3Context.isPresent()) {
80 return new ForwardingCtx(potentialL2Fd.get(), potentialL2Bd.get(), null);
82 return new ForwardingCtx(potentialL2Fd.get(), potentialL2Bd.get(), potentialL3Context.get());
85 public static final class ForwardingCtx {
87 private final L2FloodDomain l2FloodDomain;
88 private final L2BridgeDomain l2BridgeDomain;
89 private final L3Context l3Context;
91 private ForwardingCtx(L2FloodDomain l2Fd, L2BridgeDomain l2Bd, L3Context l3Context) {
92 this.l2FloodDomain = l2Fd;
93 this.l2BridgeDomain = l2Bd;
94 this.l3Context = l3Context;
97 public L2FloodDomain getL2FloodDomain() {
101 public L2BridgeDomain getL2BridgeDomain() {
102 return l2BridgeDomain;
105 public L3Context getL3Context() {
111 public static Neutron_IPs getFirstIp(List<Neutron_IPs> fixedIPs) {
112 if (fixedIPs == null || fixedIPs.isEmpty()) {
115 Neutron_IPs neutron_Ip = fixedIPs.get(0);
116 if (fixedIPs.size() > 1) {
117 NeutronPortAware.LOG.warn("Neutron mapper does not support multiple IPs on the same port. Only first IP is selected {}",