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