Shard Status logic in genius
[genius.git] / mdsalutil / mdsalutil-impl / src / main / java / org / opendaylight / genius / mdsalutil / internal / ShardStatusMonitorImpl.java
1 /*
2  * Copyright (c) 2019 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.genius.mdsalutil.internal;
9
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;
24
25 @Singleton
26 public final class ShardStatusMonitorImpl implements ShardStatusMonitor {
27
28     private static final Logger LOG = LoggerFactory.getLogger(ShardStatusMonitorImpl.class);
29
30     @Inject
31     public ShardStatusMonitorImpl() {
32         // Do nothing
33     }
34
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")) {
40                 status = false;
41                 break;
42             }
43         }
44         return status;
45     }
46
47     @SuppressWarnings("IllegalCatch")
48     private static String getDataStoreStatus(String name, String type) {
49         boolean statusResult = true;
50         try {
51             ArrayList listOfShards;
52             if (type.equalsIgnoreCase("config")) {
53                 listOfShards = getAttributeJMXCommand(JMX_OBJECT_NAME_LIST_OF_CONFIG_SHARDS, "LocalShards");
54             } else {
55                 listOfShards = getAttributeJMXCommand(JMX_OBJECT_NAME_LIST_OF_OPER_SHARDS, "LocalShards");
56             }
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";
64                         } else {
65                             jmxObjectShardStatus = "org.opendaylight.controller:Category=Shards,name="
66                                     + listOfShards.get(i) + ",type=DistributedOperationalDatastore";
67                         }
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);
73                             } else {
74                                 LOG.info("{} ::Oper DS has the Leader as:: {}", listOfShards.get(i), leader);
75                             }
76                         } else {
77                             statusResult = false;
78                         }
79                     }
80                 }
81             }
82         } catch (Exception e) {
83             LOG.error("ERROR::", e);
84             statusResult = false;
85         }
86         if (statusResult) {
87             return "OPERATIONAL";
88         } else {
89             return "ERROR";
90         }
91     }
92
93     @SuppressWarnings("IllegalCatch")
94     private static ArrayList getAttributeJMXCommand(String objectName, String attributeName) {
95         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
96         ArrayList listOfShards = new ArrayList();
97         if (mbs != null) {
98             try {
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");
110             }
111         }
112         return listOfShards;
113     }
114
115     @SuppressWarnings("IllegalCatch")
116     private static String getLeaderJMX(String objectName, String atrName) {
117         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
118         String leader = "";
119         if (mbs != null) {
120             try {
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");
132             }
133         }
134         return leader;
135     }
136 }