import sys
import util
import settings
__author__ = "Basheeruddin Ahmed"
__copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
__license__ = "New-style BSD"
__email__ = "syedbahm@cisco.com"
def addCar(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 = settings.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 = util.post(settings.getAddCarUrl(), "admin", "admin", payload)
print("the response of the POST to add car=")
print(resp)
print("getting the cars in store ")
resp = getCars(0)
# TBD: Detailed validation
def addPerson(numberOfPersons):
"""Creates the specified number of persons based on People yang model using main RPC
To enable RPC a non-user input person entry is created with personId=user0
"""
# FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF
if numberOfPersons == 0:
strId = str(numberOfPersons)
payload = settings.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 = util.nonprintpost(settings.getAddPersonUrl(), "admin", "admin", payload)
return
genderToggle = "Male"
for x in range(1, numberOfPersons + 1):
if(genderToggle == "Male"):
genderToggle = "Female"
else:
genderToggle = "Male"
strId = str(x)
payload = settings.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 = util.post(settings.getAddPersonRpcUrl(), "admin", "admin", payload)
print("payload formed after template substitution=")
print(payload)
print("the response of the POST to add person=")
print(resp)
print("getting the persons for verification")
resp = getPersons(0)
# TBD: Detailed validation
def addCarPerson(numberOfCarPersons):
"""This method is not exposed via commands as only getCarPersons is of interest
addCarPerson entry happens when buyCar is called
To enable RPC a non-user input car-person entry is created with personId=user0
"""
# FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF
if numberOfCarPersons == 0:
payload = settings.add_car_person_template.substitute(
Id=str(numberOfCarPersons), personId="user" + str(numberOfCarPersons))
# Send the POST request REST CONF
resp = util.nonprintpost(settings.getAddCarPersonUrl(), "admin", "admin", payload)
return
for x in range(1, numberOfCarPersons + 1):
strId = str(x)
payload = settings.add_car_person_template.substitute(Id=strId, personId="user" + strId)
# Send the POST request REST CONF
resp = util.post(settings.getAddCarPersonUrl(), "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(0)
# TBD detailed validation
def buyCar(numberOfCarBuyers):
"""Invokes an RPC REST call that does a car purchase by a person id
It is expected that the Car and Person entries are already created
before invoking this method
"""
for x in range(1, numberOfCarBuyers + 1):
strId = str(x)
payload = settings.buy_car_rpc_template.substitute(personId="user" + strId, carId=strId)
# Send the POST request using RPC
resp = util.post(settings.getBuyCarRpcUrl(), "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(0)
def getCars(ignore):
"""Uses the GET on car:cars resource to get all cars in the store using RESTCONF"""
resp = util.get(settings.getCarsUrl(), "admin", "admin")
print(resp)
return resp
def getPersons(ignore):
"""Uses the GET on people:people resource to get all persons in the store using RESTCONF
This also returns the dummy entry created for routed RPC
with personId being user0
"""
resp = util.get(settings.getPersonsUrl(), "admin", "admin")
print(resp)
return resp
def getCarPersonMappings(ignore):
"""Uses the GET on car-people:car-people resource to get all car-persons entry in the store using RESTCONF
This also returns the dummy entry created for routed RPC
with personId being user0
"""
resp = util.get(settings.getCarPersonUrl(), "admin", "admin")
print(resp)
return resp
def deleteAllCars(ignore):
"""delete all cars in the store using RESTCONF"""
util.delete(settings.getCarsUrl(), "admin", "admin")
resp = getCars(ignore)
print("Cars in store after deletion:" + str(resp))
def deleteAllPersons(ignore):
"""delete all persons in the store using RESTCONF"""
util.delete(settings.getPersonsUrl(), "admin", "admin")
resp = getPersons(ignore)
print("Persons in store after deletion:" + str(resp))
#
# Usage message shown to user
#
def options():
command = 'ac=Add Car\n\t\tap=Add Person \n\t\tbc=Buy Car\n\t\tgc=Get Cars\n\t\tgp=Get Persons\n\t\t' \
'gcp=Get Car-Person Mappings\n\t\tdc=Delete All Cars\n\t\tdp=Delete All Persons)'
param = '\n\t is\n\t\t' \
'number of cars to be added if =ac\n\t\t' \
'number of persons to be added if =ap\n\t\t' \
'number of car buyers if =bc\n\t\t'\
'pass 0 if =gc or gp or gcp or dc or dp'\
usageString = 'usage: python crud \nwhere\n\t = ODL server ip address' \
'\n\t = any of the following commands \n\t\t'
usageString = usageString + command + param
print(usageString)
#
# entry point for command executions
#
def main():
if len(sys.argv) < 4:
options()
quit(0)
settings.hostname = sys.argv[1]
settings.port = '8080'
call = dict(ac=addCar, ap=addPerson, bc=buyCar,
gc=getCars, gp=getPersons, gcp=getCarPersonMappings, dc=deleteAllCars, dp=deleteAllPersons)
# FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
addPerson(0)
# FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
addCarPerson(0)
call[sys.argv[2]](int(sys.argv[3]))
#
# main invoked
main()