a05ec10f0fe9b1dd10f77b9392e2d441656876c8
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / listeners / CacheBridgeRefEntryListener.java
1 /*
2  * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. 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.genius.interfacemanager.listeners;
10
11 import java.util.Collection;
12 import javax.annotation.PreDestroy;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.genius.interfacemanager.commons.InterfaceMetaUtils;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406.BridgeRefInfo;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406.bridge.ref.info.BridgeRefEntry;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * This class listens for bridgeRefEntry creation/removal/update in Operational
31  * DS and update the bridgeRefEntryCache as per changes in DS.
32  *
33  */
34 @Singleton
35 public class CacheBridgeRefEntryListener implements ClusteredDataTreeChangeListener<BridgeRefEntry> {
36     private static final Logger LOG = LoggerFactory.getLogger(CacheBridgeRefEntryListener.class);
37
38     private final ListenerRegistration<CacheBridgeRefEntryListener> registration;
39     private final DataTreeIdentifier<BridgeRefEntry> treeId = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL,
40             getWildcardPath());
41
42     @Inject
43     public CacheBridgeRefEntryListener(DataBroker dataBroker) {
44         LOG.trace("Registering on path: {}", treeId);
45         registration = dataBroker.registerDataTreeChangeListener(treeId, CacheBridgeRefEntryListener.this);
46     }
47
48     @PreDestroy
49     public void close() {
50         if (registration != null) {
51             registration.close();
52         }
53     }
54
55     protected InstanceIdentifier<BridgeRefEntry> getWildcardPath() {
56         return InstanceIdentifier.create(BridgeRefInfo.class).child(BridgeRefEntry.class);
57     }
58
59     @Override
60     public void onDataTreeChanged(Collection<DataTreeModification<BridgeRefEntry>> changes) {
61         for (DataTreeModification<BridgeRefEntry> change : changes) {
62             final DataObjectModification<BridgeRefEntry> mod = change.getRootNode();
63             switch (mod.getModificationType()) {
64                 case DELETE:
65                     /*
66                      * Note: Do we want to retain entry in cache? Ref Entry missing
67                      * means OVS being disconnected for now. It will either come
68                      * back, or will require config to be deleted.
69                      *
70                      * Removing for now, can consider this as future optimization.
71                      *
72                      */
73                     InterfaceMetaUtils.removeFromBridgeRefEntryCache(mod.getDataBefore());
74                     break;
75                 case SUBTREE_MODIFIED:
76                 case WRITE:
77                     InterfaceMetaUtils.addBridgeRefEntryToCache(mod.getDataAfter());
78                     break;
79                 default:
80                     throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
81             }
82         }
83     }
84 }