Merge "Bug 2895: Fix for FRM ClassNotFoundException"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / CommonService.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 com.google.common.base.Function;
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 org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
15 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
20 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
21 import org.opendaylight.yangtools.yang.common.RpcResult;
22 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
23 import org.slf4j.Logger;
24 import java.math.BigInteger;
25 import java.util.concurrent.Future;
26
27 public abstract class CommonService {
28     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(CommonService.class);
29     private static final long WAIT_TIME = 2000;
30     protected final static Future<RpcResult<Void>> ERROR_RPC_RESULT = Futures.immediateFuture(RpcResultBuilder
31             .<Void>failed().withError(ErrorType.APPLICATION, "", "Request quota exceeded.").build());
32     protected static final BigInteger PRIMARY_CONNECTION = new BigInteger("0");
33
34     // protected OFRpcTaskContext rpcTaskContext;
35     public short version;
36     public BigInteger datapathId;
37     public RequestContextStack requestContextStack;
38     public DeviceContext deviceContext;
39     public ConnectionAdapter primaryConnectionAdapter;
40
41     public CommonService() {
42     }
43
44     public CommonService(final RequestContextStack requestContextStack, DeviceContext deviceContext) {
45         this.requestContextStack = requestContextStack;
46
47         this.deviceContext = deviceContext;
48         final FeaturesReply features = this.deviceContext.getPrimaryConnectionContext().getFeatures();
49         this.datapathId = features.getDatapathId();
50         this.version = features.getVersion();
51         this.primaryConnectionAdapter = deviceContext.getPrimaryConnectionContext().getConnectionAdapter();
52     }
53
54     protected long provideWaitTime() {
55         return WAIT_TIME;
56     }
57
58     protected ConnectionAdapter provideConnectionAdapter(final BigInteger connectionID) {
59         if (connectionID == null) {
60             return primaryConnectionAdapter;
61         }
62         if (connectionID.equals(PRIMARY_CONNECTION)) {
63             return primaryConnectionAdapter;
64         }
65
66         final ConnectionContext auxiliaryConnectionContext =
67         deviceContext.getAuxiliaryConnectiobContexts(connectionID);
68         if (auxiliaryConnectionContext != null) {
69             return auxiliaryConnectionContext.getConnectionAdapter();
70         }
71
72         return primaryConnectionAdapter;
73     }
74
75     public <T, F> Future<RpcResult<T>> handleServiceCall(final BigInteger connectionID,
76                                                                             final Function<DataCrate<T>, ListenableFuture<RpcResult<F>>> function) {
77         LOG.debug("Calling the FlowMod RPC method on MessageDispatchService");
78
79         final RequestContext<T> requestContext = requestContextStack.createRequestContext();
80         final SettableFuture<RpcResult<T>> result = requestContextStack.storeOrFail(requestContext);
81         final DataCrate<T> dataCrate = DataCrateBuilder.<T>builder().setiDConnection(connectionID)
82                 .setRequestContext(requestContext).build();
83         if (!result.isDone()) {
84             final ListenableFuture<RpcResult<F>> resultFromOFLib = function.apply(dataCrate);
85
86             final OFJResult2RequestCtxFuture<T> OFJResult2RequestCtxFuture = new OFJResult2RequestCtxFuture<>(requestContext, deviceContext);
87             OFJResult2RequestCtxFuture.processResultFromOfJava(resultFromOFLib);
88
89         } else {
90             RequestContextUtil.closeRequstContext(requestContext);
91         }
92         return result;
93     }
94
95 }