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