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 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;
24 public class CacheElanInterfaceListener implements ClusteredDataTreeChangeListener<ElanInterface> {
26 private ListenerRegistration<CacheElanInterfaceListener> registration;
27 private static final Logger LOG = LoggerFactory.getLogger(CacheElanInterfaceListener.class);
28 private final DataBroker broker;
30 public CacheElanInterfaceListener(DataBroker dataBroker) {
31 this.broker = dataBroker;
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);
45 protected InstanceIdentifier<ElanInterface> getWildcardPath() {
46 return InstanceIdentifier.create(ElanInterfaces.class).child(ElanInterface.class);
49 public void close() throws Exception {
50 if (registration != null) {
56 public void onDataTreeChanged(Collection<DataTreeModification<ElanInterface>> changes) {
57 for (DataTreeModification<ElanInterface> change : changes) {
58 DataObjectModification<ElanInterface> mod = change.getRootNode();
59 switch (mod.getModificationType()) {
61 ElanUtils.removeElanInterfaceFromCache(mod.getDataBefore().getName());
62 ElanUtils.removeElanInterfaceToElanInstanceCache(mod.getDataBefore().getElanInstanceName(),
63 mod.getDataBefore().getName());
65 case SUBTREE_MODIFIED:
67 ElanInterface elanInterface = mod.getDataAfter();
68 ElanUtils.addElanInterfaceIntoCache(elanInterface.getName(), elanInterface);
69 ElanUtils.addElanInterfaceToElanInstanceCache(elanInterface.getElanInstanceName(),
70 elanInterface.getName());
73 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());