Check for the SHARD Status before opening the OVSDB port/HwvtepSouthboundProvider...
[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
9 package org.opendaylight.ovsdb.utils.mdsal.utils;
10
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;
16
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;
23
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public final class ShardStatusMonitor {
28
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";
33
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";
39
40     public static final Collection<String> TOPOLOGY_SHARDS =
41             Collections.unmodifiableList(Arrays.asList(TOPOLOGY_CONFIG_SHARD, TOPOLOGY_OPER_SHARD));
42
43     //To avoid the checkstyle errors
44     private ShardStatusMonitor() {
45
46     }
47
48     @SuppressWarnings("checkstyle:IllegalCatch")
49     public static String getLeaderJMX(String objectName, String atrName) {
50         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
51         String leader = "";
52         if (mbs != null) {
53             try {
54                 leader  = (String)mbs.getAttribute(new ObjectName(objectName), atrName);
55             } catch (Exception e) {
56                 LOG.error("Failed to get leader jmx {}", e.getMessage());
57             }
58         }
59         return leader;
60     }
61
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)) {
67                 status = false;
68                 break;
69             }
70         }
71         return status;
72     }
73
74     @SuppressWarnings("checkstyle:IllegalCatch")
75     public static String getDataStoreStatus(String name, String type) {
76         boolean statusResult = true;
77         try {
78             ArrayList listOfShards;
79             if (type.equalsIgnoreCase("config")) {
80                 listOfShards = getAttributeJMXCommand(JMX_OBJECT_NAME_LIST_OF_CONFIG_SHARDS, "LocalShards");
81             } else  {
82                 listOfShards = getAttributeJMXCommand(JMX_OBJECT_NAME_LIST_OF_OPER_SHARDS, "LocalShards");
83             }
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";
91                         } else {
92                             jmxObjectShardStatus = "org.opendaylight.controller:Category=Shards,name="
93                                     + listOfShards.get(i) + ",type=DistributedOperationalDatastore";
94                         }
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);
99                             } else {
100                                 LOG.info("{} ::Oper DS has the Leader as:: {}", listOfShards.get(i), leader);
101                             }
102                         } else {
103                             statusResult = false;
104                         }
105                     }
106                 }
107             }
108         } catch (Exception e) {
109             LOG.error("ERROR::", e);
110             statusResult = false;
111         }
112         if (statusResult) {
113             return STATUS_OPERATIONAL;
114         } else {
115             return "ERROR";
116         }
117     }
118
119     @SuppressWarnings("checkstyle:IllegalCatch")
120     public static ArrayList getAttributeJMXCommand(String objectName, String attributeName) {
121         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
122         ArrayList listOfShards = new ArrayList();
123         if (mbs != null) {
124             try {
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");
136             }
137         }
138         return listOfShards;
139     }
140
141 }