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