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.inventory.rev130819.nodes.Node;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
51 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
52 import org.opendaylight.yangtools.yang.common.RpcResult;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 public class SalFlowServiceImpl implements SalFlowService, ItemLifeCycleSource {
57 private static final Logger LOG = LoggerFactory.getLogger(SalFlowServiceImpl.class);
58 private final FlowService<UpdateFlowOutput> flowUpdate;
59 private final FlowService<AddFlowOutput> flowAdd;
60 private final FlowService<RemoveFlowOutput> flowRemove;
61 private final DeviceContext deviceContext;
62 private ItemLifecycleListener itemLifecycleListener;
64 public SalFlowServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext, final ConvertorExecutor convertorExecutor) {
65 this.deviceContext = deviceContext;
66 flowRemove = new FlowService<>(requestContextStack, deviceContext, RemoveFlowOutput.class, convertorExecutor);
67 flowAdd = new FlowService<>(requestContextStack, deviceContext, AddFlowOutput.class, convertorExecutor);
68 flowUpdate = new FlowService<>(requestContextStack, deviceContext, UpdateFlowOutput.class, convertorExecutor);
72 public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
73 this.itemLifecycleListener = itemLifecycleListener;
77 public Future<RpcResult<AddFlowOutput>> addFlow(final AddFlowInput input) {
78 final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(deviceContext.getDeviceInfo().getVersion(), input);
79 final ListenableFuture<RpcResult<AddFlowOutput>> future =
80 flowAdd.processFlowModInputBuilders(flowAdd.toFlowModInputs(input));
81 Futures.addCallback(future, new AddFlowCallback(input, flowRegistryKey));
86 public Future<RpcResult<RemoveFlowOutput>> removeFlow(final RemoveFlowInput input) {
87 final ListenableFuture<RpcResult<RemoveFlowOutput>> future =
88 flowRemove.processFlowModInputBuilders(flowRemove.toFlowModInputs(input));
89 Futures.addCallback(future, new RemoveFlowCallback(input));
94 public Future<RpcResult<UpdateFlowOutput>> updateFlow(final UpdateFlowInput input) {
95 final UpdatedFlow updated = input.getUpdatedFlow();
96 final OriginalFlow original = input.getOriginalFlow();
98 final List<FlowModInputBuilder> allFlowMods = new ArrayList<>();
99 final List<FlowModInputBuilder> ofFlowModInputs;
101 if (!FlowCreatorUtil.canModifyFlow(original, updated, flowUpdate.getVersion())) {
102 // We would need to remove original and add updated.
105 final RemoveFlowInputBuilder removeflow = new RemoveFlowInputBuilder(original);
106 final List<FlowModInputBuilder> ofFlowRemoveInput = flowUpdate.toFlowModInputs(removeflow.build());
107 // remove flow should be the first
108 allFlowMods.addAll(ofFlowRemoveInput);
109 final AddFlowInputBuilder addFlowInputBuilder = new AddFlowInputBuilder(updated);
110 ofFlowModInputs = flowUpdate.toFlowModInputs(addFlowInputBuilder.build());
112 ofFlowModInputs = flowUpdate.toFlowModInputs(updated);
115 allFlowMods.addAll(ofFlowModInputs);
116 ListenableFuture<RpcResult<UpdateFlowOutput>> future = flowUpdate.processFlowModInputBuilders(allFlowMods);
117 Futures.addCallback(future, new UpdateFlowCallback(input));
122 private static KeyedInstanceIdentifier<Flow, FlowKey> createFlowPath(FlowDescriptor flowDescriptor,
123 KeyedInstanceIdentifier<Node, NodeKey> nodePath) {
124 return nodePath.augmentation(FlowCapableNode.class)
125 .child(Table.class, flowDescriptor.getTableKey())
126 .child(Flow.class, new FlowKey(flowDescriptor.getFlowId()));
129 private class AddFlowCallback implements FutureCallback<RpcResult<AddFlowOutput>> {
130 private final AddFlowInput input;
131 private final FlowRegistryKey flowRegistryKey;
133 private AddFlowCallback(final AddFlowInput input,
134 final FlowRegistryKey flowRegistryKey) {
136 this.flowRegistryKey = flowRegistryKey;
140 public void onSuccess(final RpcResult<AddFlowOutput> rpcResult) {
141 if (rpcResult.isSuccessful()) {
142 final FlowDescriptor flowDescriptor;
144 if (Objects.nonNull(input.getFlowRef())) {
145 final FlowId flowId = input.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
146 flowDescriptor = FlowDescriptorFactory.create(input.getTableId(), flowId);
147 deviceContext.getDeviceFlowRegistry().storeDescriptor(flowRegistryKey, flowDescriptor);
149 deviceContext.getDeviceFlowRegistry().store(flowRegistryKey);
150 flowDescriptor = deviceContext.getDeviceFlowRegistry().retrieveDescriptor(flowRegistryKey);
153 if (LOG.isDebugEnabled()) {
154 LOG.debug("Flow add with id={} finished without error", flowDescriptor.getFlowId().getValue());
157 if (itemLifecycleListener != null) {
158 KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
159 deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
160 final FlowBuilder flowBuilder = new FlowBuilder(input).setId(flowDescriptor.getFlowId());
161 itemLifecycleListener.onAdded(flowPath, flowBuilder.build());
164 if (LOG.isDebugEnabled()) {
165 LOG.debug("Flow add failed for flow={}, errors={}", input,
166 ErrorUtil.errorsToString(rpcResult.getErrors()));
172 public void onFailure(final Throwable throwable) {
173 LOG.warn("Service call for adding flow={} failed, reason: {}", input, throwable);
177 private class RemoveFlowCallback implements FutureCallback<RpcResult<RemoveFlowOutput>> {
178 private final RemoveFlowInput input;
180 private RemoveFlowCallback(final RemoveFlowInput input) {
185 public void onSuccess(final RpcResult<RemoveFlowOutput> result) {
186 if (result.isSuccessful()) {
187 if (LOG.isDebugEnabled()) {
188 LOG.debug("Flow remove finished without error for flow={}", input);
190 FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(deviceContext.getDeviceInfo().getVersion(), input);
191 deviceContext.getDeviceFlowRegistry().addMark(flowRegistryKey);
193 if (itemLifecycleListener != null) {
194 final FlowDescriptor flowDescriptor = deviceContext.getDeviceFlowRegistry().retrieveDescriptor(flowRegistryKey);
196 if (flowDescriptor != null) {
197 KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
198 deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
199 itemLifecycleListener.onRemoved(flowPath);
203 if (LOG.isDebugEnabled()) {
204 LOG.debug("Flow remove failed for flow={}, errors={}", input,
205 ErrorUtil.errorsToString(result.getErrors()));
211 public void onFailure(final Throwable throwable) {
212 LOG.warn("Service call for removing flow={} failed, reason: {}", input, throwable);
216 private class UpdateFlowCallback implements FutureCallback<RpcResult<UpdateFlowOutput>> {
217 private final UpdateFlowInput input;
219 private UpdateFlowCallback(UpdateFlowInput input) {
224 public void onSuccess(final RpcResult<UpdateFlowOutput> o) {
225 final DeviceFlowRegistry deviceFlowRegistry = deviceContext.getDeviceFlowRegistry();
227 final UpdatedFlow updated = input.getUpdatedFlow();
228 final OriginalFlow original = input.getOriginalFlow();
229 final FlowRegistryKey origFlowRegistryKey = FlowRegistryKeyFactory.create(deviceContext.getDeviceInfo().getVersion(), original);
230 final FlowRegistryKey updatedFlowRegistryKey = FlowRegistryKeyFactory.create(deviceContext.getDeviceInfo().getVersion(), updated);
231 final FlowDescriptor origFlowDescriptor = deviceFlowRegistry.retrieveDescriptor(origFlowRegistryKey);
233 final boolean isUpdate = Objects.nonNull(origFlowDescriptor);
234 final FlowDescriptor updatedFlowDescriptor;
236 if (Objects.nonNull(input.getFlowRef())) {
237 updatedFlowDescriptor = FlowDescriptorFactory.create(updated.getTableId(), input.getFlowRef().getValue().firstKeyOf(Flow.class).getId());
240 updatedFlowDescriptor = origFlowDescriptor;
242 deviceFlowRegistry.store(updatedFlowRegistryKey);
243 updatedFlowDescriptor = deviceFlowRegistry.retrieveDescriptor(updatedFlowRegistryKey);
248 deviceFlowRegistry.addMark(origFlowRegistryKey);
249 deviceFlowRegistry.storeDescriptor(updatedFlowRegistryKey, updatedFlowDescriptor);
252 if (itemLifecycleListener != null) {
253 final KeyedInstanceIdentifier<Flow, FlowKey> flowPath =
255 updatedFlowDescriptor,
256 deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
258 final Flow flow = new FlowBuilder(updated)
259 .setId(updatedFlowDescriptor.getFlowId())
262 if (Objects.nonNull(origFlowDescriptor)) {
263 itemLifecycleListener.onUpdated(flowPath, flow);
265 itemLifecycleListener.onAdded(flowPath, flow);
271 public void onFailure(final Throwable throwable) {
272 LOG.warn("Service call for updating flow={} failed, reason: {}", input, throwable);