Switch to MD-SAL APIs
[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.JdkFutures;
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.UpdateTableOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.table.update.OriginalTableBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.table.update.UpdatedTableBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableRef;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class TableForwarder extends AbstractListeningCommiter<TableFeatures> {
36
37     private static final Logger LOG = LoggerFactory.getLogger(TableForwarder.class);
38     private ListenerRegistration<TableForwarder> listenerRegistration;
39
40     public TableForwarder(final ForwardingRulesManager manager, final DataBroker db) {
41         super(manager, db);
42     }
43
44     @SuppressWarnings("IllegalCatch")
45     @Override
46     public void registerListener() {
47         final DataTreeIdentifier<TableFeatures> treeId = DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION,
48                 getWildCardPath());
49
50         try {
51             listenerRegistration = dataBroker.registerDataTreeChangeListener(treeId, TableForwarder.this);
52         } catch (final Exception e) {
53             LOG.warn("FRM Table DataTreeChangeListener registration fail!");
54             LOG.debug("FRM Table DataTreeChangeListener registration fail ..", e);
55             throw new IllegalStateException("TableForwarder startup fail! System needs restart.", e);
56         }
57     }
58
59     @Override
60     public  void deregisterListener() {
61         close();
62     }
63
64     @Override
65     public void close() {
66         if (listenerRegistration != null) {
67             listenerRegistration.close();
68             listenerRegistration = null;
69         }
70     }
71
72     @Override
73     protected InstanceIdentifier<TableFeatures> getWildCardPath() {
74         return InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class)
75                 .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, final TableFeatures original,
86             final TableFeatures update, final InstanceIdentifier<FlowCapableNode> nodeIdent) {
87         LOG.debug("Received the Table Update request [Tbl id, node Id, original, upd {} {} {} {}", identifier,
88                 nodeIdent, original, update);
89
90         final TableFeatures originalTableFeatures = original;
91         TableFeatures updatedTableFeatures;
92         if (null == update) {
93             updatedTableFeatures = original;
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
102         // implementation
103         builder.setTableRef(new TableRef(identifier));
104
105         builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
106
107         builder.setUpdatedTable(
108                 new UpdatedTableBuilder().setTableFeatures(Collections.singletonList(updatedTableFeatures)).build());
109
110         builder.setOriginalTable(
111                 new OriginalTableBuilder().setTableFeatures(Collections.singletonList(originalTableFeatures)).build());
112         LOG.debug("Invoking SalTableService ");
113
114         if (this.provider.getSalTableService() != null) {
115             LOG.debug(" Handle to SalTableServices {}", this.provider.getSalTableService());
116         }
117
118         final Future<RpcResult<UpdateTableOutput>> resultFuture =
119                 this.provider.getSalTableService().updateTable(builder.build());
120         JdkFutures.addErrorLogging(resultFuture, LOG, "updateTable");
121
122     }
123
124     @Override
125     public Future<? extends RpcResult<?>> add(final InstanceIdentifier<TableFeatures> identifier,
126             final TableFeatures addDataObj, final InstanceIdentifier<FlowCapableNode> nodeIdent) {
127         return Futures.immediateFuture(null);
128     }
129
130     @Override
131     public void createStaleMarkEntity(InstanceIdentifier<TableFeatures> identifier, TableFeatures del,
132             InstanceIdentifier<FlowCapableNode> nodeIdent) {
133         LOG.debug("NO-OP");
134     }
135
136     @Override
137     public Future<? extends RpcResult<?>> removeWithResult(InstanceIdentifier<TableFeatures> identifier,
138             TableFeatures del, InstanceIdentifier<FlowCapableNode> nodeIdent) {
139         return null;
140     }
141 }