2 * Copyright (c) 2019 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.genius.mdsalutil.internal;
10 import java.lang.management.ManagementFactory;
11 import java.util.ArrayList;
12 import java.util.List;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import javax.management.InstanceNotFoundException;
16 import javax.management.MBeanException;
17 import javax.management.MBeanServer;
18 import javax.management.MalformedObjectNameException;
19 import javax.management.ObjectName;
20 import javax.management.ReflectionException;
21 import org.opendaylight.genius.mdsalutil.interfaces.ShardStatusMonitor;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 public final class ShardStatusMonitorImpl implements ShardStatusMonitor {
28 private static final Logger LOG = LoggerFactory.getLogger(ShardStatusMonitorImpl.class);
31 public ShardStatusMonitorImpl() {
35 public boolean getShardStatus(List<String> shards) {
36 boolean status = true;
37 for (String shard : shards) {
38 String[] params = shard.split(":");
39 if (!getDataStoreStatus(params[0], params[1]).equalsIgnoreCase("operational")) {
47 @SuppressWarnings("IllegalCatch")
48 private static String getDataStoreStatus(String name, String type) {
49 boolean statusResult = true;
51 ArrayList listOfShards;
52 if (type.equalsIgnoreCase("config")) {
53 listOfShards = getAttributeJMXCommand(JMX_OBJECT_NAME_LIST_OF_CONFIG_SHARDS, "LocalShards");
55 listOfShards = getAttributeJMXCommand(JMX_OBJECT_NAME_LIST_OF_OPER_SHARDS, "LocalShards");
57 if (listOfShards != null) {
58 for (int i = 0; i < listOfShards.size(); i++) {
59 if (listOfShards.get(i).toString().contains(name)) {
60 String jmxObjectShardStatus;
61 if (type.equalsIgnoreCase("config")) {
62 jmxObjectShardStatus = "org.opendaylight.controller:Category=Shards,name="
63 + listOfShards.get(i) + ",type=DistributedConfigDatastore";
65 jmxObjectShardStatus = "org.opendaylight.controller:Category=Shards,name="
66 + listOfShards.get(i) + ",type=DistributedOperationalDatastore";
68 LOG.info(jmxObjectShardStatus);
69 String leader = getLeaderJMX(jmxObjectShardStatus,"Leader");
70 if (leader != null && leader.length() > 1) {
71 if (type.equalsIgnoreCase("config")) {
72 LOG.info("{} ::Config DS has the Leader as:: {}", listOfShards.get(i), leader);
74 LOG.info("{} ::Oper DS has the Leader as:: {}", listOfShards.get(i), leader);
82 } catch (Exception e) {
83 LOG.error("ERROR::", e);
93 @SuppressWarnings("IllegalCatch")
94 private static ArrayList getAttributeJMXCommand(String objectName, String attributeName) {
95 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
96 ArrayList listOfShards = new ArrayList();
99 listOfShards = (ArrayList) mbs.getAttribute(new ObjectName(objectName), attributeName);
100 } catch (MalformedObjectNameException monEx) {
101 LOG.error("CRITICAL EXCEPTION : Malformed Object Name Exception");
102 } catch (MBeanException mbEx) {
103 LOG.error("CRITICAL EXCEPTION : MBean Exception");
104 } catch (InstanceNotFoundException infEx) {
105 LOG.error("CRITICAL EXCEPTION : Instance Not Found Exception");
106 } catch (ReflectionException rEx) {
107 LOG.error("CRITICAL EXCEPTION : Reflection Exception");
108 } catch (Exception e) {
109 LOG.error("Attribute not found");
115 @SuppressWarnings("IllegalCatch")
116 private static String getLeaderJMX(String objectName, String atrName) {
117 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
121 leader = (String) mbs.getAttribute(new ObjectName(objectName), atrName);
122 } catch (MalformedObjectNameException monEx) {
123 LOG.error("CRITICAL EXCEPTION : Malformed Object Name Exception");
124 } catch (MBeanException mbEx) {
125 LOG.error("CRITICAL EXCEPTION : MBean Exception");
126 } catch (InstanceNotFoundException infEx) {
127 LOG.error("CRITICAL EXCEPTION : Instance Not Found Exception");
128 } catch (ReflectionException rEx) {
129 LOG.error("CRITICAL EXCEPTION : Reflection Exception");
130 } catch (Exception e) {
131 LOG.error("Attribute not found");