Switch to MD-SAL APIs
[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 package org.opendaylight.openflowplugin.applications.frsync.impl;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.Collection;
12 import java.util.Optional;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.TimeUnit;
15 import java.util.concurrent.TimeoutException;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.mdsal.binding.api.DataTreeModification;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
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 /**
27  * Abstract Listener for node changes.
28  */
29 public abstract class AbstractFrmSyncListener<T extends DataObject> implements NodeListener<T> {
30
31     private static final Logger LOG = LoggerFactory.getLogger(AbstractFrmSyncListener.class);
32
33     @Override
34     public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<T>> modifications) {
35         for (DataTreeModification<T> modification : modifications) {
36             final NodeId nodeId = PathUtil.digNodeId(modification.getRootPath().getRootIdentifier());
37             if (LOG.isTraceEnabled()) {
38                 LOG.trace("DataTreeModification of {} in {} datastore", nodeId.getValue(), dsType());
39             }
40             try {
41                 final Optional<ListenableFuture<Boolean>> optFuture = processNodeModification(modification);
42                 if (optFuture.isPresent()) {
43                     final ListenableFuture<Boolean> future = optFuture.get();
44                     future.get(15000, TimeUnit.MILLISECONDS);
45                     if (LOG.isTraceEnabled()) {
46                         LOG.trace("Syncup for {} return from {} listener", nodeId.getValue(), dsType());
47                     }
48                 }
49             } catch (InterruptedException | ExecutionException | TimeoutException e) {
50                 LOG.error("Error processing inventory node modification: {}", nodeId.getValue(), e);
51             }
52         }
53     }
54
55     protected abstract Optional<ListenableFuture<Boolean>> processNodeModification(
56             DataTreeModification<T> modification);
57
58     protected abstract LogicalDatastoreType dsType();
59
60 }