RpcContext extends DeviceDisconnectedHandler
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / rpc / RpcContextImpl.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.rpc;
9
10 import com.google.common.util.concurrent.SettableFuture;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
16 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
19 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
21 import org.opendaylight.yangtools.yang.binding.RpcService;
22 import org.opendaylight.yangtools.yang.common.RpcError;
23 import org.opendaylight.yangtools.yang.common.RpcResult;
24 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
25 import org.slf4j.Logger;
26
27 public class RpcContextImpl implements RpcContext {
28
29     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(RpcContextImpl.class);
30     final ProviderContext providerContext;
31
32     // TODO: add private Sal salBroker
33     private final DeviceContext deviceContext;
34     private final List<RoutedRpcRegistration> rpcRegistrations = new ArrayList<>();
35     private final List<RequestContext<?>> synchronizedRequestsList = Collections
36             .<RequestContext<?>>synchronizedList(new ArrayList<RequestContext<?>>());
37
38     private int maxRequestsPerDevice;
39
40     public RpcContextImpl(final ProviderContext providerContext, final DeviceContext deviceContext) {
41         this.providerContext = providerContext;
42         this.deviceContext = deviceContext;
43     }
44
45     /**
46      * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#registerRpcServiceImplementation(java.lang.Class,
47      * org.opendaylight.yangtools.yang.binding.RpcService)
48      */
49     @Override
50     public <S extends RpcService> void registerRpcServiceImplementation(final Class<S> serviceClass,
51                                                                         final S serviceInstance) {
52         final RoutedRpcRegistration<S> routedRpcReg = providerContext.addRoutedRpcImplementation(serviceClass, serviceInstance);
53         routedRpcReg.registerPath(NodeContext.class, deviceContext.getDeviceState().getNodeInstanceIdentifier());
54         rpcRegistrations.add(routedRpcReg);
55     }
56
57     @Override
58     public <T> SettableFuture<RpcResult<T>> storeOrFail(final RequestContext<T> requestContext) {
59         final SettableFuture<RpcResult<T>> rpcResultFuture = requestContext.getFuture();
60
61         if (synchronizedRequestsList.size() < maxRequestsPerDevice) {
62             synchronizedRequestsList.add(requestContext);
63         } else {
64             final RpcResult<T> rpcResult = RpcResultBuilder.<T>failed()
65                     .withError(RpcError.ErrorType.APPLICATION, "", "Device's request queue is full.").build();
66             rpcResultFuture.set(rpcResult);
67         }
68         return rpcResultFuture;
69     }
70
71     /**
72      * Unregisters all services.
73      *
74      * @see java.lang.AutoCloseable#close()
75      */
76     @Override
77     public void close() throws Exception {
78         for (final RoutedRpcRegistration<?> rpcRegistration : rpcRegistrations) {
79             rpcRegistration.unregisterPath(NodeContext.class, deviceContext.getDeviceState().getNodeInstanceIdentifier());
80             rpcRegistration.close();
81         }
82     }
83
84     /**
85      * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#setRequestContextQuota(int)
86      */
87     @Override
88     public void setRequestContextQuota(final int maxRequestsPerDevice) {
89         this.maxRequestsPerDevice = maxRequestsPerDevice;
90     }
91
92     @Override
93     public <T> void forgetRequestContext(final RequestContext<T> requestContext) {
94         synchronizedRequestsList.remove(requestContext);
95         LOG.trace("Removed request context with xid {}. Context request in list {}.",
96                 requestContext.getXid().getValue(), synchronizedRequestsList.size());
97     }
98
99     @Override
100     public DeviceContext getDeviceContext() {
101         return deviceContext;
102     }
103
104     @Override
105     public <T> RequestContext<T> createRequestContext() {
106         return new RequestContextImpl<T>(this);
107     }
108
109     public boolean isRequestContextCapacityEmpty() {
110         return synchronizedRequestsList.size() <= maxRequestsPerDevice;
111     }
112
113     @Override
114     public void onDeviceDisconnected(final ConnectionContext connectionContext) {
115         for (RoutedRpcRegistration registration : rpcRegistrations) {
116             registration.close();
117         }
118
119         synchronizedRequestsList.clear();
120     }
121 }