Merge "Resolution for Bug 3957"
[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 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.openflowplugin.applications.frm.ForwardingRulesManager;
18 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
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.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
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(new Callable<ListenerRegistration<TableForwarder>>() {
50                 @Override
51                 public ListenerRegistration<TableForwarder> call() throws Exception {
52                     return db.registerDataTreeChangeListener(treeId, TableForwarder.this);
53                 }
54             });
55         } catch (final Exception e) {
56             LOG.warn("FRM Table DataChange listener registration fail!");
57             LOG.debug("FRM Table DataChange listener registration fail ..", e);
58             throw new IllegalStateException("TableForwarder startup fail! System needs restart.", e);
59         }
60     }
61
62     @Override
63     public void close() {
64         if (listenerRegistration != null) {
65             try {
66                 listenerRegistration.close();
67             } catch (Exception e) {
68                 LOG.warn("Error by stop FRM TableChangeListener: {}", e.getMessage());
69                 LOG.debug("Error by stop FRM TableChangeListener..", e);
70             }
71             listenerRegistration = null;
72         }
73     }
74
75     @Override
76     protected InstanceIdentifier<TableFeatures> getWildCardPath() {
77         return InstanceIdentifier.create(Nodes.class).child(Node.class)
78                 .augmentation(FlowCapableNode.class).child(Table.class).child(TableFeatures.class);
79     }
80
81     @Override
82     public void remove(final InstanceIdentifier<TableFeatures> identifier, final TableFeatures removeDataObj,
83                        final InstanceIdentifier<FlowCapableNode> nodeIdent) {
84       // DO Nothing
85     }
86
87     @Override
88     public void update(final InstanceIdentifier<TableFeatures> identifier,
89                        final TableFeatures original, final TableFeatures update,
90                        final InstanceIdentifier<FlowCapableNode> nodeIdent) {
91         LOG.debug( "Received the Table Update request [Tbl id, node Id, original, upd" +
92                        " " + identifier + " " + nodeIdent + " " + original + " " + update );
93
94         final TableFeatures originalTableFeatures = original;
95         TableFeatures updatedTableFeatures ;
96         if( null == update)
97           updatedTableFeatures = original;
98         else
99           updatedTableFeatures = update;
100
101         final UpdateTableInputBuilder builder = new UpdateTableInputBuilder();
102
103         builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
104
105         InstanceIdentifier<Table> iiToTable = identifier.firstIdentifierOf(Table.class);
106         builder.setTableRef(new TableRef(iiToTable));
107
108         builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
109
110         builder.setUpdatedTable(new UpdatedTableBuilder().setTableFeatures(
111                 Collections.singletonList(updatedTableFeatures)).build());
112
113         builder.setOriginalTable(new OriginalTableBuilder().setTableFeatures(
114                 Collections.singletonList(originalTableFeatures)).build());
115         LOG.debug( "Invoking SalTableService " ) ;
116
117         if( this.provider.getSalTableService() != null )
118                 LOG.debug( " Handle to SalTableServices" + this.provider.getSalTableService()) ;
119         this.provider.getSalTableService().updateTable(builder.build());
120
121     }
122
123     @Override
124     public void add(final InstanceIdentifier<TableFeatures> identifier, final TableFeatures addDataObj,
125                     final InstanceIdentifier<FlowCapableNode> nodeIdent) {
126        //DO NOthing
127     }
128
129
130 }