Merge "Replace odl-dlux-core with odl-dluxapps-topology"
[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.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.openflowplugin.applications.frm.ForwardingRulesManager;
19 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInputBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.table.update.OriginalTableBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.table.update.UpdatedTableBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableRef;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class TableForwarder extends AbstractListeningCommiter<TableFeatures> {
37
38     private static final Logger LOG = LoggerFactory.getLogger(TableForwarder.class);
39     private ListenerRegistration<TableForwarder> listenerRegistration;
40
41     public TableForwarder(final ForwardingRulesManager manager, final DataBroker db) {
42         super(manager, TableFeatures.class);
43         Preconditions.checkNotNull(db, "DataBroker can not be null!");
44         final DataTreeIdentifier<TableFeatures> treeId = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, getWildCardPath());
45
46         try {
47             SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(ForwardingRulesManagerImpl.STARTUP_LOOP_TICK,
48                     ForwardingRulesManagerImpl.STARTUP_LOOP_MAX_RETRIES);
49             listenerRegistration = looper.loopUntilNoException(() -> db.registerDataTreeChangeListener(treeId, TableForwarder.this));
50         } catch (final Exception e) {
51             LOG.warn("FRM Table DataTreeChangeListener registration fail!");
52             LOG.debug("FRM Table DataTreeChangeListener registration fail ..", e);
53             throw new IllegalStateException("TableForwarder startup fail! System needs restart.", e);
54         }
55     }
56
57     @Override
58     public void close() {
59         if (listenerRegistration != null) {
60             try {
61                 listenerRegistration.close();
62             } catch (Exception e) {
63                 LOG.warn("Error by stop FRM TableChangeListener: {}", e.getMessage());
64                 LOG.debug("Error by stop FRM TableChangeListener..", e);
65             }
66             listenerRegistration = null;
67         }
68     }
69
70     @Override
71     protected InstanceIdentifier<TableFeatures> getWildCardPath() {
72         return InstanceIdentifier.create(Nodes.class).child(Node.class)
73                 .augmentation(FlowCapableNode.class).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,
84                        final TableFeatures original, final TableFeatures update,
85                        final InstanceIdentifier<FlowCapableNode> nodeIdent) {
86         LOG.debug("Received the Table Update request [Tbl id, node Id, original, upd" +
87                 " " + identifier + " " + nodeIdent + " " + original + " " + update);
88
89         final TableFeatures originalTableFeatures = original;
90         TableFeatures updatedTableFeatures;
91         if (null == update) {
92             updatedTableFeatures = original;
93         }
94         else {
95             updatedTableFeatures = update;
96         }
97         final UpdateTableInputBuilder builder = new UpdateTableInputBuilder();
98
99         builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
100
101         // TODO: reconsider model - this particular field is not used in service implementation
102         builder.setTableRef(new TableRef(identifier));
103
104         builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
105
106         builder.setUpdatedTable(new UpdatedTableBuilder().setTableFeatures(
107                 Collections.singletonList(updatedTableFeatures)).build());
108
109         builder.setOriginalTable(new OriginalTableBuilder().setTableFeatures(
110                 Collections.singletonList(originalTableFeatures)).build());
111         LOG.debug("Invoking SalTableService ");
112
113         if (this.provider.getSalTableService() != null)
114             LOG.debug(" Handle to SalTableServices" + this.provider.getSalTableService());
115         this.provider.getSalTableService().updateTable(builder.build());
116
117     }
118
119     @Override
120     public Future<? extends RpcResult<?>> add(
121         final InstanceIdentifier<TableFeatures> identifier,
122         final TableFeatures addDataObj,
123         final InstanceIdentifier<FlowCapableNode> nodeIdent) {
124         return Futures.immediateFuture(null);
125     }
126
127     @Override
128     public void createStaleMarkEntity(InstanceIdentifier<TableFeatures> identifier, TableFeatures del,
129             InstanceIdentifier<FlowCapableNode> nodeIdent) {
130         LOG.debug("NO-OP");
131
132     }
133
134     @Override
135     public Future<? extends RpcResult<?>> removeWithResult(InstanceIdentifier<TableFeatures> identifier,
136             TableFeatures del, InstanceIdentifier<FlowCapableNode> nodeIdent) {
137         return null;
138     }
139
140
141 }