CommonService publishes its methods and properties
[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.SettableFuture;
12 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
13 import org.opendaylight.yangtools.yang.binding.DataObject;
14 import com.google.common.util.concurrent.Futures;
15 import java.math.BigInteger;
16 import java.util.concurrent.Future;
17 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
18 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
20 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
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     protected short version;
36     protected BigInteger datapathId;
37     protected RpcContext rpcContext;
38     protected DeviceContext deviceContext;
39     private ConnectionAdapter primaryConnectionAdapter;
40
41     public CommonService() {
42     }
43
44     public CommonService(final RpcContext rpcContext) {
45         this.rpcContext = rpcContext;
46
47         this.deviceContext = rpcContext.getDeviceContext();
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         // TODO uncomment when getAuxiali.... will be merged to APIs
67         // final ConnectionContext auxiliaryConnectionContext =
68         // deviceContext.getAuxiliaryConnectionContext(connectionID);
69         final ConnectionContext auxiliaryConnectionContext = null;
70         if (auxiliaryConnectionContext != null) {
71             return auxiliaryConnectionContext.getConnectionAdapter();
72         }
73
74         return primaryConnectionAdapter;
75     }
76
77     <T extends DataObject, F> Future<RpcResult<T>> handleServiceCall(final BigInteger connectionID,
78             final Function<DataCrate<T>, Future<RpcResult<F>>> function) {
79         LOG.debug("Calling the FlowMod RPC method on MessageDispatchService");
80
81         final RequestContext<T> requestContext = rpcContext.createRequestContext();
82         final SettableFuture<RpcResult<T>> result = rpcContext.storeOrFail(requestContext);
83         final DataCrate<T> dataCrate = DataCrateBuilder.<T> builder().setiDConnection(connectionID)
84                 .setRequestContext(requestContext).build();
85         if (!result.isDone()) {
86             final Future<RpcResult<F>> resultFromOFLib = function.apply(dataCrate);
87
88             final RpcResultConvertor<T> rpcResultConvertor = new RpcResultConvertor<>(requestContext, deviceContext);
89             rpcResultConvertor.processResultFromOfJava(resultFromOFLib);
90
91         } else {
92             RequestContextUtil.closeRequstContext(requestContext);
93         }
94         return result;
95     }
96
97 }