Simplify CommonService interface
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / SalFlowServiceImpl.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.services;
9
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;
45
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;
51
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);
56     }
57
58     @Override
59     public Future<RpcResult<AddFlowOutput>> addFlow(final AddFlowInput input) {
60         final ListenableFuture<RpcResult<AddFlowOutput>> future = flowAdd.processFlowModInputBuilders(flowAdd.toFlowModInputs(input));
61         final FlowId flowId;
62         if (null != input.getFlowRef()) {
63             flowId = input.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
64         } else {
65             flowId = FlowUtil.createAlienFlowId(input.getTableId());
66         }
67
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>>() {
73             @Override
74             public void onSuccess(final RpcResult<AddFlowOutput> rpcResult) {
75                 if (rpcResult.isSuccessful()) {
76                     LOG.debug("flow add finished without error, id={}", flowId.getValue());
77                 } else {
78                     LOG.debug("flow add failed with error, id={}", flowId.getValue());
79                 }
80             }
81
82             @Override
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);
86             }
87         });
88
89         return future;
90     }
91
92     @Override
93     public Future<RpcResult<RemoveFlowOutput>> removeFlow(final RemoveFlowInput input) {
94         LOG.trace("Calling remove flow for flow with ID ={}.", input.getFlowRef());
95
96         final ListenableFuture<RpcResult<RemoveFlowOutput>> future = flowRemove.handleServiceCall(input);
97         Futures.addCallback(future, new FutureCallback<RpcResult<RemoveFlowOutput>>() {
98             @Override
99             public void onSuccess(final RpcResult<RemoveFlowOutput> result) {
100                 if (result.isSuccessful()) {
101                     FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(input);
102                     flowRemove.getDeviceContext().getDeviceFlowRegistry().markToBeremoved(flowRegistryKey);
103                 } else {
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());
110                             }
111                         }
112                         LOG.trace("Flow modification failed. Errors : {}", errors.toString());
113                     }
114                 }
115             }
116
117             @Override
118             public void onFailure(final Throwable throwable) {
119                 LOG.trace("Flow modification failed..", throwable);
120             }
121         });
122
123         return future;
124     }
125
126     @Override
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();
131
132         final List<FlowModInputBuilder> allFlowMods = new ArrayList<>();
133         final List<FlowModInputBuilder> ofFlowModInputs;
134
135         if (!FlowCreatorUtil.canModifyFlow(original, updated, flowUpdate.getVersion())) {
136             // We would need to remove original and add updated.
137
138             // remove flow
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());
145         } else {
146             ofFlowModInputs = flowUpdate.toFlowModInputs(updated);
147         }
148
149         allFlowMods.addAll(ofFlowModInputs);
150         ListenableFuture<RpcResult<UpdateFlowOutput>> future = flowUpdate.processFlowModInputBuilders(allFlowMods);
151         Futures.addCallback(future, new FutureCallback<RpcResult<UpdateFlowOutput>>() {
152             @Override
153             public void onSuccess(final RpcResult<UpdateFlowOutput> o) {
154                 FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(original);
155
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);
162             }
163
164             @Override
165             public void onFailure(final Throwable throwable) {
166                 LOG.debug("Flow update failed", throwable);
167             }
168         });
169         return future;
170     }
171 }