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.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) {
79 final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(input);
81 final FlowDescriptor flowDescriptor;
83 if (Objects.nonNull(input.getFlowRef())) {
84 flowId = input.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
85 flowDescriptor = FlowDescriptorFactory.create(input.getTableId(), flowId);
86 deviceContext.getDeviceFlowRegistry().store(flowRegistryKey, flowDescriptor);
88 flowId = deviceContext.getDeviceFlowRegistry().storeIfNecessary(flowRegistryKey);
89 flowDescriptor = FlowDescriptorFactory.create(input.getTableId(), flowId);
92 LOG.trace("Calling add flow for flow with ID ={}.", flowId);
93 final ListenableFuture<RpcResult<AddFlowOutput>> future =
94 flowAdd.processFlowModInputBuilders(flowAdd.toFlowModInputs(input));
95 Futures.addCallback(future, new FutureCallback<RpcResult<AddFlowOutput>>() {
97 public void onSuccess(final RpcResult<AddFlowOutput> rpcResult) {
98 if (rpcResult.isSuccessful()) {
99 if(LOG.isDebugEnabled()) {
100 LOG.debug("flow add with id={},finished without error,", flowId.getValue());
102 if (itemLifecycleListener != null) {
103 KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
104 deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
105 final FlowBuilder flowBuilder = new FlowBuilder(input).setId(flowDescriptor.getFlowId());
106 itemLifecycleListener.onAdded(flowPath, flowBuilder.build());
109 LOG.error("flow add failed for id={}, errors={}", flowId.getValue(), errorsToString(rpcResult.getErrors()));
114 public void onFailure(final Throwable throwable) {
115 deviceContext.getDeviceFlowRegistry().markToBeremoved(flowRegistryKey);
116 LOG.error("Service call for adding flow with id={} failed, reason {} .", flowId.getValue(), throwable);
124 public Future<RpcResult<RemoveFlowOutput>> removeFlow(final RemoveFlowInput input) {
125 LOG.trace("Calling remove flow for flow with ID ={}.", input.getFlowRef());
127 final ListenableFuture<RpcResult<RemoveFlowOutput>> future =
128 flowRemove.processFlowModInputBuilders(flowRemove.toFlowModInputs(input));
129 Futures.addCallback(future, new FutureCallback<RpcResult<RemoveFlowOutput>>() {
131 public void onSuccess(final RpcResult<RemoveFlowOutput> result) {
132 if (result.isSuccessful()) {
133 if(LOG.isDebugEnabled()) {
134 LOG.debug("flow removed finished without error,");
136 FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(input);
137 deviceContext.getDeviceFlowRegistry().markToBeremoved(flowRegistryKey);
138 if (itemLifecycleListener != null) {
139 final FlowDescriptor flowDescriptor =
140 deviceContext.getDeviceFlowRegistry().retrieveIdForFlow(flowRegistryKey);
141 if (flowDescriptor != null) {
142 KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
143 deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
144 itemLifecycleListener.onRemoved(flowPath);
148 LOG.error("Flow remove failed with errors : {}",errorsToString(result.getErrors()));
153 public void onFailure(final Throwable throwable) {
154 LOG.error("Service call for removing flow with id {} failed ,reason {}",input.getFlowRef().getValue(), throwable);
161 private final String errorsToString(final Collection<RpcError> rpcErrors) {
162 final StringBuilder errors = new StringBuilder();
163 if ((null != rpcErrors) && (rpcErrors.size() > 0)) {
164 for (final RpcError rpcError : rpcErrors) {
165 errors.append(rpcError.getMessage());
168 return errors.toString();
172 public Future<RpcResult<UpdateFlowOutput>> updateFlow(final UpdateFlowInput input) {
173 final UpdateFlowInput in = input;
174 final UpdatedFlow updated = in.getUpdatedFlow();
175 final OriginalFlow original = in.getOriginalFlow();
177 final List<FlowModInputBuilder> allFlowMods = new ArrayList<>();
178 final List<FlowModInputBuilder> ofFlowModInputs;
180 if (!FlowCreatorUtil.canModifyFlow(original, updated, flowUpdate.getVersion())) {
181 // We would need to remove original and add updated.
184 final RemoveFlowInputBuilder removeflow = new RemoveFlowInputBuilder(original);
185 final List<FlowModInputBuilder> ofFlowRemoveInput = flowUpdate.toFlowModInputs(removeflow.build());
186 // remove flow should be the first
187 allFlowMods.addAll(ofFlowRemoveInput);
188 final AddFlowInputBuilder addFlowInputBuilder = new AddFlowInputBuilder(updated);
189 ofFlowModInputs = flowUpdate.toFlowModInputs(addFlowInputBuilder.build());
191 ofFlowModInputs = flowUpdate.toFlowModInputs(updated);
194 allFlowMods.addAll(ofFlowModInputs);
195 ListenableFuture<RpcResult<UpdateFlowOutput>> future = flowUpdate.processFlowModInputBuilders(allFlowMods);
196 Futures.addCallback(future, new FutureCallback<RpcResult<UpdateFlowOutput>>() {
198 public void onSuccess(final RpcResult<UpdateFlowOutput> o) {
199 FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(original);
201 FlowRegistryKey updatedflowRegistryKey = FlowRegistryKeyFactory.create(updated);
202 final FlowRef flowRef = input.getFlowRef();
203 final DeviceFlowRegistry deviceFlowRegistry = deviceContext.getDeviceFlowRegistry();
205 if (flowRef == null) { //then this is equivalent to a delete
206 deviceFlowRegistry.markToBeremoved(flowRegistryKey);
208 if (itemLifecycleListener != null) {
209 final FlowDescriptor flowDescriptor =
210 deviceContext.getDeviceFlowRegistry().retrieveIdForFlow( flowRegistryKey);
211 KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
212 deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
213 itemLifecycleListener.onRemoved(flowPath);
215 } else { //this is either an add or an update
216 final FlowId flowId = flowRef.getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
217 final FlowDescriptor flowDescriptor = FlowDescriptorFactory.create(updated.getTableId(), flowId);
218 deviceFlowRegistry.store(updatedflowRegistryKey, flowDescriptor);
220 if (itemLifecycleListener != null) {
221 KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
222 deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
223 final FlowBuilder flowBuilder = new FlowBuilder(
224 input.getUpdatedFlow()).setId(flowDescriptor.getFlowId());
226 boolean isUpdate = null !=
227 deviceFlowRegistry.retrieveIdForFlow(flowRegistryKey);
229 itemLifecycleListener.onUpdated(flowPath, flowBuilder.build());
231 itemLifecycleListener.onAdded(flowPath, flowBuilder.build());
240 public void onFailure(final Throwable throwable) {
241 LOG.error("Service call for updating flow failed, reason{}", throwable);
248 static KeyedInstanceIdentifier<Flow, FlowKey> createFlowPath(FlowDescriptor flowDescriptor,
249 KeyedInstanceIdentifier<Node, NodeKey> nodePath) {
250 return nodePath.augmentation(FlowCapableNode.class)
251 .child(Table.class, flowDescriptor.getTableKey())
252 .child(Flow.class, new FlowKey(flowDescriptor.getFlowId()));