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.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.FlowUtil;
29 import org.opendaylight.openflowplugin.openflow.md.util.FlowCreatorUtil;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
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.inventory.rev130819.tables.table.FlowBuilder;
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.flow.types.rev131026.FlowRef;
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.RpcError;
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) {
66 this.deviceContext = deviceContext;
67 flowRemove = new FlowService(requestContextStack, deviceContext, RemoveFlowOutput.class);
68 flowAdd = new FlowService<>(requestContextStack, deviceContext, AddFlowOutput.class);
69 flowUpdate = new FlowService<>(requestContextStack, deviceContext, UpdateFlowOutput.class);
73 public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
74 this.itemLifecycleListener = itemLifecycleListener;
78 public Future<RpcResult<AddFlowOutput>> addFlow(final AddFlowInput input) {
80 if (null != input.getFlowRef()) {
81 flowId = input.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
83 flowId = FlowUtil.createAlienFlowId(input.getTableId());
86 final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(input);
87 final FlowDescriptor flowDescriptor = FlowDescriptorFactory.create(input.getTableId(), flowId);
88 deviceContext.getDeviceFlowRegistry().store(flowRegistryKey, flowDescriptor);
89 final ListenableFuture<RpcResult<AddFlowOutput>> future = flowAdd.processFlowModInputBuilders(flowAdd.toFlowModInputs(input));
90 Futures.addCallback(future, new FutureCallback<RpcResult<AddFlowOutput>>() {
92 public void onSuccess(final RpcResult<AddFlowOutput> rpcResult) {
93 if (rpcResult.isSuccessful()) {
94 LOG.debug("flow add finished without error, id={}", flowId.getValue());
95 if (itemLifecycleListener != null) {
96 KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
97 deviceContext.getDeviceState().getNodeInstanceIdentifier());
98 final FlowBuilder flowBuilder = new FlowBuilder(input).setId(flowDescriptor.getFlowId());
99 itemLifecycleListener.onAdded(flowPath, flowBuilder.build());
102 LOG.debug("flow add failed with error, id={}", flowId.getValue());
107 public void onFailure(final Throwable throwable) {
108 deviceContext.getDeviceFlowRegistry().markToBeremoved(flowRegistryKey);
109 LOG.trace("Service call for adding flows failed, id={}.", flowId.getValue(), throwable);
117 public Future<RpcResult<RemoveFlowOutput>> removeFlow(final RemoveFlowInput input) {
118 LOG.trace("Calling remove flow for flow with ID ={}.", input.getFlowRef());
120 final ListenableFuture<RpcResult<RemoveFlowOutput>> future = flowRemove.processFlowModInputBuilders(flowRemove.toFlowModInputs(input));
121 Futures.addCallback(future, new FutureCallback<RpcResult<RemoveFlowOutput>>() {
123 public void onSuccess(final RpcResult<RemoveFlowOutput> result) {
124 if (result.isSuccessful()) {
125 FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(input);
126 deviceContext.getDeviceFlowRegistry().markToBeremoved(flowRegistryKey);
127 if (itemLifecycleListener != null) {
128 final FlowDescriptor flowDescriptor = deviceContext.getDeviceFlowRegistry().retrieveIdForFlow(flowRegistryKey);
129 if (flowDescriptor != null) {
130 KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
131 deviceContext.getDeviceState().getNodeInstanceIdentifier());
132 itemLifecycleListener.onRemoved(flowPath);
136 if (LOG.isTraceEnabled()) {
137 StringBuilder errors = new StringBuilder();
138 Collection<RpcError> rpcErrors = result.getErrors();
139 if (null != rpcErrors && rpcErrors.size() > 0) {
140 for (RpcError rpcError : rpcErrors) {
141 errors.append(rpcError.getMessage());
144 LOG.trace("Flow modification failed. Errors : {}", errors.toString());
150 public void onFailure(final Throwable throwable) {
151 LOG.trace("Flow modification failed..", throwable);
159 public Future<RpcResult<UpdateFlowOutput>> updateFlow(final UpdateFlowInput input) {
160 final UpdateFlowInput in = input;
161 final UpdatedFlow updated = in.getUpdatedFlow();
162 final OriginalFlow original = in.getOriginalFlow();
164 final List<FlowModInputBuilder> allFlowMods = new ArrayList<>();
165 final List<FlowModInputBuilder> ofFlowModInputs;
167 if (!FlowCreatorUtil.canModifyFlow(original, updated, flowUpdate.getVersion())) {
168 // We would need to remove original and add updated.
171 final RemoveFlowInputBuilder removeflow = new RemoveFlowInputBuilder(original);
172 final List<FlowModInputBuilder> ofFlowRemoveInput = flowUpdate.toFlowModInputs(removeflow.build());
173 // remove flow should be the first
174 allFlowMods.addAll(ofFlowRemoveInput);
175 final AddFlowInputBuilder addFlowInputBuilder = new AddFlowInputBuilder(updated);
176 ofFlowModInputs = flowUpdate.toFlowModInputs(addFlowInputBuilder.build());
178 ofFlowModInputs = flowUpdate.toFlowModInputs(updated);
181 allFlowMods.addAll(ofFlowModInputs);
182 ListenableFuture<RpcResult<UpdateFlowOutput>> future = flowUpdate.processFlowModInputBuilders(allFlowMods);
183 Futures.addCallback(future, new FutureCallback<RpcResult<UpdateFlowOutput>>() {
185 public void onSuccess(final RpcResult<UpdateFlowOutput> o) {
186 FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(original);
188 FlowRegistryKey updatedflowRegistryKey = FlowRegistryKeyFactory.create(updated);
189 final FlowRef flowRef = input.getFlowRef();
190 final DeviceFlowRegistry deviceFlowRegistry = deviceContext.getDeviceFlowRegistry();
191 deviceFlowRegistry.markToBeremoved(flowRegistryKey);
193 if (itemLifecycleListener != null) {
194 final FlowDescriptor flowDescriptor = deviceContext.getDeviceFlowRegistry().retrieveIdForFlow(flowRegistryKey);
195 if (flowDescriptor != null) {
196 KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
197 deviceContext.getDeviceState().getNodeInstanceIdentifier());
198 itemLifecycleListener.onRemoved(flowPath);
201 //if provided, store flow id to flow registry
202 if (flowRef != null) {
203 final FlowId flowId = flowRef.getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
204 final FlowDescriptor flowDescriptor = FlowDescriptorFactory.create(updated.getTableId(), flowId);
205 deviceFlowRegistry.store(updatedflowRegistryKey, flowDescriptor);
207 if (itemLifecycleListener != null) {
208 KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
209 deviceContext.getDeviceState().getNodeInstanceIdentifier());
210 final FlowBuilder flowBuilder = new FlowBuilder(input.getUpdatedFlow()).setId(flowDescriptor.getFlowId());
211 itemLifecycleListener.onAdded(flowPath, flowBuilder.build());
217 public void onFailure(final Throwable throwable) {
218 LOG.debug("Flow update failed", throwable);
225 static KeyedInstanceIdentifier<Flow, FlowKey> createFlowPath(FlowDescriptor flowDescriptor,
226 KeyedInstanceIdentifier<Node, NodeKey> nodePath) {
227 return nodePath.augmentation(FlowCapableNode.class)
228 .child(Table.class, flowDescriptor.getTableKey())
229 .child(Flow.class, new FlowKey(flowDescriptor.getFlowId()));