5130c9ae150bd9512407dbfb39a3b7fc8b1ca028
[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.ExecutionException;
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.TimeoutException;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.openflowplugin.applications.frsync.NodeListener;
21 import org.opendaylight.openflowplugin.applications.frsync.util.PathUtil;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Abstract Listener for node changes.
29  */
30 public abstract class AbstractFrmSyncListener<T extends DataObject> implements NodeListener<T> {
31
32     private static final Logger LOG = LoggerFactory.getLogger(AbstractFrmSyncListener.class);
33
34     @Override
35     public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<T>> modifications) {
36         for (DataTreeModification<T> modification : modifications) {
37             final NodeId nodeId = PathUtil.digNodeId(modification.getRootPath().getRootIdentifier());
38             if (LOG.isTraceEnabled()) {
39                 LOG.trace("DataTreeModification of {} in {} datastore", nodeId.getValue(), dsType());
40             }
41             try {
42                 final Optional<ListenableFuture<Boolean>> optFuture = processNodeModification(modification);
43                 if (optFuture.isPresent()) {
44                     final ListenableFuture<Boolean> future = optFuture.get();
45                     future.get(15000, TimeUnit.MILLISECONDS);
46                     if (LOG.isTraceEnabled()) {
47                         LOG.trace("Syncup for {} return from {} listener", nodeId.getValue(), dsType());
48                     }
49                 }
50             } catch (InterruptedException | ExecutionException | TimeoutException e) {
51                 LOG.error("Error processing inventory node modification: {}", nodeId.getValue(), e);
52             }
53         }
54     }
55
56     protected abstract Optional<ListenableFuture<Boolean>> processNodeModification(
57             DataTreeModification<T> modification);
58
59     protected abstract LogicalDatastoreType dsType();
60
61 }