2 * Copyright (c) 2017 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
9 package org.opendaylight.netvirt.coe.listeners;
11 import java.util.Collection;
12 import javax.annotation.Nonnull;
13 import javax.annotation.PreDestroy;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
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.DataTreeChangeListener;
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.coe.northbound.service.rev170611.ServiceInformation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.coe.northbound.service.rev170611.service.information.Services;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 public class ServiceListener implements DataTreeChangeListener<Services> {
32 private static final Logger LOG = LoggerFactory.getLogger(ServiceListener.class);
33 private ListenerRegistration<ServiceListener> listenerRegistration;
34 private final DataBroker dataBroker;
37 public ServiceListener(final DataBroker dataBroker) {
38 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
39 this.dataBroker = dataBroker;
42 protected InstanceIdentifier<Services> getWildCardPath() {
43 return InstanceIdentifier.create(ServiceInformation.class).child(Services.class);
46 public void registerListener(LogicalDatastoreType dsType, final DataBroker db) {
47 final DataTreeIdentifier<Services> treeId = new DataTreeIdentifier<>(dsType, getWildCardPath());
48 listenerRegistration = db.registerDataTreeChangeListener(treeId, ServiceListener.this);
53 if (listenerRegistration != null) {
55 listenerRegistration.close();
57 listenerRegistration = null;
63 public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Services>> changes) {
64 for (DataTreeModification<Services> change : changes) {
65 final DataObjectModification<Services> mod = change.getRootNode();
67 switch (mod.getModificationType()) {
69 LOG.info("Service deleted {}", mod.getDataBefore());
71 case SUBTREE_MODIFIED:
72 LOG.info("Service updated old : {}, new : {}", mod.getDataBefore(), mod.getDataAfter());
75 if (mod.getDataBefore() == null) {
76 LOG.info("Service added {}", mod.getDataAfter());
78 LOG.info("Service updated old : {}, new : {}", mod.getDataBefore(), mod.getDataAfter());
82 // FIXME: May be not a good idea to throw.
83 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());