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;
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;
25 public class CacheElanInterfaceListener implements ClusteredDataTreeChangeListener<ElanInterface> {
27 private ListenerRegistration<CacheElanInterfaceListener> registration;
28 private static final Logger logger = LoggerFactory.getLogger(CacheElanInterfaceListener.class);
29 private final DataBroker broker;
31 public CacheElanInterfaceListener(DataBroker dataBroker) {
32 this.broker = dataBroker;
39 private void registerListener() {
40 final DataTreeIdentifier<ElanInterface> treeId =
41 new DataTreeIdentifier<ElanInterface>(LogicalDatastoreType.CONFIGURATION, getWildcardPath());
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);
49 protected InstanceIdentifier<ElanInterface> getWildcardPath() {
50 return InstanceIdentifier.create(ElanInterfaces.class).child(ElanInterface.class);
53 public void close() throws Exception {
54 if(registration != null) {
60 public void onDataTreeChanged(Collection<DataTreeModification<ElanInterface>> changes) {
61 for (DataTreeModification<ElanInterface> change : changes) {
62 DataObjectModification<ElanInterface> mod = change.getRootNode();
63 switch (mod.getModificationType()) {
65 ElanUtils.removeElanInterfaceFromCache(mod.getDataBefore().getName());
67 case SUBTREE_MODIFIED:
69 ElanInterface elanInterface = mod.getDataAfter();
70 ElanUtils.addElanInterfaceIntoCache(elanInterface.getName(), elanInterface);
73 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());