2 * Copyright (c) 2020 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
9 package org.opendaylight.ovsdb.utils.mdsal.utils;
11 import java.lang.management.ManagementFactory;
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.Collection;
15 import java.util.Collections;
17 import javax.management.InstanceNotFoundException;
18 import javax.management.MBeanException;
19 import javax.management.MBeanServer;
20 import javax.management.MalformedObjectNameException;
21 import javax.management.ObjectName;
22 import javax.management.ReflectionException;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public final class ShardStatusMonitor {
29 private static final Logger LOG = LoggerFactory.getLogger(ShardStatusMonitor.class);
30 private static final String TOPOLOGY_CONFIG_SHARD = "topology:config";
31 private static final String TOPOLOGY_OPER_SHARD = "topology:oper";
32 private static final String STATUS_OPERATIONAL = "OPERATIONAL";
34 private static final String JMX_OBJECT_NAME_LIST_OF_CONFIG_SHARDS =
35 "org.opendaylight.controller:type=DistributedConfigDatastore,Category=ShardManager,name=shard-manager-config";
36 private static final String JMX_OBJECT_NAME_LIST_OF_OPER_SHARDS =
37 "org.opendaylight.controller:type=DistributedOperationalDatastore,"
38 + "Category=ShardManager,name=shard-manager-operational";
40 public static final Collection<String> TOPOLOGY_SHARDS =
41 Collections.unmodifiableList(Arrays.asList(TOPOLOGY_CONFIG_SHARD, TOPOLOGY_OPER_SHARD));
43 //To avoid the checkstyle errors
44 private ShardStatusMonitor() {
48 @SuppressWarnings("checkstyle:IllegalCatch")
49 public static String getLeaderJMX(String objectName, String atrName) {
50 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
54 leader = (String)mbs.getAttribute(new ObjectName(objectName), atrName);
55 } catch (Exception e) {
56 LOG.error("Failed to get leader jmx {}", e.getMessage());
62 public static boolean getShardStatus(Collection<String> shards) {
63 boolean status = true;
64 for (String shard : shards) {
65 String[] params = shard.split(":");
66 if (!getDataStoreStatus(params[0], params[1]).equalsIgnoreCase(STATUS_OPERATIONAL)) {
74 @SuppressWarnings("checkstyle:IllegalCatch")
75 public static String getDataStoreStatus(String name, String type) {
76 boolean statusResult = true;
78 ArrayList listOfShards;
79 if (type.equalsIgnoreCase("config")) {
80 listOfShards = getAttributeJMXCommand(JMX_OBJECT_NAME_LIST_OF_CONFIG_SHARDS, "LocalShards");
82 listOfShards = getAttributeJMXCommand(JMX_OBJECT_NAME_LIST_OF_OPER_SHARDS, "LocalShards");
84 if (listOfShards != null) {
85 for (int i = 0; i < listOfShards.size(); i++) {
86 if (listOfShards.get(i).toString().contains(name)) {
87 String jmxObjectShardStatus;
88 if (type.equalsIgnoreCase("config")) {
89 jmxObjectShardStatus = "org.opendaylight.controller:Category=Shards,name="
90 + listOfShards.get(i) + ",type=DistributedConfigDatastore";
92 jmxObjectShardStatus = "org.opendaylight.controller:Category=Shards,name="
93 + listOfShards.get(i) + ",type=DistributedOperationalDatastore";
95 String leader = getLeaderJMX(jmxObjectShardStatus,"Leader");
96 if (leader != null && leader.length() > 1) {
97 if (type.equalsIgnoreCase("config")) {
98 LOG.info("{} ::Config DS has the Leader as:: {}", listOfShards.get(i), leader);
100 LOG.info("{} ::Oper DS has the Leader as:: {}", listOfShards.get(i), leader);
103 statusResult = false;
108 } catch (Exception e) {
109 LOG.error("ERROR::", e);
110 statusResult = false;
113 return STATUS_OPERATIONAL;
119 @SuppressWarnings("checkstyle:IllegalCatch")
120 public static ArrayList getAttributeJMXCommand(String objectName, String attributeName) {
121 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
122 ArrayList listOfShards = new ArrayList();
125 listOfShards = (ArrayList) mbs.getAttribute(new ObjectName(objectName), attributeName);
126 } catch (MalformedObjectNameException monEx) {
127 LOG.error("CRITICAL EXCEPTION : Malformed Object Name Exception");
128 } catch (MBeanException mbEx) {
129 LOG.error("CRITICAL EXCEPTION : MBean Exception");
130 } catch (InstanceNotFoundException infEx) {
131 LOG.error("CRITICAL EXCEPTION : Instance Not Found Exception");
132 } catch (ReflectionException rEx) {
133 LOG.error("CRITICAL EXCEPTION : Reflection Exception");
134 } catch (Exception e) {
135 LOG.error("Attribute not found");