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
8 package org.opendaylight.ovsdb.utils.mdsal.utils;
10 import java.lang.management.ManagementFactory;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.Collection;
14 import java.util.Collections;
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.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 public final class ShardStatusMonitor {
26 private static final Logger LOG = LoggerFactory.getLogger(ShardStatusMonitor.class);
27 private static final String TOPOLOGY_CONFIG_SHARD = "topology:config";
28 private static final String TOPOLOGY_OPER_SHARD = "topology:oper";
29 private static final String STATUS_OPERATIONAL = "OPERATIONAL";
31 private static final String JMX_OBJECT_NAME_LIST_OF_CONFIG_SHARDS =
32 "org.opendaylight.controller:type=DistributedConfigDatastore,Category=ShardManager,name=shard-manager-config";
33 private static final String JMX_OBJECT_NAME_LIST_OF_OPER_SHARDS =
34 "org.opendaylight.controller:type=DistributedOperationalDatastore,"
35 + "Category=ShardManager,name=shard-manager-operational";
37 public static final Collection<String> TOPOLOGY_SHARDS =
38 Collections.unmodifiableList(Arrays.asList(TOPOLOGY_CONFIG_SHARD, TOPOLOGY_OPER_SHARD));
40 //To avoid the checkstyle errors
41 private ShardStatusMonitor() {
45 @SuppressWarnings("checkstyle:IllegalCatch")
46 public static String getLeaderJMX(String objectName, String atrName) {
47 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
51 leader = (String)mbs.getAttribute(new ObjectName(objectName), atrName);
52 } catch (Exception e) {
53 LOG.error("Failed to get leader jmx {}", e.getMessage());
59 public static boolean getShardStatus(Collection<String> shards) {
60 boolean status = true;
61 for (String shard : shards) {
62 String[] params = shard.split(":");
63 if (!getDataStoreStatus(params[0], params[1]).equalsIgnoreCase(STATUS_OPERATIONAL)) {
71 @SuppressWarnings("checkstyle:IllegalCatch")
72 public static String getDataStoreStatus(String name, String type) {
73 boolean statusResult = true;
75 ArrayList listOfShards;
76 if (type.equalsIgnoreCase("config")) {
77 listOfShards = getAttributeJMXCommand(JMX_OBJECT_NAME_LIST_OF_CONFIG_SHARDS, "LocalShards");
79 listOfShards = getAttributeJMXCommand(JMX_OBJECT_NAME_LIST_OF_OPER_SHARDS, "LocalShards");
81 if (listOfShards != null) {
82 for (int i = 0; i < listOfShards.size(); i++) {
83 if (listOfShards.get(i).toString().contains(name)) {
84 String jmxObjectShardStatus;
85 if (type.equalsIgnoreCase("config")) {
86 jmxObjectShardStatus = "org.opendaylight.controller:Category=Shards,name="
87 + listOfShards.get(i) + ",type=DistributedConfigDatastore";
89 jmxObjectShardStatus = "org.opendaylight.controller:Category=Shards,name="
90 + listOfShards.get(i) + ",type=DistributedOperationalDatastore";
92 String leader = getLeaderJMX(jmxObjectShardStatus,"Leader");
93 if (leader != null && leader.length() > 1) {
94 if (type.equalsIgnoreCase("config")) {
95 LOG.info("{} ::Config DS has the Leader as:: {}", listOfShards.get(i), leader);
97 LOG.info("{} ::Oper DS has the Leader as:: {}", listOfShards.get(i), leader);
100 statusResult = false;
105 } catch (Exception e) {
106 LOG.error("ERROR::", e);
107 statusResult = false;
110 return STATUS_OPERATIONAL;
116 @SuppressWarnings("checkstyle:IllegalCatch")
117 public static ArrayList getAttributeJMXCommand(String objectName, String attributeName) {
118 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
119 ArrayList listOfShards = new ArrayList();
122 listOfShards = (ArrayList) mbs.getAttribute(new ObjectName(objectName), attributeName);
123 } catch (MalformedObjectNameException monEx) {
124 LOG.error("CRITICAL EXCEPTION : Malformed Object Name Exception");
125 } catch (MBeanException mbEx) {
126 LOG.error("CRITICAL EXCEPTION : MBean Exception");
127 } catch (InstanceNotFoundException infEx) {
128 LOG.error("CRITICAL EXCEPTION : Instance Not Found Exception");
129 } catch (ReflectionException rEx) {
130 LOG.error("CRITICAL EXCEPTION : Reflection Exception");
131 } catch (Exception e) {
132 LOG.error("Attribute not found");