Merge "Remove opendaylight-karaf-empty from integration repo"
[integration.git] / test / csit / libraries / ClusterStateLibrary.py
1 __author__ = "Basheeruddin Ahmed"
2 __copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
3 __license__ = "New-style BSD"
4 __email__ = "syedbahm@cisco.com"
5
6 import SettingsLibrary
7 from time import sleep
8 import UtilLibrary
9 import json
10 import sys
11
12
13 def getClusterRoles(shardName, numOfShards=3, numOfTries=3, sleepBetweenRetriesInSecs=1, port=8181, *ips):
14     """Given a shardname (e.g. shard-inventory-config), number of shards and bunch of ips
15
16     determines what role each ip has in an Akka (Raft based) cluster
17     result would look like
18     {'10.194.126.118':'Leader', '10.194.126.118':'Follower', '10.194.126.117': None}
19     """
20     dict = {}
21     for ip in ips:
22         i = 1
23         dict[ip] = None
24
25         while i <= numOfShards:
26             shardMemberName = "member-" + str(i) + "-" + shardName
27             j = 1
28
29             while j <= numOfTries:
30                 print("Try number " + str(j))
31                 try:
32                     print("finding if" + ip + "is leader for shardName =" + shardMemberName)
33                     url = SettingsLibrary.getJolokiaURL(ip, str(port), str(i), shardName)
34                     resp = UtilLibrary.get(url)
35                     print(resp)
36                     if resp.status_code != 200:
37                         sleep(sleepBetweenRetriesInSecs)
38                         continue
39                     print(resp.text)
40                     data = json.loads(resp.text)
41                     if 'value' in data:
42                         dataValue = data['value']
43                         print("datavalue RaftState is", dataValue['RaftState'])
44                         if dataValue['RaftState'] == 'Follower':
45                             dict[ip] = 'Follower'
46                             break
47                         elif dataValue['RaftState'] == 'Leader':
48                             dict[ip] = 'Leader'
49                 except:
50                     e = sys.exc_info()[0]
51                     print("Try" + str(j) + ":An error occurred when finding leader on" + ip +
52                           " for shardName:" + shardMemberName)
53                     print(e)
54                     sleep(sleepBetweenRetriesInSecs)
55                     continue
56                 finally:
57                     j = j + 1
58
59             if dict[ip] is not None:
60                 break
61             i = i + 1
62
63     return dict
64
65
66 def isLeader(shardName, numOfShards, numOfRetries, sleepFor, port, ipAddress):
67     """Given a shardname (e.g. shard-inventory-config), number of shards and an ip determines if its a leader"""
68     ip = getClusterRoles(shardName, numOfShards, numOfRetries, sleepFor, port, ipAddress)
69     print(ip)
70     if ip[ipAddress] == 'Leader':
71         return True
72
73     return False
74
75
76 def getLeader(shardName, numOfShards=3, numOfTries=3, sleepBetweenRetriesInSecs=1, port=8181, *ips):
77     """Returns the leader of the shard given a set of IPs Or None"""
78     for i in range(3):  # Try 3 times to find a leader
79         dict = getClusterRoles(shardName, numOfShards, numOfTries, sleepBetweenRetriesInSecs, port, *ips)
80         for ip in dict.keys():
81             if dict[ip] == 'Leader':
82                 return ip
83
84     return None
85
86
87 def getFollowers(shardName, numOfShards=3, numOfTries=3, sleepBetweenRetriesInSecs=1, port=8181, *ips):
88     """Returns the follower list of a shard given a set of IPs Or []"""
89     for i in range(6):  # Try 6 times to find all followers
90         dict = getClusterRoles(shardName, numOfShards, numOfTries, sleepBetweenRetriesInSecs, port, *ips)
91         result = []
92
93         for ip in dict.keys():
94             if dict[ip] == 'Follower':
95                 result.append(ip)
96         print "i=", i, "result=", result
97         if (len(result) == (len(ips) - 1)):
98             break
99         sleep(1)
100
101     return result
102
103
104 def isFollower(shardName, numOfShards, numOfRetries, sleepFor, port, ipAddress):
105     """Given a shardname (e.g. shard-inventory-config), number of shards and an ip determines if its a leader"""
106     ip = getClusterRoles(shardName, numOfShards, numOfRetries, sleepFor, port, ipAddress)
107     print(ip)
108     if ip[ipAddress] == 'Follower':
109         return True
110
111     return False
112
113
114 def testGetClusterRoles():
115     dict = getClusterRoles("shard-inventory-config", 3, 1, 1, 8181,
116                            "10.194.126.116", "10.194.126.117", "10.194.126.118")
117     print(dict)
118
119     for ip in dict.keys():
120         if isLeader("shard-inventory-config", 3, 1, 1, 8181, ip):
121             print(ip + " is Leader")
122         elif isFollower("shard-inventory-config", 3, 1, 1, 8181, ip):
123             print(ip + " is follower")
124         else:
125             print(ip + " seems to have value " + str(dict[ip]))
126
127
128 def testGetLeader():
129     leader = getLeader("shard-inventory-config", 3, 1, 1, 8181,
130                        "10.194.126.116", "10.194.126.117", "10.194.126.118")
131     print leader
132     return leader
133
134
135 def testGetFollowers():
136     followers = getFollowers("shard-inventory-config", 3, 1, 1, 8181,
137                              "10.194.126.116", "10.194.126.117", "10.194.126.118")
138     print(followers)
139     return followers
140
141 # testGetClusterRoles()
142 # testGetLeader()
143 # testGetFollowers()