Merge "BUG-4283 experimenter msg support - registration part"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / rpc / RpcManagerImpl.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 org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
11 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
12 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
13 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
14 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
15 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
16 import org.opendaylight.openflowplugin.impl.util.MdSalRegistratorUtils;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import java.util.concurrent.ConcurrentHashMap;
23
24 public class RpcManagerImpl implements RpcManager {
25
26     private static final Logger LOG = LoggerFactory.getLogger(RpcManagerImpl.class);
27     private final RpcProviderRegistry rpcProviderRegistry;
28     private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
29     private final int maxRequestsQuota;
30     private final ConcurrentHashMap<DeviceContext, RpcContext> contexts = new ConcurrentHashMap<>();
31
32     public RpcManagerImpl(final RpcProviderRegistry rpcProviderRegistry,
33                           final int quotaValue) {
34         this.rpcProviderRegistry = rpcProviderRegistry;
35         maxRequestsQuota = quotaValue;
36     }
37
38     @Override
39     public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
40         deviceInitPhaseHandler = handler;
41     }
42
43     @Override
44     public void onDeviceContextLevelUp(final DeviceContext deviceContext) {
45         NodeId nodeId = deviceContext.getDeviceState().getNodeId();
46         OfpRole ofpRole = deviceContext.getDeviceState().getRole();
47
48         LOG.debug("Node:{}, deviceContext.getDeviceState().getRole():{}", nodeId, ofpRole);
49
50         RpcContext rpcContext = contexts.get(deviceContext);
51         if (rpcContext == null) {
52             rpcContext = new RpcContextImpl(deviceContext.getMessageSpy(), rpcProviderRegistry, deviceContext, maxRequestsQuota);
53             contexts.put(deviceContext, rpcContext);
54         }
55
56
57         if (ofpRole == OfpRole.BECOMESLAVE) {
58             // if slave, we need to de-register rpcs if any have been registered, in case of master to slave
59             LOG.info("Unregistering RPC registration (if any) for slave role for node:{}", deviceContext.getDeviceState().getNodeId());
60             try {
61                 MdSalRegistratorUtils.unregisterServices(rpcContext);
62             } catch (Exception e) {
63                 LOG.error("Exception while unregistering rpcs for slave role for node:{}. But continuing.", nodeId, e);
64             }
65
66         } else {
67             LOG.info("Registering Openflow RPCs for node:{}, role:{}", nodeId, ofpRole);
68             MdSalRegistratorUtils.registerServices(rpcContext, deviceContext);
69         }
70
71         deviceContext.addDeviceContextClosedHandler(this);
72
73         // finish device initialization cycle back to DeviceManager
74         deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
75     }
76
77     @Override
78     public void close() throws Exception {
79
80     }
81
82
83     @Override
84     public void onDeviceContextClosed(DeviceContext deviceContext) {
85         RpcContext removedContext = contexts.remove(deviceContext);
86         if (removedContext != null) {
87             try {
88                 LOG.info("Unregistering rpcs for device context closure");
89                 removedContext.close();
90             } catch (Exception e) {
91                 LOG.error("Exception while unregistering rpcs onDeviceContextClosed handler for node:{}. But continuing.",
92                         deviceContext.getDeviceState().getNodeId(), e);
93             }
94         }
95
96     }
97 }