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.List;
16 import java.util.Objects;
17 import java.util.concurrent.Future;
18 import javax.annotation.Nullable;
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.api.openflow.rpc.ItemLifeCycleSource;
25 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
26 import org.opendaylight.openflowplugin.impl.registry.flow.FlowDescriptorFactory;
27 import org.opendaylight.openflowplugin.impl.registry.flow.FlowRegistryKeyFactory;
28 import org.opendaylight.openflowplugin.impl.util.ErrorUtil;
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.RpcResult;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
57 public class SalFlowServiceImpl implements SalFlowService, ItemLifeCycleSource {
58 private static final Logger LOG = LoggerFactory.getLogger(SalFlowServiceImpl.class);
59 private final FlowService<UpdateFlowOutput> flowUpdate;
60 private final FlowService<AddFlowOutput> flowAdd;
61 private final FlowService<RemoveFlowOutput> flowRemove;
62 private final DeviceContext deviceContext;
63 private ItemLifecycleListener itemLifecycleListener;
65 public SalFlowServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext, final ConvertorExecutor convertorExecutor) {
66 this.deviceContext = deviceContext;
67 flowRemove = new FlowService<>(requestContextStack, deviceContext, RemoveFlowOutput.class, convertorExecutor);
68 flowAdd = new FlowService<>(requestContextStack, deviceContext, AddFlowOutput.class, convertorExecutor);
69 flowUpdate = new FlowService<>(requestContextStack, deviceContext, UpdateFlowOutput.class, convertorExecutor);
73 public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
74 this.itemLifecycleListener = itemLifecycleListener;
78 public Future<RpcResult<AddFlowOutput>> addFlow(final AddFlowInput input) {
79 final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(input);
80 final ListenableFuture<RpcResult<AddFlowOutput>> future =
81 flowAdd.processFlowModInputBuilders(flowAdd.toFlowModInputs(input));
82 Futures.addCallback(future, new AddFlowCallback(input, flowRegistryKey));
87 public Future<RpcResult<RemoveFlowOutput>> removeFlow(final RemoveFlowInput input) {
88 final ListenableFuture<RpcResult<RemoveFlowOutput>> future =
89 flowRemove.processFlowModInputBuilders(flowRemove.toFlowModInputs(input));
90 Futures.addCallback(future, new RemoveFlowCallback(input));
95 public Future<RpcResult<UpdateFlowOutput>> updateFlow(final UpdateFlowInput input) {
96 final UpdatedFlow updated = input.getUpdatedFlow();
97 final OriginalFlow original = input.getOriginalFlow();
99 final List<FlowModInputBuilder> allFlowMods = new ArrayList<>();
100 final List<FlowModInputBuilder> ofFlowModInputs;
102 if (!FlowCreatorUtil.canModifyFlow(original, updated, flowUpdate.getVersion())) {
103 // We would need to remove original and add updated.
106 final RemoveFlowInputBuilder removeflow = new RemoveFlowInputBuilder(original);
107 final List<FlowModInputBuilder> ofFlowRemoveInput = flowUpdate.toFlowModInputs(removeflow.build());
108 // remove flow should be the first
109 allFlowMods.addAll(ofFlowRemoveInput);
110 final AddFlowInputBuilder addFlowInputBuilder = new AddFlowInputBuilder(updated);
111 ofFlowModInputs = flowUpdate.toFlowModInputs(addFlowInputBuilder.build());
113 ofFlowModInputs = flowUpdate.toFlowModInputs(updated);
116 allFlowMods.addAll(ofFlowModInputs);
117 ListenableFuture<RpcResult<UpdateFlowOutput>> future = flowUpdate.processFlowModInputBuilders(allFlowMods);
118 Futures.addCallback(future, new UpdateFlowCallback(input));
123 private static KeyedInstanceIdentifier<Flow, FlowKey> createFlowPath(FlowDescriptor flowDescriptor,
124 KeyedInstanceIdentifier<Node, NodeKey> nodePath) {
125 return nodePath.augmentation(FlowCapableNode.class)
126 .child(Table.class, flowDescriptor.getTableKey())
127 .child(Flow.class, new FlowKey(flowDescriptor.getFlowId()));
130 private class AddFlowCallback implements FutureCallback<RpcResult<AddFlowOutput>> {
131 private final AddFlowInput input;
132 private final FlowRegistryKey flowRegistryKey;
134 private AddFlowCallback(final AddFlowInput input,
135 final FlowRegistryKey flowRegistryKey) {
137 this.flowRegistryKey = flowRegistryKey;
141 public void onSuccess(final RpcResult<AddFlowOutput> rpcResult) {
142 if (rpcResult.isSuccessful()) {
143 final FlowDescriptor flowDescriptor;
145 if (Objects.nonNull(input.getFlowRef())) {
146 final FlowId flowId = input.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
147 flowDescriptor = FlowDescriptorFactory.create(input.getTableId(), flowId);
148 deviceContext.getDeviceFlowRegistry().store(flowRegistryKey, flowDescriptor);
150 final FlowId flowId = deviceContext.getDeviceFlowRegistry().storeIfNecessary(flowRegistryKey);
151 flowDescriptor = FlowDescriptorFactory.create(input.getTableId(), flowId);
154 if (LOG.isDebugEnabled()) {
155 LOG.debug("Flow add with id={} finished without error", flowDescriptor.getFlowId().getValue());
158 if (itemLifecycleListener != null) {
159 KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
160 deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
161 final FlowBuilder flowBuilder = new FlowBuilder(input).setId(flowDescriptor.getFlowId());
162 itemLifecycleListener.onAdded(flowPath, flowBuilder.build());
165 if (LOG.isDebugEnabled()) {
166 LOG.debug("Flow add failed for flow={}, errors={}", input,
167 ErrorUtil.errorsToString(rpcResult.getErrors()));
173 public void onFailure(final Throwable throwable) {
174 LOG.warn("Service call for adding flow={} failed, reason: {}", input, throwable);
178 private class RemoveFlowCallback implements FutureCallback<RpcResult<RemoveFlowOutput>> {
179 private final RemoveFlowInput input;
181 private RemoveFlowCallback(final RemoveFlowInput input) {
186 public void onSuccess(final RpcResult<RemoveFlowOutput> result) {
187 if (result.isSuccessful()) {
188 if (LOG.isDebugEnabled()) {
189 LOG.debug("Flow remove finished without error for flow={}", input);
191 FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(input);
192 deviceContext.getDeviceFlowRegistry().removeDescriptor(flowRegistryKey);
194 if (itemLifecycleListener != null) {
195 final FlowDescriptor flowDescriptor =
196 deviceContext.getDeviceFlowRegistry().retrieveIdForFlow(flowRegistryKey);
197 if (flowDescriptor != null) {
198 KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
199 deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
200 itemLifecycleListener.onRemoved(flowPath);
204 if (LOG.isDebugEnabled()) {
205 LOG.debug("Flow remove failed for flow={}, errors={}", input,
206 ErrorUtil.errorsToString(result.getErrors()));
212 public void onFailure(final Throwable throwable) {
213 LOG.warn("Service call for removing flow={} failed, reason: {}", input, throwable);
217 private class UpdateFlowCallback implements FutureCallback<RpcResult<UpdateFlowOutput>> {
218 private final UpdateFlowInput input;
220 private UpdateFlowCallback(UpdateFlowInput input) {
225 public void onSuccess(final RpcResult<UpdateFlowOutput> o) {
226 final UpdatedFlow updated = input.getUpdatedFlow();
227 final OriginalFlow original = input.getOriginalFlow();
228 FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(original);
230 FlowRegistryKey updatedflowRegistryKey = FlowRegistryKeyFactory.create(updated);
231 final FlowRef flowRef = input.getFlowRef();
232 final DeviceFlowRegistry deviceFlowRegistry = deviceContext.getDeviceFlowRegistry();
234 if (flowRef == null) {
235 // then this is equivalent to a delete
236 deviceFlowRegistry.removeDescriptor(flowRegistryKey);
238 if (itemLifecycleListener != null) {
239 final FlowDescriptor flowDescriptor =
240 deviceContext.getDeviceFlowRegistry().retrieveIdForFlow( flowRegistryKey);
241 KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
242 deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
243 itemLifecycleListener.onRemoved(flowPath);
246 // this is either an add or an update
247 final FlowId flowId = flowRef.getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
248 final FlowDescriptor flowDescriptor = FlowDescriptorFactory.create(updated.getTableId(), flowId);
250 deviceFlowRegistry.update(updatedflowRegistryKey, flowDescriptor);
252 if (itemLifecycleListener != null) {
253 KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
254 deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
255 final FlowBuilder flowBuilder = new FlowBuilder(
256 input.getUpdatedFlow()).setId(flowDescriptor.getFlowId());
258 boolean isUpdate = null !=
259 deviceFlowRegistry.retrieveIdForFlow(flowRegistryKey);
261 itemLifecycleListener.onUpdated(flowPath, flowBuilder.build());
263 itemLifecycleListener.onAdded(flowPath, flowBuilder.build());
270 public void onFailure(final Throwable throwable) {
271 LOG.warn("Service call for updating flow={} failed, reason: {}", input, throwable);