2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.applications.frm.impl;
10 import com.google.common.base.Preconditions;
11 import java.util.concurrent.Callable;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
16 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
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.flow.inventory.rev130819.tables.TableKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowTableRef;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInputBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlowBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
32 import org.opendaylight.yangtools.concepts.ListenerRegistration;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
39 * It implements {@link org.opendaylight.controller.md.sal.binding.api.DataChangeListener}}
40 * for WildCardedPath to {@link Flow} and ForwardingRulesCommiter interface for methods:
41 * add, update and remove {@link Flow} processing for
42 * {@link org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent}.
44 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
47 public class FlowForwarder extends AbstractListeningCommiter<Flow> {
49 private static final Logger LOG = LoggerFactory.getLogger(FlowForwarder.class);
51 private ListenerRegistration<FlowForwarder> listenerRegistration;
53 public FlowForwarder (final ForwardingRulesManager manager, final DataBroker db) {
54 super(manager, Flow.class);
55 Preconditions.checkNotNull(db, "DataBroker can not be null!");
56 registrationListener(db);
59 private void registrationListener(final DataBroker db) {
60 final DataTreeIdentifier<Flow> treeId = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, getWildCardPath());
62 SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(ForwardingRulesManagerImpl.STARTUP_LOOP_TICK,
63 ForwardingRulesManagerImpl.STARTUP_LOOP_MAX_RETRIES);
64 listenerRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<FlowForwarder>>() {
66 public ListenerRegistration<FlowForwarder> call() throws Exception {
67 return db.registerDataTreeChangeListener(treeId, FlowForwarder.this);
70 } catch (final Exception e) {
71 LOG.warn("FRM Flow DataChange listener registration fail!");
72 LOG.debug("FRM Flow DataChange listener registration fail ..", e);
73 throw new IllegalStateException("FlowForwarder startup fail! System needs restart.", e);
79 if (listenerRegistration != null) {
81 listenerRegistration.close();
82 } catch (final Exception e) {
83 LOG.warn("Error by stop FRM FlowChangeListener: {}", e.getMessage());
84 LOG.debug("Error by stop FRM FlowChangeListener..", e);
86 listenerRegistration = null;
91 public void remove(final InstanceIdentifier<Flow> identifier,
92 final Flow removeDataObj,
93 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
95 final TableKey tableKey = identifier.firstKeyOf(Table.class, TableKey.class);
96 if (tableIdValidationPrecondition(tableKey, removeDataObj)) {
97 final RemoveFlowInputBuilder builder = new RemoveFlowInputBuilder(removeDataObj);
98 builder.setFlowRef(new FlowRef(identifier));
99 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
100 builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey)));
102 // This method is called only when a given flow object has been
103 // removed from datastore. So FRM always needs to set strict flag
104 // into remove-flow input so that only a flow entry associated with
105 // a given flow object is removed.
106 builder.setTransactionUri(new Uri(provider.getNewTransactionId())).
107 setStrict(Boolean.TRUE);
108 provider.getSalFlowService().removeFlow(builder.build());
113 public void update(final InstanceIdentifier<Flow> identifier,
114 final Flow original, final Flow update,
115 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
117 final TableKey tableKey = identifier.firstKeyOf(Table.class, TableKey.class);
118 if (tableIdValidationPrecondition(tableKey, update)) {
119 final UpdateFlowInputBuilder builder = new UpdateFlowInputBuilder();
121 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
122 builder.setFlowRef(new FlowRef(identifier));
123 builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
125 // This method is called only when a given flow object in datastore
126 // has been updated. So FRM always needs to set strict flag into
127 // update-flow input so that only a flow entry associated with
128 // a given flow object is updated.
129 builder.setUpdatedFlow((new UpdatedFlowBuilder(update)).setStrict(Boolean.TRUE).build());
130 builder.setOriginalFlow((new OriginalFlowBuilder(original)).setStrict(Boolean.TRUE).build());
132 provider.getSalFlowService().updateFlow(builder.build());
137 public void add(final InstanceIdentifier<Flow> identifier,
138 final Flow addDataObj,
139 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
141 final TableKey tableKey = identifier.firstKeyOf(Table.class, TableKey.class);
142 if (tableIdValidationPrecondition(tableKey, addDataObj)) {
143 final AddFlowInputBuilder builder = new AddFlowInputBuilder(addDataObj);
145 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
146 builder.setFlowRef(new FlowRef(identifier));
147 builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey)));
148 builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
149 provider.getSalFlowService().addFlow(builder.build());
154 protected InstanceIdentifier<Flow> getWildCardPath() {
155 return InstanceIdentifier.create(Nodes.class).child(Node.class)
156 .augmentation(FlowCapableNode.class).child(Table.class).child(Flow.class);
159 private static boolean tableIdValidationPrecondition (final TableKey tableKey, final Flow flow) {
160 Preconditions.checkNotNull(tableKey, "TableKey can not be null or empty!");
161 Preconditions.checkNotNull(flow, "Flow can not be null or empty!");
162 if (! tableKey.getId().equals(flow.getTableId())) {
163 LOG.warn("TableID in URI tableId={} and in palyload tableId={} is not same.",
164 flow.getTableId(), tableKey.getId());