Remove redundant names in paths
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / utils / CacheElanInstanceListener.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 javax.annotation.PostConstruct;
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.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 @Singleton
29 public class CacheElanInstanceListener implements ClusteredDataTreeChangeListener<ElanInstance> {
30
31     private static final Logger LOG = LoggerFactory.getLogger(CacheElanInstanceListener.class);
32
33     private final DataBroker broker;
34
35     private ListenerRegistration<CacheElanInstanceListener> registration;
36
37     @Inject
38     public CacheElanInstanceListener(DataBroker dataBroker) {
39         this.broker = dataBroker;
40     }
41
42     @PostConstruct
43     public void init() {
44         registerListener();
45     }
46
47     private void registerListener() {
48         final DataTreeIdentifier<ElanInstance> treeId =
49                 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, getWildcardPath());
50         LOG.trace("Registering on path: {}", treeId);
51         registration = broker.registerDataTreeChangeListener(treeId, CacheElanInstanceListener.this);
52     }
53
54     protected InstanceIdentifier<ElanInstance> getWildcardPath() {
55         return InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class);
56     }
57
58     @PreDestroy
59     public void close() throws Exception {
60         if (registration != null) {
61             registration.close();
62         }
63     }
64
65     @Override
66     public void onDataTreeChanged(Collection<DataTreeModification<ElanInstance>> changes) {
67         for (DataTreeModification<ElanInstance> change : changes) {
68             DataObjectModification<ElanInstance> mod = change.getRootNode();
69             switch (mod.getModificationType()) {
70                 case DELETE:
71                     ElanUtils.removeElanInstanceFromCache(mod.getDataBefore().getElanInstanceName());
72                     break;
73                 case SUBTREE_MODIFIED:
74                 case WRITE:
75                     ElanInstance elanInstance = mod.getDataAfter();
76                     ElanUtils.addElanInstanceIntoCache(elanInstance.getElanInstanceName(), elanInstance);
77                     break;
78                 default:
79                     throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
80             }
81         }
82     }
83
84 }