Fix build faliures due to OFPlugin checktyle fixes
[netvirt.git] / vpnservice / 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.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;
28
29 @Singleton
30 public class ServiceListener implements DataTreeChangeListener<Services> {
31
32     private static final Logger LOG = LoggerFactory.getLogger(ServiceListener.class);
33     private ListenerRegistration<ServiceListener> listenerRegistration;
34     private final DataBroker dataBroker;
35
36     @Inject
37     public ServiceListener(final DataBroker dataBroker) {
38         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
39         this.dataBroker = dataBroker;
40     }
41
42     protected InstanceIdentifier<Services> getWildCardPath() {
43         return InstanceIdentifier.create(ServiceInformation.class).child(Services.class);
44     }
45
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);
49     }
50
51     @PreDestroy
52     public void close() {
53         if (listenerRegistration != null) {
54             try {
55                 listenerRegistration.close();
56             } finally {
57                 listenerRegistration = null;
58             }
59         }
60     }
61
62     @Override
63     public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Services>> changes) {
64         for (DataTreeModification<Services> change : changes) {
65             final DataObjectModification<Services> mod = change.getRootNode();
66
67             switch (mod.getModificationType()) {
68                 case DELETE:
69                     LOG.info("Service deleted {}", mod.getDataBefore());
70                     break;
71                 case SUBTREE_MODIFIED:
72                     LOG.info("Service updated old : {}, new : {}", mod.getDataBefore(), mod.getDataAfter());
73                     break;
74                 case WRITE:
75                     if (mod.getDataBefore() == null) {
76                         LOG.info("Service added {}", mod.getDataAfter());
77                     } else {
78                         LOG.info("Service updated old : {}, new : {}", mod.getDataBefore(), mod.getDataAfter());
79                     }
80                     break;
81                 default:
82                     // FIXME: May be not a good idea to throw.
83                     throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
84             }
85         }
86     }
87 }