2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.services;
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;
53 public final class SalTableServiceImpl extends AbstractMultipartService<UpdateTableInput> implements SalTableService {
54 private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(SalTableServiceImpl.class);
56 public SalTableServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
57 super(requestContextStack, deviceContext);
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();
65 class CallBackImpl implements FutureCallback<RpcResult<List<MultipartReply>>> {
67 public void onSuccess(final RpcResult<List<MultipartReply>> result) {
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());
76 final Long xid = multipartReplies.get(0).getXid();
78 "OnSuccess, rpc result successful, multipart response for rpc update-table with xid {} obtained.",
80 final UpdateTableOutputBuilder updateTableOutputBuilder = new UpdateTableOutputBuilder();
81 updateTableOutputBuilder.setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
82 finalFuture.set(RpcResultBuilder.success(updateTableOutputBuilder.build()).build());
84 writeResponseToOperationalDatastore(multipartReplies);
85 } catch (Exception e) {
86 LOG.warn("Not able to write to operational datastore: {}", e.getMessage());
90 LOG.debug("OnSuccess, rpc result unsuccessful, multipart response for rpc update-table was unsuccessful.");
91 finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed().withRpcErrors(result.getErrors())
97 public void onFailure(final Throwable t) {
98 LOG.debug("Failure multipart response for table features request. Exception: {}", t);
99 finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed()
100 .withError(ErrorType.RPC, "Future error", t).build());
104 Futures.addCallback(multipartFuture, new CallBackImpl());
110 * @param multipartReplies
112 private void writeResponseToOperationalDatastore(final List<MultipartReply> multipartReplies) throws Exception {
114 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures> salTableFeatures = convertToSalTableFeatures(multipartReplies);
116 final DeviceContext deviceContext = getDeviceContext();
117 final NodeId nodeId = deviceContext.getPrimaryConnectionContext().getNodeId();
118 final InstanceIdentifier<FlowCapableNode> flowCapableNodeII = InstanceIdentifier.create(Nodes.class)
119 .child(Node.class, new NodeKey(nodeId)).augmentation(FlowCapableNode.class);
120 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures tableFeatureData : salTableFeatures) {
121 final Short tableId = tableFeatureData.getTableId();
122 final KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures, TableFeaturesKey> tableFeaturesII = flowCapableNodeII
123 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures.class,
124 new TableFeaturesKey(tableId));
125 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableFeaturesII,
128 deviceContext.submitTransaction();
131 protected static List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures> convertToSalTableFeatures(
132 final List<MultipartReply> multipartReplies) {
133 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures> salTableFeaturesAll = new ArrayList<>();
134 for (final MultipartReply multipartReply : multipartReplies) {
135 if (multipartReply.getType().equals(MultipartType.OFPMPTABLEFEATURES)) {
136 final MultipartReplyBody multipartReplyBody = multipartReply.getMultipartReplyBody();
137 if (multipartReplyBody instanceof MultipartReplyTableFeaturesCase) {
138 final MultipartReplyTableFeaturesCase tableFeaturesCase = ((MultipartReplyTableFeaturesCase) multipartReplyBody);
139 final MultipartReplyTableFeatures salTableFeatures = tableFeaturesCase
140 .getMultipartReplyTableFeatures();
141 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures> salTableFeaturesPartial = TableFeaturesReplyConvertor
142 .toTableFeaturesReply(salTableFeatures);
143 salTableFeaturesAll.addAll(salTableFeaturesPartial);
144 LOG.debug("TableFeature {} for xid {}.", salTableFeatures, multipartReply.getXid());
148 return salTableFeaturesAll;
151 private MultipartRequestInputBuilder createMultipartHeader(final MultipartType multipart, final Long xid) {
152 final MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
153 mprInput.setType(multipart);
154 mprInput.setVersion(getVersion());
155 mprInput.setXid(xid);
156 mprInput.setFlags(new MultipartRequestFlags(false));
161 protected OfHeader buildRequest(final Xid xid, final UpdateTableInput input) {
162 final MultipartRequestTableFeaturesCaseBuilder caseBuilder = new MultipartRequestTableFeaturesCaseBuilder();
163 final MultipartRequestTableFeaturesBuilder requestBuilder = new MultipartRequestTableFeaturesBuilder();
164 final List<TableFeatures> ofTableFeatureList = TableFeaturesConvertor.toTableFeaturesRequest(input
166 requestBuilder.setTableFeatures(ofTableFeatureList);
167 caseBuilder.setMultipartRequestTableFeatures(requestBuilder.build());
169 // Set request body to main multipart request
170 final MultipartRequestInputBuilder mprInput = createMultipartHeader(MultipartType.OFPMPTABLEFEATURES,
172 mprInput.setMultipartRequestBody(caseBuilder.build());
174 return mprInput.build();