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.endpoint;
11 import static org.opendaylight.groupbasedpolicy.util.DataStoreHelper.readFromDs;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.List;
21 import java.util.concurrent.ConcurrentHashMap;
22 import java.util.concurrent.ConcurrentMap;
23 import java.util.concurrent.CopyOnWriteArrayList;
24 import java.util.concurrent.ScheduledExecutorService;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
28 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
29 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
32 import org.opendaylight.groupbasedpolicy.dto.EgKey;
33 import org.opendaylight.groupbasedpolicy.dto.EpKey;
34 import org.opendaylight.groupbasedpolicy.dto.IndexedTenant;
35 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.EndpointListener;
36 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.arp.ArpTasker;
37 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.node.SwitchManager;
38 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
39 import org.opendaylight.groupbasedpolicy.util.IidFactory;
40 import org.opendaylight.groupbasedpolicy.util.SetUtils;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.Endpoints;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3Address;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3AddressBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3AddressKey;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointKey;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Prefix;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.l3endpoint.rev151217.NatAddress;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.EndpointLocation;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.EndpointLocation.LocationType;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayConfig;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContextBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayL3Context;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.Tenant;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2BridgeDomain;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L3Context;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
64 import org.opendaylight.yangtools.concepts.ListenerRegistration;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
68 import com.google.common.base.Function;
69 import com.google.common.base.Optional;
70 import com.google.common.base.Preconditions;
71 import com.google.common.base.Predicate;
72 import com.google.common.collect.Collections2;
73 import com.google.common.collect.ImmutableList;
74 import com.google.common.collect.ImmutableSet;
75 import com.google.common.collect.Sets;
78 * Keep track of endpoints on the system. Maintain an index of endpoints and
79 * their locations for rendering. The endpoint manager will maintain
80 * appropriate indexes only for switches that are attached to the current
82 * In order to render the policy, we need to be able to efficiently enumerate
83 * all endpoints on a particular switch and also all the switches containing
84 * each particular endpoint group
86 public class EndpointManager implements AutoCloseable {
88 private static final Logger LOG = LoggerFactory.getLogger(EndpointManager.class);
89 private final EndpointManagerListener endpointListener;
90 private final ConcurrentHashMap<EpKey, Endpoint> endpoints = new ConcurrentHashMap<>();
91 private final ConcurrentHashMap<EpKey, Endpoint> externalEndpointsWithoutLocation = new ConcurrentHashMap<>();
92 private final ConcurrentHashMap<NodeId, ConcurrentMap<EgKey, Set<EpKey>>> endpointsByGroupByNode =
93 new ConcurrentHashMap<>();
94 private final ConcurrentHashMap<NodeId, Set<EpKey>> endpointsByNode = new ConcurrentHashMap<>();
95 private final ConcurrentHashMap<EgKey, Set<EpKey>> endpointsByGroup = new ConcurrentHashMap<>();
96 private final ScheduledExecutorService executor;
97 private final DataBroker dataProvider;
98 private final ArpTasker arpTasker;
99 private final ListenerRegistration<ArpTasker> notificationListenerRegistration;
100 private List<EndpointListener> listeners = new CopyOnWriteArrayList<>();
101 private Function<EpKey, Endpoint> indexTransform = new Function<EpKey, Endpoint>() {
104 public Endpoint apply(EpKey input) {
105 return endpoints.get(input);
109 public EndpointManager(DataBroker dataProvider, RpcProviderRegistry rpcRegistry,
110 NotificationService notificationService, ScheduledExecutorService executor, SwitchManager switchManager) {
111 this.executor = executor;
112 this.dataProvider = dataProvider;
113 if (rpcRegistry != null) {
114 if (notificationService != null && dataProvider != null) {
115 this.arpTasker = new ArpTasker(rpcRegistry, dataProvider);
116 notificationListenerRegistration = notificationService.registerNotificationListener(arpTasker);
118 LOG.info("Missing service {}", NotificationService.class.getSimpleName());
119 this.arpTasker = null;
120 this.notificationListenerRegistration = null;
123 LOG.warn("Missing service {}", RpcProviderRegistry.class.getSimpleName());
124 this.arpTasker = null;
125 this.notificationListenerRegistration = null;
127 if (dataProvider != null) {
128 endpointListener = new EndpointManagerListener(this.dataProvider, this);
130 endpointListener = null;
132 LOG.debug("Initialized OFOverlay endpoint manager");
136 * Add a {@link EndpointListener} to get notifications of switch events
138 * @param listener - the {@link EndpointListener} to add
140 public void registerListener(EndpointListener listener) {
141 listeners.add(listener);
145 * Get a collection of endpoints attached to a particular switch
147 * @param nodeId - the nodeId of the switch to get endpoints for
148 * @return a collection of {@link Endpoint} objects.
150 public synchronized Set<EgKey> getGroupsForNode(NodeId nodeId) {
151 Map<EgKey, Set<EpKey>> nodeEps = endpointsByGroupByNode.get(nodeId);
153 return Collections.emptySet();
154 return ImmutableSet.copyOf(nodeEps.keySet());
158 * Get the set of nodes
160 * @param egKey - the egKey of the endpoint group to get nodes for
161 * @return a collection of {@link NodeId} objects.
163 public synchronized Set<NodeId> getNodesForGroup(final EgKey egKey) {
164 return ImmutableSet.copyOf(Sets.filter(endpointsByGroupByNode.keySet(), new Predicate<NodeId>() {
167 public boolean apply(NodeId input) {
168 Map<EgKey, Set<EpKey>> nodeEps = endpointsByGroupByNode.get(input);
169 return (nodeEps != null && nodeEps.containsKey(egKey));
176 * Get the endpoints in a particular group on a particular node
178 * @param nodeId - the node ID to look up
179 * @param eg - the group to look up
180 * @return the endpoints
182 public synchronized Collection<Endpoint> getEndpointsForNode(NodeId nodeId, EgKey eg) {
183 // TODO: alagalah Create method findEndpointsByNode() that uses
186 Map<EgKey, Set<EpKey>> nodeEps = endpointsByGroupByNode.get(nodeId);
188 return Collections.emptyList();
189 Collection<EpKey> ebn = nodeEps.get(eg);
191 return Collections.emptyList();
192 return ImmutableList.copyOf(Collections2.transform(ebn, indexTransform));
196 * Get the endpoints on a particular node
198 * @param nodeId - the node ID to look up
199 * @return the endpoints
201 public synchronized Collection<Endpoint> getEndpointsForNode(final NodeId nodeId) {
202 // TODO: alagalah Create method findEndpointsByNode() that uses
203 // data store. See commented code below.
205 Collection<EpKey> ebn = endpointsByNode.get(nodeId);
207 return Collections.emptyList();
208 return ImmutableList.copyOf(Collections2.transform(ebn, indexTransform));
212 * Get the endpoint object for the given key
214 * @param epKey - the key
215 * @return the {@link Endpoint} corresponding to the key
217 public Endpoint getEndpoint(EpKey epKey) {
218 return endpoints.get(epKey);
222 * Get a collection of endpoints in a particular endpoint group
224 * @param eg - Endpoint group key (contains endpoint group and tenant ID)
225 * @return a collection of {@link Endpoint} objects.
227 public synchronized Collection<Endpoint> getEndpointsForGroup(EgKey eg) {
228 Collection<EpKey> ebg = endpointsByGroup.get(eg);
230 return Collections.emptyList();
231 return ImmutableList.copyOf(Collections2.transform(ebg, indexTransform));
235 * Return set of external endpoints without location belonging to a particular endpoint group
237 * @param eg - Endpoint group key (contains endpoint group and tenant ID)
238 * @return a collection of {@link Endpoint} objects.
240 public synchronized Collection<Endpoint> getExtEpsNoLocForGroup(final EgKey eg) {
243 .copyOf(Collections2.filter(externalEndpointsWithoutLocation.values(), new Predicate<Endpoint>() {
246 public boolean apply(Endpoint input) {
247 Set<EndpointGroupId> epgIds = new HashSet<>();
248 if (input.getEndpointGroup() != null) {
249 epgIds.add(input.getEndpointGroup());
251 if (input.getEndpointGroups() != null) {
252 epgIds.addAll(input.getEndpointGroups());
254 if (epgIds.isEmpty()) {
255 LOG.error("No EPGs for {}. This is not a valid Endpoint.", input.getKey());
258 return (epgIds.contains(eg.getEgId()));
265 * Character of input parameters will determine action - create, update or delete L3Endpoint
267 * @param oldL3Ep the old L3 endpoint
268 * @param newL3Ep the new L3 endpoint
270 protected synchronized void processL3Endpoint(EndpointL3 oldL3Ep, EndpointL3 newL3Ep) {
272 // create L3 endpoint
273 if (oldL3Ep == null && newL3Ep != null) {
274 createL3Endpoint(newL3Ep);
277 // update L3 endpoint
278 if (oldL3Ep != null && newL3Ep != null) {
279 updateL3Endpoint(newL3Ep);
282 // remove L3 endpoint
283 if (oldL3Ep != null && newL3Ep == null) {
284 removeL3Endpoint(oldL3Ep);
289 * Character of input parameters will determine action - create, update or delete Endpoint
291 * @param oldEp - oldEp the new endpoint
292 * @param newEp - newEp the new endpoint
294 protected synchronized void processEndpoint(Endpoint oldEp, Endpoint newEp) {
295 NodeId oldLoc = getLocation(oldEp);
296 NodeId newLoc = getLocation(newEp);
297 EpKey oldEpKey = getEpKey(oldEp);
298 EpKey newEpKey = getEpKey(newEp);
299 TenantId tenantId = (newEp == null) ? null : newEp.getTenant();
301 Set<EndpointGroupId> oldEpgIds = getEndpointGroupsFromEndpoint(oldEp);
302 Set<EndpointGroupId> newEpgIds = getEndpointGroupsFromEndpoint(newEp);
304 boolean notifyOldLoc = false;
305 boolean notifyNewLoc = false;
306 boolean notifyOldEg = false;
307 boolean notifyNewEg = false;
310 if (oldEp == null && newEp != null) {
311 if (newLoc != null) {
312 createEndpoint(newLoc, newEpKey, newEpgIds, tenantId);
313 endpoints.put(newEpKey, newEp);
314 notifyEndpointUpdated(newEpKey);
318 externalEndpointsWithoutLocation.put(newEpKey, newEp);
323 else if (oldEp != null && newEp != null && oldEpKey != null && newEpKey != null) {
324 // endpoint is not external anymore
325 if (newLoc != null && oldLoc == null) {
326 createEndpoint(newLoc, newEpKey, newEpgIds, tenantId);
327 externalEndpointsWithoutLocation.remove(oldEpKey);
328 endpoints.put(newEpKey, newEp);
329 notifyEndpointUpdated(newEpKey);
333 // endpoint changed to external
334 else if (newLoc == null && oldLoc != null) {
335 removeEndpoint(oldEp, oldLoc, oldEpKey, oldEpgIds);
336 externalEndpointsWithoutLocation.put(newEpKey, newEp);
337 endpoints.remove(oldEpKey);
338 notifyEndpointUpdated(oldEpKey);
341 // endpoint might have changed location, EPGs or it's properties
342 } else if (newLoc != null && oldLoc != null) {
343 // endpoit changed location
344 if (!(oldLoc.getValue().equals(newLoc.getValue()))) {
348 // endpoint changed EPGs
349 if (!oldEpgIds.equals(newEpgIds)) {
353 removeEndpoint(oldEp, oldLoc, oldEpKey, oldEpgIds);
354 createEndpoint(newLoc, newEpKey, newEpgIds, tenantId);
355 notifyEndpointUpdated(newEpKey);
360 else if (oldEp != null && newEp == null) {
361 if (oldLoc != null) {
362 removeEndpoint(oldEp, oldLoc, oldEpKey, oldEpgIds);
363 endpoints.remove(oldEpKey);
364 notifyEndpointUpdated(oldEpKey);
368 externalEndpointsWithoutLocation.remove(oldEpKey);
374 notifyNodeEndpointUpdated(oldLoc, oldEpKey);
376 notifyNodeEndpointUpdated(newLoc, newEpKey);
378 for (EndpointGroupId oldEpgId : oldEpgIds) {
379 EgKey oldEgKey = new EgKey(oldEp.getTenant(), oldEpgId);
380 notifyGroupEndpointUpdated(oldEgKey, oldEpKey);
383 for (EndpointGroupId newEpgId : newEpgIds) {
384 EgKey newEgKey = new EgKey(newEp.getTenant(), newEpgId);
385 notifyGroupEndpointUpdated(newEgKey, newEpKey);
389 private void createEndpoint(NodeId newLoc, EpKey newEpKey, Set<EndpointGroupId> newEpgIds, TenantId tenantId) {
390 // Update endpointsByNode
391 if (endpointsByNode.get(newLoc) == null) {
392 Set<EpKey> epsNode = new HashSet<>();
393 epsNode.add(newEpKey);
394 endpointsByNode.put(newLoc, epsNode);
395 SwitchManager.activatingSwitch(newLoc);
397 Set<EpKey> epsNode = endpointsByNode.get(newLoc);
398 epsNode.add(newEpKey);
401 // Update endpointsByGroupByNode and endpointsByGroup
402 for (EndpointGroupId newEpgId : newEpgIds) {
403 // endpointsByGroupByNode
404 EgKey newEgKey = new EgKey(tenantId, newEpgId);
405 Set<EpKey> eps = getEpNGSet(newLoc, newEgKey);
408 Set<EpKey> geps = endpointsByGroup.get(newEgKey);
410 geps = new HashSet<>();
413 endpointsByGroup.put(newEgKey, geps);
414 LOG.debug("Endpoint {} added to node {}", newEpKey, newLoc);
419 private void removeEndpoint(Endpoint oldEp, NodeId oldLoc, EpKey oldEpKey, Set<EndpointGroupId> oldEpgIds) {
420 // Update endpointsByNode
421 Set<EpKey> epsNode = endpointsByNode.get(oldLoc);
422 if (epsNode != null) {
423 epsNode.remove(oldEpKey);
424 if (epsNode.isEmpty()) {
425 endpointsByNode.remove(oldLoc);
426 SwitchManager.deactivatingSwitch(oldLoc);
430 // Update endpointsByGroupByNode and endpointsByGroup, get map of EPGs and their Endpoints
432 ConcurrentMap<EgKey, Set<EpKey>> map = endpointsByGroupByNode.get(oldLoc);
433 for (EndpointGroupId oldEpgId : oldEpgIds) {
434 // endpointsByGroupByNode
435 EgKey oldEgKey = new EgKey(oldEp.getTenant(), oldEpgId);
436 Set<EpKey> eps = map.get(oldEgKey);
438 eps.remove(oldEpKey);
440 map.remove(oldEgKey, Collections.emptySet());
443 Set<EpKey> geps = endpointsByGroup.get(oldEgKey);
445 geps.remove(oldEpKey);
447 endpointsByGroup.remove(oldEgKey);
451 // If map is empty, no more EPGs on this node, remove node from map
453 endpointsByGroupByNode.remove(oldLoc);
457 private void createL3Endpoint(EndpointL3 newL3Ep) {
458 LOG.trace("Processing L3Endpoint {}", newL3Ep.getKey());
459 if (isValidL3Ep(newL3Ep)) {
460 if (newL3Ep.getMacAddress() == null && getLocationType(newL3Ep) != null
461 && getLocationType(newL3Ep).equals(LocationType.External)) {
462 if (newL3Ep.getNetworkContainment() != null) {
463 arpTasker.addMacForL3EpAndCreateEp(newL3Ep);
465 LOG.error("Cannot generate MacAddress for L3Endpoint {}. NetworkContainment is null.", newL3Ep);
469 if (newL3Ep.getL2Context() != null && newL3Ep.getMacAddress() != null) {
470 notifyEndpointUpdated(new EpKey(newL3Ep.getL2Context(), newL3Ep.getMacAddress()));
474 LOG.error("{} is not a valid L3 Endpoint", newL3Ep);
477 if (newL3Ep.getAugmentation(OfOverlayL3Context.class) == null) {
478 LOG.info("L3Endpoint created but no augmentation information");
482 private void updateL3Endpoint(EndpointL3 newL3Ep) {
483 LOG.trace("Updating L3 Endpoint {}");
484 notifyEndpointUpdated(new EpKey(newL3Ep.getL2Context(), newL3Ep.getMacAddress()));
485 if (newL3Ep.getAugmentation(OfOverlayL3Context.class) == null) {
486 LOG.info("L3Endpoint updated but no augmentation information");
490 private void removeL3Endpoint(EndpointL3 oldL3Ep) {
491 LOG.trace("Removing L3 Endpoint {}");
492 notifyEndpointUpdated(new EpKey(oldL3Ep.getL2Context(), oldL3Ep.getMacAddress()));
498 public void close() throws Exception {
499 if (endpointListener != null)
500 endpointListener.close();
501 if (notificationListenerRegistration != null) {
502 notificationListenerRegistration.close();
506 private Set<EpKey> getEpNGSet(NodeId location, EgKey eg) {
507 ConcurrentMap<EgKey, Set<EpKey>> map = endpointsByGroupByNode.get(location);
509 map = new ConcurrentHashMap<>();
510 ConcurrentMap<EgKey, Set<EpKey>> old = endpointsByGroupByNode.putIfAbsent(location, map);
514 return SetUtils.getNestedSet(eg, map);
517 protected boolean isInternal(Endpoint ep) {
518 Preconditions.checkNotNull(ep);
519 OfOverlayContext ofc = ep.getAugmentation(OfOverlayContext.class);
520 return ofc == null || ofc.getLocationType() == null
521 || ofc.getLocationType().equals(EndpointLocation.LocationType.Internal);
524 public boolean isExternal(Endpoint ep) {
525 return !isInternal(ep);
529 * Get the endpoints container from data store.
530 * Note: There are maps maintained by listener when higher performance is required.
533 * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.Endpoints}
535 protected Endpoints getEndpointsFromDataStore() {
537 * XXX: alagalah I wanted to avoid adding another Map. Due to not being able to
538 * get to the granularity of the L3PrefixEndpoint List within the Endpoints container
539 * in the data store, we have to pull all the Endpoints. If this causes performance issues
540 * we may have to revisit a Map in updateEndpoint but note, this Endpoint doesn't have a
542 * and hence we would have to process it outside the null location check.
544 if (dataProvider == null) {
545 LOG.error("Null DataProvider in EndpointManager getEndpointsL3Prefix");
548 ReadOnlyTransaction rTx = dataProvider.newReadOnlyTransaction();
549 Optional<Endpoints> endpoints =
550 DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL, IidFactory.endpointsIidWildcard(), rTx);
551 if (!endpoints.isPresent()) {
552 LOG.warn("No Endpoints present in data store.");
555 return endpoints.get();
559 * Return all L3Endpoints from data store.
562 * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3}
564 protected Collection<EndpointL3> getL3Endpoints() {
565 Endpoints endpoints = getEndpointsFromDataStore();
566 if (endpoints == null || endpoints.getEndpointL3() == null) {
567 LOG.warn("No L3 Endpoints present in data store.");
570 return endpoints.getEndpointL3();
574 * Return all L3Prefix Endpoints from data store.
577 * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Prefix}
579 private Collection<EndpointL3Prefix> getEndpointsL3Prefix() {
580 Endpoints endpoints = getEndpointsFromDataStore();
581 if (endpoints == null || endpoints.getEndpointL3Prefix() == null) {
582 LOG.warn("No L3 Prefix Endpoints present in data store.");
585 return endpoints.getEndpointL3Prefix();
589 * Return all L3Prefix Endpoints which come under particular tenant
591 * @param tenantId - the
592 * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId}
595 * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Prefix}
597 public Collection<EndpointL3Prefix> getEndpointsL3PrefixForTenant(final TenantId tenantId) {
598 Collection<EndpointL3Prefix> l3PrefixEndpoints = getEndpointsL3Prefix();
599 if (l3PrefixEndpoints == null) {
600 // Log message already generated in getEndpointsL3Prefix()
603 return ImmutableSet.copyOf(Collections2.filter(l3PrefixEndpoints, new Predicate<EndpointL3Prefix>() {
606 public boolean apply(EndpointL3Prefix input) {
607 return (input.getTenant().equals(tenantId));
614 * Return all L3Endpoints containing network and port address translation in augmentation
617 * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3}
619 public Collection<EndpointL3> getL3EndpointsWithNat() {
620 Collection<EndpointL3> l3Endpoints = getL3Endpoints();
621 if (l3Endpoints == null) {
624 l3Endpoints = Collections2.filter(l3Endpoints, new Predicate<EndpointL3>() {
627 public boolean apply(EndpointL3 input) {
628 return !((input.getAugmentation(NatAddress.class) == null)
629 || (input.getAugmentation(NatAddress.class).getNatAddress() == null));
632 if (l3Endpoints == null) {
633 return Collections.emptySet();
635 return ImmutableSet.copyOf(l3Endpoints);
639 * Set the learning mode to the specified value
641 * @param learningMode - the learning mode to set
643 @SuppressWarnings({"UnusedParameters", "EmptyMethod"})
644 public void setLearningMode(OfOverlayConfig.LearningMode learningMode) {
649 * Get the effective list of conditions that apply to a particular endpoint.
650 * This could include additional conditions over the condition labels
651 * directly represented in the endpoint object
654 * @param endpoint - the
655 * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint}
657 * @return the list of
658 * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName}
660 public List<ConditionName> getConditionsForEndpoint(Endpoint endpoint) {
661 // TODO Be alagalah From Helium: consider group conditions as well. Also
663 // endpoint updated if the endpoint group conditions change
664 if (endpoint.getCondition() != null)
665 return endpoint.getCondition();
667 return Collections.emptyList();
670 private void notifyEndpointUpdated(EpKey epKey) {
671 for (EndpointListener l : listeners) {
672 l.endpointUpdated(epKey);
676 private void notifyNodeEndpointUpdated(NodeId nodeId, EpKey epKey) {
677 for (EndpointListener l : listeners) {
678 l.nodeEndpointUpdated(nodeId, epKey);
682 private void notifyGroupEndpointUpdated(EgKey egKey, EpKey epKey) {
683 for (EndpointListener l : listeners) {
684 l.groupEndpointUpdated(egKey, epKey);
688 private boolean isValidEp(Endpoint endpoint) {
689 return (endpoint != null && endpoint.getTenant() != null
690 && (endpoint.getEndpointGroup() != null || endpoint.getEndpointGroups() != null)
691 && endpoint.getL2Context() != null && endpoint.getMacAddress() != null);
694 private boolean isValidL3Ep(EndpointL3 endpoint) {
695 return (endpoint != null && endpoint.getTenant() != null
696 && (endpoint.getEndpointGroup() != null || endpoint.getEndpointGroups() != null)
697 && endpoint.getL3Context() != null && endpoint.getIpAddress() != null);
700 private NodeId getLocation(Endpoint endpoint) {
701 if (isValidEp(endpoint)) {
702 OfOverlayContext context = endpoint.getAugmentation(OfOverlayContext.class);
704 return context.getNodeId();
709 private EpKey getEpKey(Endpoint endpoint) {
710 if (isValidEp(endpoint))
711 return new EpKey(endpoint.getL2Context(), endpoint.getMacAddress());
715 public Set<EgKey> getEgKeysForEndpoint(Endpoint ep) {
716 Set<EgKey> egKeys = new HashSet<>();
718 if (ep.getEndpointGroup() != null) {
719 egKeys.add(new EgKey(ep.getTenant(), ep.getEndpointGroup()));
721 if (ep.getEndpointGroups() != null) {
722 for (EndpointGroupId epgId : ep.getEndpointGroups()) {
723 egKeys.add(new EgKey(ep.getTenant(), epgId));
729 private EndpointLocation.LocationType getLocationType(EndpointL3 epL3) {
730 if (epL3 == null || epL3.getAugmentation(OfOverlayL3Context.class) == null
731 || epL3.getAugmentation(OfOverlayL3Context.class).getLocationType() == null) {
734 return epL3.getAugmentation(OfOverlayL3Context.class).getLocationType();
737 @SuppressWarnings("unused")
738 private Endpoint addEndpointFromL3Endpoint(EndpointL3 l3Ep, ReadWriteTransaction rwTx) {
739 // Make an indexed tenant and resolveL2BridgeDomain from L3EP containment if not L3
741 OfOverlayL3Context ofL3Ctx = l3Ep.getAugmentation(OfOverlayL3Context.class);
742 OfOverlayContext ofCtx = getOfOverlayContextFromL3Endpoint(ofL3Ctx);
743 if (l3Ep.getNetworkContainment() instanceof L3Context) {
744 LOG.error("Cannot generate Endpoint from EndpointL3, network containment is L3Context.");
749 IndexedTenant indexedTenant;
750 Optional<Tenant> tenant =
751 readFromDs(LogicalDatastoreType.CONFIGURATION, IidFactory.tenantIid(l3Ep.getTenant()), rwTx);
752 if (tenant.isPresent()) {
753 indexedTenant = new IndexedTenant(tenant.get());
755 LOG.error("Could not find tenant {} for EndpointL3 {}", l3Ep.getTenant(), l3Ep);
759 List<L3Address> l3Address = new ArrayList<>();
760 l3Address.add(new L3AddressBuilder().setIpAddress(l3Ep.getIpAddress())
761 .setL3Context(l3Ep.getL3Context())
762 .setKey(new L3AddressKey(l3Ep.getIpAddress(), l3Ep.getL3Context()))
764 L2BridgeDomain l2Bd = indexedTenant.resolveL2BridgeDomain(l3Ep.getNetworkContainment());
765 Endpoint ep = new EndpointBuilder().setKey(new EndpointKey(l2Bd.getId(), l3Ep.getMacAddress()))
766 .setMacAddress(l3Ep.getMacAddress())
767 .setL2Context(l2Bd.getId())
768 .setEndpointGroups(l3Ep.getEndpointGroups())
769 .setTenant(l3Ep.getTenant())
770 .setL3Address(l3Address)
771 .setCondition(l3Ep.getCondition())
772 .setNetworkContainment(l3Ep.getNetworkContainment())
773 .addAugmentation(OfOverlayContext.class, ofCtx)
775 rwTx.put(LogicalDatastoreType.OPERATIONAL, IidFactory.endpointIid(ep.getL2Context(), ep.getMacAddress()), ep);
779 private OfOverlayContext getOfOverlayContextFromL3Endpoint(OfOverlayL3Context ofL3Ctx) {
780 OfOverlayContextBuilder ofBuilder = new OfOverlayContextBuilder();
781 if (ofL3Ctx.getInterfaceId() != null) {
782 ofBuilder.setInterfaceId(ofL3Ctx.getInterfaceId());
784 if (ofL3Ctx.getLocationType() != null) {
785 ofBuilder.setLocationType(ofL3Ctx.getLocationType());
787 if (ofL3Ctx.getNodeConnectorId() != null) {
788 ofBuilder.setNodeConnectorId(ofL3Ctx.getNodeConnectorId());
790 if (ofL3Ctx.getNodeId() != null) {
791 ofBuilder.setNodeId(ofL3Ctx.getNodeId());
793 if (ofL3Ctx.getPortName() != null) {
794 ofBuilder.setPortName(ofL3Ctx.getPortName());
797 return ofBuilder.build();
800 private Set<EndpointGroupId> getEndpointGroupsFromEndpoint(Endpoint ep) {
802 return new HashSet<>();
803 Set<EndpointGroupId> epgIds = new HashSet<>();
804 if (ep.getEndpointGroups() != null) {
805 epgIds.addAll(ep.getEndpointGroups());
807 if (ep.getEndpointGroup() != null) {
808 epgIds.add(ep.getEndpointGroup());
813 protected Map<EndpointKey, EndpointL3> getL3EpWithNatByL2Key() {
814 Map<EndpointKey, EndpointL3> l3EpByL2EpKey = new HashMap<>();
816 Collection<EndpointL3> l3Eps = getL3EndpointsWithNat();
818 l3EpByL2EpKey = Collections.emptyMap();
819 return l3EpByL2EpKey;
821 for (EndpointL3 l3Ep : l3Eps) {
822 if (l3Ep.getL2Context() != null && l3Ep.getMacAddress() != null) {
823 EndpointKey epKey = new EndpointKey(l3Ep.getL2Context(), l3Ep.getMacAddress());
824 l3EpByL2EpKey.put(epKey, l3Ep);
827 if (l3EpByL2EpKey.isEmpty()) {
828 l3EpByL2EpKey = Collections.emptyMap();
830 return l3EpByL2EpKey;
833 public EgKey getEgKey(Endpoint endpoint) {
834 if (!isValidEp(endpoint))
836 return new EgKey(endpoint.getTenant(), endpoint.getEndpointGroup());