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.apache.aries.blueprint.annotation.service.Reference;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.coe.northbound.service.rev170611.ServiceInformation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.coe.northbound.service.rev170611.service.information.Services;
25 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 public class ServiceListener implements DataTreeChangeListener<Services> {
33 private static final Logger LOG = LoggerFactory.getLogger(ServiceListener.class);
34 private ListenerRegistration<ServiceListener> listenerRegistration;
37 public ServiceListener(@Reference final DataBroker dataBroker) {
38 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
41 protected InstanceIdentifier<Services> getWildCardPath() {
42 return InstanceIdentifier.create(ServiceInformation.class).child(Services.class);
45 public void registerListener(LogicalDatastoreType dsType, final DataBroker db) {
46 final DataTreeIdentifier<Services> treeId = new DataTreeIdentifier<>(dsType, getWildCardPath());
47 listenerRegistration = db.registerDataTreeChangeListener(treeId, ServiceListener.this);
52 if (listenerRegistration != null) {
54 listenerRegistration.close();
56 listenerRegistration = null;
62 public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Services>> changes) {
63 for (DataTreeModification<Services> change : changes) {
64 final DataObjectModification<Services> mod = change.getRootNode();
66 switch (mod.getModificationType()) {
68 LOG.info("Service deleted {}", mod.getDataBefore());
70 case SUBTREE_MODIFIED:
71 LOG.info("Service updated old : {}, new : {}", mod.getDataBefore(), mod.getDataAfter());
74 if (mod.getDataBefore() == null) {
75 LOG.info("Service added {}", mod.getDataAfter());
77 LOG.info("Service updated old : {}, new : {}", mod.getDataBefore(), mod.getDataAfter());
81 // FIXME: May be not a good idea to throw.
82 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());