7f81e93c8f0f314a9eea31500ddef1c69b7ce279
[netvirt.git] / coe / impl / src / main / java / org / opendaylight / netvirt / coe / listeners / ServiceListener.java
1 /*
2  * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netvirt.coe.listeners;
10
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;
29
30 @Singleton
31 public class ServiceListener implements DataTreeChangeListener<Services> {
32
33     private static final Logger LOG = LoggerFactory.getLogger(ServiceListener.class);
34     private ListenerRegistration<ServiceListener> listenerRegistration;
35
36     @Inject
37     public ServiceListener(@Reference final DataBroker dataBroker) {
38         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
39     }
40
41     protected InstanceIdentifier<Services> getWildCardPath() {
42         return InstanceIdentifier.create(ServiceInformation.class).child(Services.class);
43     }
44
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);
48     }
49
50     @PreDestroy
51     public void close() {
52         if (listenerRegistration != null) {
53             try {
54                 listenerRegistration.close();
55             } finally {
56                 listenerRegistration = null;
57             }
58         }
59     }
60
61     @Override
62     public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Services>> changes) {
63         for (DataTreeModification<Services> change : changes) {
64             final DataObjectModification<Services> mod = change.getRootNode();
65
66             switch (mod.getModificationType()) {
67                 case DELETE:
68                     LOG.info("Service deleted {}", mod.getDataBefore());
69                     break;
70                 case SUBTREE_MODIFIED:
71                     LOG.info("Service updated old : {}, new : {}", mod.getDataBefore(), mod.getDataAfter());
72                     break;
73                 case WRITE:
74                     if (mod.getDataBefore() == null) {
75                         LOG.info("Service added {}", mod.getDataAfter());
76                     } else {
77                         LOG.info("Service updated old : {}, new : {}", mod.getDataBefore(), mod.getDataAfter());
78                     }
79                     break;
80                 default:
81                     // FIXME: May be not a good idea to throw.
82                     throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
83             }
84         }
85     }
86 }