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 javax.annotation.Nonnull;
20 import org.opendaylight.openflowplugin.api.OFConstants;
21 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
22 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
23 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
24 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
25 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
26 import org.opendaylight.openflowplugin.impl.registry.flow.FlowDescriptorFactory;
27 import org.opendaylight.openflowplugin.impl.registry.flow.FlowRegistryKeyFactory;
28 import org.opendaylight.openflowplugin.impl.services.multilayer.MultiLayerFlowService;
29 import org.opendaylight.openflowplugin.impl.services.singlelayer.SingleLayerFlowService;
30 import org.opendaylight.openflowplugin.impl.util.ErrorUtil;
31 import org.opendaylight.openflowplugin.impl.util.FlowCreatorUtil;
32 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
48 import org.opendaylight.yangtools.yang.common.RpcError;
49 import org.opendaylight.yangtools.yang.common.RpcResult;
50 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
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 final MultiLayerFlowService<UpdateFlowOutput> flowUpdate;
57 private final MultiLayerFlowService<AddFlowOutput> flowAdd;
58 private final MultiLayerFlowService<RemoveFlowOutput> flowRemove;
59 private final SingleLayerFlowService<AddFlowOutput> flowAddMessage;
60 private final SingleLayerFlowService<UpdateFlowOutput> flowUpdateMessage;
61 private final SingleLayerFlowService<RemoveFlowOutput> flowRemoveMessage;
62 private final DeviceContext deviceContext;
64 public SalFlowServiceImpl(final RequestContextStack requestContextStack,
65 final DeviceContext deviceContext,
66 final ConvertorExecutor convertorExecutor) {
67 this.deviceContext = deviceContext;
68 flowRemove = new MultiLayerFlowService<>(requestContextStack,
70 RemoveFlowOutput.class,
72 flowAdd = new MultiLayerFlowService<>(requestContextStack,
76 flowUpdate = new MultiLayerFlowService<>(requestContextStack,
78 UpdateFlowOutput.class,
80 flowAddMessage = new SingleLayerFlowService<>(requestContextStack, deviceContext, AddFlowOutput.class);
81 flowUpdateMessage = new SingleLayerFlowService<>(requestContextStack, deviceContext, UpdateFlowOutput.class);
82 flowRemoveMessage = new SingleLayerFlowService<>(requestContextStack, deviceContext, RemoveFlowOutput.class);
86 public Future<RpcResult<AddFlowOutput>> addFlow(final AddFlowInput input) {
87 final FlowRegistryKey flowRegistryKey =
88 FlowRegistryKeyFactory.create(deviceContext.getDeviceInfo().getVersion(), input);
89 final ListenableFuture<RpcResult<AddFlowOutput>> future;
91 if (flowAddMessage.canUseSingleLayerSerialization()) {
92 future = flowAddMessage.handleServiceCall(input);
93 Futures.addCallback(future, new AddFlowCallback(input, flowRegistryKey), MoreExecutors.directExecutor());
95 future = flowAdd.processFlowModInputBuilders(flowAdd.toFlowModInputs(input));
96 Futures.addCallback(future, new AddFlowCallback(input, flowRegistryKey), MoreExecutors.directExecutor());
103 public Future<RpcResult<RemoveFlowOutput>> removeFlow(final RemoveFlowInput input) {
104 final ListenableFuture<RpcResult<RemoveFlowOutput>> future;
106 if (flowRemoveMessage.canUseSingleLayerSerialization()) {
107 future = flowRemoveMessage.handleServiceCall(input);
108 Futures.addCallback(future, new RemoveFlowCallback(input), MoreExecutors.directExecutor());
111 future = flowRemove.processFlowModInputBuilders(flowRemove.toFlowModInputs(input));
112 Futures.addCallback(future, new RemoveFlowCallback(input), MoreExecutors.directExecutor());
119 public Future<RpcResult<UpdateFlowOutput>> updateFlow(final UpdateFlowInput input) {
120 final UpdatedFlow updated = input.getUpdatedFlow();
121 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(@Nonnull 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,
200 final FlowRegistryKey flowRegistryKey) {
202 this.flowRegistryKey = flowRegistryKey;
206 public void onSuccess(@Nonnull final RpcResult<AddFlowOutput> rpcResult) {
207 if (rpcResult.isSuccessful()) {
208 final FlowDescriptor flowDescriptor;
210 if (Objects.nonNull(input.getFlowRef())) {
211 final FlowId flowId = input.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
212 flowDescriptor = FlowDescriptorFactory.create(input.getTableId(), flowId);
213 deviceContext.getDeviceFlowRegistry().storeDescriptor(flowRegistryKey, flowDescriptor);
215 deviceContext.getDeviceFlowRegistry().store(flowRegistryKey);
216 flowDescriptor = deviceContext.getDeviceFlowRegistry().retrieveDescriptor(flowRegistryKey);
219 if (LOG.isDebugEnabled()) {
220 LOG.debug("Flow add with id={} finished without error", flowDescriptor.getFlowId().getValue());
223 if (LOG.isDebugEnabled()) {
224 LOG.debug("Flow add failed for flow={}, errors={}", input,
225 ErrorUtil.errorsToString(rpcResult.getErrors()));
231 public void onFailure(final Throwable throwable) {
232 LOG.warn("Service call for adding flow={} failed, reason: {}", input, throwable);
236 private final class RemoveFlowCallback implements FutureCallback<RpcResult<RemoveFlowOutput>> {
237 private final RemoveFlowInput input;
239 private RemoveFlowCallback(final RemoveFlowInput input) {
244 public void onSuccess(@Nonnull final RpcResult<RemoveFlowOutput> result) {
245 if (result.isSuccessful()) {
246 if (LOG.isDebugEnabled()) {
247 LOG.debug("Flow remove finished without error for flow={}", input);
249 FlowRegistryKey flowRegistryKey =
250 FlowRegistryKeyFactory.create(deviceContext.getDeviceInfo().getVersion(), input);
251 deviceContext.getDeviceFlowRegistry().addMark(flowRegistryKey);
253 if (LOG.isDebugEnabled()) {
254 LOG.debug("Flow remove failed for flow={}, errors={}", input,
255 ErrorUtil.errorsToString(result.getErrors()));
261 public void onFailure(final Throwable throwable) {
262 LOG.warn("Service call for removing flow={} failed, reason: {}", input, throwable);
266 private final class UpdateFlowCallback implements FutureCallback<RpcResult<UpdateFlowOutput>> {
267 private final UpdateFlowInput input;
269 private UpdateFlowCallback(UpdateFlowInput input) {
274 public void onSuccess(@Nonnull final RpcResult<UpdateFlowOutput> updateFlowOutputRpcResult) {
275 final DeviceFlowRegistry deviceFlowRegistry = deviceContext.getDeviceFlowRegistry();
277 final UpdatedFlow updated = input.getUpdatedFlow();
278 final OriginalFlow original = input.getOriginalFlow();
279 final FlowRegistryKey origFlowRegistryKey =
280 FlowRegistryKeyFactory.create(deviceContext.getDeviceInfo().getVersion(), original);
281 final FlowRegistryKey updatedFlowRegistryKey =
282 FlowRegistryKeyFactory.create(deviceContext.getDeviceInfo().getVersion(), updated);
283 final FlowDescriptor origFlowDescriptor = deviceFlowRegistry.retrieveDescriptor(origFlowRegistryKey);
285 final boolean isUpdate = Objects.nonNull(origFlowDescriptor);
286 final FlowDescriptor updatedFlowDescriptor;
288 if (Objects.nonNull(input.getFlowRef())) {
289 updatedFlowDescriptor =
290 FlowDescriptorFactory.create(updated.getTableId(),
291 input.getFlowRef().getValue().firstKeyOf(Flow.class).getId());
294 updatedFlowDescriptor = origFlowDescriptor;
296 deviceFlowRegistry.store(updatedFlowRegistryKey);
297 updatedFlowDescriptor = deviceFlowRegistry.retrieveDescriptor(updatedFlowRegistryKey);
302 deviceFlowRegistry.addMark(origFlowRegistryKey);
303 deviceFlowRegistry.storeDescriptor(updatedFlowRegistryKey, updatedFlowDescriptor);
308 public void onFailure(final Throwable throwable) {
309 LOG.warn("Service call for updating flow={} failed, reason: {}", input, throwable);