OPNFLWPLUG-985: Service recovery for openfplowplugin
[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.util.concurrent.Futures;
12 import java.util.Collections;
13 import java.util.concurrent.Future;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.infrautils.utils.concurrent.JdkFutures;
18 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutput;
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, db);
43     }
44
45     @SuppressWarnings("IllegalCatch")
46     @Override
47     public void registerListener() {
48         final DataTreeIdentifier<TableFeatures> treeId = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION,
49                 getWildCardPath());
50
51         try {
52             listenerRegistration = dataBroker.registerDataTreeChangeListener(treeId, TableForwarder.this);
53         } catch (final Exception e) {
54             LOG.warn("FRM Table DataTreeChangeListener registration fail!");
55             LOG.debug("FRM Table DataTreeChangeListener registration fail ..", e);
56             throw new IllegalStateException("TableForwarder startup fail! System needs restart.", e);
57         }
58     }
59
60     @Override
61     public  void deregisterListener() {
62         close();
63     }
64
65     @Override
66     public void close() {
67         if (listenerRegistration != null) {
68             listenerRegistration.close();
69             listenerRegistration = null;
70         }
71     }
72
73     @Override
74     protected InstanceIdentifier<TableFeatures> getWildCardPath() {
75         return InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class)
76                 .child(TableFeatures.class);
77     }
78
79     @Override
80     public void remove(final InstanceIdentifier<TableFeatures> identifier, final TableFeatures removeDataObj,
81             final InstanceIdentifier<FlowCapableNode> nodeIdent) {
82         // DO Nothing
83     }
84
85     @Override
86     public void update(final InstanceIdentifier<TableFeatures> identifier, final TableFeatures original,
87             final TableFeatures update, final InstanceIdentifier<FlowCapableNode> nodeIdent) {
88         LOG.debug("Received the Table Update request [Tbl id, node Id, original, upd" + " " + identifier + " "
89                 + nodeIdent + " " + original + " " + update);
90
91         final TableFeatures originalTableFeatures = original;
92         TableFeatures updatedTableFeatures;
93         if (null == update) {
94             updatedTableFeatures = original;
95         } else {
96             updatedTableFeatures = update;
97         }
98         final UpdateTableInputBuilder builder = new UpdateTableInputBuilder();
99
100         builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
101
102         // TODO: reconsider model - this particular field is not used in service
103         // implementation
104         builder.setTableRef(new TableRef(identifier));
105
106         builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
107
108         builder.setUpdatedTable(
109                 new UpdatedTableBuilder().setTableFeatures(Collections.singletonList(updatedTableFeatures)).build());
110
111         builder.setOriginalTable(
112                 new OriginalTableBuilder().setTableFeatures(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         }
118
119         final Future<RpcResult<UpdateTableOutput>> resultFuture =
120                 this.provider.getSalTableService().updateTable(builder.build());
121         JdkFutures.addErrorLogging(resultFuture, LOG, "updateTable");
122
123     }
124
125     @Override
126     public Future<? extends RpcResult<?>> add(final InstanceIdentifier<TableFeatures> identifier,
127             final TableFeatures addDataObj, final InstanceIdentifier<FlowCapableNode> nodeIdent) {
128         return Futures.immediateFuture(null);
129     }
130
131     @Override
132     public void createStaleMarkEntity(InstanceIdentifier<TableFeatures> identifier, TableFeatures del,
133             InstanceIdentifier<FlowCapableNode> nodeIdent) {
134         LOG.debug("NO-OP");
135     }
136
137     @Override
138     public Future<? extends RpcResult<?>> removeWithResult(InstanceIdentifier<TableFeatures> identifier,
139             TableFeatures del, InstanceIdentifier<FlowCapableNode> nodeIdent) {
140         return null;
141     }
142 }