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 javax.annotation.Nonnull;
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.service.rev130819.AddFlowInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
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.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 public class SalFlowServiceImpl implements SalFlowService {
53 private static final Logger LOG = LoggerFactory.getLogger(SalFlowServiceImpl.class);
54 private final MultiLayerFlowService<UpdateFlowOutput> flowUpdate;
55 private final MultiLayerFlowService<AddFlowOutput> flowAdd;
56 private final MultiLayerFlowService<RemoveFlowOutput> flowRemove;
57 private final SingleLayerFlowService<AddFlowOutput> flowAddMessage;
58 private final SingleLayerFlowService<UpdateFlowOutput> flowUpdateMessage;
59 private final SingleLayerFlowService<RemoveFlowOutput> flowRemoveMessage;
60 private final DeviceContext deviceContext;
62 public SalFlowServiceImpl(final RequestContextStack requestContextStack,
63 final DeviceContext deviceContext,
64 final ConvertorExecutor convertorExecutor) {
65 this.deviceContext = deviceContext;
66 flowRemove = new MultiLayerFlowService<>(requestContextStack,
68 RemoveFlowOutput.class,
70 flowAdd = new MultiLayerFlowService<>(requestContextStack,
74 flowUpdate = new MultiLayerFlowService<>(requestContextStack,
76 UpdateFlowOutput.class,
78 flowAddMessage = new SingleLayerFlowService<>(requestContextStack, deviceContext, AddFlowOutput.class);
79 flowUpdateMessage = new SingleLayerFlowService<>(requestContextStack, deviceContext, UpdateFlowOutput.class);
80 flowRemoveMessage = new SingleLayerFlowService<>(requestContextStack, deviceContext, RemoveFlowOutput.class);
84 public ListenableFuture<RpcResult<AddFlowOutput>> addFlow(final AddFlowInput input) {
85 final FlowRegistryKey flowRegistryKey =
86 FlowRegistryKeyFactory.create(deviceContext.getDeviceInfo().getVersion(), input);
87 final ListenableFuture<RpcResult<AddFlowOutput>> future;
89 if (flowAddMessage.canUseSingleLayerSerialization()) {
90 future = flowAddMessage.handleServiceCall(input);
91 Futures.addCallback(future, new AddFlowCallback(input, flowRegistryKey), MoreExecutors.directExecutor());
93 future = flowAdd.processFlowModInputBuilders(flowAdd.toFlowModInputs(input));
94 Futures.addCallback(future, new AddFlowCallback(input, flowRegistryKey), MoreExecutors.directExecutor());
101 public ListenableFuture<RpcResult<RemoveFlowOutput>> removeFlow(final RemoveFlowInput input) {
102 final ListenableFuture<RpcResult<RemoveFlowOutput>> future;
104 if (flowRemoveMessage.canUseSingleLayerSerialization()) {
105 future = flowRemoveMessage.handleServiceCall(input);
106 Futures.addCallback(future, new RemoveFlowCallback(input), MoreExecutors.directExecutor());
109 future = flowRemove.processFlowModInputBuilders(flowRemove.toFlowModInputs(input));
110 Futures.addCallback(future, new RemoveFlowCallback(input), MoreExecutors.directExecutor());
117 public ListenableFuture<RpcResult<UpdateFlowOutput>> updateFlow(final UpdateFlowInput input) {
118 final UpdatedFlow updated = input.getUpdatedFlow();
119 final OriginalFlow original = input.getOriginalFlow();
121 final List<FlowModInputBuilder> allFlowMods = new ArrayList<>();
122 final List<FlowModInputBuilder> ofFlowModInputs;
124 ListenableFuture<RpcResult<UpdateFlowOutput>> future;
125 if (flowUpdateMessage.canUseSingleLayerSerialization()) {
127 if (!FlowCreatorUtil.canModifyFlow(original, updated, flowUpdateMessage.getVersion())) {
128 final SettableFuture<RpcResult<UpdateFlowOutput>> objectSettableFuture = SettableFuture.create();
130 final ListenableFuture<List<RpcResult<UpdateFlowOutput>>> listListenableFuture =
131 Futures.successfulAsList(flowUpdateMessage.handleServiceCall(input.getOriginalFlow()),
132 flowUpdateMessage.handleServiceCall(input.getUpdatedFlow()));
134 Futures.addCallback(listListenableFuture, new FutureCallback<List<RpcResult<UpdateFlowOutput>>>() {
136 public void onSuccess(@Nonnull final List<RpcResult<UpdateFlowOutput>> results) {
137 final ArrayList<RpcError> errors = new ArrayList();
138 for (RpcResult<UpdateFlowOutput> flowModResult : results) {
139 if (flowModResult == null) {
140 errors.add(RpcResultBuilder.newError(
141 RpcError.ErrorType.PROTOCOL, OFConstants.APPLICATION_TAG,
142 "unexpected flowMod result (null) occurred"));
143 } else if (!flowModResult.isSuccessful()) {
144 errors.addAll(flowModResult.getErrors());
148 final RpcResultBuilder<UpdateFlowOutput> rpcResultBuilder;
149 if (errors.isEmpty()) {
150 rpcResultBuilder = RpcResultBuilder.success();
152 rpcResultBuilder = RpcResultBuilder.<UpdateFlowOutput>failed().withRpcErrors(errors);
155 objectSettableFuture.set(rpcResultBuilder.build());
159 public void onFailure(final Throwable throwable) {
160 RpcResultBuilder<UpdateFlowOutput> rpcResultBuilder = RpcResultBuilder.failed();
161 objectSettableFuture.set(rpcResultBuilder.build());
163 }, MoreExecutors.directExecutor());
165 future = objectSettableFuture;
167 future = flowUpdateMessage.handleServiceCall(input.getUpdatedFlow());
170 if (!FlowCreatorUtil.canModifyFlow(original, updated, flowUpdate.getVersion())) {
171 // We would need to remove original and add updated.
174 final RemoveFlowInputBuilder removeflow = new RemoveFlowInputBuilder(original);
175 final List<FlowModInputBuilder> ofFlowRemoveInput = flowUpdate.toFlowModInputs(removeflow.build());
176 // remove flow should be the first
177 allFlowMods.addAll(ofFlowRemoveInput);
178 final AddFlowInputBuilder addFlowInputBuilder = new AddFlowInputBuilder(updated);
179 ofFlowModInputs = flowUpdate.toFlowModInputs(addFlowInputBuilder.build());
181 ofFlowModInputs = flowUpdate.toFlowModInputs(updated);
184 allFlowMods.addAll(ofFlowModInputs);
186 future = flowUpdate.processFlowModInputBuilders(allFlowMods);
189 Futures.addCallback(future, new UpdateFlowCallback(input), MoreExecutors.directExecutor());
193 private final class AddFlowCallback implements FutureCallback<RpcResult<AddFlowOutput>> {
194 private final AddFlowInput input;
195 private final FlowRegistryKey flowRegistryKey;
197 private AddFlowCallback(final AddFlowInput input,
198 final FlowRegistryKey flowRegistryKey) {
200 this.flowRegistryKey = flowRegistryKey;
204 public void onSuccess(@Nonnull final RpcResult<AddFlowOutput> rpcResult) {
205 if (rpcResult.isSuccessful()) {
206 final FlowDescriptor flowDescriptor;
208 if (Objects.nonNull(input.getFlowRef())) {
209 final FlowId flowId = input.getFlowRef().getValue().firstKeyOf(Flow.class).getId();
210 flowDescriptor = FlowDescriptorFactory.create(input.getTableId(), flowId);
211 deviceContext.getDeviceFlowRegistry().storeDescriptor(flowRegistryKey, flowDescriptor);
213 deviceContext.getDeviceFlowRegistry().store(flowRegistryKey);
214 flowDescriptor = deviceContext.getDeviceFlowRegistry().retrieveDescriptor(flowRegistryKey);
217 if (LOG.isDebugEnabled()) {
218 LOG.debug("Flow add with id={} finished without error", flowDescriptor.getFlowId().getValue());
221 if (LOG.isDebugEnabled()) {
222 LOG.debug("Flow add failed for flow={}, errors={}", input,
223 ErrorUtil.errorsToString(rpcResult.getErrors()));
229 public void onFailure(final Throwable throwable) {
230 LOG.warn("Service call for adding flow={} failed, reason: {}", input, throwable);
234 private final class RemoveFlowCallback implements FutureCallback<RpcResult<RemoveFlowOutput>> {
235 private final RemoveFlowInput input;
237 private RemoveFlowCallback(final RemoveFlowInput input) {
242 public void onSuccess(@Nonnull final RpcResult<RemoveFlowOutput> result) {
243 if (result.isSuccessful()) {
244 if (LOG.isDebugEnabled()) {
245 LOG.debug("Flow remove finished without error for flow={}", input);
247 FlowRegistryKey flowRegistryKey =
248 FlowRegistryKeyFactory.create(deviceContext.getDeviceInfo().getVersion(), input);
249 deviceContext.getDeviceFlowRegistry().addMark(flowRegistryKey);
251 if (LOG.isDebugEnabled()) {
252 LOG.debug("Flow remove failed for flow={}, errors={}", input,
253 ErrorUtil.errorsToString(result.getErrors()));
259 public void onFailure(final Throwable throwable) {
260 LOG.warn("Service call for removing flow={} failed, reason: {}", input, throwable);
264 private final class UpdateFlowCallback implements FutureCallback<RpcResult<UpdateFlowOutput>> {
265 private final UpdateFlowInput input;
267 private UpdateFlowCallback(UpdateFlowInput input) {
272 public void onSuccess(@Nonnull final RpcResult<UpdateFlowOutput> updateFlowOutputRpcResult) {
273 final DeviceFlowRegistry deviceFlowRegistry = deviceContext.getDeviceFlowRegistry();
275 final UpdatedFlow updated = input.getUpdatedFlow();
276 final OriginalFlow original = input.getOriginalFlow();
277 final FlowRegistryKey origFlowRegistryKey =
278 FlowRegistryKeyFactory.create(deviceContext.getDeviceInfo().getVersion(), original);
279 final FlowRegistryKey updatedFlowRegistryKey =
280 FlowRegistryKeyFactory.create(deviceContext.getDeviceInfo().getVersion(), updated);
281 final FlowDescriptor origFlowDescriptor = deviceFlowRegistry.retrieveDescriptor(origFlowRegistryKey);
283 final boolean isUpdate = Objects.nonNull(origFlowDescriptor);
284 final FlowDescriptor updatedFlowDescriptor;
286 if (Objects.nonNull(input.getFlowRef())) {
287 updatedFlowDescriptor =
288 FlowDescriptorFactory.create(updated.getTableId(),
289 input.getFlowRef().getValue().firstKeyOf(Flow.class).getId());
292 updatedFlowDescriptor = origFlowDescriptor;
294 deviceFlowRegistry.store(updatedFlowRegistryKey);
295 updatedFlowDescriptor = deviceFlowRegistry.retrieveDescriptor(updatedFlowRegistryKey);
300 deviceFlowRegistry.addMark(origFlowRegistryKey);
301 deviceFlowRegistry.storeDescriptor(updatedFlowRegistryKey, updatedFlowDescriptor);
306 public void onFailure(final Throwable throwable) {
307 LOG.warn("Service call for updating flow={} failed, reason: {}", input, throwable);