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