2 * Copyright (c) 2016 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
9 package org.opendaylight.openflowplugin.applications.frsync.impl.strategy;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import org.opendaylight.openflowplugin.applications.frsync.ForwardingRulesCommitter;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowTableRef;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
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.UpdateFlowOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlowBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.common.ErrorType;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * Implements {@link ForwardingRulesCommitter} methods for processing add, update and remove of {@link Flow}.
42 public class FlowForwarder implements ForwardingRulesCommitter<Flow, AddFlowOutput, RemoveFlowOutput,
45 private static final Logger LOG = LoggerFactory.getLogger(FlowForwarder.class);
46 private static final String TABLE_ID_MISMATCH = "tableId mismatch";
47 private final SalFlowService salFlowService;
49 public FlowForwarder(final SalFlowService salFlowService) {
50 this.salFlowService = salFlowService;
54 public ListenableFuture<RpcResult<RemoveFlowOutput>> remove(final InstanceIdentifier<Flow> identifier,
55 final Flow removeDataObj,
56 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
57 LOG.trace("Forwarding Flow REMOVE request Tbl id, node Id {} {}",
58 identifier, nodeIdent);
60 final TableKey tableKey = identifier.firstKeyOf(Table.class);
61 if (tableIdValidationPrecondition(tableKey, removeDataObj)) {
62 final RemoveFlowInputBuilder builder = new RemoveFlowInputBuilder(removeDataObj);
63 builder.setFlowRef(new FlowRef(identifier));
64 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
65 builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey)));
67 // always needs to set strict flag into remove-flow input so that
68 // only a flow entry associated with a given flow object will be removed.
69 builder.setStrict(Boolean.TRUE);
70 return salFlowService.removeFlow(builder.build());
72 return RpcResultBuilder.<RemoveFlowOutput>failed()
73 .withError(ErrorType.APPLICATION, TABLE_ID_MISMATCH).buildFuture();
78 public ListenableFuture<RpcResult<UpdateFlowOutput>> update(final InstanceIdentifier<Flow> identifier,
79 final Flow original, final Flow update,
80 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
81 LOG.trace("Forwarding Flow UPDATE request [Tbl id, node Id {} {} {}",
82 identifier, nodeIdent, update);
84 final ListenableFuture<RpcResult<UpdateFlowOutput>> output;
85 final TableKey tableKey = identifier.firstKeyOf(Table.class);
86 if (tableIdValidationPrecondition(tableKey, update)) {
87 final UpdateFlowInputBuilder builder = new UpdateFlowInputBuilder();
89 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
90 builder.setFlowRef(new FlowRef(identifier));
92 // always needs to set strict flag into update-flow input so that
93 // only a flow entry associated with a given flow object is updated.
94 builder.setUpdatedFlow(new UpdatedFlowBuilder(update).setStrict(Boolean.TRUE).build());
95 builder.setOriginalFlow(new OriginalFlowBuilder(original).setStrict(Boolean.TRUE).build());
97 output = salFlowService.updateFlow(builder.build());
99 output = RpcResultBuilder.<UpdateFlowOutput>failed()
100 .withError(ErrorType.APPLICATION, TABLE_ID_MISMATCH).buildFuture();
107 public ListenableFuture<RpcResult<AddFlowOutput>> add(final InstanceIdentifier<Flow> identifier,
108 final Flow addDataObj,
109 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
110 LOG.trace("Forwarding the Flow ADD request [Tbl id, node Id {} {} {}",
111 identifier, nodeIdent, addDataObj);
113 final ListenableFuture<RpcResult<AddFlowOutput>> output;
114 final TableKey tableKey = identifier.firstKeyOf(Table.class);
115 if (tableIdValidationPrecondition(tableKey, addDataObj)) {
116 final AddFlowInputBuilder builder = new AddFlowInputBuilder(addDataObj);
118 builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
119 builder.setFlowRef(new FlowRef(identifier));
120 builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey)));
121 output = salFlowService.addFlow(builder.build());
123 output = RpcResultBuilder.<AddFlowOutput>failed().withError(ErrorType.APPLICATION, TABLE_ID_MISMATCH)
129 private static boolean tableIdValidationPrecondition(final TableKey tableKey, final Flow flow) {
130 requireNonNull(tableKey, "TableKey can not be null or empty!");
131 requireNonNull(flow, "Flow can not be null or empty!");
132 if (!tableKey.getId().equals(flow.getTableId())) {
133 LOG.warn("TableID in URI tableId={} and in payload tableId={} is not same.",
134 flow.getTableId(), tableKey.getId());