Merge "OPNFLWPLUG-983 Group and flow removal stats are not reported in order"
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / TableForwarder.java
1 /*
2  * Copyright (c) 2015, 2017 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.frm.impl;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.Futures;
13 import java.util.Collections;
14 import java.util.concurrent.Future;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.infrautils.utils.concurrent.JdkFutures;
19 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
20 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.table.update.OriginalTableBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.table.update.UpdatedTableBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableRef;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
32 import org.opendaylight.yangtools.concepts.ListenerRegistration;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public class TableForwarder extends AbstractListeningCommiter<TableFeatures> {
39
40     private static final Logger LOG = LoggerFactory.getLogger(TableForwarder.class);
41     private ListenerRegistration<TableForwarder> listenerRegistration;
42
43     @SuppressWarnings("IllegalCatch")
44     public TableForwarder(final ForwardingRulesManager manager, final DataBroker db) {
45         super(manager);
46         Preconditions.checkNotNull(db, "DataBroker can not be null!");
47         final DataTreeIdentifier<TableFeatures> treeId = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION,
48                 getWildCardPath());
49
50         try {
51             SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(ForwardingRulesManagerImpl.STARTUP_LOOP_TICK,
52                     ForwardingRulesManagerImpl.STARTUP_LOOP_MAX_RETRIES);
53             listenerRegistration = looper
54                     .loopUntilNoException(() -> db.registerDataTreeChangeListener(treeId, TableForwarder.this));
55         } catch (final Exception e) {
56             LOG.warn("FRM Table DataTreeChangeListener registration fail!");
57             LOG.debug("FRM Table DataTreeChangeListener registration fail ..", e);
58             throw new IllegalStateException("TableForwarder startup fail! System needs restart.", e);
59         }
60     }
61
62     @Override
63     public void close() {
64         if (listenerRegistration != null) {
65             listenerRegistration.close();
66             listenerRegistration = null;
67         }
68     }
69
70     @Override
71     protected InstanceIdentifier<TableFeatures> getWildCardPath() {
72         return InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class)
73                 .child(TableFeatures.class);
74     }
75
76     @Override
77     public void remove(final InstanceIdentifier<TableFeatures> identifier, final TableFeatures removeDataObj,
78             final InstanceIdentifier<FlowCapableNode> nodeIdent) {
79         // DO Nothing
80     }
81
82     @Override
83     public void update(final InstanceIdentifier<TableFeatures> identifier, final TableFeatures original,
84             final TableFeatures update, final InstanceIdentifier<FlowCapableNode> nodeIdent) {
85         LOG.debug("Received the Table Update request [Tbl id, node Id, original, upd" + " " + identifier + " "
86                 + nodeIdent + " " + original + " " + update);
87
88         final TableFeatures originalTableFeatures = original;
89         TableFeatures updatedTableFeatures;
90         if (null == update) {
91             updatedTableFeatures = original;
92         } else {
93             updatedTableFeatures = update;
94         }
95         final UpdateTableInputBuilder builder = new UpdateTableInputBuilder();
96
97         builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
98
99         // TODO: reconsider model - this particular field is not used in service
100         // implementation
101         builder.setTableRef(new TableRef(identifier));
102
103         builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
104
105         builder.setUpdatedTable(
106                 new UpdatedTableBuilder().setTableFeatures(Collections.singletonList(updatedTableFeatures)).build());
107
108         builder.setOriginalTable(
109                 new OriginalTableBuilder().setTableFeatures(Collections.singletonList(originalTableFeatures)).build());
110         LOG.debug("Invoking SalTableService ");
111
112         if (this.provider.getSalTableService() != null) {
113             LOG.debug(" Handle to SalTableServices" + this.provider.getSalTableService());
114         }
115
116         final Future<RpcResult<UpdateTableOutput>> resultFuture =
117                 this.provider.getSalTableService().updateTable(builder.build());
118         JdkFutures.addErrorLogging(resultFuture, LOG, "updateTable");
119
120     }
121
122     @Override
123     public Future<? extends RpcResult<?>> add(final InstanceIdentifier<TableFeatures> identifier,
124             final TableFeatures addDataObj, final InstanceIdentifier<FlowCapableNode> nodeIdent) {
125         return Futures.immediateFuture(null);
126     }
127
128     @Override
129     public void createStaleMarkEntity(InstanceIdentifier<TableFeatures> identifier, TableFeatures del,
130             InstanceIdentifier<FlowCapableNode> nodeIdent) {
131         LOG.debug("NO-OP");
132     }
133
134     @Override
135     public Future<? extends RpcResult<?>> removeWithResult(InstanceIdentifier<TableFeatures> identifier,
136             TableFeatures del, InstanceIdentifier<FlowCapableNode> nodeIdent) {
137         return null;
138     }
139 }