b6d691f939fc2ad99fb72b38977a3a2e5c720322
[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 java.math.BigInteger;
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.openflowplugin.api.openflow.device.Xid;
20 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
22 import org.opendaylight.yangtools.yang.common.RpcError;
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     private static final BigInteger PRIMARY_CONNECTION = BigInteger.ZERO;
31
32     private final short version;
33     private final BigInteger datapathId;
34     private final RequestContextStack requestContextStack;
35     private final DeviceContext deviceContext;
36     private final ConnectionAdapter primaryConnectionAdapter;
37     private final MessageSpy messageSpy;
38
39
40     public CommonService(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
41         this.requestContextStack = requestContextStack;
42         this.deviceContext = deviceContext;
43         final FeaturesReply features = this.deviceContext.getPrimaryConnectionContext().getFeatures();
44         this.datapathId = features.getDatapathId();
45         this.version = features.getVersion();
46         this.primaryConnectionAdapter = deviceContext.getPrimaryConnectionContext().getConnectionAdapter();
47         this.messageSpy = deviceContext.getMessageSpy();
48     }
49
50     public static BigInteger getPrimaryConnection() {
51         return PRIMARY_CONNECTION;
52     }
53
54     public short getVersion() {
55         return version;
56     }
57
58     public BigInteger getDatapathId() {
59         return datapathId;
60     }
61
62     public RequestContextStack getRequestContextStack() {
63         return requestContextStack;
64     }
65
66     public DeviceContext getDeviceContext() {
67         return deviceContext;
68     }
69
70     public ConnectionAdapter getPrimaryConnectionAdapter() {
71         return primaryConnectionAdapter;
72     }
73
74     public MessageSpy getMessageSpy() {
75         return messageSpy;
76     }
77
78     protected long provideWaitTime() {
79         return WAIT_TIME;
80     }
81
82
83     protected ConnectionAdapter provideConnectionAdapter(final BigInteger connectionID) {
84         if (connectionID == null) {
85             return primaryConnectionAdapter;
86         }
87         if (connectionID.equals(PRIMARY_CONNECTION)) {
88             return primaryConnectionAdapter;
89         }
90
91         final ConnectionContext auxiliaryConnectionContext =
92                 deviceContext.getAuxiliaryConnectiobContexts(connectionID);
93         if (auxiliaryConnectionContext != null) {
94             return auxiliaryConnectionContext.getConnectionAdapter();
95         }
96
97         return primaryConnectionAdapter;
98     }
99
100
101     /**
102      * @param <T>
103      * @param <F>
104      * @param function
105      * @return
106      */
107     public final <T, F> ListenableFuture<RpcResult<T>> handleServiceCall(final Function<RequestContext<T>, ListenableFuture<RpcResult<F>>> function) {
108
109         LOG.trace("Handling general service call");
110         final RequestContext<T> requestContext = createRequestContext();
111         if (requestContext == null) {
112             LOG.trace("Request context refused.");
113             deviceContext.getMessageSpy().spyMessage(null, MessageSpy.STATISTIC_GROUP.TO_SWITCH_DISREGARDED);
114             return failedFuture();
115         }
116
117         Long reservedXid = deviceContext.getReservedXid();
118         if (null == reservedXid) {
119             deviceContext.getMessageSpy().spyMessage(requestContext.getClass(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_RESERVATION_REJECTED);
120             return RequestContextUtil.closeRequestContextWithRpcError(requestContext, "Outbound queue wasn't able to reserve XID.");
121         }
122         final Xid xid = new Xid(reservedXid);
123         requestContext.setXid(xid);
124         final ListenableFuture<RpcResult<F>> resultFromOFLib;
125
126         LOG.trace("Hooking xid {} to device context - precaution.", requestContext.getXid().getValue());
127         deviceContext.hookRequestCtx(xid, requestContext);
128
129         messageSpy.spyMessage(requestContext.getClass(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_READY_FOR_SUBMIT);
130         function.apply(requestContext);
131
132         return requestContext.getFuture();
133
134     }
135
136     protected final <T> RequestContext<T> createRequestContext() {
137         return requestContextStack.createRequestContext();
138     }
139
140     protected static <T> ListenableFuture<RpcResult<T>> failedFuture() {
141         final RpcResult<T> rpcResult = RpcResultBuilder.<T>failed()
142                 .withError(RpcError.ErrorType.APPLICATION, "", "Request quota exceeded").build();
143         return Futures.immediateFuture(rpcResult);
144     }
145 }