a7b1d7cdb1e75895e2b2575b180597211fb8a646
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / endpoint / EndpointManager.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.endpoint;
10
11 import static org.opendaylight.groupbasedpolicy.util.DataStoreHelper.readFromDs;
12
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;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.concurrent.ConcurrentHashMap;
22 import java.util.concurrent.ConcurrentMap;
23 import java.util.concurrent.CopyOnWriteArrayList;
24 import java.util.concurrent.ScheduledExecutorService;
25
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;
67
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;
76
77 /**
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
81  * controller node.
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
85  */
86 public class EndpointManager implements AutoCloseable {
87
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>() {
102
103         @Override
104         public Endpoint apply(EpKey input) {
105             return endpoints.get(input);
106         }
107     };
108
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);
117             } else {
118                 LOG.info("Missing service {}", NotificationService.class.getSimpleName());
119                 this.arpTasker = null;
120                 this.notificationListenerRegistration = null;
121             }
122         } else {
123             LOG.warn("Missing service {}", RpcProviderRegistry.class.getSimpleName());
124             this.arpTasker = null;
125             this.notificationListenerRegistration = null;
126         }
127         if (dataProvider != null) {
128             endpointListener = new EndpointManagerListener(this.dataProvider, this);
129         } else {
130             endpointListener = null;
131         }
132         LOG.debug("Initialized OFOverlay endpoint manager");
133     }
134
135     /**
136      * Add a {@link EndpointListener} to get notifications of switch events
137      *
138      * @param listener - the {@link EndpointListener} to add
139      */
140     public void registerListener(EndpointListener listener) {
141         listeners.add(listener);
142     }
143
144     /**
145      * Get a collection of endpoints attached to a particular switch
146      *
147      * @param nodeId - the nodeId of the switch to get endpoints for
148      * @return a collection of {@link Endpoint} objects.
149      */
150     public synchronized Set<EgKey> getGroupsForNode(NodeId nodeId) {
151         Map<EgKey, Set<EpKey>> nodeEps = endpointsByGroupByNode.get(nodeId);
152         if (nodeEps == null)
153             return Collections.emptySet();
154         return ImmutableSet.copyOf(nodeEps.keySet());
155     }
156
157     /**
158      * Get the set of nodes
159      *
160      * @param egKey - the egKey of the endpoint group to get nodes for
161      * @return a collection of {@link NodeId} objects.
162      */
163     public synchronized Set<NodeId> getNodesForGroup(final EgKey egKey) {
164         return ImmutableSet.copyOf(Sets.filter(endpointsByGroupByNode.keySet(), new Predicate<NodeId>() {
165
166             @Override
167             public boolean apply(NodeId input) {
168                 Map<EgKey, Set<EpKey>> nodeEps = endpointsByGroupByNode.get(input);
169                 return (nodeEps != null && nodeEps.containsKey(egKey));
170             }
171
172         }));
173     }
174
175     /**
176      * Get the endpoints in a particular group on a particular node
177      *
178      * @param nodeId - the node ID to look up
179      * @param eg - the group to look up
180      * @return the endpoints
181      */
182     public synchronized Collection<Endpoint> getEndpointsForNode(NodeId nodeId, EgKey eg) {
183         // TODO: alagalah Create method findEndpointsByNode() that uses
184         // data store
185
186         Map<EgKey, Set<EpKey>> nodeEps = endpointsByGroupByNode.get(nodeId);
187         if (nodeEps == null)
188             return Collections.emptyList();
189         Collection<EpKey> ebn = nodeEps.get(eg);
190         if (ebn == null)
191             return Collections.emptyList();
192         return ImmutableList.copyOf(Collections2.transform(ebn, indexTransform));
193     }
194
195     /**
196      * Get the endpoints on a particular node
197      *
198      * @param nodeId - the node ID to look up
199      * @return the endpoints
200      */
201     public synchronized Collection<Endpoint> getEndpointsForNode(final NodeId nodeId) {
202         // TODO: alagalah Create method findEndpointsByNode() that uses
203         // data store. See commented code below.
204
205         Collection<EpKey> ebn = endpointsByNode.get(nodeId);
206         if (ebn == null)
207             return Collections.emptyList();
208         return ImmutableList.copyOf(Collections2.transform(ebn, indexTransform));
209     }
210
211     /**
212      * Get the endpoint object for the given key
213      *
214      * @param epKey - the key
215      * @return the {@link Endpoint} corresponding to the key
216      */
217     public Endpoint getEndpoint(EpKey epKey) {
218         return endpoints.get(epKey);
219     }
220
221     /**
222      * Get a collection of endpoints in a particular endpoint group
223      *
224      * @param eg - Endpoint group key (contains endpoint group and tenant ID)
225      * @return a collection of {@link Endpoint} objects.
226      */
227     public synchronized Collection<Endpoint> getEndpointsForGroup(EgKey eg) {
228         Collection<EpKey> ebg = endpointsByGroup.get(eg);
229         if (ebg == null)
230             return Collections.emptyList();
231         return ImmutableList.copyOf(Collections2.transform(ebg, indexTransform));
232     }
233
234     /**
235      * Return set of external endpoints without location belonging to a particular endpoint group
236      *
237      * @param eg - Endpoint group key (contains endpoint group and tenant ID)
238      * @return a collection of {@link Endpoint} objects.
239      */
240     public synchronized Collection<Endpoint> getExtEpsNoLocForGroup(final EgKey eg) {
241
242         return ImmutableSet
243             .copyOf(Collections2.filter(externalEndpointsWithoutLocation.values(), new Predicate<Endpoint>() {
244
245                 @Override
246                 public boolean apply(Endpoint input) {
247                     Set<EndpointGroupId> epgIds = new HashSet<>();
248                     if (input.getEndpointGroup() != null) {
249                         epgIds.add(input.getEndpointGroup());
250                     }
251                     if (input.getEndpointGroups() != null) {
252                         epgIds.addAll(input.getEndpointGroups());
253                     }
254                     if (epgIds.isEmpty()) {
255                         LOG.error("No EPGs for {}. This is not a valid Endpoint.", input.getKey());
256                         return false;
257                     }
258                     return (epgIds.contains(eg.getEgId()));
259                 }
260
261             }));
262     }
263
264     /**
265      * Character of input parameters will determine action - create, update or delete L3Endpoint
266      *
267      * @param oldL3Ep the old L3 endpoint
268      * @param newL3Ep the new L3 endpoint
269      */
270     protected synchronized void processL3Endpoint(EndpointL3 oldL3Ep, EndpointL3 newL3Ep) {
271         // TODO Bug 3543
272         // create L3 endpoint
273         if (oldL3Ep == null && newL3Ep != null) {
274             createL3Endpoint(newL3Ep);
275         }
276
277         // update L3 endpoint
278         if (oldL3Ep != null && newL3Ep != null) {
279             updateL3Endpoint(newL3Ep);
280         }
281
282         // remove L3 endpoint
283         if (oldL3Ep != null && newL3Ep == null) {
284             removeL3Endpoint(oldL3Ep);
285         }
286     }
287
288     /**
289      * Character of input parameters will determine action - create, update or delete Endpoint
290      *
291      * @param oldEp - oldEp the new endpoint
292      * @param newEp - newEp the new endpoint
293      */
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();
300
301         Set<EndpointGroupId> oldEpgIds = getEndpointGroupsFromEndpoint(oldEp);
302         Set<EndpointGroupId> newEpgIds = getEndpointGroupsFromEndpoint(newEp);
303
304         boolean notifyOldLoc = false;
305         boolean notifyNewLoc = false;
306         boolean notifyOldEg = false;
307         boolean notifyNewEg = false;
308
309         // create endpoint
310         if (oldEp == null && newEp != null) {
311             if (newLoc != null) {
312                 createEndpoint(newLoc, newEpKey, newEpgIds, tenantId);
313                 endpoints.put(newEpKey, newEp);
314                 notifyEndpointUpdated(newEpKey);
315                 notifyNewLoc = true;
316                 notifyNewEg = true;
317             } else {
318                 externalEndpointsWithoutLocation.put(newEpKey, newEp);
319             }
320         }
321
322         // update endpoint
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);
330                 notifyNewLoc = true;
331                 notifyNewEg = true;
332             }
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);
339                 notifyOldLoc = true;
340                 notifyOldEg = true;
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()))) {
345                         notifyOldLoc = true;
346                         notifyNewLoc = true;
347                     }
348                     // endpoint changed EPGs
349                     if (!oldEpgIds.equals(newEpgIds)) {
350                         notifyOldEg = true;
351                         notifyNewEg = true;
352                     }
353                     removeEndpoint(oldEp, oldLoc, oldEpKey, oldEpgIds);
354                     createEndpoint(newLoc, newEpKey, newEpgIds, tenantId);
355                     notifyEndpointUpdated(newEpKey);
356             }
357         }
358
359         // remove endpoint
360         else if (oldEp != null && newEp == null) {
361             if (oldLoc != null) {
362                 removeEndpoint(oldEp, oldLoc, oldEpKey, oldEpgIds);
363                 endpoints.remove(oldEpKey);
364                 notifyEndpointUpdated(oldEpKey);
365                 notifyOldLoc = true;
366                 notifyOldEg = true;
367             } else {
368                 externalEndpointsWithoutLocation.remove(oldEpKey);
369             }
370         }
371
372         // notifications
373         if (notifyOldLoc)
374             notifyNodeEndpointUpdated(oldLoc, oldEpKey);
375         if (notifyNewLoc)
376             notifyNodeEndpointUpdated(newLoc, newEpKey);
377         if (notifyOldEg)
378             for (EndpointGroupId oldEpgId : oldEpgIds) {
379                 EgKey oldEgKey = new EgKey(oldEp.getTenant(), oldEpgId);
380                 notifyGroupEndpointUpdated(oldEgKey, oldEpKey);
381             }
382         if (notifyNewEg)
383             for (EndpointGroupId newEpgId : newEpgIds) {
384                 EgKey newEgKey = new EgKey(newEp.getTenant(), newEpgId);
385                 notifyGroupEndpointUpdated(newEgKey, newEpKey);
386             }
387     }
388
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);
396         } else {
397             Set<EpKey> epsNode = endpointsByNode.get(newLoc);
398             epsNode.add(newEpKey);
399         }
400
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);
406             eps.add(newEpKey);
407             // endpointsByGroup
408             Set<EpKey> geps = endpointsByGroup.get(newEgKey);
409             if (geps == null) {
410                 geps = new HashSet<>();
411             }
412             geps.add(newEpKey);
413             endpointsByGroup.put(newEgKey, geps);
414             LOG.debug("Endpoint {} added to node {}", newEpKey, newLoc);
415
416         }
417     }
418
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);
427             }
428         }
429
430         // Update endpointsByGroupByNode and endpointsByGroup, get map of EPGs and their Endpoints
431         // for Node
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);
437             if (eps != null) {
438                 eps.remove(oldEpKey);
439                 if (eps.isEmpty())
440                     map.remove(oldEgKey, Collections.emptySet());
441             }
442             // endpointsByGroup
443             Set<EpKey> geps = endpointsByGroup.get(oldEgKey);
444             if (geps != null) {
445                 geps.remove(oldEpKey);
446                 if (geps.isEmpty())
447                     endpointsByGroup.remove(oldEgKey);
448             }
449         }
450
451         // If map is empty, no more EPGs on this node, remove node from map
452         if (map.isEmpty())
453             endpointsByGroupByNode.remove(oldLoc);
454
455     }
456
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);
464                 } else {
465                     LOG.error("Cannot generate MacAddress for L3Endpoint {}. NetworkContainment is null.", newL3Ep);
466                     return;
467                 }
468             }
469             if (newL3Ep.getL2Context() != null && newL3Ep.getMacAddress() != null) {
470                 notifyEndpointUpdated(new EpKey(newL3Ep.getL2Context(), newL3Ep.getMacAddress()));
471                 return;
472             }
473         } else {
474             LOG.error("{} is not a valid L3 Endpoint", newL3Ep);
475             return;
476         }
477         if (newL3Ep.getAugmentation(OfOverlayL3Context.class) == null) {
478             LOG.info("L3Endpoint created but no augmentation information");
479         }
480     }
481
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");
487         }
488     }
489
490     private void removeL3Endpoint(EndpointL3 oldL3Ep) {
491         LOG.trace("Removing L3 Endpoint {}");
492         notifyEndpointUpdated(new EpKey(oldL3Ep.getL2Context(), oldL3Ep.getMacAddress()));
493
494     }
495
496     // auto closeable
497     @Override
498     public void close() throws Exception {
499         if (endpointListener != null)
500             endpointListener.close();
501         if (notificationListenerRegistration != null) {
502             notificationListenerRegistration.close();
503         }
504     }
505
506     private Set<EpKey> getEpNGSet(NodeId location, EgKey eg) {
507         ConcurrentMap<EgKey, Set<EpKey>> map = endpointsByGroupByNode.get(location);
508         if (map == null) {
509             map = new ConcurrentHashMap<>();
510             ConcurrentMap<EgKey, Set<EpKey>> old = endpointsByGroupByNode.putIfAbsent(location, map);
511             if (old != null)
512                 map = old;
513         }
514         return SetUtils.getNestedSet(eg, map);
515     }
516
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);
522     }
523
524     public boolean isExternal(Endpoint ep) {
525         return !isInternal(ep);
526     }
527
528     /**
529      * Get the endpoints container from data store.
530      * Note: There are maps maintained by listener when higher performance is required.
531      *
532      * @return the
533      *         {@link org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.Endpoints}
534      */
535     protected Endpoints getEndpointsFromDataStore() {
536         /*
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
541          * location
542          * and hence we would have to process it outside the null location check.
543          */
544         if (dataProvider == null) {
545             LOG.error("Null DataProvider in EndpointManager getEndpointsL3Prefix");
546             return null;
547         }
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.");
553             return null;
554         }
555         return endpoints.get();
556     }
557
558     /**
559      * Return all L3Endpoints from data store.
560      *
561      * @return the
562      *         {@link org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3}
563      */
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.");
568             return null;
569         }
570         return endpoints.getEndpointL3();
571     }
572
573     /**
574      * Return all L3Prefix Endpoints from data store.
575      *
576      * @return the
577      *         {@link org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Prefix}
578      */
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.");
583             return null;
584         }
585         return endpoints.getEndpointL3Prefix();
586     }
587
588     /**
589      * Return all L3Prefix Endpoints which come under particular tenant
590      *
591      * @param tenantId - the
592      *        {@link org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId}
593      *        to resolve
594      * @return the
595      *         {@link org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Prefix}
596      */
597     public Collection<EndpointL3Prefix> getEndpointsL3PrefixForTenant(final TenantId tenantId) {
598         Collection<EndpointL3Prefix> l3PrefixEndpoints = getEndpointsL3Prefix();
599         if (l3PrefixEndpoints == null) {
600             // Log message already generated in getEndpointsL3Prefix()
601             return null;
602         }
603         return ImmutableSet.copyOf(Collections2.filter(l3PrefixEndpoints, new Predicate<EndpointL3Prefix>() {
604
605             @Override
606             public boolean apply(EndpointL3Prefix input) {
607                 return (input.getTenant().equals(tenantId));
608             }
609
610         }));
611     }
612
613     /**
614      * Return all L3Endpoints containing network and port address translation in augmentation
615      *
616      * @return the
617      *         {@link org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3}
618      */
619     public Collection<EndpointL3> getL3EndpointsWithNat() {
620         Collection<EndpointL3> l3Endpoints = getL3Endpoints();
621         if (l3Endpoints == null) {
622             return null;
623         }
624         l3Endpoints = Collections2.filter(l3Endpoints, new Predicate<EndpointL3>() {
625
626             @Override
627             public boolean apply(EndpointL3 input) {
628                 return !((input.getAugmentation(NatAddress.class) == null)
629                         || (input.getAugmentation(NatAddress.class).getNatAddress() == null));
630             }
631         });
632         if (l3Endpoints == null) {
633             return Collections.emptySet();
634         }
635         return ImmutableSet.copyOf(l3Endpoints);
636     }
637
638     /**
639      * Set the learning mode to the specified value
640      *
641      * @param learningMode - the learning mode to set
642      */
643     @SuppressWarnings({"UnusedParameters", "EmptyMethod"})
644     public void setLearningMode(OfOverlayConfig.LearningMode learningMode) {
645         // No-op for now
646     }
647
648     /**
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
652      * set
653      *
654      * @param endpoint - the
655      *        {@link org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint}
656      *        to resolve
657      * @return the list of
658      *         {@link org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName}
659      */
660     public List<ConditionName> getConditionsForEndpoint(Endpoint endpoint) {
661         // TODO Be alagalah From Helium: consider group conditions as well. Also
662         // need to notify
663         // endpoint updated if the endpoint group conditions change
664         if (endpoint.getCondition() != null)
665             return endpoint.getCondition();
666         else
667             return Collections.emptyList();
668     }
669
670     private void notifyEndpointUpdated(EpKey epKey) {
671         for (EndpointListener l : listeners) {
672             l.endpointUpdated(epKey);
673         }
674     }
675
676     private void notifyNodeEndpointUpdated(NodeId nodeId, EpKey epKey) {
677         for (EndpointListener l : listeners) {
678             l.nodeEndpointUpdated(nodeId, epKey);
679         }
680     }
681
682     private void notifyGroupEndpointUpdated(EgKey egKey, EpKey epKey) {
683         for (EndpointListener l : listeners) {
684             l.groupEndpointUpdated(egKey, epKey);
685         }
686     }
687
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);
692     }
693
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);
698     }
699
700     private NodeId getLocation(Endpoint endpoint) {
701         if (isValidEp(endpoint)) {
702             OfOverlayContext context = endpoint.getAugmentation(OfOverlayContext.class);
703             if (context != null)
704                 return context.getNodeId();
705         }
706         return null;
707     }
708
709     private EpKey getEpKey(Endpoint endpoint) {
710         if (isValidEp(endpoint))
711             return new EpKey(endpoint.getL2Context(), endpoint.getMacAddress());
712         return null;
713     }
714
715     public Set<EgKey> getEgKeysForEndpoint(Endpoint ep) {
716         Set<EgKey> egKeys = new HashSet<>();
717
718         if (ep.getEndpointGroup() != null) {
719             egKeys.add(new EgKey(ep.getTenant(), ep.getEndpointGroup()));
720         }
721         if (ep.getEndpointGroups() != null) {
722             for (EndpointGroupId epgId : ep.getEndpointGroups()) {
723                 egKeys.add(new EgKey(ep.getTenant(), epgId));
724             }
725         }
726         return egKeys;
727     }
728
729     private EndpointLocation.LocationType getLocationType(EndpointL3 epL3) {
730         if (epL3 == null || epL3.getAugmentation(OfOverlayL3Context.class) == null
731                 || epL3.getAugmentation(OfOverlayL3Context.class).getLocationType() == null) {
732             return null;
733         }
734         return epL3.getAugmentation(OfOverlayL3Context.class).getLocationType();
735     }
736
737     @SuppressWarnings("unused")
738     private Endpoint addEndpointFromL3Endpoint(EndpointL3 l3Ep, ReadWriteTransaction rwTx) {
739         // Make an indexed tenant and resolveL2BridgeDomain from L3EP containment if not L3
740         // (instanceof)
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.");
745             rwTx.cancel();
746             return null;
747         }
748
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());
754         } else {
755             LOG.error("Could not find tenant {} for EndpointL3 {}", l3Ep.getTenant(), l3Ep);
756             rwTx.cancel();
757             return null;
758         }
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()))
763             .build());
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)
774             .build();
775         rwTx.put(LogicalDatastoreType.OPERATIONAL, IidFactory.endpointIid(ep.getL2Context(), ep.getMacAddress()), ep);
776         return ep;
777     }
778
779     private OfOverlayContext getOfOverlayContextFromL3Endpoint(OfOverlayL3Context ofL3Ctx) {
780         OfOverlayContextBuilder ofBuilder = new OfOverlayContextBuilder();
781         if (ofL3Ctx.getInterfaceId() != null) {
782             ofBuilder.setInterfaceId(ofL3Ctx.getInterfaceId());
783         }
784         if (ofL3Ctx.getLocationType() != null) {
785             ofBuilder.setLocationType(ofL3Ctx.getLocationType());
786         }
787         if (ofL3Ctx.getNodeConnectorId() != null) {
788             ofBuilder.setNodeConnectorId(ofL3Ctx.getNodeConnectorId());
789         }
790         if (ofL3Ctx.getNodeId() != null) {
791             ofBuilder.setNodeId(ofL3Ctx.getNodeId());
792         }
793         if (ofL3Ctx.getPortName() != null) {
794             ofBuilder.setPortName(ofL3Ctx.getPortName());
795         }
796
797         return ofBuilder.build();
798     }
799
800     private Set<EndpointGroupId> getEndpointGroupsFromEndpoint(Endpoint ep) {
801         if (ep == null)
802             return new HashSet<>();
803         Set<EndpointGroupId> epgIds = new HashSet<>();
804         if (ep.getEndpointGroups() != null) {
805             epgIds.addAll(ep.getEndpointGroups());
806         }
807         if (ep.getEndpointGroup() != null) {
808             epgIds.add(ep.getEndpointGroup());
809         }
810         return epgIds;
811     }
812
813     protected Map<EndpointKey, EndpointL3> getL3EpWithNatByL2Key() {
814         Map<EndpointKey, EndpointL3> l3EpByL2EpKey = new HashMap<>();
815
816         Collection<EndpointL3> l3Eps = getL3EndpointsWithNat();
817         if (l3Eps == null) {
818             l3EpByL2EpKey = Collections.emptyMap();
819             return l3EpByL2EpKey;
820         }
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);
825             }
826         }
827         if (l3EpByL2EpKey.isEmpty()) {
828             l3EpByL2EpKey = Collections.emptyMap();
829         }
830         return l3EpByL2EpKey;
831     }
832
833     public EgKey getEgKey(Endpoint endpoint) {
834         if (!isValidEp(endpoint))
835             return null;
836         return new EgKey(endpoint.getTenant(), endpoint.getEndpointGroup());
837     }
838 }