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 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;
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;
63 public SalFlowServiceImpl(final RequestContextStack requestContextStack,
64 final DeviceContext deviceContext,
65 final ConvertorExecutor convertorExecutor) {
66 this.deviceContext = deviceContext;
67 flowRemove = new MultiLayerFlowService<>(requestContextStack,
69 RemoveFlowOutput.class,
71 flowAdd = new MultiLayerFlowService<>(requestContextStack,
75 flowUpdate = new MultiLayerFlowService<>(requestContextStack,
77 UpdateFlowOutput.class,
79 flowAddMessage = new SingleLayerFlowService<>(requestContextStack, deviceContext, AddFlowOutput.class);
80 flowUpdateMessage = new SingleLayerFlowService<>(requestContextStack, deviceContext, UpdateFlowOutput.class);
81 flowRemoveMessage = new SingleLayerFlowService<>(requestContextStack, deviceContext, RemoveFlowOutput.class);
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;
90 if (flowAddMessage.canUseSingleLayerSerialization()) {
91 future = flowAddMessage.handleServiceCall(input);
92 Futures.addCallback(future, new AddFlowCallback(input, flowRegistryKey), MoreExecutors.directExecutor());
94 future = flowAdd.processFlowModInputBuilders(flowAdd.toFlowModInputs(input));
95 Futures.addCallback(future, new AddFlowCallback(input, flowRegistryKey), MoreExecutors.directExecutor());
102 public Future<RpcResult<RemoveFlowOutput>> removeFlow(final RemoveFlowInput input) {
103 final ListenableFuture<RpcResult<RemoveFlowOutput>> future;
105 if (flowRemoveMessage.canUseSingleLayerSerialization()) {
106 future = flowRemoveMessage.handleServiceCall(input);
107 Futures.addCallback(future, new RemoveFlowCallback(input), MoreExecutors.directExecutor());
110 future = flowRemove.processFlowModInputBuilders(flowRemove.toFlowModInputs(input));
111 Futures.addCallback(future, new RemoveFlowCallback(input), MoreExecutors.directExecutor());
118 public Future<RpcResult<UpdateFlowOutput>> updateFlow(final UpdateFlowInput input) {
119 final UpdatedFlow updated = input.getUpdatedFlow();
120 final OriginalFlow original = input.getOriginalFlow();
122 final List<FlowModInputBuilder> allFlowMods = new ArrayList<>();
123 final List<FlowModInputBuilder> ofFlowModInputs;
125 ListenableFuture<RpcResult<UpdateFlowOutput>> future;
126 if (flowUpdateMessage.canUseSingleLayerSerialization()) {
128 if (!FlowCreatorUtil.canModifyFlow(original, updated, flowUpdateMessage.getVersion())) {
129 final SettableFuture<RpcResult<UpdateFlowOutput>> objectSettableFuture = SettableFuture.create();
131 final ListenableFuture<List<RpcResult<UpdateFlowOutput>>> listListenableFuture =
132 Futures.successfulAsList(flowUpdateMessage.handleServiceCall(input.getOriginalFlow()),
133 flowUpdateMessage.handleServiceCall(input.getUpdatedFlow()));
135 Futures.addCallback(listListenableFuture, new FutureCallback<List<RpcResult<UpdateFlowOutput>>>() {
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());
149 final RpcResultBuilder<UpdateFlowOutput> rpcResultBuilder;
150 if (errors.isEmpty()) {
151 rpcResultBuilder = RpcResultBuilder.success();
153 rpcResultBuilder = RpcResultBuilder.<UpdateFlowOutput>failed().withRpcErrors(errors);
156 objectSettableFuture.set(rpcResultBuilder.build());
160 public void onFailure(final Throwable throwable) {
161 RpcResultBuilder<UpdateFlowOutput> rpcResultBuilder = RpcResultBuilder.failed();
162 objectSettableFuture.set(rpcResultBuilder.build());
164 }, MoreExecutors.directExecutor());
166 future = objectSettableFuture;
168 future = flowUpdateMessage.handleServiceCall(input.getUpdatedFlow());
171 if (!FlowCreatorUtil.canModifyFlow(original, updated, flowUpdate.getVersion())) {
172 // We would need to remove original and add updated.
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());
182 ofFlowModInputs = flowUpdate.toFlowModInputs(updated);
185 allFlowMods.addAll(ofFlowModInputs);
187 future = flowUpdate.processFlowModInputBuilders(allFlowMods);
190 Futures.addCallback(future, new UpdateFlowCallback(input), MoreExecutors.directExecutor());
194 private final class AddFlowCallback implements FutureCallback<RpcResult<AddFlowOutput>> {
195 private final AddFlowInput input;
196 private final FlowRegistryKey flowRegistryKey;
198 private AddFlowCallback(final AddFlowInput input,
199 final FlowRegistryKey flowRegistryKey) {
201 this.flowRegistryKey = flowRegistryKey;
205 public void onSuccess(final RpcResult<AddFlowOutput> rpcResult) {
206 if (rpcResult.isSuccessful()) {
207 final FlowDescriptor flowDescriptor;
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);
214 deviceContext.getDeviceFlowRegistry().store(flowRegistryKey);
215 flowDescriptor = deviceContext.getDeviceFlowRegistry().retrieveDescriptor(flowRegistryKey);
218 if (LOG.isDebugEnabled()) {
219 LOG.debug("Flow add with id={} finished without error", flowDescriptor.getFlowId().getValue());
222 if (LOG.isDebugEnabled()) {
223 LOG.debug("Flow add failed for flow={}, errors={}", input,
224 ErrorUtil.errorsToString(rpcResult.getErrors()));
230 public void onFailure(final Throwable throwable) {
231 LOG.warn("Service call for adding flow={} failed, reason: {}", input, throwable);
235 private final class RemoveFlowCallback implements FutureCallback<RpcResult<RemoveFlowOutput>> {
236 private final RemoveFlowInput input;
238 private RemoveFlowCallback(final RemoveFlowInput input) {
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);
248 FlowRegistryKey flowRegistryKey =
249 FlowRegistryKeyFactory.create(deviceContext.getDeviceInfo().getVersion(), input);
250 deviceContext.getDeviceFlowRegistry().addMark(flowRegistryKey);
252 if (LOG.isDebugEnabled()) {
253 LOG.debug("Flow remove failed for flow={}, errors={}", input,
254 ErrorUtil.errorsToString(result.getErrors()));
260 public void onFailure(final Throwable throwable) {
261 LOG.warn("Service call for removing flow={} failed, reason: {}", input, throwable);
265 private final class UpdateFlowCallback implements FutureCallback<RpcResult<UpdateFlowOutput>> {
266 private final UpdateFlowInput input;
268 private UpdateFlowCallback(UpdateFlowInput input) {
273 public void onSuccess(final RpcResult<UpdateFlowOutput> updateFlowOutputRpcResult) {
274 final DeviceFlowRegistry deviceFlowRegistry = deviceContext.getDeviceFlowRegistry();
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);
284 final boolean isUpdate = Objects.nonNull(origFlowDescriptor);
285 final FlowDescriptor updatedFlowDescriptor;
287 if (Objects.nonNull(input.getFlowRef())) {
288 updatedFlowDescriptor =
289 FlowDescriptorFactory.create(updated.getTableId(),
290 input.getFlowRef().getValue().firstKeyOf(Flow.class).getId());
293 updatedFlowDescriptor = origFlowDescriptor;
295 deviceFlowRegistry.store(updatedFlowRegistryKey);
296 updatedFlowDescriptor = deviceFlowRegistry.retrieveDescriptor(updatedFlowRegistryKey);
301 deviceFlowRegistry.addMark(origFlowRegistryKey);
302 deviceFlowRegistry.storeDescriptor(updatedFlowRegistryKey, updatedFlowDescriptor);
307 public void onFailure(final Throwable throwable) {
308 LOG.warn("Service call for updating flow={} failed, reason: {}", input, throwable);