Merge "BUG 1839 - HTTP delete of non existing data"
[controller.git] / opendaylight / md-sal / statistics-manager / src / main / java / org / opendaylight / controller / md / statistics / manager / impl / StatAbstractNotifyCommit.java
1 /**
2  * Copyright (c) 2014 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.controller.md.statistics.manager.impl;
10
11 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.TimeUnit;
13 import java.util.concurrent.TimeoutException;
14
15 import org.opendaylight.controller.md.statistics.manager.StatNotifyCommiter;
16 import org.opendaylight.controller.md.statistics.manager.StatRpcMsgManager.TransactionCacheContainer;
17 import org.opendaylight.controller.md.statistics.manager.StatisticsManager;
18 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.binding.NotificationListener;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import com.google.common.base.Optional;
29 import com.google.common.base.Preconditions;
30
31 /**
32  * statistics-manager
33  * org.opendaylight.controller.md.statistics.manager.impl
34  *
35  * StatAbstratNotifiCommiter
36  * Class is abstract implementation for all no Configuration/DataStore DataObjects
37  * and represent common functionality for all DataObject Statistics Commiters.
38  * Class defines contract between DataObject and relevant Statistics NotificationListener.
39  *
40  */
41 public abstract class StatAbstractNotifyCommit<N extends NotificationListener> implements StatNotifyCommiter<N> {
42
43     private static final Logger LOG = LoggerFactory.getLogger(StatAbstractNotifyCommit.class);
44
45     protected final StatisticsManager manager;
46     private ListenerRegistration<NotificationListener> notifyListenerRegistration;
47
48     public StatAbstractNotifyCommit(final StatisticsManager manager,
49             final NotificationProviderService nps) {
50         Preconditions.checkArgument(nps != null, "NotificationProviderService can not be null!");
51         this.manager = Preconditions.checkNotNull(manager, "StatisticManager can not be null!");
52         notifyListenerRegistration = nps.registerNotificationListener(getStatNotificationListener());
53     }
54
55     @Override
56     public void close() {
57         if (notifyListenerRegistration != null) {
58             try {
59                 notifyListenerRegistration.close();
60             }
61             catch (final Exception e) {
62                 LOG.error("Error by stop {} StatNotificationListener.", this.getClass().getSimpleName());
63             }
64             notifyListenerRegistration = null;
65         }
66     }
67
68     /**
69      * Method returns Statistics Notification Listener for relevant DataObject implementation,
70      * which is declared for {@link StatNotifyCommiter} interface.
71      *
72      * @return
73      */
74     protected abstract N getStatNotificationListener();
75
76     /**
77      * PreConfigurationCheck - Node identified by input InstanceIdentifier<Node>
78      * has to be registered in {@link org.opendaylight.controller.md.statistics.manager.StatPermCollector}
79      *
80      * @param InstanceIdentifier<Node> nodeIdent
81      */
82     protected boolean preConfigurationCheck(final InstanceIdentifier<Node> nodeIdent) {
83         Preconditions.checkNotNull(nodeIdent, "FlowCapableNode ident can not be null!");
84         return manager.isProvidedFlowNodeActive(nodeIdent);
85     }
86
87     protected void notifyToCollectNextStatistics(final InstanceIdentifier<Node> nodeIdent) {
88         Preconditions.checkNotNull(nodeIdent, "FlowCapableNode ident can not be null!");
89         manager.collectNextStatistics(nodeIdent);
90     }
91
92     /**
93      * Wrapping Future object call for {@link org.opendaylight.controller.md.statistics.manager.StatRpcMsgManager}
94      * getTransactionCacheContainer with 10sec TimeOut.
95      * Method has returned {@link Optional} which could contains a {@link TransactionCacheContainer}
96      *
97      * @param TransactionId transId
98      * @param NodeId nodeId
99      * @return
100      */
101     protected Optional<TransactionCacheContainer<?>> getTransactionCacheContainer(final TransactionId transId, final NodeId nodeId) {
102         Optional<TransactionCacheContainer<?>> txContainer;
103         try {
104             txContainer = manager.getRpcMsgManager().getTransactionCacheContainer(transId, nodeId).get(10, TimeUnit.SECONDS);
105         }
106         catch (InterruptedException | ExecutionException | TimeoutException e) {
107             LOG.warn("Get TransactionCacheContainer fail!", e);
108             txContainer = Optional.absent();
109         }
110         return txContainer;
111     }
112
113     /**
114      * Wrapping Future object call to {@link org.opendaylight.controller.md.statistics.manager.StatRpcMsgManager}
115      * isExpectedStatistics with 10sec TimeOut.
116      * Method has checked registration for provided {@link TransactionId} and {@link NodeId}
117      *
118      * @param TransactionId transId - Transaction identification
119      * @param NodeId nodeId - Node identification
120      * @return boolean
121      */
122     protected boolean isExpectedStatistics(final TransactionId transId, final NodeId nodeId) {
123         Boolean isExpectedStat = Boolean.FALSE;
124         try {
125             isExpectedStat = manager.getRpcMsgManager().isExpectedStatistics(transId, nodeId).get(10, TimeUnit.SECONDS);
126         }
127         catch (InterruptedException | ExecutionException | TimeoutException e) {
128             LOG.warn("Check Transaction registraion {} fail!", transId, e);
129             return false;
130         }
131         return isExpectedStat.booleanValue();
132     }
133 }
134