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.renderer.ofoverlay.mapper.destination;
12 import com.google.common.base.Preconditions;
13 import org.opendaylight.groupbasedpolicy.dto.EpKey;
14 import org.opendaylight.groupbasedpolicy.dto.IndexedTenant;
15 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
16 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.NetworkDomainId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.l3.prefix.fields.EndpointL3Gateways;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Prefix;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.ForwardingContext;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L3Context;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.Subnet;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.HashSet;
36 import java.util.List;
39 class DestinationMapperUtils {
41 private static final Logger LOG = LoggerFactory.getLogger(DestinationMapperUtils.class);
43 private final OfContext ctx;
45 DestinationMapperUtils(OfContext ctx) {
46 this.ctx = Preconditions.checkNotNull(ctx);
49 HashSet<Subnet> getSubnets(TenantId tenantId) {
50 IndexedTenant indexedTenant = ctx.getTenant(tenantId);
51 if (indexedTenant != null && indexedTenant.getTenant() != null) {
52 ForwardingContext forwardingContext = indexedTenant.getTenant().getForwardingContext();
53 if (forwardingContext != null && forwardingContext.getSubnet() != null) {
54 return new HashSet<>(forwardingContext.getSubnet());
58 return new HashSet<>();
61 L3Context getL3ContextForSubnet(IndexedTenant indexedTenant, Subnet subnet) {
62 if (indexedTenant == null) {
65 return indexedTenant.resolveL3Context(subnet.getId());
68 NetworkDomainId getEPNetworkContainment(Endpoint endpoint, IndexedTenant tenant) {
69 if (endpoint.getNetworkContainment() != null) {
70 return endpoint.getNetworkContainment();
71 } else if (tenant != null) {
72 return tenant.getEndpointGroup(endpoint.getEndpointGroup())
79 // Need a method to get subnets for EPs attached to the node locally
80 // to set the source Mac address for the router interface.
81 List<Subnet> getLocalSubnets(NodeId nodeId) {
82 Collection<Endpoint> endpointsForNode = ctx.getEndpointManager().getEndpointsForNode(nodeId);
84 List<Subnet> localSubnets = new ArrayList<>();
86 for (Endpoint endpoint : endpointsForNode) {
87 HashSet<Subnet> subnets = getSubnets(endpoint.getTenant());
88 if (subnets.isEmpty()) {
89 LOG.debug("No local subnets in tenant {} for EP {}.", endpoint.getTenant(), endpoint.getKey());
92 NetworkDomainId epNetworkContainment = getEPNetworkContainment(endpoint, ctx.getTenant(endpoint.getTenant()));
93 for (Subnet subnet : subnets) {
94 if (epNetworkContainment.getValue().equals(subnet.getId().getValue())) {
95 localSubnets.add(subnet);
102 Endpoint getL2EpOfSubnetGateway(TenantId tenantId, Subnet subnet) {
103 if (subnet != null && subnet.getVirtualRouterIp() != null) {
104 IpAddress gwIpAddress = subnet.getVirtualRouterIp();
105 Collection<EndpointL3Prefix> prefixEps = ctx.getEndpointManager().getEndpointsL3PrefixForTenant(tenantId);
106 if (prefixEps != null) {
107 for (EndpointL3Prefix prefixEp : prefixEps) {
108 for (EndpointL3Gateways gw : prefixEp.getEndpointL3Gateways()) {
109 EndpointL3 l3Ep = ctx.getEndpointManager().getL3Endpoint(gw.getL3Context(), gwIpAddress,
110 prefixEp.getTenant());
111 if (l3Ep != null && l3Ep.getL2Context() != null && l3Ep.getMacAddress() != null) {
112 return ctx.getEndpointManager().getEndpoint(
113 new EpKey(l3Ep.getL2Context(), l3Ep.getMacAddress()));
122 MacAddress routerPortMac(L3Context l3c, IpAddress ipAddress, TenantId tenantId) {
123 MacAddress defaultMacAddress = DestinationMapper.ROUTER_MAC;
124 if (l3c.getId() != null) {
125 EndpointL3 endpointL3 = ctx.getEndpointManager().getL3Endpoint(l3c.getId(), ipAddress, tenantId);
126 if (endpointL3 == null || endpointL3.getMacAddress() == null) {
127 return defaultMacAddress;
129 return endpointL3.getMacAddress();
132 return defaultMacAddress;
136 IndexedTenant getIndexedTenant(TenantId tenantId) {
137 return ctx.getTenant(tenantId);
140 Set<EndpointGroupId> getAllEndpointGroups(Endpoint endpoint) {
141 Set<EndpointGroupId> groupIds = new HashSet<>();
142 if (endpoint.getEndpointGroup() != null) {
143 groupIds.add(endpoint.getEndpointGroup());
145 if (endpoint.getEndpointGroups() != null) {
146 groupIds.addAll(endpoint.getEndpointGroups());
151 OrdinalFactory.EndpointFwdCtxOrdinals getEndpointOrdinals(Endpoint endpoint) {
153 return OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, endpoint);
154 } catch (Exception e) {
155 LOG.error("Failed to get fwd ctx ordinals for endpoint {}", endpoint);