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