2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.services.sal;
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;
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;
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;
65 public SalFlowServiceImpl(final RequestContextStack requestContextStack,
66 final DeviceContext deviceContext,
67 final ConvertorExecutor convertorExecutor) {
68 this.deviceContext = deviceContext;
69 flowRemove = new MultiLayerFlowService<>(requestContextStack,
71 RemoveFlowOutput.class,
73 flowAdd = new MultiLayerFlowService<>(requestContextStack,
77 flowUpdate = new MultiLayerFlowService<>(requestContextStack,
79 UpdateFlowOutput.class,
81 flowAddMessage = new SingleLayerFlowService<>(requestContextStack, deviceContext, AddFlowOutput.class);
82 flowUpdateMessage = new SingleLayerFlowService<>(requestContextStack, deviceContext, UpdateFlowOutput.class);
83 flowRemoveMessage = new SingleLayerFlowService<>(requestContextStack, deviceContext, RemoveFlowOutput.class);
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());
96 future = flowAdd.processFlowModInputBuilders(flowAdd.toFlowModInputs(input));
97 Futures.addCallback(future, new AddFlowCallback(input, flowRegistryKey),
98 MoreExecutors.directExecutor());
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());
112 future = flowRemove.processFlowModInputBuilders(flowRemove.toFlowModInputs(input));
113 Futures.addCallback(future, new RemoveFlowCallback(input), MoreExecutors.directExecutor());
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;
126 ListenableFuture<RpcResult<UpdateFlowOutput>> future;
127 if (flowUpdateMessage.canUseSingleLayerSerialization()) {
129 if (!FlowCreatorUtil.canModifyFlow(original, updated, flowUpdateMessage.getVersion())) {
130 final SettableFuture<RpcResult<UpdateFlowOutput>> objectSettableFuture = SettableFuture.create();
132 final ListenableFuture<List<RpcResult<UpdateFlowOutput>>> listListenableFuture =
133 Futures.successfulAsList(flowUpdateMessage.handleServiceCall(input.getOriginalFlow()),
134 flowUpdateMessage.handleServiceCall(input.getUpdatedFlow()));
136 Futures.addCallback(listListenableFuture, new FutureCallback<List<RpcResult<UpdateFlowOutput>>>() {
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());
150 final RpcResultBuilder<UpdateFlowOutput> rpcResultBuilder;
151 if (errors.isEmpty()) {
152 rpcResultBuilder = RpcResultBuilder.success();
154 rpcResultBuilder = RpcResultBuilder.<UpdateFlowOutput>failed().withRpcErrors(errors);
157 objectSettableFuture.set(rpcResultBuilder.build());
161 public void onFailure(final Throwable throwable) {
162 RpcResultBuilder<UpdateFlowOutput> rpcResultBuilder = RpcResultBuilder.failed();
163 objectSettableFuture.set(rpcResultBuilder.build());
165 }, MoreExecutors.directExecutor());
167 future = objectSettableFuture;
169 future = flowUpdateMessage.handleServiceCall(input.getUpdatedFlow());
172 if (!FlowCreatorUtil.canModifyFlow(original, updated, flowUpdate.getVersion())) {
173 // We would need to remove original and add updated.
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());
183 ofFlowModInputs = flowUpdate.toFlowModInputs(updated);
186 allFlowMods.addAll(ofFlowModInputs);
188 future = flowUpdate.processFlowModInputBuilders(allFlowMods);
191 Futures.addCallback(future, new UpdateFlowCallback(input), MoreExecutors.directExecutor());
195 private final class AddFlowCallback implements FutureCallback<RpcResult<AddFlowOutput>> {
196 private final AddFlowInput input;
197 private final FlowRegistryKey flowRegistryKey;
199 private AddFlowCallback(final AddFlowInput input, final FlowRegistryKey flowRegistryKey) {
201 this.flowRegistryKey = flowRegistryKey;
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()));
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);
222 // FIXME: this looks like an atomic operation
223 flowRegistry.appendHistoryFlow(flowId, tableId, FlowGroupStatus.ADDED);
224 flowRegistry.storeDescriptor(flowRegistryKey, flowDescriptor);
226 // FIXME: this looks like an atomic operation
227 flowRegistry.store(flowRegistryKey);
228 flowDescriptor = flowRegistry.retrieveDescriptor(flowRegistryKey);
231 if (LOG.isDebugEnabled()) {
232 LOG.debug("Flow add with id={} finished without error", flowDescriptor.getFlowId().getValue());
237 public void onFailure(final Throwable throwable) {
238 LOG.warn("Service call for adding flow={} failed", input, throwable);
242 private final class RemoveFlowCallback implements FutureCallback<RpcResult<RemoveFlowOutput>> {
243 private final RemoveFlowInput input;
245 private RemoveFlowCallback(final RemoveFlowInput input) {
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);
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);
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);
267 flowRegistry.clearFlowRegistry();
270 if (LOG.isDebugEnabled()) {
271 LOG.debug("Flow remove failed for flow={}, errors={}", input,
272 ErrorUtil.errorsToString(result.getErrors()));
278 public void onFailure(final Throwable throwable) {
279 LOG.warn("Service call for removing flow={} failed", input, throwable);
283 private final class UpdateFlowCallback implements FutureCallback<RpcResult<UpdateFlowOutput>> {
284 private final UpdateFlowInput input;
286 private UpdateFlowCallback(final UpdateFlowInput input) {
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);
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);
310 updatedFlowDescriptor = FlowDescriptorFactory.create(tableId, flowId);
311 } else if (isUpdate) {
312 updatedFlowDescriptor = origFlowDescriptor;
314 deviceFlowRegistry.store(updatedFlowRegistryKey);
315 updatedFlowDescriptor = deviceFlowRegistry.retrieveDescriptor(updatedFlowRegistryKey);
319 deviceFlowRegistry.addMark(origFlowRegistryKey);
320 deviceFlowRegistry.storeDescriptor(updatedFlowRegistryKey, updatedFlowDescriptor);
325 public void onFailure(final Throwable throwable) {
326 LOG.warn("Service call for updating flow={} failed", input, throwable);