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;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.Objects;
18 import java.util.concurrent.Future;
19 import javax.annotation.Nullable;
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.api.openflow.rpc.ItemLifeCycleSource;
26 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
27 import org.opendaylight.openflowplugin.impl.registry.flow.FlowDescriptorFactory;
28 import org.opendaylight.openflowplugin.impl.registry.flow.FlowRegistryKeyFactory;
29 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
30 import org.opendaylight.openflowplugin.openflow.md.util.FlowCreatorUtil;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
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;
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.FlowBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
52 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
53 import org.opendaylight.yangtools.yang.common.RpcError;
54 import org.opendaylight.yangtools.yang.common.RpcResult;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
58 public class SalFlowServiceImpl implements SalFlowService, ItemLifeCycleSource {
59 private static final Logger LOG = LoggerFactory.getLogger(SalFlowServiceImpl.class);
60 private final FlowService<UpdateFlowOutput> flowUpdate;
61 private final FlowService<AddFlowOutput> flowAdd;
62 private final FlowService<RemoveFlowOutput> flowRemove;
63 private final DeviceContext deviceContext;
64 private ItemLifecycleListener itemLifecycleListener;
66 public SalFlowServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext, final ConvertorExecutor convertorExecutor) {
67 this.deviceContext = deviceContext;
68 flowRemove = new FlowService<>(requestContextStack, deviceContext, RemoveFlowOutput.class, convertorExecutor);
69 flowAdd = new FlowService<>(requestContextStack, deviceContext, AddFlowOutput.class, convertorExecutor);
70 flowUpdate = new FlowService<>(requestContextStack, deviceContext, UpdateFlowOutput.class, convertorExecutor);
74 public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
75 this.itemLifecycleListener = itemLifecycleListener;
79 public Future<RpcResult<AddFlowOutput>> addFlow(final AddFlowInput input) {
80 final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(input);
81 final ListenableFuture<RpcResult<AddFlowOutput>> future =
82 flowAdd.processFlowModInputBuilders(flowAdd.toFlowModInputs(input));
83 Futures.addCallback(future, new AddFlowCallback(input, flowRegistryKey));
89 public Future<RpcResult<RemoveFlowOutput>> removeFlow(final RemoveFlowInput input) {
90 LOG.trace("Calling remove flow for flow with ID ={}.", input.getFlowRef());
92 final ListenableFuture<RpcResult<RemoveFlowOutput>> future =
93 flowRemove.processFlowModInputBuilders(flowRemove.toFlowModInputs(input));
94 Futures.addCallback(future, new RemoveFlowCallback(input));
99 private final String errorsToString(final Collection<RpcError> rpcErrors) {
100 final StringBuilder errors = new StringBuilder();
101 if ((null != rpcErrors) && (rpcErrors.size() > 0)) {
102 for (final RpcError rpcError : rpcErrors) {
103 errors.append(rpcError.getMessage());
106 return errors.toString();
110 public Future<RpcResult<UpdateFlowOutput>> updateFlow(final UpdateFlowInput input) {
111 final UpdatedFlow updated = input.getUpdatedFlow();
112 final OriginalFlow original = input.getOriginalFlow();
114 final List<FlowModInputBuilder> allFlowMods = new ArrayList<>();
115 final List<FlowModInputBuilder> ofFlowModInputs;
117 if (!FlowCreatorUtil.canModifyFlow(original, updated, flowUpdate.getVersion())) {
118 // We would need to remove original and add updated.
121 final RemoveFlowInputBuilder removeflow = new RemoveFlowInputBuilder(original);
122 final List<FlowModInputBuilder> ofFlowRemoveInput = flowUpdate.toFlowModInputs(removeflow.build());
123 // remove flow should be the first
124 allFlowMods.addAll(ofFlowRemoveInput);
125 final AddFlowInputBuilder addFlowInputBuilder = new AddFlowInputBuilder(updated);
126 ofFlowModInputs = flowUpdate.toFlowModInputs(addFlowInputBuilder.build());
128 ofFlowModInputs = flowUpdate.toFlowModInputs(updated);
131 allFlowMods.addAll(ofFlowModInputs);
132 ListenableFuture<RpcResult<UpdateFlowOutput>> future = flowUpdate.processFlowModInputBuilders(allFlowMods);
133 Futures.addCallback(future, new UpdateFlowCallback(input));
138 static KeyedInstanceIdentifier<Flow, FlowKey> createFlowPath(FlowDescriptor flowDescriptor,
139 KeyedInstanceIdentifier<Node, NodeKey> nodePath) {
140 return nodePath.augmentation(FlowCapableNode.class)
141 .child(Table.class, flowDescriptor.getTableKey())
142 .child(Flow.class, new FlowKey(flowDescriptor.getFlowId()));
145 private class AddFlowCallback implements FutureCallback<RpcResult<AddFlowOutput>> {
146 private final AddFlowInput input;
147 private final FlowRegistryKey flowRegistryKey;
149 private AddFlowCallback(final AddFlowInput input,
150 final FlowRegistryKey flowRegistryKey) {
152 this.flowRegistryKey = flowRegistryKey;
156 public void onSuccess(final RpcResult<AddFlowOutput> rpcResult) {
157 if (rpcResult.isSuccessful()) {
158 final FlowDescriptor flowDescriptor;
160 if (Objects.nonNull(input.getFlowRef())) {
161 final FlowId flowId = input.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
162 flowDescriptor = FlowDescriptorFactory.create(input.getTableId(), flowId);
163 deviceContext.getDeviceFlowRegistry().store(flowRegistryKey, flowDescriptor);
165 final FlowId flowId = deviceContext.getDeviceFlowRegistry().storeIfNecessary(flowRegistryKey);
166 flowDescriptor = FlowDescriptorFactory.create(input.getTableId(), flowId);
169 LOG.debug("flow add with id={},finished without error,", flowDescriptor.getFlowId().getValue());
171 if (itemLifecycleListener != null) {
172 KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
173 deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
174 final FlowBuilder flowBuilder = new FlowBuilder(input).setId(flowDescriptor.getFlowId());
175 itemLifecycleListener.onAdded(flowPath, flowBuilder.build());
178 LOG.error("flow add failed for flow={}, errors={}", input.toString(), errorsToString(rpcResult.getErrors()));
183 public void onFailure(final Throwable throwable) {
184 LOG.error("Service call for adding flow={} failed, reason {} .", input.toString(), throwable);
188 private class RemoveFlowCallback implements FutureCallback<RpcResult<RemoveFlowOutput>> {
189 private final RemoveFlowInput input;
191 private RemoveFlowCallback(final RemoveFlowInput input) {
196 public void onSuccess(final RpcResult<RemoveFlowOutput> result) {
197 if (result.isSuccessful()) {
198 if(LOG.isDebugEnabled()) {
199 LOG.debug("flow removed finished without error,");
201 FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(input);
202 deviceContext.getDeviceFlowRegistry().markToBeremoved(flowRegistryKey);
203 if (itemLifecycleListener != null) {
204 final FlowDescriptor flowDescriptor =
205 deviceContext.getDeviceFlowRegistry().retrieveIdForFlow(flowRegistryKey);
206 if (flowDescriptor != null) {
207 KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
208 deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
209 itemLifecycleListener.onRemoved(flowPath);
213 LOG.error("Flow remove failed with errors : {}",errorsToString(result.getErrors()));
218 public void onFailure(final Throwable throwable) {
219 LOG.error("Service call for removing flow with id {} failed ,reason {}",input.getFlowRef().getValue(), throwable);
223 private class UpdateFlowCallback implements FutureCallback<RpcResult<UpdateFlowOutput>> {
224 private final UpdateFlowInput input;
226 private UpdateFlowCallback(UpdateFlowInput input) {
231 public void onSuccess(final RpcResult<UpdateFlowOutput> o) {
232 final UpdatedFlow updated = input.getUpdatedFlow();
233 final OriginalFlow original = input.getOriginalFlow();
234 FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(original);
236 FlowRegistryKey updatedflowRegistryKey = FlowRegistryKeyFactory.create(updated);
237 final FlowRef flowRef = input.getFlowRef();
238 final DeviceFlowRegistry deviceFlowRegistry = deviceContext.getDeviceFlowRegistry();
240 if (flowRef == null) {
241 // then this is equivalent to a delete
242 deviceFlowRegistry.markToBeremoved(flowRegistryKey);
244 if (itemLifecycleListener != null) {
245 final FlowDescriptor flowDescriptor =
246 deviceContext.getDeviceFlowRegistry().retrieveIdForFlow( flowRegistryKey);
247 KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
248 deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
249 itemLifecycleListener.onRemoved(flowPath);
252 // this is either an add or an update
253 final FlowId flowId = flowRef.getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
254 final FlowDescriptor flowDescriptor = FlowDescriptorFactory.create(updated.getTableId(), flowId);
255 deviceFlowRegistry.store(updatedflowRegistryKey, flowDescriptor);
257 if (itemLifecycleListener != null) {
258 KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
259 deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
260 final FlowBuilder flowBuilder = new FlowBuilder(
261 input.getUpdatedFlow()).setId(flowDescriptor.getFlowId());
263 boolean isUpdate = null !=
264 deviceFlowRegistry.retrieveIdForFlow(flowRegistryKey);
266 itemLifecycleListener.onUpdated(flowPath, flowBuilder.build());
268 itemLifecycleListener.onAdded(flowPath, flowBuilder.build());
275 public void onFailure(final Throwable throwable) {
276 LOG.error("Service call for updating flow failed, reason{}", throwable);