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