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