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