MRI version bump for Aluminium
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / listeners / CacheBridgeEntryConfigListener.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.apache.aries.blueprint.annotation.service.Reference;
16 import org.opendaylight.genius.interfacemanager.commons.InterfaceMetaUtils;
17 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
18 import org.opendaylight.mdsal.binding.api.DataBroker;
19 import org.opendaylight.mdsal.binding.api.DataObjectModification;
20 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
21 import org.opendaylight.mdsal.binding.api.DataTreeModification;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406.BridgeInterfaceInfo;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406.bridge._interface.info.BridgeEntry;
25 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * This class listens for bridgeEntry creation/removal/update in Configuration
32  * DS and update the bridgeEntryCache as per changes in DS.
33  *
34  */
35 @Singleton
36 public class CacheBridgeEntryConfigListener implements ClusteredDataTreeChangeListener<BridgeEntry> {
37
38     private static final Logger LOG = LoggerFactory.getLogger(CacheBridgeEntryConfigListener.class);
39
40     private final InterfaceMetaUtils interfaceMetaUtils;
41     private final ListenerRegistration<CacheBridgeEntryConfigListener> registration;
42     private final DataTreeIdentifier<BridgeEntry> treeId = DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION,
43             InstanceIdentifier.create(BridgeInterfaceInfo.class).child(BridgeEntry.class));
44
45     @Inject
46     public CacheBridgeEntryConfigListener(@Reference final DataBroker dataBroker,
47                                           final InterfaceMetaUtils interfaceMetaUtils) {
48         LOG.trace("Registering on path: {}", treeId);
49         this.interfaceMetaUtils = interfaceMetaUtils;
50         registration = dataBroker.registerDataTreeChangeListener(treeId, CacheBridgeEntryConfigListener.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<BridgeEntry>> changes) {
62         for (DataTreeModification<BridgeEntry> change : changes) {
63             final DataObjectModification<BridgeEntry> mod = change.getRootNode();
64             switch (mod.getModificationType()) {
65                 case DELETE:
66                     if (mod.getDataBefore() != null) {
67                         interfaceMetaUtils.removeFromBridgeEntryCache(mod.getDataBefore());
68                     }
69                     break;
70                 case SUBTREE_MODIFIED:
71                 case WRITE:
72                     interfaceMetaUtils.addBridgeEntryToCache(mod.getDataAfter());
73                     break;
74                 default:
75                     throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
76             }
77         }
78     }
79 }