Merge "BUG-4117: add support of Old Notif. for Flow Statistics"
[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 java.util.Collections;
13 import java.util.concurrent.Callable;
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.rev100924.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
40     private ListenerRegistration<TableForwarder> listenerRegistration;
41
42     public TableForwarder(final ForwardingRulesManager manager, final DataBroker db) {
43         super(manager, TableFeatures.class);
44         Preconditions.checkNotNull(db, "DataBroker can not be null!");
45         final DataTreeIdentifier<TableFeatures> treeId = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, getWildCardPath());
46
47         try {
48             SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(ForwardingRulesManagerImpl.STARTUP_LOOP_TICK,
49                     ForwardingRulesManagerImpl.STARTUP_LOOP_MAX_RETRIES);
50             listenerRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<TableForwarder>>() {
51                 @Override
52                 public ListenerRegistration<TableForwarder> call() throws Exception {
53                     return db.registerDataTreeChangeListener(treeId, TableForwarder.this);
54                 }
55             });
56         } catch (final Exception e) {
57             LOG.warn("FRM Table DataChange listener registration fail!");
58             LOG.debug("FRM Table DataChange listener registration fail ..", e);
59             throw new IllegalStateException("TableForwarder startup fail! System needs restart.", e);
60         }
61     }
62
63     @Override
64     public void close() {
65         if (listenerRegistration != null) {
66             try {
67                 listenerRegistration.close();
68             } catch (Exception e) {
69                 LOG.warn("Error by stop FRM TableChangeListener: {}", e.getMessage());
70                 LOG.debug("Error by stop FRM TableChangeListener..", e);
71             }
72             listenerRegistration = null;
73         }
74     }
75
76     @Override
77     protected InstanceIdentifier<TableFeatures> getWildCardPath() {
78         return InstanceIdentifier.create(Nodes.class).child(Node.class)
79                 .augmentation(FlowCapableNode.class).child(TableFeatures.class);
80     }
81
82     @Override
83     public void remove(final InstanceIdentifier<TableFeatures> identifier, final TableFeatures removeDataObj,
84                        final InstanceIdentifier<FlowCapableNode> nodeIdent) {
85         // DO Nothing
86     }
87
88     @Override
89     public void update(final InstanceIdentifier<TableFeatures> identifier,
90                        final TableFeatures original, final TableFeatures update,
91                        final InstanceIdentifier<FlowCapableNode> nodeIdent) {
92         LOG.debug("Received the Table Update request [Tbl id, node Id, original, upd" +
93                 " " + identifier + " " + nodeIdent + " " + original + " " + update);
94
95         final TableFeatures originalTableFeatures = original;
96         TableFeatures updatedTableFeatures;
97         if (null == update)
98             updatedTableFeatures = original;
99         else
100             updatedTableFeatures = update;
101
102         final UpdateTableInputBuilder builder = new UpdateTableInputBuilder();
103
104         builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
105
106         // TODO: reconsider model - this particular field is not used in service implementation
107         builder.setTableRef(new TableRef(identifier));
108
109         builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
110
111         builder.setUpdatedTable(new UpdatedTableBuilder().setTableFeatures(
112                 Collections.singletonList(updatedTableFeatures)).build());
113
114         builder.setOriginalTable(new OriginalTableBuilder().setTableFeatures(
115                 Collections.singletonList(originalTableFeatures)).build());
116         LOG.debug("Invoking SalTableService ");
117
118         if (this.provider.getSalTableService() != null)
119             LOG.debug(" Handle to SalTableServices" + this.provider.getSalTableService());
120         this.provider.getSalTableService().updateTable(builder.build());
121
122     }
123
124     @Override
125     public void add(final InstanceIdentifier<TableFeatures> identifier, final TableFeatures addDataObj,
126                     final InstanceIdentifier<FlowCapableNode> nodeIdent) {
127         //DO NOthing
128     }
129
130     @Override
131     public void createStaleMarkEntity(InstanceIdentifier<TableFeatures> identifier, TableFeatures del, InstanceIdentifier<FlowCapableNode> nodeIdent) {
132         LOG.debug("NO-OP");
133
134     }
135
136     @Override
137     public Future<? extends RpcResult> removeWithResult(InstanceIdentifier<TableFeatures> identifier, TableFeatures del, InstanceIdentifier<FlowCapableNode> nodeIdent) {
138         return null;
139     }
140
141
142 }