Fix unused import warnings
[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.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Iterators;
13 import java.util.Iterator;
14 import java.util.Map.Entry;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.ConcurrentMap;
17 import java.util.concurrent.Semaphore;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
19 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
20 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
21 import org.opendaylight.openflowplugin.api.openflow.device.XidSequencer;
22 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
23 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
27 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.binding.RpcService;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class RpcContextImpl implements RpcContext {
33     private static final Logger LOG = LoggerFactory.getLogger(RpcContextImpl.class);
34     private final RpcProviderRegistry rpcProviderRegistry;
35     private final MessageSpy messageSpy;
36     private final Semaphore tracker;
37     private final XidSequencer xidSequencer;
38
39     // TODO: add private Sal salBroker
40     private final ConcurrentMap<Class<?>, RoutedRpcRegistration<?>> rpcRegistrations = new ConcurrentHashMap<>();
41     private final KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
42
43     public RpcContextImpl(final RpcProviderRegistry rpcProviderRegistry,
44                           final XidSequencer xidSequencer,
45                           final MessageSpy messageSpy,
46                           final int maxRequests,
47                           final KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier) {
48         this.xidSequencer = Preconditions.checkNotNull(xidSequencer);
49         this.messageSpy = Preconditions.checkNotNull(messageSpy);
50         this.rpcProviderRegistry = Preconditions.checkNotNull(rpcProviderRegistry);
51         this.nodeInstanceIdentifier = nodeInstanceIdentifier;
52
53         tracker = new Semaphore(maxRequests, true);
54     }
55
56     /**
57      * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#registerRpcServiceImplementation(java.lang.Class,
58      * org.opendaylight.yangtools.yang.binding.RpcService)
59      */
60     @Override
61     public <S extends RpcService> void registerRpcServiceImplementation(final Class<S> serviceClass,
62                                                                         final S serviceInstance) {
63         LOG.trace("Try to register service {} for device {}.", serviceClass, nodeInstanceIdentifier);
64         if (! rpcRegistrations.containsKey(serviceClass)) {
65             final RoutedRpcRegistration<S> routedRpcReg = rpcProviderRegistry.addRoutedRpcImplementation(serviceClass, serviceInstance);
66             routedRpcReg.registerPath(NodeContext.class, nodeInstanceIdentifier);
67             rpcRegistrations.put(serviceClass, routedRpcReg);
68             LOG.debug("Registration of service {} for device {}.", serviceClass, nodeInstanceIdentifier);
69         }
70     }
71
72     @Override
73     public <S extends RpcService> S lookupRpcService(final Class<S> serviceClass) {
74         RoutedRpcRegistration<?> registration = rpcRegistrations.get(serviceClass);
75         final RpcService rpcService = registration.getInstance();
76         return (S) rpcService;
77     }
78
79     /**
80      * Unregisters all services.
81      *
82      * @see java.lang.AutoCloseable#close()
83      */
84     @Override
85     public void close() {
86         for (final Iterator<Entry<Class<?>, RoutedRpcRegistration<?>>> iterator = Iterators
87                 .consumingIterator(rpcRegistrations.entrySet().iterator()); iterator.hasNext();) {
88             final RoutedRpcRegistration<?> rpcRegistration = iterator.next().getValue();
89             rpcRegistration.unregisterPath(NodeContext.class, nodeInstanceIdentifier);
90             rpcRegistration.close();
91             LOG.debug("Closing RPC Registration of service {} for device {}.", rpcRegistration.getServiceType(),
92                     nodeInstanceIdentifier);
93         }
94     }
95
96     @Override
97     public <T> RequestContext<T> createRequestContext() {
98         if (!tracker.tryAcquire()) {
99             LOG.trace("Device queue {} at capacity", this);
100             return null;
101         } else {
102             LOG.trace("Acquired semaphore for {}, available permits:{} ", nodeInstanceIdentifier.getKey().getId(), tracker.availablePermits());
103         }
104
105         final Long xid = xidSequencer.reserveXidForDeviceMessage();
106         if (xid == null) {
107             LOG.warn("Xid cannot be reserved for new RequestContext, node:{}", nodeInstanceIdentifier.getKey().getId());
108             tracker.release();
109             return null;
110         }
111
112         return new AbstractRequestContext<T>(xid) {
113             @Override
114             public void close() {
115                 tracker.release();
116                 final long xid = getXid().getValue();
117                 LOG.trace("Removed request context with xid {}", xid);
118                 messageSpy.spyMessage(RpcContextImpl.class, MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
119             }
120         };
121     }
122
123     @Override
124     public <S extends RpcService> void unregisterRpcServiceImplementation(final Class<S> serviceClass) {
125         LOG.trace("Try to unregister serviceClass {} for Node {}", serviceClass, nodeInstanceIdentifier.getKey().getId());
126         final RoutedRpcRegistration<?> rpcRegistration = rpcRegistrations.remove(serviceClass);
127         if (rpcRegistration != null) {
128             rpcRegistration.unregisterPath(NodeContext.class, nodeInstanceIdentifier);
129             rpcRegistration.close();
130             LOG.debug("Unregistration serviceClass {} for Node {}", serviceClass, nodeInstanceIdentifier.getKey().getId());
131         }
132     }
133
134     @VisibleForTesting
135     public boolean isEmptyRpcRegistrations() {
136         return this.rpcRegistrations.isEmpty();
137     }
138
139
140 }