def addCarPerson(hostname,port,numberOfCarPersons): #FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF if(numberOfCarPersons==0): payload = SettingsLibrary.add_car_person_template.substitute(Id=str(numberOfCarPersons),personId="user"+str(numberOfCarPersons)) # Send the POST request REST CONF resp = UtilLibrary.nonprintpost(SettingsLibrary.getAddCarPersonUrl(hostname,port),"admin", "admin",payload) return for x in range(1, numberOfCarPersons+1): strId = str(x) payload = SettingsLibrary.add_car_person_template.substitute(Id=strId,personId="user"+strId) # Send the POST request REST CONF resp = UtilLibrary.post(SettingsLibrary.getAddCarPersonUrl(hostname,port),"admin", "admin",payload) print("payload formed after template substitution=") print(payload) print("the response of the POST to add car_person=") print(resp) print("getting the car_persons for verification") resp=getCarPersonMappings(hostname,port,0)
def addCarPerson(hostname, port, numberOfCarPersons): """This method is not exposed via commands as only getCarPersons is of interest addCarPerson entry happens when buyCar is called <note> To enable RPC a non-user input car-person entry is created with personId=user0 </note> """ # FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF if (numberOfCarPersons == 0): payload = SettingsLibrary.add_car_person_template.substitute( Id=str(numberOfCarPersons), personId="user" + str(numberOfCarPersons)) # Send the POST request REST CONF resp = UtilLibrary.nonprintpost(SettingsLibrary.getAddCarPersonUrl(hostname, port), "admin", "admin", payload) return resp for x in range(1, numberOfCarPersons + 1): strId = str(x) payload = SettingsLibrary.add_car_person_template.substitute(Id=strId, personId="user" + strId) # Send the POST request REST CONF resp = UtilLibrary.post(SettingsLibrary.getAddCarPersonUrl(hostname, port), "admin", "admin", payload) print("payload formed after template substitution=") print(payload) print("the response of the POST to add car_person=") print(resp) print("getting the car_persons for verification") resp = getCarPersonMappings(hostname, port, 0) # TBD detailed validation return resp
def addPerson(hostname,port,numberOfPersons): #FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF if(numberOfPersons==0): strId =str(numberOfPersons) payload = SettingsLibrary.add_person_payload_template.substitute(personId="user"+strId,gender="unknown",age=0, address=strId + "Way, Some Country, Some Zip "+strId, contactNo= "some number"+strId) # Send the POST request using RESTCONF resp = UtilLibrary.nonprintpost(SettingsLibrary.getAddPersonUrl(hostname,port),"admin", "admin",payload) return resp genderToggle = "Male" for x in range(1, numberOfPersons+1): if(genderToggle == "Male"): genderToggle = "Female" else: genderToggle = "Male" strId = str(x) payload = SettingsLibrary.add_person_rpc_payload_template.substitute(personId="user"+strId,gender=genderToggle,age=(20+x%100), address=strId + "Way, Some Country, Some Zip "+str(x%1000), contactNo= "some number"+strId) # Send the POST request using RPC resp = UtilLibrary.post(SettingsLibrary.getAddPersonRpcUrl(hostname,port),"admin", "admin",payload) print("payload formed after template substitution=") print(payload) print("the response of the POST to add person=") print(resp) return resp
def addPerson(hostname, port, numberOfPersons, *expected): """Creates the specified number of persons based on People yang model using main RPC <note> To enable RPC a non-user input person entry is created with personId=user0 </note> """ # FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF if numberOfPersons == 0: strId = str(numberOfPersons) payload = SettingsLibrary.add_person_payload_template.substitute( personId="user" + strId, gender="unknown", age=0, address=strId + "Way, Some Country, Some Zip " + strId, contactNo="some number" + strId, ) # Send the POST request using RESTCONF resp = UtilLibrary.nonprintpost( SettingsLibrary.getAddPersonUrl(hostname, port), "admin", "admin", payload) return resp genderToggle = "Male" for x in range(1, numberOfPersons + 1): if genderToggle == "Male": genderToggle = "Female" else: genderToggle = "Male" strId = str(x) payload = SettingsLibrary.add_person_rpc_payload_template.substitute( personId="user" + strId, gender=genderToggle, age=(20 + x % 100), address=strId + "Way, Some Country, Some Zip " + str(x % 1000), contactNo="some number" + strId, ) # Send the POST request using RPC resp = UtilLibrary.post( SettingsLibrary.getAddPersonRpcUrl(hostname, port), "admin", "admin", payload, ) print("payload formed after template substitution=") print(payload) print("the response of the POST to add person=") print(resp) if expected and str(resp.status_code) not in expected: raise RuntimeError( "Add person failed for {}:{} with status {}".format( hostname, port, resp.status_code)) return resp
def buyCar(hostname, port, numberOfCarBuyers, start=0): """Invokes an RPC REST call that does a car purchase by a person id <note> It is expected that the Car and Person entries are already created before invoking this method </note> """ print("Buying " + str(numberOfCarBuyers) + " Cars") for x in range(start, start + numberOfCarBuyers): strId = str(x + 1) payload = SettingsLibrary.buy_car_rpc_template.substitute( personId="user" + strId, carId=strId) # Send the POST request using RPC resp = UtilLibrary.post( SettingsLibrary.getBuyCarRpcUrl(hostname, port), "admin", "admin", payload) print(resp) print(resp.text) if resp.status_code != 200: raise RuntimeError( "Buy car failed for {}:{} with status {}".format( hostname, port, resp.status_code))
def getCars(hostname, port, ignore): """Uses the GET on car:cars resource to get all cars in the store using RESTCONF""" resp = UtilLibrary.get(SettingsLibrary.getCarsUrl(hostname, port), "admin", "admin") resp.encoding = "utf-8" print(resp.text) return resp
def addCar(hostname, port, numberOfCars, *expected): """Creates the specified number of cars based on Cars yang model using RESTCONF""" for x in range(1, numberOfCars + 1): strId = str(x) payload = SettingsLibrary.add_car_payload_template.substitute( id=strId, category="category" + strId, model="model" + strId, manufacturer="manufacturer" + strId, year=(2000 + x % 100), ) print("payload formed after template substitution=") print(payload) # Send the POST request resp = UtilLibrary.post(SettingsLibrary.getAddCarUrl(hostname, port), "admin", "admin", payload) print("the response of the POST to add car=") print(resp) if expected and str(resp.status_code) not in expected: raise RuntimeError( "Add car failed for {}:{} with status {}".format( hostname, port, resp.status_code)) return resp
def getClusterRoles(shardName, numOfShards=3, numOfTries=3, sleepBetweenRetriesInSecs=3, port=8181, *ips): """Given a shardname (e.g. shard-inventory-config), number of shards and bunch of ips determines what role each ip has in an Akka (Raft based) cluster result would look like {'10.194.126.118':'Leader', '10.194.126.118':'Follower', '10.194.126.117': None} """ dict = {} for ip in ips: i = 1 dict[ip] = None print "numOfShards => " + str(numOfShards) while i <= numOfShards: shardMemberName = "member-" + str(i) + "-" + shardName j = 1 print 'j => ' + str(j) print 'numOfTries => ' + str(numOfTries) while int(j) <= int(numOfTries): print("Try number " + str(j)) try: print("getting role of " + ip + " for shardName = " + shardMemberName) url = SettingsLibrary.getJolokiaURL( ip, str(port), str(i), shardName) print url resp = UtilLibrary.get(url) print(resp) if resp.status_code != 200: sleep(sleepBetweenRetriesInSecs) continue print(resp.text) data = json.loads(resp.text) if 'value' in data: dataValue = data['value'] print("datavalue RaftState is", dataValue['RaftState']) dict[ip] = dataValue['RaftState'] except: e = sys.exc_info()[0] print("Try" + str(j) + ":An error occurred when finding leader on" + ip + " for shardName:" + shardMemberName) print(e) sleep(sleepBetweenRetriesInSecs) continue finally: j = j + 1 if dict[ip] is not None: break i = i + 1 return dict
def addPerson(hostname, port, numberOfPersons, *expected): """Creates the specified number of persons based on People yang model using main RPC <note> To enable RPC a non-user input person entry is created with personId=user0 </note> """ # FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF if (numberOfPersons == 0): strId = str(numberOfPersons) payload = SettingsLibrary.add_person_payload_template.substitute( personId="user" + strId, gender="unknown", age=0, address=strId + "Way, Some Country, Some Zip " + strId, contactNo="some number" + strId) # Send the POST request using RESTCONF resp = UtilLibrary.nonprintpost(SettingsLibrary.getAddPersonUrl(hostname, port), "admin", "admin", payload) return resp genderToggle = "Male" for x in range(1, numberOfPersons + 1): if(genderToggle == "Male"): genderToggle = "Female" else: genderToggle = "Male" strId = str(x) payload = SettingsLibrary.add_person_rpc_payload_template.substitute( personId="user" + strId, gender=genderToggle, age=(20 + x % 100), address=strId + "Way, Some Country, Some Zip " + str(x % 1000), contactNo="some number" + strId) # Send the POST request using RPC resp = UtilLibrary.post(SettingsLibrary.getAddPersonRpcUrl(hostname, port), "admin", "admin", payload) print("payload formed after template substitution=") print(payload) print("the response of the POST to add person=") print(resp) if expected and str(resp.status_code) not in expected: raise RuntimeError('Add person failed for {}:{} with status {}'. format(hostname, port, resp.status_code)) return resp
def getPersons(hostname, port, ignore): """Uses the GET on people:people resource to get all persons in the store using RESTCONF <note> This also returns the dummy entry created for routed RPC with personId being user0 </note> """ resp = UtilLibrary.get(SettingsLibrary.getPersonsUrl(hostname, port), "admin", "admin") resp.encoding = 'utf-8' print(resp.text) return resp
def getClusterRoles(shardName, numOfShards=3, numOfTries=3, sleepBetweenRetriesInSecs=1, port=8181, *ips): """Given a shardname (e.g. shard-inventory-config), number of shards and bunch of ips determines what role each ip has in an Akka (Raft based) cluster result would look like {'10.194.126.118':'Leader', '10.194.126.118':'Follower', '10.194.126.117': None} """ dict = {} for ip in ips: i = 1 dict[ip] = None while i <= numOfShards: shardMemberName = "member-" + str(i) + "-" + shardName j = 1 while j <= numOfTries: print("Try number " + str(j)) try: print("finding if" + ip + "is leader for shardName =" + shardMemberName) url = SettingsLibrary.getJolokiaURL(ip, str(port), str(i), shardName) resp = UtilLibrary.get(url) print(resp) if resp.status_code != 200: sleep(sleepBetweenRetriesInSecs) continue print(resp.text) data = json.loads(resp.text) if 'value' in data: dataValue = data['value'] print("datavalue RaftState is", dataValue['RaftState']) if dataValue['RaftState'] == 'Follower': dict[ip] = 'Follower' break elif dataValue['RaftState'] == 'Leader': dict[ip] = 'Leader' except: e = sys.exc_info()[0] print("Try" + str(j) + ":An error occurred when finding leader on" + ip + " for shardName:" + shardMemberName) print(e) sleep(sleepBetweenRetriesInSecs) continue finally: j = j + 1 if dict[ip] is not None: break i = i + 1 return dict
def initCar(hostname, port): """Initiales the car shard""" x = 0 strId = str(x) payload = SettingsLibrary.add_car_init_payload_template.substitute( id=strId, category="category" + strId, model="model" + strId, manufacturer="manufacturer" + strId, year=(2000 + x % 100)) print("Initialization payload=") print(payload) resp = UtilLibrary.post(SettingsLibrary.getAddCarInitUrl(hostname, port), "admin", "admin", payload) print("the response of the POST to add car=") print(resp) return resp
def getClusterRoles(shardName, numOfShards=3, numOfTries=3, sleepBetweenRetriesInSecs=1, port=8181, *ips): dict = {} for ip in ips: i = 1 dict[ip] = None bFollower = 0 while i <= numOfShards: shardMemberName = "member-" + str(i) + "-" + shardName j = 1 while j <= numOfTries: print "Try number " + str(j) try: print "finding if" + ip + "is leader for shardName =" + shardMemberName url = SettingsLibrary.getJolokiaURL( ip, str(port), str(i), shardName) resp = UtilLibrary.get(url) print resp if (resp.status_code != 200): continue data = json.loads(resp.text) if ('value' in data): dataValue = data['value'] if (dataValue['RaftState'] == 'Follower'): dict[ip] = 'Follower' break elif (dataValue['RaftState'] == 'Leader'): dict[ip] = 'Leader' except: e = sys.exc_info()[0] print "Try" + str( j ) + ":An error occurred when finding leader on" + ip + " for shardName:" + shardMemberName print e sleep(sleepBetweenRetriesInSecs) continue finally: j = j + 1 if (dict[ip] != None): break i = i + 1 return dict
def getCarPersonMappings(hostname, port, ignore): """Uses the GET on car-people:car-people resource to get all car-persons entry in the store using RESTCONF <note> This also returns the dummy entry created for routed RPC with personId being user0 </note> """ resp = UtilLibrary.get(SettingsLibrary.getCarPersonUrl(hostname, port), "admin", "admin") resp.encoding = "utf-8" print(resp) return resp
def addCar(hostname,port,numberOfCars): for x in range(1, numberOfCars+1): strId = str(x) payload = SettingsLibrary.add_car_payload_template.substitute(id=strId,category="category"+strId,model="model"+strId, manufacturer="manufacturer"+strId, year=(2000+x%100)) print("payload formed after template substitution=") print(payload) # Send the POST request resp = UtilLibrary.post(SettingsLibrary.getAddCarUrl(hostname,port),"admin", "admin",payload) print("the response of the POST to add car=") print(resp) return resp
def buyCar(hostname,port,numberOfCarBuyers): for x in range(1, numberOfCarBuyers+1): strId = str(x) payload = SettingsLibrary.buy_car_rpc_template.substitute(personId="user"+strId,carId=strId) # Send the POST request using RPC resp = UtilLibrary.post(SettingsLibrary.getBuyCarRpcUrl(hostname,port),"admin", "admin",payload) print("payload formed after template substitution=") print(payload) print("the response of the POST to buycar=") print(resp) print("getting the car_persons for verification") resp=getCarPersonMappings(hostname,port,0)
def addCar(hostname, port, numberOfCars): """Creates the specified number of cars based on Cars yang model using RESTCONF""" for x in range(1, numberOfCars+1): strId = str(x) payload = SettingsLibrary.add_car_payload_template.substitute( id=strId, category="category" + strId, model="model" + strId, manufacturer="manufacturer" + strId, year=(2000 + x % 100)) print("payload formed after template substitution=") print(payload) # Send the POST request resp = UtilLibrary.post(SettingsLibrary.getAddCarUrl(hostname, port), "admin", "admin", payload) print("the response of the POST to add car=") print(resp) time.sleep(5) # Let the add finish return resp
def addCar(hostname, port, numberOfCars, *expected): """Creates the specified number of cars based on Cars yang model using RESTCONF""" for x in range(1, numberOfCars + 1): strId = str(x) payload = SettingsLibrary.add_car_payload_template.substitute( id=strId, category="category" + strId, model="model" + strId, manufacturer="manufacturer" + strId, year=(2000 + x % 100)) print("payload formed after template substitution=") print(payload) # Send the POST request resp = UtilLibrary.post(SettingsLibrary.getAddCarUrl(hostname, port), "admin", "admin", payload) print("the response of the POST to add car=") print(resp) if expected and str(resp.status_code) not in expected: raise RuntimeError('Add car failed for {}:{} with status {}'. format(hostname, port, resp.status_code)) return resp
def getClusterRoles(shardName,numOfShards=3,numOfTries=3,sleepBetweenRetriesInSecs=1,port=8181,*ips): dict={} for ip in ips: i=1 dict[ip]=None bFollower = 0 while i <= numOfShards: shardMemberName = "member-"+str(i)+"-"+shardName; j=1 while j <= numOfTries: print "Try number "+str(j) try: print "finding if"+ ip +"is leader for shardName ="+shardMemberName url = SettingsLibrary.getJolokiaURL(ip,str(port),str(i),shardName) resp = UtilLibrary.get(url) print resp if(resp.status_code != 200): continue data = json.loads(resp.text) if('value' in data): dataValue = data['value'] if(dataValue['RaftState']=='Follower'): dict[ip]='Follower' break; elif(dataValue['RaftState']=='Leader'): dict[ip]='Leader' except: e = sys.exc_info()[0] print "Try"+str(j)+":An error occurred when finding leader on"+ip+" for shardName:" +shardMemberName print e sleep(sleepBetweenRetriesInSecs) continue finally: j=j+1 if(dict[ip]!=None): break; i=i+1 return dict
def buyCar(hostname, port, numberOfCarBuyers, start=0): """Invokes an RPC REST call that does a car purchase by a person id <note> It is expected that the Car and Person entries are already created before invoking this method </note> """ for x in range(start, start+numberOfCarBuyers): strId = str(x+1) payload = SettingsLibrary.buy_car_rpc_template.substitute(personId="user" + strId, carId=strId) # Send the POST request using RPC resp = UtilLibrary.post(SettingsLibrary.getBuyCarRpcUrl(hostname, port), "admin", "admin", payload) print(resp) print(resp.text) if (resp.status_code != 204): return False return True
def getCars(hostname, port, ignore): """Uses the GET on car:cars resource to get all cars in the store using RESTCONF""" resp = UtilLibrary.get(SettingsLibrary.getCarsUrl(hostname, port), "admin", "admin") resp.encoding = 'utf-8' print(resp.text) return resp
def getPersons(hostname,port,ignore): resp = UtilLibrary.get(SettingsLibrary.getPersonsUrl(hostname,port),"admin","admin") resp.encoding = 'utf-8' print (resp.text) return resp
def deleteAllCarsPersons(hostname, port, ignore): """delete all car -poeple s in the store using RESTCONF""" UtilLibrary.delete(SettingsLibrary.getCarPersonUrl(hostname, port), "admin", "admin") resp = getPersons(hostname, port, ignore) print("Persons in store after deletion:" + str(resp))
def getCarPersonMappings(hostname,port,ignore): resp = UtilLibrary.get(SettingsLibrary.getCarPersonUrl(hostname,port),"admin","admin") resp.encoding = 'utf-8' print (resp) return resp
def deleteAllCarsPersons(hostname,port,ignore): UtilLibrary.delete(SettingsLibrary.getCarPersonsUrl(hostname,port),"admin","admin") resp = getPersons(hostname,port,ignore) print("Persons in store after deletion:"+ str(resp))
"user": "******", "pass": "******" } } Usage:python rejoin.py """ import sys sys.path.append('../../../csit/libraries') import UtilLibrary import json try: with open('cluster.json') as cluster_file: data = json.load(cluster_file) except: print str(sys.exc_info()) print "unable to open the file cluster.json" exit(1) try: cluster_list = data["cluster"]["controllers"] user_name = data["cluster"]["user"] user_pass = data["cluster"]["pass"] except: print str(sys.exc_info()) print 'Error reading the file cluster.json' exit(1) print UtilLibrary.flush_iptables(cluster_list, user_name, user_pass)
import_utility_modules() try: with open('cluster.json') as cluster_file: data = json.load(cluster_file) except: print str(sys.exc_info()) print "unable to open the file cluster.json" exit(1) try: cluster_list = data["cluster"]["controllers"] cluster_ips = [] for controller in cluster_list: cluster_ips.append(controller["ip"]) user_name = data["cluster"]["user"] user_pass = data["cluster"]["pass"] except: print str(sys.exc_info()) print 'Error reading the file cluster.json' exit(1) try: isolate = int(sys.argv[1]) except: print 'You must specify the number (e.g. 1, 2, 3) of the controller to isolate.' exit(1) print "isolating controller " + str(isolate) print UtilLibrary.isolate_controller(cluster_ips, user_name, user_pass, isolate)
with open('cluster.json') as cluster_file: data = json.load(cluster_file) except: print str(sys.exc_info()) print 'unable to open the file cluster.json' exit(1) try: controllers = data["cluster"]["controllers"] user_name = data["cluster"]["user"] user_pass = data["cluster"]["pass"] except: print str(sys.exc_info()) print 'Error reading the file cluster.json' exit(1) try: isolate = int(sys.argv[1]) duration = int(sys.argv[2]) except: print 'You must specify the number (e.g. 1, 2, 3) of the controller to isolate.' exit(1) print 'Isolating controller ' + str(isolate) print UtilLibrary.isolate_controller(controllers, user_name, user_pass, isolate) print 'Pausing for ' + str(duration) + ' seconds...' time.sleep(duration) print UtilLibrary.flush_iptables(controllers, user_name, user_pass)
try: with open('cluster.json') as cluster_file: data = json.load(cluster_file) except: print str(sys.exc_info()) print 'unable to open the file cluster.json' exit(1) try: controllers = data["cluster"]["controllers"] user_name = data["cluster"]["user"] user_pass = data["cluster"]["pass"] except: print str(sys.exc_info()) print 'Error reading the file cluster.json' exit(1) try: isolate = int(sys.argv[1]) duration = int(sys.argv[2]) except: print 'You must specify the number (e.g. 1, 2, 3) of the controller to isolate.' exit(1) print 'Isolating controller ' + str(isolate) print UtilLibrary.isolate_controller(controllers, user_name, user_pass, isolate) print 'Pausing for ' + str(duration) + ' seconds...' time.sleep(duration) print UtilLibrary.flush_iptables(controllers, user_name, user_pass)