Bump MRI upstreams
[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.util.function.Function;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
19 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
20 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
21 import org.opendaylight.openflowplugin.api.openflow.device.DeviceRegistry;
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.openflowplugin.impl.services.util.RequestContextUtil;
29 import org.opendaylight.openflowplugin.impl.services.util.ServiceException;
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.ErrorTag;
33 import org.opendaylight.yangtools.yang.common.ErrorType;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
36 import org.opendaylight.yangtools.yang.common.Uint32;
37 import org.opendaylight.yangtools.yang.common.Uint64;
38 import org.opendaylight.yangtools.yang.common.Uint8;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public abstract class AbstractService<I, O> {
43     private static final Logger LOG = LoggerFactory.getLogger(AbstractService.class);
44
45     private final Uint8 version;
46     private final Uint64 datapathId;
47     private final RequestContextStack requestContextStack;
48     private final DeviceContext deviceContext;
49     private final MessageSpy messageSpy;
50     private EventIdentifier eventIdentifier;
51
52     AbstractService(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
53         final DeviceInfo deviceInfo = deviceContext.getDeviceInfo();
54
55         this.requestContextStack = requestContextStack;
56         this.deviceContext = deviceContext;
57         this.datapathId = deviceInfo.getDatapathId();
58         this.version = deviceInfo.getVersion();
59         this.messageSpy = deviceContext.getMessageSpy();
60     }
61
62     public boolean canUseSingleLayerSerialization() {
63         return deviceContext.canUseSingleLayerSerialization();
64     }
65
66     public EventIdentifier getEventIdentifier() {
67         return eventIdentifier;
68     }
69
70     public void setEventIdentifier(final EventIdentifier eventIdentifier) {
71         this.eventIdentifier = eventIdentifier;
72     }
73
74     public final Uint8 getVersion() {
75         return version;
76     }
77
78     public final Uint64 getDatapathId() {
79         return datapathId;
80     }
81
82     public RequestContextStack getRequestContextStack() {
83         return requestContextStack;
84     }
85
86     @Deprecated
87     public DeviceContext getDeviceContext() {
88         return deviceContext;
89     }
90
91     public DeviceRegistry getDeviceRegistry() {
92         return deviceContext;
93     }
94
95     public DeviceInfo getDeviceInfo() {
96         return deviceContext.getDeviceInfo();
97     }
98
99     public TxFacade getTxFacade() {
100         return deviceContext;
101     }
102
103     public MessageSpy getMessageSpy() {
104         return messageSpy;
105     }
106
107     protected abstract OfHeader buildRequest(Xid xid, I input) throws ServiceException;
108
109     protected abstract FutureCallback<OfHeader> createCallback(RequestContext<O> context, Class<?> requestType);
110
111     public ListenableFuture<RpcResult<O>> handleServiceCall(@NonNull final I input) {
112         return handleServiceCall(input, null);
113     }
114
115     public ListenableFuture<RpcResult<O>> handleServiceCall(@NonNull final I input,
116             @Nullable final Function<OfHeader, Boolean> isComplete) {
117         Preconditions.checkNotNull(input);
118
119         final Class<?> requestType = input instanceof DataContainer
120             ? ((DataContainer) input).implementedInterface()
121             : input.getClass();
122
123         getMessageSpy().spyMessage(requestType, MessageSpy.StatisticsGroup.TO_SWITCH_ENTERED);
124
125         LOG.trace("Handling general service call");
126         final RequestContext<O> requestContext = requestContextStack.createRequestContext();
127
128         if (requestContext == null) {
129             LOG.trace("Request context refused.");
130             getMessageSpy().spyMessage(AbstractService.class, MessageSpy.StatisticsGroup.TO_SWITCH_DISREGARDED);
131             return Futures.immediateFuture(RpcResultBuilder
132                     .<O>failed()
133                     .withError(ErrorType.APPLICATION, ErrorTag.ACCESS_DENIED, "Request quota exceeded")
134                     .build());
135         }
136
137         if (requestContext.getXid() == null) {
138             getMessageSpy().spyMessage(requestContext.getClass(),
139                                        MessageSpy.StatisticsGroup.TO_SWITCH_RESERVATION_REJECTED);
140             return RequestContextUtil
141                     .closeRequestContextWithRpcError(requestContext, "Outbound queue wasn't able to reserve XID.");
142         }
143
144         getMessageSpy().spyMessage(requestContext.getClass(), MessageSpy.StatisticsGroup.TO_SWITCH_READY_FOR_SUBMIT);
145
146         final Xid xid = requestContext.getXid();
147         OfHeader request = null;
148         try {
149             request = buildRequest(xid, input);
150             Verify.verify(xid.getValue().equals(request.getXid()),
151                           "Expected XID %s got %s",
152                           xid.getValue(),
153                           request.getXid());
154         } catch (ServiceException ex) {
155             LOG.error("Failed to build request for {}, forfeiting request {}", input, xid.getValue(), ex);
156             RequestContextUtil.closeRequestContextWithRpcError(requestContext,
157                                                                "failed to build request input: " + ex.getMessage());
158         } finally {
159             final OutboundQueue outboundQueue =
160                     getDeviceContext().getPrimaryConnectionContext().getOutboundQueueProvider();
161
162             final Uint32 queueXid = xid.getValue();
163             if (isComplete != null) {
164                 outboundQueue.commitEntry(queueXid, request, createCallback(requestContext, requestType), isComplete);
165             } else {
166                 outboundQueue.commitEntry(queueXid, request, createCallback(requestContext, requestType));
167             }
168         }
169
170         return requestContext.getFuture();
171     }
172 }