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.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.List;
16 import java.util.concurrent.Future;
17 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
19 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
20 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
21 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
22 import org.opendaylight.openflowplugin.impl.registry.flow.FlowDescriptorFactory;
23 import org.opendaylight.openflowplugin.impl.registry.flow.FlowRegistryKeyFactory;
24 import org.opendaylight.openflowplugin.impl.util.FlowUtil;
25 import org.opendaylight.openflowplugin.openflow.md.util.FlowCreatorUtil;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
41 import org.opendaylight.yangtools.yang.common.RpcError;
42 import org.opendaylight.yangtools.yang.common.RpcResult;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 public class SalFlowServiceImpl implements SalFlowService {
47 private static final Logger LOG = LoggerFactory.getLogger(SalFlowServiceImpl.class);
48 private final FlowService<UpdateFlowOutput> flowUpdate;
49 private final FlowService<AddFlowOutput> flowAdd;
50 private final FlowRemoveService flowRemove;
52 public SalFlowServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
53 flowRemove = new FlowRemoveService(requestContextStack, deviceContext);
54 flowAdd = new FlowService<>(requestContextStack, deviceContext, AddFlowOutput.class);
55 flowUpdate = new FlowService<>(requestContextStack, deviceContext, UpdateFlowOutput.class);
59 public Future<RpcResult<AddFlowOutput>> addFlow(final AddFlowInput input) {
60 final ListenableFuture<RpcResult<AddFlowOutput>> future = flowAdd.processFlowModInputBuilders(flowAdd.toFlowModInputs(input));
62 if (null != input.getFlowRef()) {
63 flowId = input.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
65 flowId = FlowUtil.createAlienFlowId(input.getTableId());
68 final DeviceContext deviceContext = flowAdd.getDeviceContext();
69 final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(input);
70 final FlowDescriptor flowDescriptor = FlowDescriptorFactory.create(input.getTableId(), flowId);
71 deviceContext.getDeviceFlowRegistry().store(flowRegistryKey, flowDescriptor);
72 Futures.addCallback(future, new FutureCallback<RpcResult<AddFlowOutput>>() {
74 public void onSuccess(final RpcResult<AddFlowOutput> rpcResult) {
75 if (rpcResult.isSuccessful()) {
76 LOG.debug("flow add finished without error, id={}", flowId.getValue());
78 LOG.debug("flow add failed with error, id={}", flowId.getValue());
83 public void onFailure(final Throwable throwable) {
84 deviceContext.getDeviceFlowRegistry().markToBeremoved(flowRegistryKey);
85 LOG.trace("Service call for adding flows failed, id={}.", flowId.getValue(), throwable);
93 public Future<RpcResult<RemoveFlowOutput>> removeFlow(final RemoveFlowInput input) {
94 LOG.trace("Calling remove flow for flow with ID ={}.", input.getFlowRef());
96 final ListenableFuture<RpcResult<RemoveFlowOutput>> future = flowRemove.handleServiceCall(input);
97 Futures.addCallback(future, new FutureCallback<RpcResult<RemoveFlowOutput>>() {
99 public void onSuccess(final RpcResult<RemoveFlowOutput> result) {
100 if (result.isSuccessful()) {
101 FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(input);
102 flowRemove.getDeviceContext().getDeviceFlowRegistry().markToBeremoved(flowRegistryKey);
104 if (LOG.isTraceEnabled()) {
105 StringBuilder errors = new StringBuilder();
106 Collection<RpcError> rpcErrors = result.getErrors();
107 if (null != rpcErrors && rpcErrors.size() > 0) {
108 for (RpcError rpcError : rpcErrors) {
109 errors.append(rpcError.getMessage());
112 LOG.trace("Flow modification failed. Errors : {}", errors.toString());
118 public void onFailure(final Throwable throwable) {
119 LOG.trace("Flow modification failed..", throwable);
127 public Future<RpcResult<UpdateFlowOutput>> updateFlow(final UpdateFlowInput input) {
128 final UpdateFlowInput in = input;
129 final UpdatedFlow updated = in.getUpdatedFlow();
130 final OriginalFlow original = in.getOriginalFlow();
132 final List<FlowModInputBuilder> allFlowMods = new ArrayList<>();
133 final List<FlowModInputBuilder> ofFlowModInputs;
135 if (!FlowCreatorUtil.canModifyFlow(original, updated, flowUpdate.getVersion())) {
136 // We would need to remove original and add updated.
139 final RemoveFlowInputBuilder removeflow = new RemoveFlowInputBuilder(original);
140 final List<FlowModInputBuilder> ofFlowRemoveInput = flowUpdate.toFlowModInputs(removeflow.build());
141 // remove flow should be the first
142 allFlowMods.addAll(ofFlowRemoveInput);
143 final AddFlowInputBuilder addFlowInputBuilder = new AddFlowInputBuilder(updated);
144 ofFlowModInputs = flowUpdate.toFlowModInputs(addFlowInputBuilder.build());
146 ofFlowModInputs = flowUpdate.toFlowModInputs(updated);
149 allFlowMods.addAll(ofFlowModInputs);
150 ListenableFuture<RpcResult<UpdateFlowOutput>> future = flowUpdate.processFlowModInputBuilders(allFlowMods);
151 Futures.addCallback(future, new FutureCallback<RpcResult<UpdateFlowOutput>>() {
153 public void onSuccess(final RpcResult<UpdateFlowOutput> o) {
154 FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(original);
156 FlowRegistryKey updatedflowRegistryKey = FlowRegistryKeyFactory.create(updated);
157 FlowId flowId = input.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
158 FlowDescriptor flowDescriptor = FlowDescriptorFactory.create(updated.getTableId(), flowId);
159 final DeviceFlowRegistry deviceFlowRegistry = flowUpdate.getDeviceContext().getDeviceFlowRegistry();
160 deviceFlowRegistry.markToBeremoved(flowRegistryKey);
161 deviceFlowRegistry.store(updatedflowRegistryKey, flowDescriptor);
165 public void onFailure(final Throwable throwable) {
166 LOG.debug("Flow update failed", throwable);