Update MRI projects for Aluminium
[ovsdb.git] / utils / mdsal-utils / src / main / java / org / opendaylight / ovsdb / utils / mdsal / utils / ShardStatusMonitor.java
1 /*
2  * Copyright (c) 2020 Ericsson India Global Services Pvt Ltd. 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.ovsdb.utils.mdsal.utils;
9
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;
23
24 public final class ShardStatusMonitor {
25
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";
30
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";
36
37     public static final Collection<String> TOPOLOGY_SHARDS =
38             Collections.unmodifiableList(Arrays.asList(TOPOLOGY_CONFIG_SHARD, TOPOLOGY_OPER_SHARD));
39
40     //To avoid the checkstyle errors
41     private ShardStatusMonitor() {
42
43     }
44
45     @SuppressWarnings("checkstyle:IllegalCatch")
46     public static String getLeaderJMX(String objectName, String atrName) {
47         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
48         String leader = "";
49         if (mbs != null) {
50             try {
51                 leader  = (String)mbs.getAttribute(new ObjectName(objectName), atrName);
52             } catch (Exception e) {
53                 LOG.error("Failed to get leader jmx {}", e.getMessage());
54             }
55         }
56         return leader;
57     }
58
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)) {
64                 status = false;
65                 break;
66             }
67         }
68         return status;
69     }
70
71     @SuppressWarnings("checkstyle:IllegalCatch")
72     public static String getDataStoreStatus(String name, String type) {
73         boolean statusResult = true;
74         try {
75             ArrayList listOfShards;
76             if (type.equalsIgnoreCase("config")) {
77                 listOfShards = getAttributeJMXCommand(JMX_OBJECT_NAME_LIST_OF_CONFIG_SHARDS, "LocalShards");
78             } else  {
79                 listOfShards = getAttributeJMXCommand(JMX_OBJECT_NAME_LIST_OF_OPER_SHARDS, "LocalShards");
80             }
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";
88                         } else {
89                             jmxObjectShardStatus = "org.opendaylight.controller:Category=Shards,name="
90                                     + listOfShards.get(i) + ",type=DistributedOperationalDatastore";
91                         }
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);
96                             } else {
97                                 LOG.info("{} ::Oper DS has the Leader as:: {}", listOfShards.get(i), leader);
98                             }
99                         } else {
100                             statusResult = false;
101                         }
102                     }
103                 }
104             }
105         } catch (Exception e) {
106             LOG.error("ERROR::", e);
107             statusResult = false;
108         }
109         if (statusResult) {
110             return STATUS_OPERATIONAL;
111         } else {
112             return "ERROR";
113         }
114     }
115
116     @SuppressWarnings("checkstyle:IllegalCatch")
117     public static ArrayList getAttributeJMXCommand(String objectName, String attributeName) {
118         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
119         ArrayList listOfShards = new ArrayList();
120         if (mbs != null) {
121             try {
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");
133             }
134         }
135         return listOfShards;
136     }
137
138 }