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