2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.netvirt.elan.utils;
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;
29 public class CacheElanInstanceListener implements ClusteredDataTreeChangeListener<ElanInstance> {
31 private static final Logger LOG = LoggerFactory.getLogger(CacheElanInstanceListener.class);
33 private final DataBroker broker;
35 private ListenerRegistration<CacheElanInstanceListener> registration;
38 public CacheElanInstanceListener(DataBroker dataBroker) {
39 this.broker = dataBroker;
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);
54 protected InstanceIdentifier<ElanInstance> getWildcardPath() {
55 return InstanceIdentifier.create(ElanInstances.class).child(ElanInstance.class);
59 public void close() throws Exception {
60 if (registration != null) {
66 public void onDataTreeChanged(Collection<DataTreeModification<ElanInstance>> changes) {
67 for (DataTreeModification<ElanInstance> change : changes) {
68 DataObjectModification<ElanInstance> mod = change.getRootNode();
69 switch (mod.getModificationType()) {
71 ElanUtils.removeElanInstanceFromCache(mod.getDataBefore().getElanInstanceName());
73 case SUBTREE_MODIFIED:
75 ElanInstance elanInstance = mod.getDataAfter();
76 ElanUtils.addElanInstanceIntoCache(elanInstance.getElanInstanceName(), elanInstance);
79 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());