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