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