Merge "OPNFLWPLUG-929 : Remove deprecated guava library in openflowplugin-impl"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / AbstractMultipartCollectorService.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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 com.google.common.util.concurrent.FutureCallback;
11 import java.util.List;
12 import javax.annotation.Nonnull;
13 import javax.annotation.Nullable;
14 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
15 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
17 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
18 import org.opendaylight.openflowplugin.impl.common.MultipartRequestInputFactory;
19 import org.opendaylight.openflowplugin.impl.util.DeviceInitializationUtil;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
22
23 public abstract class AbstractMultipartCollectorService<T extends OfHeader>
24         extends AbstractMultipartService<MultipartType, T> {
25
26     protected AbstractMultipartCollectorService(final RequestContextStack requestContextStack,
27                                                 final DeviceContext deviceContext) {
28         super(requestContextStack, deviceContext);
29     }
30
31     @Override
32     protected FutureCallback<OfHeader> createCallback(RequestContext<List<T>> context, Class<?> requestType) {
33         final FutureCallback<OfHeader> callback = super.createCallback(context, requestType);
34
35         return new FutureCallback<OfHeader>() {
36             @Override
37             public void onSuccess(@Nullable final OfHeader result) {
38                 callback.onSuccess(result);
39             }
40
41             @Override
42             public void onFailure(@Nonnull final Throwable throwable) {
43                 // If we failed getting table features, at least create empty tables
44                 if (MultipartType.OFPMPTABLEFEATURES.getClass().equals(requestType)) {
45                     DeviceInitializationUtil.makeEmptyTables(
46                         getTxFacade(),
47                         getDeviceInfo(),
48                         getDeviceContext().getPrimaryConnectionContext().getFeatures().getTables());
49                 }
50
51                 callback.onFailure(throwable);
52             }
53         };
54     }
55
56     @Override
57     protected OfHeader buildRequest(final Xid xid, final MultipartType input) {
58         return MultipartRequestInputFactory.makeMultipartRequest(xid.getValue(),
59                                                                  getVersion(),
60                                                                  input,
61                                                                  canUseSingleLayerSerialization());
62     }
63 }