CommonService has its fields private
[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.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
23 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
26 import org.slf4j.Logger;
27
28 public abstract class CommonService {
29     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(CommonService.class);
30     private static final long WAIT_TIME = 2000;
31     private final static Future<RpcResult<Void>> ERROR_RPC_RESULT = Futures.immediateFuture(RpcResultBuilder
32             .<Void>failed().withError(ErrorType.APPLICATION, "", "Request quota exceeded.").build());
33
34
35     private static final BigInteger PRIMARY_CONNECTION = new BigInteger("0");
36
37     private final short version;
38     private final BigInteger datapathId;
39     private final RequestContextStack requestContextStack;
40     private final DeviceContext deviceContext;
41     private final ConnectionAdapter primaryConnectionAdapter;
42     private final MessageSpy messageSpy;
43
44
45     public CommonService(final RequestContextStack requestContextStack, DeviceContext deviceContext) {
46         this.requestContextStack = requestContextStack;
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         this.messageSpy = deviceContext.getMessageSpy();
53     }
54     public static BigInteger getPrimaryConnection() {
55         return PRIMARY_CONNECTION;
56     }
57
58     public short getVersion(){
59         return version;
60     }
61
62     public BigInteger getDatapathId() {
63         return datapathId;
64     }
65
66     public RequestContextStack getRequestContextStack() {
67         return requestContextStack;
68     }
69
70     public DeviceContext getDeviceContext() {
71         return deviceContext;
72     }
73
74     public ConnectionAdapter getPrimaryConnectionAdapter() {
75         return primaryConnectionAdapter;
76     }
77
78     public MessageSpy getMessageSpy() {
79         return messageSpy;
80     }
81
82     protected long provideWaitTime() {
83         return WAIT_TIME;
84     }
85
86
87     protected ConnectionAdapter provideConnectionAdapter(final BigInteger connectionID) {
88         if (connectionID == null) {
89             return primaryConnectionAdapter;
90         }
91         if (connectionID.equals(PRIMARY_CONNECTION)) {
92             return primaryConnectionAdapter;
93         }
94
95         final ConnectionContext auxiliaryConnectionContext =
96                 deviceContext.getAuxiliaryConnectiobContexts(connectionID);
97         if (auxiliaryConnectionContext != null) {
98             return auxiliaryConnectionContext.getConnectionAdapter();
99         }
100
101         return primaryConnectionAdapter;
102     }
103
104     /**
105      * @param connectionID connection identifier
106      * @param function     data sender
107      * @param <T>          rpc result backend type
108      * @param <F>          final rpc backend type
109      * @return
110      */
111     public <T, F> ListenableFuture<RpcResult<T>> handleServiceCall(final BigInteger connectionID,
112                                                                    final Function<DataCrate<T>, ListenableFuture<RpcResult<F>>> function) {
113         DataCrateBuilder<T> dataCrateBuilder = DataCrateBuilder.<T>builder();
114         return handleServiceCall(connectionID, function, dataCrateBuilder);
115     }
116     public <T, F> ListenableFuture<RpcResult<T>> handleServiceCall(final Function<DataCrate<T>, ListenableFuture<RpcResult<F>>> function) {
117         DataCrateBuilder<T> dataCrateBuilder = DataCrateBuilder.<T>builder();
118         return handleServiceCall(PRIMARY_CONNECTION, function, dataCrateBuilder);
119     }
120
121     /**
122      * @param <T>
123      * @param <F>
124      * @param connectionID
125      * @param function
126      * @param dataCrateBuilder predefined data
127      * @return
128      */
129     public final <T, F> ListenableFuture<RpcResult<T>> handleServiceCall(final BigInteger connectionID,
130                                                                          final Function<DataCrate<T>, ListenableFuture<RpcResult<F>>> function,
131                                                                          final DataCrateBuilder<T> dataCrateBuilder) {
132
133         LOG.trace("Handling general service call");
134         final RequestContext<T> requestContext = requestContextStack.createRequestContext();
135         final SettableFuture<RpcResult<T>> result = requestContextStack.storeOrFail(requestContext);
136         if (result.isDone()) {
137             messageSpy.spyMessage(requestContext.getClass(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMITTED_FAILURE);
138             return result;
139         }
140         DataCrate<T> dataCrate = dataCrateBuilder.setiDConnection(connectionID).setRequestContext(requestContext)
141                 .build();
142         final ListenableFuture<RpcResult<F>> resultFromOFLib;
143
144             requestContext.setXid(deviceContext.getNextXid());
145             LOG.trace("Hooking xid {} to device context - precaution.", requestContext.getXid().getValue());
146             deviceContext.hookRequestCtx(requestContext.getXid(), requestContext);
147         synchronized (deviceContext) {
148             resultFromOFLib = function.apply(dataCrate);
149         }
150
151
152         final OFJResult2RequestCtxFuture<T> OFJResult2RequestCtxFuture = new OFJResult2RequestCtxFuture<>(requestContext, deviceContext);
153         OFJResult2RequestCtxFuture.processResultFromOfJava(resultFromOFLib);
154         return result;
155
156     }
157
158 }