Bug 5540 - TableFeaturesResponseConvertor
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / SalTableServiceImpl.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 com.google.common.util.concurrent.SettableFuture;
14 import java.math.BigInteger;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.Optional;
19 import java.util.concurrent.Future;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
22 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
23 import org.opendaylight.openflowplugin.api.openflow.device.TxFacade;
24 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
25 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.MultipartReplyBody;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableFeaturesCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeatures;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestTableFeaturesCaseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table.features._case.MultipartRequestTableFeaturesBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table.features._case.multipart.request.table.features.TableFeatures;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutputBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeaturesKey;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
50 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
51 import org.opendaylight.yangtools.yang.common.RpcResult;
52 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
53 import org.slf4j.Logger;
54
55 public final class SalTableServiceImpl extends AbstractMultipartService<UpdateTableInput> implements SalTableService {
56     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(SalTableServiceImpl.class);
57     private final TxFacade txFacade;
58     private final NodeId nodeId;
59
60     public SalTableServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext,
61                                final NodeId nodeId) {
62         super(requestContextStack, deviceContext);
63         this.txFacade = deviceContext;
64         this.nodeId = nodeId;
65     }
66
67     @Override
68     public Future<RpcResult<UpdateTableOutput>> updateTable(final UpdateTableInput input) {
69         final ListenableFuture<RpcResult<List<MultipartReply>>> multipartFuture = handleServiceCall(input);
70         final SettableFuture<RpcResult<UpdateTableOutput>> finalFuture = SettableFuture.create();
71
72         class CallBackImpl implements FutureCallback<RpcResult<List<MultipartReply>>> {
73             @Override
74             public void onSuccess(final RpcResult<List<MultipartReply>> result) {
75
76                 if (result.isSuccessful()) {
77                     final List<MultipartReply> multipartReplies = result.getResult();
78                     if (multipartReplies.isEmpty()) {
79                         LOG.debug("Multipart reply to table features request shouldn't be empty list.");
80                         finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed()
81                                 .withError(ErrorType.RPC, "Multipart reply list is empty.").build());
82                     } else {
83                         final Long xid = multipartReplies.get(0).getXid();
84                         LOG.debug(
85                                 "OnSuccess, rpc result successful, multipart response for rpc update-table with xid {} obtained.",
86                                 xid);
87                         final UpdateTableOutputBuilder updateTableOutputBuilder = new UpdateTableOutputBuilder();
88                         updateTableOutputBuilder.setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
89                         finalFuture.set(RpcResultBuilder.success(updateTableOutputBuilder.build()).build());
90                         try {
91                             writeResponseToOperationalDatastore(multipartReplies);
92                         } catch (Exception e) {
93                             LOG.warn("Not able to write to operational datastore: {}", e.getMessage());
94                         }
95                     }
96                 } else {
97                     LOG.debug("OnSuccess, rpc result unsuccessful, multipart response for rpc update-table was unsuccessful.");
98                     finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed().withRpcErrors(result.getErrors())
99                             .build());
100                 }
101             }
102
103             @Override
104             public void onFailure(final Throwable t) {
105                 LOG.error("Failure multipart response for table features request. Exception: {}", t);
106                 finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed()
107                         .withError(ErrorType.RPC, "Future error", t).build());
108             }
109         }
110
111         Futures.addCallback(multipartFuture, new CallBackImpl());
112
113         return finalFuture;
114     }
115
116     /**
117      * @param multipartReplies
118      */
119     private void writeResponseToOperationalDatastore(final List<MultipartReply> multipartReplies) throws Exception {
120
121         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures> salTableFeatures = convertToSalTableFeatures(multipartReplies);
122
123         final InstanceIdentifier<FlowCapableNode> flowCapableNodeII = InstanceIdentifier.create(Nodes.class)
124                 .child(Node.class, new NodeKey(getDeviceInfo().getNodeId())).augmentation(FlowCapableNode.class);
125         for (final org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures tableFeatureData : salTableFeatures) {
126             final Short tableId = tableFeatureData.getTableId();
127             final KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures, TableFeaturesKey> tableFeaturesII = flowCapableNodeII
128                     .child(org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures.class,
129                             new TableFeaturesKey(tableId));
130             txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableFeaturesII,
131                     tableFeatureData);
132         }
133
134         txFacade.submitTransaction();
135     }
136
137     protected static List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures> convertToSalTableFeatures(
138             final List<MultipartReply> multipartReplies) {
139         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures> salTableFeaturesAll = new ArrayList<>();
140         for (final MultipartReply multipartReply : multipartReplies) {
141             if (multipartReply.getType().equals(MultipartType.OFPMPTABLEFEATURES)) {
142                 final MultipartReplyBody multipartReplyBody = multipartReply.getMultipartReplyBody();
143                 if (multipartReplyBody instanceof MultipartReplyTableFeaturesCase) {
144                     final MultipartReplyTableFeaturesCase tableFeaturesCase = ((MultipartReplyTableFeaturesCase) multipartReplyBody);
145                     final MultipartReplyTableFeatures salTableFeatures = tableFeaturesCase
146                             .getMultipartReplyTableFeatures();
147
148                     final Optional<List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures>> salTableFeaturesPartial =
149                             ConvertorManager.getInstance().convert(salTableFeatures);
150
151                     if (salTableFeaturesPartial.isPresent()) {
152                         salTableFeaturesAll.addAll(salTableFeaturesPartial.get());
153                     }
154
155                     LOG.debug("TableFeature {} for xid {}.", salTableFeatures, multipartReply.getXid());
156                 }
157             }
158         }
159         return salTableFeaturesAll;
160     }
161
162     private MultipartRequestInputBuilder createMultipartHeader(final MultipartType multipart, final Long xid) {
163         final MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
164         mprInput.setType(multipart);
165         mprInput.setVersion(getVersion());
166         mprInput.setXid(xid);
167         mprInput.setFlags(new MultipartRequestFlags(false));
168         return mprInput;
169     }
170
171     @Override
172     protected OfHeader buildRequest(final Xid xid, final UpdateTableInput input) {
173         final MultipartRequestTableFeaturesCaseBuilder caseBuilder = new MultipartRequestTableFeaturesCaseBuilder();
174         final MultipartRequestTableFeaturesBuilder requestBuilder = new MultipartRequestTableFeaturesBuilder();
175
176         final Optional<List<TableFeatures>> ofTableFeatureList = ConvertorManager.getInstance().convert(input.getUpdatedTable());
177         requestBuilder.setTableFeatures(ofTableFeatureList.orElse(Collections.emptyList()));
178         caseBuilder.setMultipartRequestTableFeatures(requestBuilder.build());
179
180         // Set request body to main multipart request
181         final MultipartRequestInputBuilder mprInput = createMultipartHeader(MultipartType.OFPMPTABLEFEATURES,
182             xid.getValue());
183         mprInput.setMultipartRequestBody(caseBuilder.build());
184
185         return mprInput.build();
186     }
187 }