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.inventory.rev130819.tables.Table;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
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;
55 public final class SalTableServiceImpl extends AbstractMultipartService<UpdateTableInput> implements SalTableService {
56 private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(SalTableServiceImpl.class);
58 public SalTableServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
59 super(requestContextStack, deviceContext);
63 public Future<RpcResult<UpdateTableOutput>> updateTable(final UpdateTableInput input) {
64 final ListenableFuture<RpcResult<List<MultipartReply>>> multipartFuture = handleServiceCall(input);
65 final SettableFuture<RpcResult<UpdateTableOutput>> finalFuture = SettableFuture.create();
67 class CallBackImpl implements FutureCallback<RpcResult<List<MultipartReply>>> {
69 public void onSuccess(final RpcResult<List<MultipartReply>> result) {
71 if (result.isSuccessful()) {
72 final List<MultipartReply> multipartReplies = result.getResult();
73 if (multipartReplies.isEmpty()) {
74 LOG.debug("Multipart reply to table features request shouldn't be empty list.");
75 finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed()
76 .withError(ErrorType.RPC, "Multipart reply list is empty.").build());
78 final Long xid = multipartReplies.get(0).getXid();
80 "OnSuccess, rpc result successful, multipart response for rpc update-table with xid {} obtained.",
82 final UpdateTableOutputBuilder updateTableOutputBuilder = new UpdateTableOutputBuilder();
83 updateTableOutputBuilder.setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
84 finalFuture.set(RpcResultBuilder.success(updateTableOutputBuilder.build()).build());
85 writeResponseToOperationalDatastore(multipartReplies);
88 LOG.debug("OnSuccess, rpc result unsuccessful, multipart response for rpc update-table was unsuccessful.");
89 finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed().withRpcErrors(result.getErrors())
95 public void onFailure(final Throwable t) {
96 LOG.debug("Failure multipart response for table features request. Exception: {}", t);
97 finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed()
98 .withError(ErrorType.RPC, "Future error", t).build());
102 Futures.addCallback(multipartFuture, new CallBackImpl());
108 * @param multipartReplies
110 private void writeResponseToOperationalDatastore(final List<MultipartReply> multipartReplies) {
112 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures> salTableFeatures = convertToSalTableFeatures(multipartReplies);
114 final DeviceContext deviceContext = getDeviceContext();
115 final NodeId nodeId = deviceContext.getPrimaryConnectionContext().getNodeId();
116 final InstanceIdentifier<FlowCapableNode> flowCapableNodeII = InstanceIdentifier.create(Nodes.class)
117 .child(Node.class, new NodeKey(nodeId)).augmentation(FlowCapableNode.class);
118 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures tableFeatureData : salTableFeatures) {
119 final Short tableId = tableFeatureData.getTableId();
120 final KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures, TableFeaturesKey> tableFeaturesII = flowCapableNodeII
121 .child(Table.class, new TableKey(tableId))
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,
129 protected static List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures> convertToSalTableFeatures(
130 final List<MultipartReply> multipartReplies) {
131 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures> salTableFeaturesAll = new ArrayList<>();
132 for (final MultipartReply multipartReply : multipartReplies) {
133 if (multipartReply.getType().equals(MultipartType.OFPMPTABLEFEATURES)) {
134 final MultipartReplyBody multipartReplyBody = multipartReply.getMultipartReplyBody();
135 if (multipartReplyBody instanceof MultipartReplyTableFeaturesCase) {
136 final MultipartReplyTableFeaturesCase tableFeaturesCase = ((MultipartReplyTableFeaturesCase) multipartReplyBody);
137 final MultipartReplyTableFeatures salTableFeatures = tableFeaturesCase
138 .getMultipartReplyTableFeatures();
139 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures> salTableFeaturesPartial = TableFeaturesReplyConvertor
140 .toTableFeaturesReply(salTableFeatures);
141 salTableFeaturesAll.addAll(salTableFeaturesPartial);
142 LOG.debug("TableFeature {} for xid {}.", salTableFeatures, multipartReply.getXid());
146 return salTableFeaturesAll;
149 private MultipartRequestInputBuilder createMultipartHeader(final MultipartType multipart, final Long xid) {
150 final MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
151 mprInput.setType(multipart);
152 mprInput.setVersion(getVersion());
153 mprInput.setXid(xid);
154 mprInput.setFlags(new MultipartRequestFlags(false));
159 protected OfHeader buildRequest(final Xid xid, final UpdateTableInput input) {
160 final MultipartRequestTableFeaturesCaseBuilder caseBuilder = new MultipartRequestTableFeaturesCaseBuilder();
161 final MultipartRequestTableFeaturesBuilder requestBuilder = new MultipartRequestTableFeaturesBuilder();
162 final List<TableFeatures> ofTableFeatureList = TableFeaturesConvertor.toTableFeaturesRequest(input
164 requestBuilder.setTableFeatures(ofTableFeatureList);
165 caseBuilder.setMultipartRequestTableFeatures(requestBuilder.build());
167 // Set request body to main multipart request
168 final MultipartRequestInputBuilder mprInput = createMultipartHeader(MultipartType.OFPMPTABLEFEATURES,
170 mprInput.setMultipartRequestBody(caseBuilder.build());
172 return mprInput.build();