Merge "Fix issues related to checkstyle enforcement on openflow-impl module"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / sal / SalFlowServiceImpl.java
1 /**
2  * Copyright (c) 2015 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 package org.opendaylight.openflowplugin.impl.services.sal;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.SettableFuture;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Objects;
18 import java.util.concurrent.Future;
19 import org.opendaylight.openflowplugin.api.OFConstants;
20 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
21 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
22 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
23 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
24 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
25 import org.opendaylight.openflowplugin.impl.registry.flow.FlowDescriptorFactory;
26 import org.opendaylight.openflowplugin.impl.registry.flow.FlowRegistryKeyFactory;
27 import org.opendaylight.openflowplugin.impl.services.multilayer.MultiLayerFlowService;
28 import org.opendaylight.openflowplugin.impl.services.singlelayer.SingleLayerFlowService;
29 import org.opendaylight.openflowplugin.impl.util.ErrorUtil;
30 import org.opendaylight.openflowplugin.impl.util.FlowCreatorUtil;
31 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
51 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
52 import org.opendaylight.yangtools.yang.common.RpcError;
53 import org.opendaylight.yangtools.yang.common.RpcResult;
54 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57
58 public class SalFlowServiceImpl implements SalFlowService {
59     private static final Logger LOG = LoggerFactory.getLogger(SalFlowServiceImpl.class);
60     private final MultiLayerFlowService<UpdateFlowOutput> flowUpdate;
61     private final MultiLayerFlowService<AddFlowOutput> flowAdd;
62     private final MultiLayerFlowService<RemoveFlowOutput> flowRemove;
63     private final SingleLayerFlowService<AddFlowOutput> flowAddMessage;
64     private final SingleLayerFlowService<UpdateFlowOutput> flowUpdateMessage;
65     private final SingleLayerFlowService<RemoveFlowOutput> flowRemoveMessage;
66     private final DeviceContext deviceContext;
67
68     public SalFlowServiceImpl(final RequestContextStack requestContextStack,
69                               final DeviceContext deviceContext,
70                               final ConvertorExecutor convertorExecutor) {
71         this.deviceContext = deviceContext;
72         flowRemove = new MultiLayerFlowService<>(requestContextStack,
73                                                  deviceContext,
74                                                  RemoveFlowOutput.class,
75                                                  convertorExecutor);
76         flowAdd = new MultiLayerFlowService<>(requestContextStack,
77                                               deviceContext,
78                                               AddFlowOutput.class,
79                                               convertorExecutor);
80         flowUpdate = new MultiLayerFlowService<>(requestContextStack,
81                                                  deviceContext,
82                                                  UpdateFlowOutput.class,
83                                                  convertorExecutor);
84         flowAddMessage = new SingleLayerFlowService<>(requestContextStack, deviceContext, AddFlowOutput.class);
85         flowUpdateMessage = new SingleLayerFlowService<>(requestContextStack, deviceContext, UpdateFlowOutput.class);
86         flowRemoveMessage = new SingleLayerFlowService<>(requestContextStack, deviceContext, RemoveFlowOutput.class);
87     }
88
89     @Override
90     public Future<RpcResult<AddFlowOutput>> addFlow(final AddFlowInput input) {
91         final FlowRegistryKey flowRegistryKey =
92                 FlowRegistryKeyFactory.create(deviceContext.getDeviceInfo().getVersion(), input);
93         final ListenableFuture<RpcResult<AddFlowOutput>> future;
94
95         if (flowAddMessage.canUseSingleLayerSerialization()) {
96             future = flowAddMessage.handleServiceCall(input);
97             Futures.addCallback(future, new AddFlowCallback(input, flowRegistryKey));
98         } else {
99             future = flowAdd.processFlowModInputBuilders(flowAdd.toFlowModInputs(input));
100             Futures.addCallback(future, new AddFlowCallback(input, flowRegistryKey));
101
102         }
103         return future;
104     }
105
106     @Override
107     public Future<RpcResult<RemoveFlowOutput>> removeFlow(final RemoveFlowInput input) {
108         final ListenableFuture<RpcResult<RemoveFlowOutput>> future;
109
110         if (flowRemoveMessage.canUseSingleLayerSerialization()) {
111             future = flowRemoveMessage.handleServiceCall(input);
112             Futures.addCallback(future, new RemoveFlowCallback(input));
113
114         } else {
115             future = flowRemove.processFlowModInputBuilders(flowRemove.toFlowModInputs(input));
116             Futures.addCallback(future, new RemoveFlowCallback(input));
117         }
118
119         return future;
120     }
121
122     @Override
123     public Future<RpcResult<UpdateFlowOutput>> updateFlow(final UpdateFlowInput input) {
124         final UpdatedFlow updated = input.getUpdatedFlow();
125         final OriginalFlow original = input.getOriginalFlow();
126
127         final List<FlowModInputBuilder> allFlowMods = new ArrayList<>();
128         final List<FlowModInputBuilder> ofFlowModInputs;
129
130         ListenableFuture<RpcResult<UpdateFlowOutput>> future;
131         if (flowUpdateMessage.canUseSingleLayerSerialization()) {
132
133             if (!FlowCreatorUtil.canModifyFlow(original, updated, flowUpdateMessage.getVersion())) {
134                 final SettableFuture<RpcResult<UpdateFlowOutput>> objectSettableFuture = SettableFuture.create();
135
136                 final ListenableFuture<List<RpcResult<UpdateFlowOutput>>> listListenableFuture =
137                         Futures.successfulAsList(flowUpdateMessage.handleServiceCall(input.getOriginalFlow()),
138                                                  flowUpdateMessage.handleServiceCall(input.getUpdatedFlow()));
139
140                 Futures.addCallback(listListenableFuture, new FutureCallback<List<RpcResult<UpdateFlowOutput>>>() {
141                     @Override
142                     public void onSuccess(final List<RpcResult<UpdateFlowOutput>> results) {
143                         final ArrayList<RpcError> errors = new ArrayList();
144                         for (RpcResult<UpdateFlowOutput> flowModResult : results) {
145                             if (flowModResult == null) {
146                                 errors.add(RpcResultBuilder.newError(
147                                         RpcError.ErrorType.PROTOCOL, OFConstants.APPLICATION_TAG,
148                                         "unexpected flowMod result (null) occurred"));
149                             } else if (!flowModResult.isSuccessful()) {
150                                 errors.addAll(flowModResult.getErrors());
151                             }
152                         }
153
154                         final RpcResultBuilder<UpdateFlowOutput> rpcResultBuilder;
155                         if (errors.isEmpty()) {
156                             rpcResultBuilder = RpcResultBuilder.success();
157                         } else {
158                             rpcResultBuilder = RpcResultBuilder.<UpdateFlowOutput>failed().withRpcErrors(errors);
159                         }
160
161                         objectSettableFuture.set(rpcResultBuilder.build());
162                     }
163
164                     @Override
165                     public void onFailure(final Throwable throwable) {
166                         RpcResultBuilder<UpdateFlowOutput> rpcResultBuilder = RpcResultBuilder.failed();
167                         objectSettableFuture.set(rpcResultBuilder.build());
168                     }
169                 });
170
171                 future = objectSettableFuture;
172             } else {
173                 future = flowUpdateMessage.handleServiceCall(input.getUpdatedFlow());
174             }
175         } else {
176             if (!FlowCreatorUtil.canModifyFlow(original, updated, flowUpdate.getVersion())) {
177                 // We would need to remove original and add updated.
178
179                 // remove flow
180                 final RemoveFlowInputBuilder removeflow = new RemoveFlowInputBuilder(original);
181                 final List<FlowModInputBuilder> ofFlowRemoveInput = flowUpdate.toFlowModInputs(removeflow.build());
182                 // remove flow should be the first
183                 allFlowMods.addAll(ofFlowRemoveInput);
184                 final AddFlowInputBuilder addFlowInputBuilder = new AddFlowInputBuilder(updated);
185                 ofFlowModInputs = flowUpdate.toFlowModInputs(addFlowInputBuilder.build());
186             } else {
187                 ofFlowModInputs = flowUpdate.toFlowModInputs(updated);
188             }
189
190             allFlowMods.addAll(ofFlowModInputs);
191
192             future = flowUpdate.processFlowModInputBuilders(allFlowMods);
193         }
194
195         Futures.addCallback(future, new UpdateFlowCallback(input));
196         return future;
197     }
198
199     @VisibleForTesting
200     private static KeyedInstanceIdentifier<Flow, FlowKey> createFlowPath(
201                                                                     FlowDescriptor flowDescriptor,
202                                                                     KeyedInstanceIdentifier<Node, NodeKey> nodePath) {
203         return nodePath.augmentation(FlowCapableNode.class)
204                 .child(Table.class, flowDescriptor.getTableKey())
205                 .child(Flow.class, new FlowKey(flowDescriptor.getFlowId()));
206     }
207
208     private final class AddFlowCallback implements FutureCallback<RpcResult<AddFlowOutput>> {
209         private final AddFlowInput input;
210         private final FlowRegistryKey flowRegistryKey;
211
212         private AddFlowCallback(final AddFlowInput input,
213                                 final FlowRegistryKey flowRegistryKey) {
214             this.input = input;
215             this.flowRegistryKey = flowRegistryKey;
216         }
217
218         @Override
219         public void onSuccess(final RpcResult<AddFlowOutput> rpcResult) {
220             if (rpcResult.isSuccessful()) {
221                 final FlowDescriptor flowDescriptor;
222
223                 if (Objects.nonNull(input.getFlowRef())) {
224                     final FlowId flowId = input.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
225                     flowDescriptor = FlowDescriptorFactory.create(input.getTableId(), flowId);
226                     deviceContext.getDeviceFlowRegistry().storeDescriptor(flowRegistryKey, flowDescriptor);
227                 } else {
228                     deviceContext.getDeviceFlowRegistry().store(flowRegistryKey);
229                     flowDescriptor = deviceContext.getDeviceFlowRegistry().retrieveDescriptor(flowRegistryKey);
230                 }
231
232                 if (LOG.isDebugEnabled()) {
233                     LOG.debug("Flow add with id={} finished without error", flowDescriptor.getFlowId().getValue());
234                 }
235             } else {
236                 if (LOG.isDebugEnabled()) {
237                     LOG.debug("Flow add failed for flow={}, errors={}", input,
238                             ErrorUtil.errorsToString(rpcResult.getErrors()));
239                 }
240             }
241         }
242
243         @Override
244         public void onFailure(final Throwable throwable) {
245             LOG.warn("Service call for adding flow={} failed, reason: {}", input, throwable);
246         }
247     }
248
249     private final class RemoveFlowCallback implements FutureCallback<RpcResult<RemoveFlowOutput>> {
250         private final RemoveFlowInput input;
251
252         private RemoveFlowCallback(final RemoveFlowInput input) {
253             this.input = input;
254         }
255
256         @Override
257         public void onSuccess(final RpcResult<RemoveFlowOutput> result) {
258             if (result.isSuccessful()) {
259                 if (LOG.isDebugEnabled()) {
260                     LOG.debug("Flow remove finished without error for flow={}", input);
261                 }
262                 FlowRegistryKey flowRegistryKey =
263                         FlowRegistryKeyFactory.create(deviceContext.getDeviceInfo().getVersion(), input);
264                 deviceContext.getDeviceFlowRegistry().addMark(flowRegistryKey);
265             } else {
266                 if (LOG.isDebugEnabled()) {
267                     LOG.debug("Flow remove failed for flow={}, errors={}", input,
268                             ErrorUtil.errorsToString(result.getErrors()));
269                 }
270             }
271         }
272
273         @Override
274         public void onFailure(final Throwable throwable) {
275             LOG.warn("Service call for removing flow={} failed, reason: {}", input, throwable);
276         }
277     }
278
279     private final class UpdateFlowCallback implements FutureCallback<RpcResult<UpdateFlowOutput>> {
280         private final UpdateFlowInput input;
281
282         private UpdateFlowCallback(UpdateFlowInput input) {
283             this.input = input;
284         }
285
286         @Override
287         public void onSuccess(final RpcResult<UpdateFlowOutput> updateFlowOutputRpcResult) {
288             final DeviceFlowRegistry deviceFlowRegistry = deviceContext.getDeviceFlowRegistry();
289
290             final UpdatedFlow updated = input.getUpdatedFlow();
291             final OriginalFlow original = input.getOriginalFlow();
292             final FlowRegistryKey origFlowRegistryKey =
293                     FlowRegistryKeyFactory.create(deviceContext.getDeviceInfo().getVersion(), original);
294             final FlowRegistryKey updatedFlowRegistryKey =
295                     FlowRegistryKeyFactory.create(deviceContext.getDeviceInfo().getVersion(), updated);
296             final FlowDescriptor origFlowDescriptor = deviceFlowRegistry.retrieveDescriptor(origFlowRegistryKey);
297
298             final boolean isUpdate = Objects.nonNull(origFlowDescriptor);
299             final FlowDescriptor updatedFlowDescriptor;
300
301             if (Objects.nonNull(input.getFlowRef())) {
302                 updatedFlowDescriptor =
303                         FlowDescriptorFactory.create(updated.getTableId(),
304                                                      input.getFlowRef().getValue().firstKeyOf(Flow.class).getId());
305             } else {
306                 if (isUpdate) {
307                     updatedFlowDescriptor = origFlowDescriptor;
308                 } else {
309                     deviceFlowRegistry.store(updatedFlowRegistryKey);
310                     updatedFlowDescriptor = deviceFlowRegistry.retrieveDescriptor(updatedFlowRegistryKey);
311                 }
312             }
313
314             if (isUpdate) {
315                 deviceFlowRegistry.addMark(origFlowRegistryKey);
316                 deviceFlowRegistry.storeDescriptor(updatedFlowRegistryKey, updatedFlowDescriptor);
317             }
318         }
319
320         @Override
321         public void onFailure(final Throwable throwable) {
322             LOG.warn("Service call for updating flow={} failed, reason: {}", input, throwable);
323         }
324     }
325 }