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.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;
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;
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;
66 public SalFlowServiceImpl(final RequestContextStack requestContextStack,
67 final DeviceContext deviceContext,
68 final ConvertorExecutor convertorExecutor) {
69 this.deviceContext = deviceContext;
70 flowRemove = new MultiLayerFlowService<>(requestContextStack,
72 RemoveFlowOutput.class,
74 flowAdd = new MultiLayerFlowService<>(requestContextStack,
78 flowUpdate = new MultiLayerFlowService<>(requestContextStack,
80 UpdateFlowOutput.class,
82 flowAddMessage = new SingleLayerFlowService<>(requestContextStack, deviceContext, AddFlowOutput.class);
83 flowUpdateMessage = new SingleLayerFlowService<>(requestContextStack, deviceContext, UpdateFlowOutput.class);
84 flowRemoveMessage = new SingleLayerFlowService<>(requestContextStack, deviceContext, RemoveFlowOutput.class);
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());
97 future = flowAdd.processFlowModInputBuilders(flowAdd.toFlowModInputs(input));
98 Futures.addCallback(future, new AddFlowCallback(input, flowRegistryKey),
99 MoreExecutors.directExecutor());
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());
113 future = flowRemove.processFlowModInputBuilders(flowRemove.toFlowModInputs(input));
114 Futures.addCallback(future, new RemoveFlowCallback(input), MoreExecutors.directExecutor());
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;
127 ListenableFuture<RpcResult<UpdateFlowOutput>> future;
128 if (flowUpdateMessage.canUseSingleLayerSerialization()) {
130 if (!FlowCreatorUtil.canModifyFlow(original, updated, flowUpdateMessage.getVersion())) {
131 final SettableFuture<RpcResult<UpdateFlowOutput>> objectSettableFuture = SettableFuture.create();
133 final ListenableFuture<List<RpcResult<UpdateFlowOutput>>> listListenableFuture =
134 Futures.successfulAsList(flowUpdateMessage.handleServiceCall(input.getOriginalFlow()),
135 flowUpdateMessage.handleServiceCall(input.getUpdatedFlow()));
137 Futures.addCallback(listListenableFuture, new FutureCallback<List<RpcResult<UpdateFlowOutput>>>() {
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());
151 final RpcResultBuilder<UpdateFlowOutput> rpcResultBuilder;
152 if (errors.isEmpty()) {
153 rpcResultBuilder = RpcResultBuilder.success();
155 rpcResultBuilder = RpcResultBuilder.<UpdateFlowOutput>failed().withRpcErrors(errors);
158 objectSettableFuture.set(rpcResultBuilder.build());
162 public void onFailure(final Throwable throwable) {
163 RpcResultBuilder<UpdateFlowOutput> rpcResultBuilder = RpcResultBuilder.failed();
164 objectSettableFuture.set(rpcResultBuilder.build());
166 }, MoreExecutors.directExecutor());
168 future = objectSettableFuture;
170 future = flowUpdateMessage.handleServiceCall(input.getUpdatedFlow());
173 if (!FlowCreatorUtil.canModifyFlow(original, updated, flowUpdate.getVersion())) {
174 // We would need to remove original and add updated.
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());
184 ofFlowModInputs = flowUpdate.toFlowModInputs(updated);
187 allFlowMods.addAll(ofFlowModInputs);
189 future = flowUpdate.processFlowModInputBuilders(allFlowMods);
192 Futures.addCallback(future, new UpdateFlowCallback(input), MoreExecutors.directExecutor());
196 private final class AddFlowCallback implements FutureCallback<RpcResult<AddFlowOutput>> {
197 private final AddFlowInput input;
198 private final FlowRegistryKey flowRegistryKey;
200 private AddFlowCallback(final AddFlowInput input, final FlowRegistryKey flowRegistryKey) {
202 this.flowRegistryKey = flowRegistryKey;
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()));
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);
223 // FIXME: this looks like an atomic operation
224 flowRegistry.appendHistoryFlow(flowId, tableId, FlowGroupStatus.ADDED);
225 flowRegistry.storeDescriptor(flowRegistryKey, flowDescriptor);
227 // FIXME: this looks like an atomic operation
228 flowRegistry.store(flowRegistryKey);
229 flowDescriptor = flowRegistry.retrieveDescriptor(flowRegistryKey);
232 if (LOG.isDebugEnabled()) {
233 LOG.debug("Flow add with id={} finished without error", flowDescriptor.getFlowId().getValue());
238 public void onFailure(final Throwable throwable) {
239 LOG.warn("Service call for adding flow={} failed", input, throwable);
243 private final class RemoveFlowCallback implements FutureCallback<RpcResult<RemoveFlowOutput>> {
244 private final RemoveFlowInput input;
246 private RemoveFlowCallback(final RemoveFlowInput input) {
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);
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);
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);
268 flowRegistry.clearFlowRegistry();
271 if (LOG.isDebugEnabled()) {
272 LOG.debug("Flow remove failed for flow={}, errors={}", input,
273 ErrorUtil.errorsToString(result.getErrors()));
279 public void onFailure(final Throwable throwable) {
280 LOG.warn("Service call for removing flow={} failed", input, throwable);
284 private final class UpdateFlowCallback implements FutureCallback<RpcResult<UpdateFlowOutput>> {
285 private final UpdateFlowInput input;
287 private UpdateFlowCallback(final UpdateFlowInput input) {
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);
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);
311 updatedFlowDescriptor = FlowDescriptorFactory.create(tableId, flowId);
312 } else if (isUpdate) {
313 updatedFlowDescriptor = origFlowDescriptor;
315 deviceFlowRegistry.store(updatedFlowRegistryKey);
316 updatedFlowDescriptor = deviceFlowRegistry.retrieveDescriptor(updatedFlowRegistryKey);
320 deviceFlowRegistry.addMark(origFlowRegistryKey);
321 deviceFlowRegistry.storeDescriptor(updatedFlowRegistryKey, updatedFlowDescriptor);
326 public void onFailure(final Throwable throwable) {
327 LOG.warn("Service call for updating flow={} failed", input, throwable);