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 com.google.common.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.SettableFuture;
15 import java.util.concurrent.Callable;
16 import java.util.concurrent.Future;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
19 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
22 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
23 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlow;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlowBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlowKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowTableRef;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInputBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlowBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
45 import org.opendaylight.yangtools.concepts.ListenerRegistration;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.opendaylight.yangtools.yang.common.RpcResult;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
53 * It implements {@link org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener}
54 * for WildCardedPath to {@link Flow} and ForwardingRulesCommiter interface for methods:
55 * add, update and remove {@link Flow} processing for
56 * {@link org.opendaylight.controller.md.sal.binding.api.DataTreeModification}.
58 public class FlowForwarder extends AbstractListeningCommiter<Flow> {
60 private static final Logger LOG = LoggerFactory.getLogger(FlowForwarder.class);
61 private final DataBroker dataBroker;
62 private ListenerRegistration<FlowForwarder> listenerRegistration;
64 public FlowForwarder (final ForwardingRulesManager manager, final DataBroker db) {
65 super(manager, Flow.class);
66 dataBroker = Preconditions.checkNotNull(db, "DataBroker can not be null!");
67 registrationListener(db);
70 private void registrationListener(final DataBroker db) {
71 final DataTreeIdentifier<Flow> treeId = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, getWildCardPath());
73 SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(ForwardingRulesManagerImpl.STARTUP_LOOP_TICK,
74 ForwardingRulesManagerImpl.STARTUP_LOOP_MAX_RETRIES);
75 listenerRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<FlowForwarder>>() {
77 public ListenerRegistration<FlowForwarder> call() throws Exception {
78 return db.registerDataTreeChangeListener(treeId, FlowForwarder.this);
81 } catch (final Exception e) {
82 LOG.warn("FRM Flow DataTreeChange listener registration fail!");
83 LOG.debug("FRM Flow DataTreeChange listener registration fail ..", e);
84 throw new IllegalStateException("FlowForwarder startup fail! System needs restart.", e);
90 if (listenerRegistration != null) {
92 listenerRegistration.close();
93 } catch (final Exception e) {
94 LOG.warn("Error by stop FRM FlowChangeListener: {}", e.getMessage());
95 LOG.debug("Error by stop FRM FlowChangeListener..", e);
97 listenerRegistration = null;
102 public void remove(final InstanceIdentifier<Flow> identifier,
103 final Flow removeDataObj,
104 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
106 final TableKey tableKey = identifier.firstKeyOf(Table.class, TableKey.class);
107 if (tableIdValidationPrecondition(tableKey, removeDataObj)) {
108 final RemoveFlowInputBuilder builder = new RemoveFlowInputBuilder(removeDataObj);
109 builder.setFlowRef(new FlowRef(identifier));
110 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
111 builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey)));
113 // This method is called only when a given flow object has been
114 // removed from datastore. So FRM always needs to set strict flag
115 // into remove-flow input so that only a flow entry associated with
116 // a given flow object is removed.
117 builder.setTransactionUri(new Uri(provider.getNewTransactionId())).
118 setStrict(Boolean.TRUE);
119 provider.getSalFlowService().removeFlow(builder.build());
126 //TODO: Pull this into ForwardingRulesCommiter and override it here
129 public Future<RpcResult<RemoveFlowOutput>> removeWithResult(final InstanceIdentifier<Flow> identifier,
130 final Flow removeDataObj,
131 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
133 Future<RpcResult<RemoveFlowOutput>> resultFuture = SettableFuture.create();
134 final TableKey tableKey = identifier.firstKeyOf(Table.class, TableKey.class);
135 if (tableIdValidationPrecondition(tableKey, removeDataObj)) {
136 final RemoveFlowInputBuilder builder = new RemoveFlowInputBuilder(removeDataObj);
137 builder.setFlowRef(new FlowRef(identifier));
138 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
139 builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey)));
141 // This method is called only when a given flow object has been
142 // removed from datastore. So FRM always needs to set strict flag
143 // into remove-flow input so that only a flow entry associated with
144 // a given flow object is removed.
145 builder.setTransactionUri(new Uri(provider.getNewTransactionId())).
146 setStrict(Boolean.TRUE);
147 resultFuture = provider.getSalFlowService().removeFlow(builder.build());
156 public void update(final InstanceIdentifier<Flow> identifier,
157 final Flow original, final Flow update,
158 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
160 final TableKey tableKey = identifier.firstKeyOf(Table.class, TableKey.class);
161 if (tableIdValidationPrecondition(tableKey, update)) {
162 final UpdateFlowInputBuilder builder = new UpdateFlowInputBuilder();
164 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
165 builder.setFlowRef(new FlowRef(identifier));
166 builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
168 // This method is called only when a given flow object in datastore
169 // has been updated. So FRM always needs to set strict flag into
170 // update-flow input so that only a flow entry associated with
171 // a given flow object is updated.
172 builder.setUpdatedFlow((new UpdatedFlowBuilder(update)).setStrict(Boolean.TRUE).build());
173 builder.setOriginalFlow((new OriginalFlowBuilder(original)).setStrict(Boolean.TRUE).build());
175 provider.getSalFlowService().updateFlow(builder.build());
180 public Future<RpcResult<AddFlowOutput>> add(
181 final InstanceIdentifier<Flow> identifier, final Flow addDataObj,
182 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
184 Future<RpcResult<AddFlowOutput>> future;
185 final TableKey tableKey = identifier.firstKeyOf(Table.class, TableKey.class);
186 if (tableIdValidationPrecondition(tableKey, addDataObj)) {
187 final AddFlowInputBuilder builder = new AddFlowInputBuilder(addDataObj);
189 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
190 builder.setFlowRef(new FlowRef(identifier));
191 builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey)));
192 builder.setTransactionUri(new Uri(provider.getNewTransactionId()));
193 future = provider.getSalFlowService().addFlow(builder.build());
195 future = Futures.<RpcResult<AddFlowOutput>>immediateFuture(null);
202 public void createStaleMarkEntity(InstanceIdentifier<Flow> identifier, Flow del, InstanceIdentifier<FlowCapableNode> nodeIdent) {
203 LOG.debug("Creating Stale-Mark entry for the switch {} for flow {} ", nodeIdent.toString(), del.toString());
205 StaleFlow staleFlow = makeStaleFlow(identifier, del, nodeIdent);
206 persistStaleFlow(staleFlow, nodeIdent);
213 protected InstanceIdentifier<Flow> getWildCardPath() {
214 return InstanceIdentifier.create(Nodes.class).child(Node.class)
215 .augmentation(FlowCapableNode.class).child(Table.class).child(Flow.class);
218 private static boolean tableIdValidationPrecondition (final TableKey tableKey, final Flow flow) {
219 Preconditions.checkNotNull(tableKey, "TableKey can not be null or empty!");
220 Preconditions.checkNotNull(flow, "Flow can not be null or empty!");
221 if (! tableKey.getId().equals(flow.getTableId())) {
222 LOG.warn("TableID in URI tableId={} and in palyload tableId={} is not same.",
223 flow.getTableId(), tableKey.getId());
229 private StaleFlow makeStaleFlow(InstanceIdentifier<Flow> identifier, Flow del, InstanceIdentifier<FlowCapableNode> nodeIdent){
230 StaleFlowBuilder staleFlowBuilder = new StaleFlowBuilder(del);
231 return staleFlowBuilder.setId(del.getId()).build();
234 private void persistStaleFlow(StaleFlow staleFlow, InstanceIdentifier<FlowCapableNode> nodeIdent){
235 WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
236 writeTransaction.put(LogicalDatastoreType.CONFIGURATION, getStaleFlowInstanceIdentifier(staleFlow, nodeIdent), staleFlow, false);
238 CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTransaction.submit();
239 handleStaleFlowResultFuture(submitFuture);
242 private void handleStaleFlowResultFuture(CheckedFuture<Void, TransactionCommitFailedException> submitFuture) {
243 Futures.addCallback(submitFuture, new FutureCallback<Void>() {
245 public void onSuccess(Void result) {
246 LOG.debug("Stale Flow creation success");
250 public void onFailure(Throwable t) {
251 LOG.error("Stale Flow creation failed {}", t);
257 private InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlow> getStaleFlowInstanceIdentifier(StaleFlow staleFlow, InstanceIdentifier<FlowCapableNode> nodeIdent) {
259 .child(Table.class, new TableKey(staleFlow.getTableId()))
260 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.StaleFlow.class,
261 new StaleFlowKey(new FlowId(staleFlow.getId())));