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
8 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow;
10 import java.util.List;
12 import java.util.TreeSet;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ConcurrentMap;
15 import java.util.concurrent.atomic.AtomicInteger;
17 import org.opendaylight.groupbasedpolicy.endpoint.EpKey;
18 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
19 import org.opendaylight.groupbasedpolicy.resolver.ConditionGroup;
20 import org.opendaylight.groupbasedpolicy.resolver.EgKey;
21 import org.opendaylight.groupbasedpolicy.resolver.IndexedTenant;
22 import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.NetworkDomainId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.UniqueId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.EndpointGroup;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L2BridgeDomain;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L2FloodDomain;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L3Context;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class OrdinalFactory {
39 private final static Logger LOG = LoggerFactory.getLogger(OrdinalFactory.class);
42 * Counter used to allocate ordinal values for forwarding contexts and VNIDs
44 private final static AtomicInteger policyOrdinal = new AtomicInteger(1);
46 private final static ConcurrentMap<String, Integer> ordinals =
47 new ConcurrentHashMap<>();
48 // XXX - need to garbage collect
49 private final static ConcurrentMap<ConditionGroup, Integer> cgOrdinals =
50 new ConcurrentHashMap<>();
52 public static int getContextOrdinal(final TenantId tenantId,
53 final UniqueId id) throws Exception {
54 if (tenantId == null || id == null)
56 return getContextOrdinalFromString(tenantId.getValue() + "|" + id.getValue());
60 * Get a unique ordinal for the given condition group, suitable for use in
61 * the data plane. This is unique only for this node, and not globally.
64 * the {@link ConditionGroup}
65 * @return the unique ID
67 public static int getCondGroupOrdinal(final ConditionGroup cg) {
70 Integer ord = cgOrdinals.get(cg);
72 ord = policyOrdinal.getAndIncrement();
73 Integer old = cgOrdinals.putIfAbsent(cg, ord);
77 return ord.intValue();
81 * Get a 32-bit context ordinal suitable for use in the OF data plane for
82 * the given policy item.
85 * the tenant ID of the element
87 * the unique ID for the element
88 * @return the 32-bit ordinal value
92 public static int getContextOrdinal(NodeId destNode) throws Exception {
93 return getContextOrdinalFromString(destNode.getValue());
96 public static int getContextOrdinal(Endpoint ep, NetworkDomainId networkContainment) throws Exception {
97 // TODO: Define private static final Comparator<EndpointGroupId>
98 // COMPARATOR = new Comparator<EndpointGroupId>() { ... }
99 // pass to constructor: new TreeSet<>(COMPARATOR);
101 Set<String> epgs = new TreeSet<>();
103 // Get EPGs and add to ordered Set
104 if (ep.getEndpointGroup() != null) {
105 epgs.add(ep.getEndpointGroup().getValue());
107 if (ep.getEndpointGroups() != null) {
108 for (EndpointGroupId epgId : ep.getEndpointGroups()) {
109 epgs.add(epgId.getValue());
113 StringBuilder key = new StringBuilder(ep.getTenant().getValue());
115 for (String epg : epgs) {
121 .append(networkContainment);
123 return getContextOrdinalFromString(key.toString());
128 * Get a 32-bit context ordinal suitable for use in the OF data plane for
129 * the given policy item.
132 * the unique ID for the element
133 * @return the 32-bit ordinal value
135 private static int getContextOrdinalFromString(final String id) throws Exception {
137 Integer ord = ordinals.get(id);
139 ord = policyOrdinal.getAndIncrement();
140 Integer old = ordinals.putIfAbsent(id, ord);
144 return ord.intValue();
147 public static final EndpointFwdCtxOrdinals getEndpointFwdCtxOrdinals(OfContext ctx, PolicyInfo policyInfo,
148 Endpoint ep) throws Exception {
149 return new EndpointFwdCtxOrdinals(ep, policyInfo, ctx);
152 // TODO alagalah Li: Move to either OrdinalFactory or EndpointManager
153 public static class EndpointFwdCtxOrdinals {
155 private NetworkDomainId networkContainment;
157 private int epgId = 0, bdId = 0, fdId = 0, l3Id = 0, cgId = 0;
159 private EndpointFwdCtxOrdinals(Endpoint ep, PolicyInfo policyInfo, OfContext ctx) throws Exception {
160 this.ep = new EpKey(ep.getL2Context(), ep.getMacAddress());
162 IndexedTenant tenant = ctx.getPolicyResolver().getTenant(ep.getTenant());
163 // Set network containment either from ep, or from primary EPG
164 if (ep.getNetworkContainment() != null) {
165 this.networkContainment = ep.getNetworkContainment();
167 EndpointGroup epg = tenant.getEndpointGroup(ep.getEndpointGroup());
168 if (epg.getNetworkDomain() != null) {
169 this.networkContainment = epg.getNetworkDomain();
172 "endPoint ordinals for {} not processed in SourceMapper. Must be able to resolve network containment either directly, or from primary EPG",
178 // TODO: alagalah: I have a draft to address structure of mEPG
180 // out of scope until broader bugs with conditions are fixed.
181 List<ConditionName> conds = ctx.getEndpointManager().getCondsForEndpoint(ep);
183 policyInfo.getEgCondGroup(new EgKey(ep.getTenant(), ep.getEndpointGroup()), conds);
184 this.cgId = getCondGroupOrdinal(cg);
186 // Based on network containment, determine components of
187 // forwarding context
188 L3Context l3c = tenant.resolveL3Context(networkContainment);
189 L2BridgeDomain bd = tenant.resolveL2BridgeDomain(networkContainment);
190 L2FloodDomain fd = tenant.resolveL2FloodDomain(networkContainment);
192 // Set ordinal id's for use in flows for each forwarding context
195 this.epgId = getContextOrdinal(ep, networkContainment);
197 this.bdId = getContextOrdinal(ep.getTenant(),
200 this.fdId = getContextOrdinal(ep.getTenant(),
203 this.l3Id = getContextOrdinal(ep.getTenant(),
208 public int getEpgId() {
212 public NetworkDomainId getNetworkContainment() {
213 return this.networkContainment;
216 public EpKey getEp() {
220 public int getBdId() {
224 public int getFdId() {
228 public int getL3Id() {
232 public int getCgId() {