Simplify CommonService interface
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / MultipartRequestCallback.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 java.util.List;
11 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
12 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
13 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
16 import org.opendaylight.yangtools.yang.common.RpcError;
17 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 final class MultipartRequestCallback extends AbstractRequestCallback<List<MultipartReply>> {
22     private static final Logger LOG = LoggerFactory.getLogger(MultipartRequestCallback.class);
23     private final MultiMsgCollector collector;
24
25     public MultipartRequestCallback(final RequestContext<List<MultipartReply>> context, final Class<?> requestType, final DeviceContext deviceContext) {
26         super(context, requestType, deviceContext.getMessageSpy());
27         collector = deviceContext.getMultiMsgCollector(context);
28     }
29
30     @Override
31     public void onSuccess(final OfHeader result) {
32         if (result == null) {
33             LOG.info("Ofheader was null.");
34             collector.endCollecting();
35             return;
36         }
37
38         if (!(result instanceof MultipartReply)) {
39             LOG.info("Unexpected response type received {}.", result.getClass());
40             final RpcResultBuilder<List<MultipartReply>> rpcResultBuilder =
41                     RpcResultBuilder.<List<MultipartReply>>failed().withError(RpcError.ErrorType.APPLICATION,
42                         String.format("Unexpected response type received %s.", result.getClass()));
43             setResult(rpcResultBuilder.build());
44         } else {
45             collector.addMultipartMsg((MultipartReply) result);
46         }
47     }
48
49 }