Neutron-mapper uses only DTOs from neutron.yang
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / mapper / destination / DestinationMapperUtils.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.renderer.ofoverlay.mapper.destination;
10
11
12 import com.google.common.base.Preconditions;
13
14 import org.opendaylight.groupbasedpolicy.dto.EpKey;
15 import org.opendaylight.groupbasedpolicy.dto.IndexedTenant;
16 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
17 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2FloodDomainId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.NetworkDomainId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.l3.prefix.fields.EndpointL3Gateways;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Prefix;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.ForwardingContext;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L3Context;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.Subnet;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.HashSet;
39 import java.util.List;
40 import java.util.Set;
41
42 class DestinationMapperUtils {
43
44     private static final Logger LOG = LoggerFactory.getLogger(DestinationMapperUtils.class);
45
46     private final OfContext ctx;
47
48     DestinationMapperUtils(OfContext ctx) {
49         this.ctx = Preconditions.checkNotNull(ctx);
50     }
51
52     HashSet<Subnet> getSubnets(TenantId tenantId) {
53         IndexedTenant indexedTenant = ctx.getTenant(tenantId);
54         if (indexedTenant != null && indexedTenant.getTenant() != null) {
55             ForwardingContext forwardingContext = indexedTenant.getTenant().getForwardingContext();
56             if (forwardingContext != null && forwardingContext.getSubnet() != null) {
57                 return new HashSet<>(forwardingContext.getSubnet());
58             }
59         }
60
61         return new HashSet<>();
62     }
63
64     L3Context getL3ContextForSubnet(IndexedTenant indexedTenant, Subnet subnet) {
65         if (indexedTenant == null || subnet.getParent() == null) {
66             return null;
67         }
68         return indexedTenant.resolveL3Context(new L2FloodDomainId(subnet.getParent().getValue()));
69     }
70
71     NetworkDomainId getEPNetworkContainment(Endpoint endpoint, IndexedTenant tenant) {
72         if (endpoint.getNetworkContainment() != null) {
73             return endpoint.getNetworkContainment();
74         } else if (tenant != null) {
75             return tenant.getEndpointGroup(endpoint.getEndpointGroup())
76                     .getNetworkDomain();
77         } else {
78             return null;
79         }
80     }
81
82     // Need a method to get subnets for EPs attached to the node locally
83     // to set the source Mac address for the router interface.
84     List<Subnet> getLocalSubnets(NodeId nodeId) {
85         Collection<Endpoint> endpointsForNode = ctx.getEndpointManager().getEndpointsForNode(nodeId);
86
87         List<Subnet> localSubnets = new ArrayList<>();
88
89         for (Endpoint endpoint : endpointsForNode) {
90             HashSet<Subnet> subnets = getSubnets(endpoint.getTenant());
91             if (subnets.isEmpty()) {
92                 LOG.debug("No local subnets in tenant {} for EP {}.", endpoint.getTenant(), endpoint.getKey());
93                 continue;
94             }
95             NetworkDomainId epNetworkContainment = getEPNetworkContainment(endpoint, ctx.getTenant(endpoint.getTenant()));
96             for (Subnet subnet : subnets) {
97                 if (epNetworkContainment.getValue().equals(subnet.getId().getValue())) {
98                     localSubnets.add(subnet);
99                 }
100             }
101         }
102         return localSubnets;
103     }
104
105     Endpoint getL2EpOfSubnetGateway(TenantId tenantId, Subnet subnet) {
106         if (subnet != null && subnet.getVirtualRouterIp() != null) {
107             IpAddress gwIpAddress = subnet.getVirtualRouterIp();
108             Collection<EndpointL3Prefix> prefixEps = ctx.getEndpointManager().getEndpointsL3PrefixForTenant(tenantId);
109             if (prefixEps != null) {
110                 for (EndpointL3Prefix prefixEp : prefixEps) {
111                     for (EndpointL3Gateways gw : prefixEp.getEndpointL3Gateways()) {
112                         EndpointL3 l3Ep = ctx.getEndpointManager().getL3Endpoint(gw.getL3Context(), gwIpAddress,
113                                 prefixEp.getTenant());
114                         if (l3Ep != null && l3Ep.getL2Context() != null && l3Ep.getMacAddress() != null) {
115                             return ctx.getEndpointManager().getEndpoint(
116                                     new EpKey(l3Ep.getL2Context(), l3Ep.getMacAddress()));
117                         }
118                     }
119                 }
120             }
121         }
122         return null;
123     }
124
125     MacAddress routerPortMac(L3Context l3c, IpAddress ipAddress, TenantId tenantId) {
126         MacAddress defaultMacAddress = DestinationMapper.ROUTER_MAC;
127         if (l3c.getId() != null) {
128             EndpointL3 endpointL3 = ctx.getEndpointManager().getL3Endpoint(l3c.getId(), ipAddress, tenantId);
129             if (endpointL3 == null || endpointL3.getMacAddress() == null) {
130                 return defaultMacAddress;
131             } else {
132                 return endpointL3.getMacAddress();
133             }
134         } else {
135             return defaultMacAddress;
136         }
137     }
138
139     IndexedTenant getIndexedTenant(TenantId tenantId) {
140         return ctx.getTenant(tenantId);
141     }
142
143     Set<EndpointGroupId> getAllEndpointGroups(Endpoint endpoint) {
144         Set<EndpointGroupId> groupIds = new HashSet<>();
145         if (endpoint.getEndpointGroup() != null) {
146             groupIds.add(endpoint.getEndpointGroup());
147         }
148         if (endpoint.getEndpointGroups() != null) {
149             groupIds.addAll(endpoint.getEndpointGroups());
150         }
151         return groupIds;
152     }
153
154     OrdinalFactory.EndpointFwdCtxOrdinals getEndpointOrdinals(Endpoint endpoint) {
155         try {
156             return OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, endpoint);
157         } catch (Exception e) {
158             LOG.error("Failed to get fwd ctx ordinals for endpoint {}", endpoint);
159             return null;
160         }
161     }
162
163 }