OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[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,
42         UpdateFlowOutput> {
43
44     private static final Logger LOG = LoggerFactory.getLogger(FlowForwarder.class);
45     private static final String TABLE_ID_MISMATCH = "tableId mismatch";
46     private final SalFlowService salFlowService;
47
48     public FlowForwarder(final SalFlowService salFlowService) {
49         this.salFlowService = salFlowService;
50     }
51
52     @Override
53     public Future<RpcResult<RemoveFlowOutput>> remove(final InstanceIdentifier<Flow> identifier,
54                                                       final Flow removeDataObj,
55                                                       final InstanceIdentifier<FlowCapableNode> nodeIdent) {
56         LOG.trace("Forwarding Flow REMOVE request Tbl id, node Id {} {}",
57                 identifier, nodeIdent);
58
59         final TableKey tableKey = identifier.firstKeyOf(Table.class);
60         if (tableIdValidationPrecondition(tableKey, removeDataObj)) {
61             final RemoveFlowInputBuilder builder = new RemoveFlowInputBuilder(removeDataObj);
62             builder.setFlowRef(new FlowRef(identifier));
63             builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
64             builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey)));
65
66             // always needs to set strict flag into remove-flow input so that
67             // only a flow entry associated with a given flow object will be removed.
68             builder.setStrict(Boolean.TRUE);
69             return salFlowService.removeFlow(builder.build());
70         } else {
71             return RpcResultBuilder.<RemoveFlowOutput>failed()
72                     .withError(RpcError.ErrorType.APPLICATION, TABLE_ID_MISMATCH).buildFuture();
73         }
74     }
75
76     @Override
77     public Future<RpcResult<UpdateFlowOutput>> update(final InstanceIdentifier<Flow> identifier,
78                                                       final Flow original, final Flow update,
79                                                       final InstanceIdentifier<FlowCapableNode> nodeIdent) {
80         LOG.trace("Forwarding Flow UPDATE request [Tbl id, node Id {} {} {}",
81                 identifier, nodeIdent, update);
82
83         final Future<RpcResult<UpdateFlowOutput>> output;
84         final TableKey tableKey = identifier.firstKeyOf(Table.class);
85         if (tableIdValidationPrecondition(tableKey, update)) {
86             final UpdateFlowInputBuilder builder = new UpdateFlowInputBuilder();
87
88             builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
89             builder.setFlowRef(new FlowRef(identifier));
90
91             // always needs to set strict flag into update-flow input so that
92             // only a flow entry associated with a given flow object is updated.
93             builder.setUpdatedFlow(new UpdatedFlowBuilder(update).setStrict(Boolean.TRUE).build());
94             builder.setOriginalFlow(new OriginalFlowBuilder(original).setStrict(Boolean.TRUE).build());
95
96             output = salFlowService.updateFlow(builder.build());
97         } else {
98             output = RpcResultBuilder.<UpdateFlowOutput>failed()
99                     .withError(RpcError.ErrorType.APPLICATION, TABLE_ID_MISMATCH).buildFuture();
100         }
101
102         return output;
103     }
104
105     @Override
106     public Future<RpcResult<AddFlowOutput>> add(final InstanceIdentifier<Flow> identifier,
107                                                 final Flow addDataObj,
108                                                 final InstanceIdentifier<FlowCapableNode> nodeIdent) {
109         LOG.trace("Forwarding the Flow ADD request [Tbl id, node Id {} {} {}",
110                 identifier, nodeIdent, addDataObj);
111
112         final Future<RpcResult<AddFlowOutput>> output;
113         final TableKey tableKey = identifier.firstKeyOf(Table.class);
114         if (tableIdValidationPrecondition(tableKey, addDataObj)) {
115             final AddFlowInputBuilder builder = new AddFlowInputBuilder(addDataObj);
116
117             builder.setNode(new NodeRef(nodeIdent.firstIdentifierOf(Node.class)));
118             builder.setFlowRef(new FlowRef(identifier));
119             builder.setFlowTable(new FlowTableRef(nodeIdent.child(Table.class, tableKey)));
120             output = salFlowService.addFlow(builder.build());
121         } else {
122             output = RpcResultBuilder.<AddFlowOutput>failed().withError(RpcError.ErrorType.APPLICATION,
123                     TABLE_ID_MISMATCH).buildFuture();
124         }
125         return output;
126     }
127
128     private static boolean tableIdValidationPrecondition(final TableKey tableKey, final Flow flow) {
129         Preconditions.checkNotNull(tableKey, "TableKey can not be null or empty!");
130         Preconditions.checkNotNull(flow, "Flow can not be null or empty!");
131         if (!tableKey.getId().equals(flow.getTableId())) {
132             LOG.warn("TableID in URI tableId={} and in payload tableId={} is not same.",
133                     flow.getTableId(), tableKey.getId());
134             return false;
135         }
136         return true;
137     }
138
139 }