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