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.base.Function;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.SettableFuture;
15 import java.math.BigInteger;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.concurrent.Future;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
21 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
22 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
23 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
24 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
25 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
26 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
27 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.TableFeaturesConvertor;
28 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.TableFeaturesReplyConvertor;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.MultipartReplyBody;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableFeaturesCase;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeatures;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestTableFeaturesCaseBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table.features._case.MultipartRequestTableFeaturesBuilder;
48 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;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutputBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeaturesKey;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
56 import org.opendaylight.yangtools.yang.common.RpcError;
57 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
58 import org.opendaylight.yangtools.yang.common.RpcResult;
59 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
60 import org.slf4j.Logger;
62 public class SalTableServiceImpl extends CommonService implements SalTableService {
64 private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(SalTableServiceImpl.class);
66 public SalTableServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
67 super(requestContextStack, deviceContext);
71 public Future<RpcResult<UpdateTableOutput>> updateTable(final UpdateTableInput input) {
72 class FunctionImpl implements
73 Function<RequestContext<List<MultipartReply>>, ListenableFuture<RpcResult<Void>>> {
76 public ListenableFuture<RpcResult<Void>> apply(final RequestContext<List<MultipartReply>> requestContext) {
77 getMessageSpy().spyMessage(input.getImplementedInterface(),
78 MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
80 final SettableFuture<RpcResult<List<MultipartReply>>> result = SettableFuture.create();
82 final MultipartRequestTableFeaturesCaseBuilder caseBuilder = new MultipartRequestTableFeaturesCaseBuilder();
83 final MultipartRequestTableFeaturesBuilder requestBuilder = new MultipartRequestTableFeaturesBuilder();
84 final List<TableFeatures> ofTableFeatureList = TableFeaturesConvertor.toTableFeaturesRequest(input
86 requestBuilder.setTableFeatures(ofTableFeatureList);
87 caseBuilder.setMultipartRequestTableFeatures(requestBuilder.build());
89 // Set request body to main multipart request
90 final Xid xid = requestContext.getXid();
91 getDeviceContext().getMultiMsgCollector().registerMultipartXid(xid.getValue());
92 final MultipartRequestInputBuilder mprInput = createMultipartHeader(MultipartType.OFPMPTABLEFEATURES,
94 mprInput.setMultipartRequestBody(caseBuilder.build());
95 final OutboundQueue outboundQueue = getDeviceContext().getPrimaryConnectionContext().getOutboundQueueProvider();
97 final SettableFuture<RpcResult<Void>> settableFuture = SettableFuture.create();
98 final MultiMsgCollector multiMsgCollector = getDeviceContext().getMultiMsgCollector();
99 multiMsgCollector.registerMultipartXid(xid.getValue());
101 final MultipartRequestInput multipartRequestInput = mprInput.build();
102 outboundQueue.commitEntry(xid.getValue(), multipartRequestInput, new FutureCallback<OfHeader>() {
104 public void onSuccess(final OfHeader ofHeader) {
105 if (ofHeader instanceof MultipartReply) {
106 MultipartReply multipartReply = (MultipartReply) ofHeader;
107 multiMsgCollector.addMultipartMsg(multipartReply);
109 if (null != ofHeader) {
110 LOG.info("Unexpected response type received {}.", ofHeader.getClass());
112 LOG.info("Response received is null.");
115 getMessageSpy().spyMessage(multipartRequestInput.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
116 settableFuture.set(RpcResultBuilder.<Void>success().build());
120 public void onFailure(final Throwable throwable) {
121 RpcResultBuilder<Void> rpcResultBuilder = RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, throwable.getMessage(), throwable);
122 RequestContextUtil.closeRequstContext(requestContext);
123 getDeviceContext().unhookRequestCtx(requestContext.getXid());
124 getMessageSpy().spyMessage(multipartRequestInput.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
125 settableFuture.set(rpcResultBuilder.build());
128 return settableFuture;
132 final ListenableFuture<RpcResult<List<MultipartReply>>> multipartFuture = handleServiceCall(new FunctionImpl());
133 final SettableFuture<RpcResult<UpdateTableOutput>> finalFuture = SettableFuture.create();
135 class CallBackImpl implements FutureCallback<RpcResult<List<MultipartReply>>> {
136 private final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(CallBackImpl.class);
139 public void onSuccess(final RpcResult<List<MultipartReply>> result) {
141 if (result.isSuccessful()) {
142 final List<MultipartReply> multipartReplies = result.getResult();
143 if (multipartReplies.isEmpty()) {
144 LOGGER.debug("Multipart reply to table features request shouldn't be empty list.");
145 finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed()
146 .withError(ErrorType.RPC, "Multipart reply list is empty.").build());
148 final Long xid = multipartReplies.get(0).getXid();
150 "OnSuccess, rpc result successful, multipart response for rpc update-table with xid {} obtained.",
152 final UpdateTableOutputBuilder updateTableOutputBuilder = new UpdateTableOutputBuilder();
153 updateTableOutputBuilder.setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
154 finalFuture.set(RpcResultBuilder.success(updateTableOutputBuilder.build()).build());
155 writeResponseToOperationalDatastore(multipartReplies);
158 LOGGER.debug("OnSuccess, rpc result unsuccessful, multipart response for rpc update-table was unsuccessful.");
159 finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed().withRpcErrors(result.getErrors())
165 public void onFailure(final Throwable t) {
166 LOGGER.debug("Failure multipart response for table features request. Exception: {}", t);
167 finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed()
168 .withError(ErrorType.RPC, "Future error", t).build());
172 * @param multipartReplies
174 private void writeResponseToOperationalDatastore(final List<MultipartReply> multipartReplies) {
176 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures> salTableFeatures = convertToSalTableFeatures(multipartReplies);
178 final DeviceContext deviceContext = getDeviceContext();
179 final NodeId nodeId = deviceContext.getPrimaryConnectionContext().getNodeId();
180 final InstanceIdentifier<FlowCapableNode> flowCapableNodeII = InstanceIdentifier.create(Nodes.class)
181 .child(Node.class, new NodeKey(nodeId)).augmentation(FlowCapableNode.class);
182 for (org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures tableFeatureData : salTableFeatures) {
183 final Short tableId = tableFeatureData.getTableId();
184 KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures, TableFeaturesKey> tableFeaturesII = flowCapableNodeII
185 .child(Table.class, new TableKey(tableId))
186 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures.class,
187 new TableFeaturesKey(tableId));
188 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tableFeaturesII,
194 private List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures> convertToSalTableFeatures(
195 final List<MultipartReply> multipartReplies) {
196 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures> salTableFeaturesAll = new ArrayList<>();
197 for (MultipartReply multipartReply : multipartReplies) {
198 if (multipartReply.getType().equals(MultipartType.OFPMPTABLEFEATURES)) {
199 MultipartReplyBody multipartReplyBody = multipartReply.getMultipartReplyBody();
200 if (multipartReplyBody instanceof MultipartReplyTableFeaturesCase) {
201 MultipartReplyTableFeaturesCase tableFeaturesCase = ((MultipartReplyTableFeaturesCase) multipartReplyBody);
202 MultipartReplyTableFeatures salTableFeatures = tableFeaturesCase
203 .getMultipartReplyTableFeatures();
204 List<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures> salTableFeaturesPartial = TableFeaturesReplyConvertor
205 .toTableFeaturesReply(salTableFeatures);
206 salTableFeaturesAll.addAll(salTableFeaturesPartial);
207 LOGGER.debug("TableFeature {} for xid {}.", salTableFeatures, multipartReply.getXid());
211 return salTableFeaturesAll;
216 Futures.addCallback(multipartFuture, new CallBackImpl());
221 private MultipartRequestInputBuilder createMultipartHeader(final MultipartType multipart, final Long xid) {
222 final MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
223 mprInput.setType(multipart);
224 mprInput.setVersion(getVersion());
225 mprInput.setXid(xid);
226 mprInput.setFlags(new MultipartRequestFlags(false));