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