5c07b8ea28527d2bcbc05d7ff2a8e1c6b9e274dd
[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.util.concurrent.Futures;
11 import java.math.BigInteger;
12 import java.util.concurrent.Future;
13 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
14 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
16 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
18 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
19 import org.opendaylight.yangtools.yang.common.RpcResult;
20 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
21 import org.slf4j.Logger;
22
23 public class CommonService {
24     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(CommonService.class);
25     private static final long WAIT_TIME = 2000;
26     protected final static Future<RpcResult<Void>> ERROR_RPC_RESULT = Futures.immediateFuture(RpcResultBuilder
27             .<Void> failed().withError(ErrorType.APPLICATION, "", "Request quota exceeded.").build());
28     protected static final BigInteger PRIMARY_CONNECTION = new BigInteger("0");
29
30     // protected OFRpcTaskContext rpcTaskContext;
31     protected short version;
32     protected BigInteger datapathId;
33     protected RpcContext rpcContext;
34     protected DeviceContext deviceContext;
35     private ConnectionAdapter primaryConnectionAdapter;
36
37     public CommonService() {
38     }
39
40     public CommonService(final RpcContext rpcContext) {
41         this.rpcContext = rpcContext;
42
43         this.deviceContext = rpcContext.getDeviceContext();
44         final FeaturesReply features = this.deviceContext.getPrimaryConnectionContext().getFeatures();
45         this.datapathId = features.getDatapathId();
46         this.version = features.getVersion();
47         this.primaryConnectionAdapter = deviceContext.getPrimaryConnectionContext().getConnectionAdapter();
48     }
49
50     protected long provideWaitTime() {
51         return WAIT_TIME;
52     }
53
54     protected ConnectionAdapter provideConnectionAdapter(final BigInteger connectionID) {
55         if (connectionID == null) {
56             return primaryConnectionAdapter;
57         }
58         if (connectionID.equals(PRIMARY_CONNECTION)) {
59             return primaryConnectionAdapter;
60         }
61
62         // TODO uncomment when getAuxiali.... will be merged to APIs
63         // final ConnectionContext auxiliaryConnectionContext =
64         // deviceContext.getAuxiliaryConnectionContext(connectionID);
65         final ConnectionContext auxiliaryConnectionContext = null;
66         if (auxiliaryConnectionContext != null) {
67             return auxiliaryConnectionContext.getConnectionAdapter();
68         }
69
70         return primaryConnectionAdapter;
71     }
72
73 }