Merge "blueprint cleanup"
[netvirt.git] / vpnservice / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / netvirt / elan / utils / CacheElanInterfaceListener.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.netvirt.elan.utils;
9
10 import java.util.Collection;
11
12 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
15 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
16 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaces;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class CacheElanInterfaceListener implements ClusteredDataTreeChangeListener<ElanInterface> {
26
27     private ListenerRegistration<CacheElanInterfaceListener> registration;
28     private static final Logger logger = LoggerFactory.getLogger(CacheElanInterfaceListener.class);
29     private final DataBroker broker;
30
31     public CacheElanInterfaceListener(DataBroker dataBroker) {
32         this.broker = dataBroker;
33     }
34
35     public void init() {
36         registerListener();
37     }
38
39     private void registerListener() {
40         final DataTreeIdentifier<ElanInterface> treeId =
41                 new DataTreeIdentifier<ElanInterface>(LogicalDatastoreType.CONFIGURATION, getWildcardPath());
42         try {
43             logger.trace("Registering on path: {}", treeId);
44             registration = broker.registerDataTreeChangeListener(treeId, CacheElanInterfaceListener.this);
45         } catch (final Exception e) {
46             logger.warn("CacheInterfaceConfigListener registration failed", e);
47         }
48     }
49     protected InstanceIdentifier<ElanInterface> getWildcardPath() {
50         return InstanceIdentifier.create(ElanInterfaces.class).child(ElanInterface.class);
51     }
52
53     public void close() throws Exception {
54         if(registration != null) {
55             registration.close();
56         }
57     }
58
59     @Override
60     public void onDataTreeChanged(Collection<DataTreeModification<ElanInterface>> changes) {
61         for (DataTreeModification<ElanInterface> change : changes) {
62             DataObjectModification<ElanInterface> mod = change.getRootNode();
63             switch (mod.getModificationType()) {
64             case DELETE:
65                 ElanUtils.removeElanInterfaceFromCache(mod.getDataBefore().getName());
66                 break;
67             case SUBTREE_MODIFIED:
68             case WRITE:
69                 ElanInterface elanInterface = mod.getDataAfter();
70                 ElanUtils.addElanInterfaceIntoCache(elanInterface.getName(), elanInterface);
71                 break;
72             default:
73                 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
74             }
75         }               
76     }
77
78 }