Merge "BUG-4148: Improving the logging in flow/group/meter to provide more contextual...
[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.base.Preconditions;
11 import com.google.common.collect.Iterators;
12 import java.util.Iterator;
13 import java.util.Map.Entry;
14 import java.util.concurrent.ConcurrentHashMap;
15 import java.util.concurrent.ConcurrentMap;
16 import java.util.concurrent.Semaphore;
17
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.controller.md.sal.binding.api.NotificationPublishService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
28 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.binding.RpcService;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class RpcContextImpl implements RpcContext {
34     private static final Logger LOG = LoggerFactory.getLogger(RpcContextImpl.class);
35     private final RpcProviderRegistry rpcProviderRegistry;
36     private final MessageSpy messageSpy;
37     private final Semaphore tracker;
38     private final XidSequencer xidSequencer;
39
40     // TODO: add private Sal salBroker
41     private final ConcurrentMap<Class<?>, RoutedRpcRegistration<?>> rpcRegistrations = new ConcurrentHashMap<>();
42     private final KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
43
44     public RpcContextImpl(final RpcProviderRegistry rpcProviderRegistry,
45                           final XidSequencer xidSequencer,
46                           final MessageSpy messageSpy,
47                           final int maxRequests,
48                           final KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier) {
49         this.xidSequencer = Preconditions.checkNotNull(xidSequencer);
50         this.messageSpy = Preconditions.checkNotNull(messageSpy);
51         this.rpcProviderRegistry = Preconditions.checkNotNull(rpcProviderRegistry);
52         this.nodeInstanceIdentifier = nodeInstanceIdentifier;
53
54         tracker = new Semaphore(maxRequests, true);
55     }
56
57     /**
58      * @see org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext#registerRpcServiceImplementation(java.lang.Class,
59      * org.opendaylight.yangtools.yang.binding.RpcService)
60      */
61     @Override
62     public <S extends RpcService> void registerRpcServiceImplementation(final Class<S> serviceClass,
63                                                                         final S serviceInstance) {
64         LOG.trace("Try to register service {} for device {}.", serviceClass, nodeInstanceIdentifier);
65         if (! rpcRegistrations.containsKey(serviceClass)) {
66             final RoutedRpcRegistration<S> routedRpcReg = rpcProviderRegistry.addRoutedRpcImplementation(serviceClass, serviceInstance);
67             routedRpcReg.registerPath(NodeContext.class, nodeInstanceIdentifier);
68             rpcRegistrations.put(serviceClass, routedRpcReg);
69             LOG.debug("Registration of service {} for device {}.", serviceClass, nodeInstanceIdentifier);
70         }
71     }
72
73     @Override
74     public <S extends RpcService> S lookupRpcService(final Class<S> serviceClass) {
75         final RpcService rpcService = rpcRegistrations.get(serviceClass).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 }