Merge "SONAR TD - Remove unused fields, fix naming"
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / impl / strategy / FlowForwarder.java
1 /*
2  * Copyright (c) 2016 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.frsync.impl.strategy;
10
11 import com.google.common.base.Preconditions;
12 import java.util.concurrent.Future;
13 import org.opendaylight.openflowplugin.applications.frsync.ForwardingRulesCommitter;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowTableRef;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
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.Node;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.common.RpcError;
33 import org.opendaylight.yangtools.yang.common.RpcResult;
34 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * Implements {@link ForwardingRulesCommitter} methods for processing add, update and remove of {@link Flow}.
40  */
41 public class FlowForwarder implements ForwardingRulesCommitter<Flow, AddFlowOutput, RemoveFlowOutput, UpdateFlowOutput> {
42
43     private static final Logger LOG = LoggerFactory.getLogger(FlowForwarder.class);
44     private static final String TABLE_ID_MISMATCH = "tableId mismatch";
45     private final SalFlowService salFlowService;
46
47     public FlowForwarder(final SalFlowService salFlowService) {
48         this.salFlowService = salFlowService;
49     }
50
51     @Override
52     public Future<RpcResult<RemoveFlowOutput>> remove(final InstanceIdentifier<Flow> identifier,
53                                                       final Flow removeDataObj,
54                                                       final InstanceIdentifier<FlowCapableNode> nodeIdent) {
55         LOG.trace("Forwarding Flow REMOVE request Tbl id, node Id {} {}",
56                 identifier, nodeIdent);
57
58         final TableKey tableKey = identifier.firstKeyOf(Table.class, TableKey.class);
59         if (tableIdValidationPrecondition(tableKey, removeDataObj)) {
60             final RemoveFlowInputBuilder builder = new RemoveFlowInputBuilder(removeDataObj);
61             builder.setFlowRef(new FlowRef(identifier));
62             builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
63             builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey)));
64
65             // always needs to set strict flag into remove-flow input so that
66             // only a flow entry associated with a given flow object will be removed.
67             builder.setStrict(Boolean.TRUE);
68             return salFlowService.removeFlow(builder.build());
69         } else {
70             return RpcResultBuilder.<RemoveFlowOutput>failed()
71                     .withError(RpcError.ErrorType.APPLICATION, TABLE_ID_MISMATCH).buildFuture();
72         }
73     }
74
75     @Override
76     public Future<RpcResult<UpdateFlowOutput>> update(final InstanceIdentifier<Flow> identifier,
77                                                       final Flow original, final Flow update,
78                                                       final InstanceIdentifier<FlowCapableNode> nodeIdent) {
79         LOG.trace("Forwarding Flow UPDATE request [Tbl id, node Id {} {} {}",
80                 identifier, nodeIdent, update);
81
82         final Future<RpcResult<UpdateFlowOutput>> output;
83         final TableKey tableKey = identifier.firstKeyOf(Table.class, TableKey.class);
84         if (tableIdValidationPrecondition(tableKey, update)) {
85             final UpdateFlowInputBuilder builder = new UpdateFlowInputBuilder();
86
87             builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
88             builder.setFlowRef(new FlowRef(identifier));
89
90             // always needs to set strict flag into update-flow input so that
91             // only a flow entry associated with a given flow object is updated.
92             builder.setUpdatedFlow((new UpdatedFlowBuilder(update)).setStrict(Boolean.TRUE).build());
93             builder.setOriginalFlow((new OriginalFlowBuilder(original)).setStrict(Boolean.TRUE).build());
94
95             output = salFlowService.updateFlow(builder.build());
96         } else {
97             output = RpcResultBuilder.<UpdateFlowOutput>failed()
98                     .withError(RpcError.ErrorType.APPLICATION, TABLE_ID_MISMATCH).buildFuture();
99         }
100
101         return output;
102     }
103
104     @Override
105     public Future<RpcResult<AddFlowOutput>> add(final InstanceIdentifier<Flow> identifier,
106                                                 final Flow addDataObj,
107                                                 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
108         LOG.trace("Forwarding the Flow ADD request [Tbl id, node Id {} {} {}",
109                 identifier, nodeIdent, addDataObj);
110
111         final Future<RpcResult<AddFlowOutput>> output;
112         final TableKey tableKey = identifier.firstKeyOf(Table.class, TableKey.class);
113         if (tableIdValidationPrecondition(tableKey, addDataObj)) {
114             final AddFlowInputBuilder builder = new AddFlowInputBuilder(addDataObj);
115
116             builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
117             builder.setFlowRef(new FlowRef(identifier));
118             builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey)));
119             output = salFlowService.addFlow(builder.build());
120         } else {
121             output = RpcResultBuilder.<AddFlowOutput>failed().withError(RpcError.ErrorType.APPLICATION, TABLE_ID_MISMATCH).buildFuture();
122         }
123         return output;
124     }
125
126     private static boolean tableIdValidationPrecondition(final TableKey tableKey, final Flow flow) {
127         Preconditions.checkNotNull(tableKey, "TableKey can not be null or empty!");
128         Preconditions.checkNotNull(flow, "Flow can not be null or empty!");
129         if (!tableKey.getId().equals(flow.getTableId())) {
130             LOG.warn("TableID in URI tableId={} and in payload tableId={} is not same.",
131                     flow.getTableId(), tableKey.getId());
132             return false;
133         }
134         return true;
135     }
136
137 }