Merge "Bug 6176 - Decrease logging level in Sal-F/G/M-Service and use synchronized...
[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.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.flow.types.rev131026.FlowRef;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
52 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
53 import org.opendaylight.yangtools.yang.common.RpcResult;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
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;
64
65     public SalFlowServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext, final ConvertorExecutor convertorExecutor) {
66         this.deviceContext = deviceContext;
67         flowRemove = new FlowService<>(requestContextStack, deviceContext, RemoveFlowOutput.class, convertorExecutor);
68         flowAdd = new FlowService<>(requestContextStack, deviceContext, AddFlowOutput.class, convertorExecutor);
69         flowUpdate = new FlowService<>(requestContextStack, deviceContext, UpdateFlowOutput.class, convertorExecutor);
70     }
71
72     @Override
73     public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
74         this.itemLifecycleListener = itemLifecycleListener;
75     }
76
77     @Override
78     public Future<RpcResult<AddFlowOutput>> addFlow(final AddFlowInput input) {
79         final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(input);
80         final ListenableFuture<RpcResult<AddFlowOutput>> future =
81                 flowAdd.processFlowModInputBuilders(flowAdd.toFlowModInputs(input));
82         Futures.addCallback(future, new AddFlowCallback(input, flowRegistryKey));
83         return future;
84     }
85
86     @Override
87     public Future<RpcResult<RemoveFlowOutput>> removeFlow(final RemoveFlowInput input) {
88         final ListenableFuture<RpcResult<RemoveFlowOutput>> future =
89                 flowRemove.processFlowModInputBuilders(flowRemove.toFlowModInputs(input));
90         Futures.addCallback(future, new RemoveFlowCallback(input));
91         return future;
92     }
93
94     @Override
95     public Future<RpcResult<UpdateFlowOutput>> updateFlow(final UpdateFlowInput input) {
96         final UpdatedFlow updated = input.getUpdatedFlow();
97         final OriginalFlow original = input.getOriginalFlow();
98
99         final List<FlowModInputBuilder> allFlowMods = new ArrayList<>();
100         final List<FlowModInputBuilder> ofFlowModInputs;
101
102         if (!FlowCreatorUtil.canModifyFlow(original, updated, flowUpdate.getVersion())) {
103             // We would need to remove original and add updated.
104
105             // remove flow
106             final RemoveFlowInputBuilder removeflow = new RemoveFlowInputBuilder(original);
107             final List<FlowModInputBuilder> ofFlowRemoveInput = flowUpdate.toFlowModInputs(removeflow.build());
108             // remove flow should be the first
109             allFlowMods.addAll(ofFlowRemoveInput);
110             final AddFlowInputBuilder addFlowInputBuilder = new AddFlowInputBuilder(updated);
111             ofFlowModInputs = flowUpdate.toFlowModInputs(addFlowInputBuilder.build());
112         } else {
113             ofFlowModInputs = flowUpdate.toFlowModInputs(updated);
114         }
115
116         allFlowMods.addAll(ofFlowModInputs);
117         ListenableFuture<RpcResult<UpdateFlowOutput>> future = flowUpdate.processFlowModInputBuilders(allFlowMods);
118         Futures.addCallback(future, new UpdateFlowCallback(input));
119         return future;
120     }
121
122     @VisibleForTesting
123     private static KeyedInstanceIdentifier<Flow, FlowKey> createFlowPath(FlowDescriptor flowDescriptor,
124                                                                  KeyedInstanceIdentifier<Node, NodeKey> nodePath) {
125         return nodePath.augmentation(FlowCapableNode.class)
126                 .child(Table.class, flowDescriptor.getTableKey())
127                 .child(Flow.class, new FlowKey(flowDescriptor.getFlowId()));
128     }
129
130     private class AddFlowCallback implements FutureCallback<RpcResult<AddFlowOutput>> {
131         private final AddFlowInput input;
132         private final FlowRegistryKey flowRegistryKey;
133
134         private AddFlowCallback(final AddFlowInput input,
135                                 final FlowRegistryKey flowRegistryKey) {
136             this.input = input;
137             this.flowRegistryKey = flowRegistryKey;
138         }
139
140         @Override
141         public void onSuccess(final RpcResult<AddFlowOutput> rpcResult) {
142             if (rpcResult.isSuccessful()) {
143                 final FlowDescriptor flowDescriptor;
144
145                 if (Objects.nonNull(input.getFlowRef())) {
146                     final FlowId flowId = input.getFlowRef().getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
147                     flowDescriptor = FlowDescriptorFactory.create(input.getTableId(), flowId);
148                     deviceContext.getDeviceFlowRegistry().store(flowRegistryKey, flowDescriptor);
149                 } else {
150                     final FlowId flowId = deviceContext.getDeviceFlowRegistry().storeIfNecessary(flowRegistryKey);
151                     flowDescriptor = FlowDescriptorFactory.create(input.getTableId(), flowId);
152                 }
153
154                 if (LOG.isDebugEnabled()) {
155                     LOG.debug("Flow add with id={} finished without error", flowDescriptor.getFlowId().getValue());
156                 }
157
158                 if (itemLifecycleListener != null) {
159                     KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
160                             deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
161                     final FlowBuilder flowBuilder = new FlowBuilder(input).setId(flowDescriptor.getFlowId());
162                     itemLifecycleListener.onAdded(flowPath, flowBuilder.build());
163                 }
164             } else {
165                 if (LOG.isDebugEnabled()) {
166                     LOG.debug("Flow add failed for flow={}, errors={}", input,
167                             ErrorUtil.errorsToString(rpcResult.getErrors()));
168                 }
169             }
170         }
171
172         @Override
173         public void onFailure(final Throwable throwable) {
174             LOG.warn("Service call for adding flow={} failed, reason: {}", input, throwable);
175         }
176     }
177
178     private class RemoveFlowCallback implements FutureCallback<RpcResult<RemoveFlowOutput>> {
179         private final RemoveFlowInput input;
180
181         private RemoveFlowCallback(final RemoveFlowInput input) {
182             this.input = input;
183         }
184
185         @Override
186         public void onSuccess(final RpcResult<RemoveFlowOutput> result) {
187             if (result.isSuccessful()) {
188                 if (LOG.isDebugEnabled()) {
189                     LOG.debug("Flow remove finished without error for flow={}", input);
190                 }
191                 FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(input);
192                 deviceContext.getDeviceFlowRegistry().removeDescriptor(flowRegistryKey);
193
194                 if (itemLifecycleListener != null) {
195                     final FlowDescriptor flowDescriptor =
196                             deviceContext.getDeviceFlowRegistry().retrieveIdForFlow(flowRegistryKey);
197                     if (flowDescriptor != null) {
198                         KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
199                                 deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
200                         itemLifecycleListener.onRemoved(flowPath);
201                     }
202                 }
203             } else {
204                 if (LOG.isDebugEnabled()) {
205                     LOG.debug("Flow remove failed for flow={}, errors={}", input,
206                             ErrorUtil.errorsToString(result.getErrors()));
207                 }
208             }
209         }
210
211         @Override
212         public void onFailure(final Throwable throwable) {
213             LOG.warn("Service call for removing flow={} failed, reason: {}", input, throwable);
214         }
215     }
216
217     private class UpdateFlowCallback implements FutureCallback<RpcResult<UpdateFlowOutput>> {
218         private final UpdateFlowInput input;
219
220         private UpdateFlowCallback(UpdateFlowInput input) {
221             this.input = input;
222         }
223
224         @Override
225         public void onSuccess(final RpcResult<UpdateFlowOutput> o) {
226             final UpdatedFlow updated = input.getUpdatedFlow();
227             final OriginalFlow original = input.getOriginalFlow();
228             FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(original);
229
230             FlowRegistryKey updatedflowRegistryKey = FlowRegistryKeyFactory.create(updated);
231             final FlowRef flowRef = input.getFlowRef();
232             final DeviceFlowRegistry deviceFlowRegistry = deviceContext.getDeviceFlowRegistry();
233
234             if (flowRef == null) {
235                 // then this is equivalent to a delete
236                 deviceFlowRegistry.removeDescriptor(flowRegistryKey);
237
238                 if (itemLifecycleListener != null) {
239                     final FlowDescriptor flowDescriptor =
240                             deviceContext.getDeviceFlowRegistry().retrieveIdForFlow( flowRegistryKey);
241                     KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
242                             deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
243                     itemLifecycleListener.onRemoved(flowPath);
244                 }
245             } else {
246                 // this is either an add or an update
247                 final FlowId flowId = flowRef.getValue().firstKeyOf(Flow.class, FlowKey.class).getId();
248                 final FlowDescriptor flowDescriptor = FlowDescriptorFactory.create(updated.getTableId(), flowId);
249
250                 deviceFlowRegistry.update(updatedflowRegistryKey, flowDescriptor);
251
252                 if (itemLifecycleListener != null) {
253                     KeyedInstanceIdentifier<Flow, FlowKey> flowPath = createFlowPath(flowDescriptor,
254                             deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
255                     final FlowBuilder flowBuilder = new FlowBuilder(
256                             input.getUpdatedFlow()).setId(flowDescriptor.getFlowId());
257
258                     boolean isUpdate = null !=
259                             deviceFlowRegistry.retrieveIdForFlow(flowRegistryKey);
260                     if (isUpdate) {
261                         itemLifecycleListener.onUpdated(flowPath, flowBuilder.build());
262                     } else {
263                         itemLifecycleListener.onAdded(flowPath, flowBuilder.build());
264                     }
265                 }
266             }
267         }
268
269         @Override
270         public void onFailure(final Throwable throwable) {
271             LOG.warn("Service call for updating flow={} failed, reason: {}", input, throwable);
272         }
273     }
274 }