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