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