7ab9586fafd606efff0fcd8b814b324ae249693a
[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 java.math.BigInteger;
15 import java.util.concurrent.Future;
16 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
17 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
20 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
22 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
23 import org.opendaylight.yangtools.yang.common.RpcResult;
24 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
25 import org.slf4j.Logger;
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     /**
42      * @deprecated use {@link #CommonService(RequestContextStack, DeviceContext)}
43      */
44     @Deprecated
45     public CommonService() {
46     }
47
48     public CommonService(final RequestContextStack requestContextStack, DeviceContext deviceContext) {
49         this.requestContextStack = requestContextStack;
50
51         this.deviceContext = deviceContext;
52         final FeaturesReply features = this.deviceContext.getPrimaryConnectionContext().getFeatures();
53         this.datapathId = features.getDatapathId();
54         this.version = features.getVersion();
55         this.primaryConnectionAdapter = deviceContext.getPrimaryConnectionContext().getConnectionAdapter();
56     }
57
58     protected long provideWaitTime() {
59         return WAIT_TIME;
60     }
61
62     protected ConnectionAdapter provideConnectionAdapter(final BigInteger connectionID) {
63         if (connectionID == null) {
64             return primaryConnectionAdapter;
65         }
66         if (connectionID.equals(PRIMARY_CONNECTION)) {
67             return primaryConnectionAdapter;
68         }
69
70         final ConnectionContext auxiliaryConnectionContext =
71         deviceContext.getAuxiliaryConnectiobContexts(connectionID);
72         if (auxiliaryConnectionContext != null) {
73             return auxiliaryConnectionContext.getConnectionAdapter();
74         }
75
76         return primaryConnectionAdapter;
77     }
78
79     public <T, F> Future<RpcResult<T>> handleServiceCall(final BigInteger connectionID,
80                                                                             final Function<DataCrate<T>, ListenableFuture<RpcResult<F>>> function) {
81         LOG.debug("Calling the FlowMod RPC method on MessageDispatchService");
82
83         final RequestContext<T> requestContext = requestContextStack.createRequestContext();
84         final SettableFuture<RpcResult<T>> result = requestContextStack.storeOrFail(requestContext);
85         if (!result.isDone()) {
86             final DataCrate<T> dataCrate = DataCrateBuilder.<T>builder().setiDConnection(connectionID)
87                     .setRequestContext(requestContext).build();
88             requestContext.setXid(deviceContext.getNextXid());
89
90             LOG.trace("Hooking xid {} to device context - precaution.", requestContext.getXid().getValue());
91             deviceContext.hookRequestCtx(requestContext.getXid(), requestContext);
92
93             final ListenableFuture<RpcResult<F>> resultFromOFLib = function.apply(dataCrate);
94
95             final OFJResult2RequestCtxFuture<T> OFJResult2RequestCtxFuture = new OFJResult2RequestCtxFuture<>(requestContext, deviceContext);
96             OFJResult2RequestCtxFuture.processResultFromOfJava(resultFromOFLib);
97
98         } else {
99             RequestContextUtil.closeRequstContext(requestContext);
100         }
101         return result;
102     }
103
104 }