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.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.ConvertorExecutor;
26 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionConvertorData;
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.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);
57 private final TxFacade txFacade;
58 private final ConvertorExecutor convertorExecutor;
59 private final VersionConvertorData data;
61 public SalTableServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext, final ConvertorExecutor convertorExecutor) {
62 super(requestContextStack, deviceContext);
63 this.txFacade = deviceContext;
64 this.convertorExecutor = convertorExecutor;
65 data = new VersionConvertorData(getVersion());
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();
73 class CallBackImpl implements FutureCallback<RpcResult<List<MultipartReply>>> {
75 public void onSuccess(final RpcResult<List<MultipartReply>> result) {
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());
84 final Long xid = multipartReplies.get(0).getXid();
86 "OnSuccess, rpc result successful, multipart response for rpc update-table with xid {} obtained.",
88 final UpdateTableOutputBuilder updateTableOutputBuilder = new UpdateTableOutputBuilder();
89 updateTableOutputBuilder.setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
90 finalFuture.set(RpcResultBuilder.success(updateTableOutputBuilder.build()).build());
92 writeResponseToOperationalDatastore(multipartReplies);
93 } catch (Exception e) {
94 LOG.warn("Not able to write to operational datastore: {}", e.getMessage());
98 LOG.debug("OnSuccess, rpc result unsuccessful, multipart response for rpc update-table was unsuccessful.");
99 finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed().withRpcErrors(result.getErrors())
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());
112 Futures.addCallback(multipartFuture, new CallBackImpl());
118 * @param multipartReplies
120 private void writeResponseToOperationalDatastore(final List<MultipartReply> multipartReplies) throws Exception {
122 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures> salTableFeatures = convertToSalTableFeatures(multipartReplies);
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,
135 txFacade.submitTransaction();
138 protected 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();
149 final Optional<List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures>> salTableFeaturesPartial =
150 convertorExecutor.convert(salTableFeatures, data);
152 if (salTableFeaturesPartial.isPresent()) {
153 salTableFeaturesAll.addAll(salTableFeaturesPartial.get());
156 LOG.debug("TableFeature {} for xid {}.", salTableFeatures, multipartReply.getXid());
160 return salTableFeaturesAll;
163 private MultipartRequestInputBuilder createMultipartHeader(final MultipartType multipart, final Long xid) {
164 final MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
165 mprInput.setType(multipart);
166 mprInput.setVersion(getVersion());
167 mprInput.setXid(xid);
168 mprInput.setFlags(new MultipartRequestFlags(false));
173 protected OfHeader buildRequest(final Xid xid, final UpdateTableInput input) {
174 final MultipartRequestTableFeaturesCaseBuilder caseBuilder = new MultipartRequestTableFeaturesCaseBuilder();
175 final MultipartRequestTableFeaturesBuilder requestBuilder = new MultipartRequestTableFeaturesBuilder();
177 final Optional<List<TableFeatures>> ofTableFeatureList = convertorExecutor.convert(input.getUpdatedTable(), data);
178 requestBuilder.setTableFeatures(ofTableFeatureList.orElse(Collections.emptyList()));
179 caseBuilder.setMultipartRequestTableFeatures(requestBuilder.build());
181 // Set request body to main multipart request
182 final MultipartRequestInputBuilder mprInput = createMultipartHeader(MultipartType.OFPMPTABLEFEATURES,
184 mprInput.setMultipartRequestBody(caseBuilder.build());
186 return mprInput.build();