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