f1bdbf116da899234e9f5a02b5c49e90875194a6
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / AbstractService.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.Preconditions;
11 import com.google.common.base.Verify;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.math.BigInteger;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
18 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
20 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
21 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
22 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
23 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
24 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
28 import org.opendaylight.yangtools.yang.binding.DataContainer;
29 import org.opendaylight.yangtools.yang.common.RpcError;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 abstract class AbstractService<I, O> {
36     private static final Logger LOG = LoggerFactory.getLogger(AbstractService.class);
37     private static final long WAIT_TIME = 2000;
38     private static final BigInteger PRIMARY_CONNECTION = BigInteger.ZERO;
39
40     private final short version;
41     private final BigInteger datapathId;
42     private final RequestContextStack requestContextStack;
43     private final DeviceContext deviceContext;
44     private final MessageSpy messageSpy;
45     private final NodeId nodeId;
46     private EventIdentifier eventIdentifier;
47
48     public AbstractService(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
49         final DeviceState deviceState = deviceContext.getDeviceState();
50         final GetFeaturesOutput features = deviceState.getFeatures();
51
52         this.requestContextStack = requestContextStack;
53         this.deviceContext = deviceContext;
54         this.datapathId = features.getDatapathId();
55         this.version = features.getVersion();
56         this.messageSpy = deviceContext.getMessageSpy();
57         this.nodeId = deviceState.getNodeId();
58     }
59
60     public EventIdentifier getEventIdentifier() {
61         return eventIdentifier;
62     }
63
64     public void setEventIdentifier(final EventIdentifier eventIdentifier) {
65         this.eventIdentifier = eventIdentifier;
66     }
67
68     public short getVersion() {
69         return version;
70     }
71
72     public BigInteger getDatapathId() {
73         return datapathId;
74     }
75
76     public NodeId getNodeId() {
77         return nodeId;
78     }
79
80     public RequestContextStack getRequestContextStack() {
81         return requestContextStack;
82     }
83
84     public DeviceContext getDeviceContext() {
85         return deviceContext;
86     }
87
88     public MessageSpy getMessageSpy() {
89         return messageSpy;
90     }
91
92     protected abstract OfHeader buildRequest(Xid xid, I input) throws Exception;
93
94     protected abstract FutureCallback<OfHeader> createCallback(RequestContext<O> context, Class<?> requestType);
95
96     public final ListenableFuture<RpcResult<O>> handleServiceCall(@Nonnull final I input) {
97         Preconditions.checkNotNull(input);
98
99         final Class<?> requestType;
100         if (input instanceof DataContainer) {
101             requestType = ((DataContainer) input).getImplementedInterface();
102         } else {
103             requestType = input.getClass();
104         }
105         getMessageSpy().spyMessage(requestType, MessageSpy.STATISTIC_GROUP.TO_SWITCH_ENTERED);
106
107         LOG.trace("Handling general service call");
108         final RequestContext<O> requestContext = requestContextStack.createRequestContext();
109         if (requestContext == null) {
110             LOG.trace("Request context refused.");
111             getMessageSpy().spyMessage(AbstractService.class, MessageSpy.STATISTIC_GROUP.TO_SWITCH_DISREGARDED);
112             return failedFuture();
113         }
114
115         if (requestContext.getXid() == null) {
116             getMessageSpy().spyMessage(requestContext.getClass(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_RESERVATION_REJECTED);
117             return RequestContextUtil.closeRequestContextWithRpcError(requestContext, "Outbound queue wasn't able to reserve XID.");
118         }
119
120         getMessageSpy().spyMessage(requestContext.getClass(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_READY_FOR_SUBMIT);
121
122         final Xid xid = requestContext.getXid();
123         OfHeader request = null;
124         try {
125             request = buildRequest(xid, input);
126             Verify.verify(xid.getValue().equals(request.getXid()), "Expected XID %s got %s", xid.getValue(), request.getXid());
127         } catch (Exception e) {
128             LOG.error("Failed to build request for {}, forfeiting request {}", input, xid.getValue(), e);
129             RequestContextUtil.closeRequestContextWithRpcError(requestContext, "failed to build request input: " + e.getMessage());
130         } finally {
131             final OutboundQueue outboundQueue = getDeviceContext().getPrimaryConnectionContext().getOutboundQueueProvider();
132             outboundQueue.commitEntry(xid.getValue(), request, createCallback(requestContext, requestType));
133         }
134
135         return requestContext.getFuture();
136     }
137
138     protected static <T> ListenableFuture<RpcResult<T>> failedFuture() {
139         final RpcResult<T> rpcResult = RpcResultBuilder.<T>failed()
140                 .withError(RpcError.ErrorType.APPLICATION, "", "Request quota exceeded").build();
141         return Futures.immediateFuture(rpcResult);
142     }
143 }