Merge "Sonar issues"
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / impl / AbstractFrmSyncListener.java
1 /**
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.openflowplugin.applications.frsync.impl;
10
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.Collection;
14 import java.util.concurrent.TimeUnit;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.openflowplugin.applications.frsync.NodeListener;
19 import org.opendaylight.openflowplugin.applications.frsync.util.PathUtil;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Abstract Listener for node changes.
27  */
28 public abstract class AbstractFrmSyncListener<T extends DataObject> implements NodeListener<T> {
29     private static final Logger LOG = LoggerFactory.getLogger(AbstractFrmSyncListener.class);
30
31     @Override
32     public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<T>> modifications) {
33         for (DataTreeModification<T> modification : modifications) {
34             final NodeId nodeId = PathUtil.digNodeId(modification.getRootPath().getRootIdentifier());
35
36             try {
37                 final Optional<ListenableFuture<Boolean>> optFuture = processNodeModification(modification);
38                 if (optFuture.isPresent()) {
39                     final ListenableFuture<Boolean> future = optFuture.get();
40                     final Boolean ret = future.get(15000, TimeUnit.MILLISECONDS);
41                     LOG.debug("syncup return in {} listener for: {} [{}] thread:{}", dsType(), nodeId.getValue(), ret, threadName());
42                 }
43             } catch (InterruptedException e) {
44                 LOG.warn("permit for forwarding rules sync not acquired: {}", nodeId.getValue());
45             } catch (Exception e) {
46                 LOG.error("error processing inventory node modification: {}", nodeId.getValue(), e);
47             }
48         }
49     }
50
51     protected abstract Optional<ListenableFuture<Boolean>> processNodeModification(
52             DataTreeModification<T> modification) throws InterruptedException;
53
54     protected abstract LogicalDatastoreType dsType();
55
56     private static String threadName() {
57         final Thread currentThread = Thread.currentThread();
58         return currentThread.getName();
59     }
60 }