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