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