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