1 package org.opendaylight.openflowplugin.applications.frm.impl;
3 import java.util.Collections;
5 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.table.update.OriginalTableBuilder;
6 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.table.update.UpdatedTableBuilder;
7 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
8 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableRef;
9 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInputBuilder;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
12 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
13 import java.util.concurrent.Callable;
14 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import com.google.common.base.Preconditions;
28 public class TableForwarder extends AbstractListeningCommiter<TableFeatures> {
30 private static final Logger LOG = LoggerFactory.getLogger(TableForwarder.class);
32 private ListenerRegistration<TableForwarder> listenerRegistration;
34 public TableForwarder (final ForwardingRulesManager manager, final DataBroker db) {
35 super(manager, TableFeatures.class);
36 Preconditions.checkNotNull(db, "DataBroker can not be null!");
37 final DataTreeIdentifier<TableFeatures> treeId = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, getWildCardPath());
40 SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(ForwardingRulesManagerImpl.STARTUP_LOOP_TICK,
41 ForwardingRulesManagerImpl.STARTUP_LOOP_MAX_RETRIES);
42 listenerRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<TableForwarder>>() {
44 public ListenerRegistration<TableForwarder> call() throws Exception {
45 return db.registerDataTreeChangeListener(treeId, TableForwarder.this);
48 } catch (final Exception e) {
49 LOG.warn("FRM Table DataChange listener registration fail!");
50 LOG.debug("FRM Table DataChange listener registration fail ..", e);
51 throw new IllegalStateException("TableForwarder startup fail! System needs restart.", e);
57 if (listenerRegistration != null) {
59 listenerRegistration.close();
60 } catch (Exception e) {
61 LOG.error("Error by stop FRM TableChangeListener.", e);
63 listenerRegistration = null;
68 protected InstanceIdentifier<TableFeatures> getWildCardPath() {
69 return InstanceIdentifier.create(Nodes.class).child(Node.class)
70 .augmentation(FlowCapableNode.class).child(Table.class).child(TableFeatures.class);
74 public void remove(final InstanceIdentifier<TableFeatures> identifier, final TableFeatures removeDataObj,
75 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
80 public void update(final InstanceIdentifier<TableFeatures> identifier,
81 final TableFeatures original, final TableFeatures update,
82 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
83 LOG.debug( "Received the Table Update request [Tbl id, node Id, original, upd" +
84 " " + identifier + " " + nodeIdent + " " + original + " " + update );
86 final TableFeatures originalTableFeatures = original;
87 TableFeatures updatedTableFeatures ;
89 updatedTableFeatures = original;
91 updatedTableFeatures = update;
93 final UpdateTableInputBuilder builder = new UpdateTableInputBuilder();
95 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
97 InstanceIdentifier<Table> iiToTable = identifier.firstIdentifierOf(Table.class);
98 builder.setTableRef(new TableRef(iiToTable));
100 builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
102 builder.setUpdatedTable(new UpdatedTableBuilder().setTableFeatures(
103 Collections.singletonList(updatedTableFeatures)).build());
105 builder.setOriginalTable(new OriginalTableBuilder().setTableFeatures(
106 Collections.singletonList(originalTableFeatures)).build());
107 LOG.debug( "Invoking SalTableService " ) ;
109 if( this.provider.getSalTableService() != null )
110 LOG.debug( " Handle to SalTableServices" + this.provider.getSalTableService()) ;
111 this.provider.getSalTableService().updateTable(builder.build());
116 public void add(final InstanceIdentifier<TableFeatures> identifier, final TableFeatures addDataObj,
117 final InstanceIdentifier<FlowCapableNode> nodeIdent) {