openbmctool: add network related commands #1

added below givem commands:
-enableDHCP
-disableDHCP
-getHostName
-setHostName
-getDomainName
-setDomainName
-getMACAddress
-setMACAddress
-getDefaultGW
-setDefaultGW
-view-config
-getNTP
-setNTP
-getDNS
-setDNS

Tested: used below given commands in testing
1.python openbmctool.py -H <BMC_IP> -U root -P <root password> network enableDHCP -I <Interface name>
2.python openbmctool.py -H <BMC_IP> -U root -P <root password> network disableDHCP -I <Interface name>
3.python openbmctool.py -H <BMC_IP> -U root -P <root password> network getHostName
4.python openbmctool.py -H <BMC_IP> -U root -P <root password> network setHostName -H <host name>
5.python openbmctool.py -H <BMC_IP> -U root -P <root password> network getDomainName -I <Interface name>
6.python openbmctool.py -H <BMC_IP> -U root -P <root password> network setDomainName -I <Interface name> -D DomainName1,DomainName2,..
7.python openbmctool.py -H <BMC_IP> -U root -P <root password> network getMACAddress -I  <Interface name>
8.python openbmctool.py -H <BMC_IP> -U root -P <root password> network setMACAddress -I  <Interface name> -MA xx:xx:xx:xx:xx
9.python openbmctool.py -H <BMC_IP> -U root -P <root password> network getDefaultGW
10.python openbmctool.py -H <BMC_IP> -U root -P <root password> network setDefaultGW  -GW <default gw>
11.python openbmctool.py -H <BMC_IP> -U root -P <root password> network view-config
12.python openbmctool.py -H <BMC_IP> -U root -P <root password> network getNTP -I <Interface name>
13.python openbmctool.py -H <BMC_IP> -U root -P <root password> network setNTP -I <Interface name> -N NTP1,NTP2,...
14.python openbmctool.py -H <BMC_IP> -U root -P <root password> network getDNS -I <Interface name>
15.python openbmctool.py -H <BMC_IP> -U root -P <root password> network setDNS -I <Interface name> -d DNS1,DNS2,...

Change-Id: I767536e93588285fcff89ab241586cdb122529c7
Signed-off-by: Nagaraju Goruganti <ngorugan@in.ibm.com>
diff --git a/thalerj/openbmctool.py b/thalerj/openbmctool.py
index 7829603..9a09ddd 100755
--- a/thalerj/openbmctool.py
+++ b/thalerj/openbmctool.py
@@ -2275,6 +2275,440 @@
 
     return res.text
 
+
+def enableDHCP(host, args, session):
+
+    """
+        Called by the network function. Enables DHCP.
+
+        @param host: string, the hostname or IP address of the bmc
+        @param args: contains additional arguments used by the ldap subcommand
+                args.json: boolean, if this flag is set to true, the output
+                will be provided in json format for programmatic consumption
+        @param session: the active session to use
+    """
+
+    url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\
+    "/attr/DHCPEnabled"
+    httpHeader = {'Content-Type': 'application/json'}
+    data = "{\"data\": 1 }"
+    try:
+        res = session.put(url, headers=httpHeader, data=data, verify=False,
+                          timeout=30)
+
+    except(requests.exceptions.Timeout):
+        return(connectionErrHandler(args.json, "Timeout", None))
+    except(requests.exceptions.ConnectionError) as err:
+        return connectionErrHandler(args.json, "ConnectionError", err)
+    if res.status_code == 403:
+        return "The specified Interface"+"("+args.Interface+")"+\
+        " doesn't exist"
+
+    return res.text
+
+
+def disableDHCP(host, args, session):
+    """
+        Called by the network function. Disables DHCP.
+
+        @param host: string, the hostname or IP address of the bmc
+        @param args: contains additional arguments used by the ldap subcommand
+                args.json: boolean, if this flag is set to true, the output
+                will be provided in json format for programmatic consumption
+        @param session: the active session to use
+    """
+
+    url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\
+    "/attr/DHCPEnabled"
+    httpHeader = {'Content-Type': 'application/json'}
+    data = "{\"data\": 0 }"
+    try:
+        res = session.put(url, headers=httpHeader, data=data, verify=False,
+                          timeout=30)
+    except(requests.exceptions.Timeout):
+        return(connectionErrHandler(args.json, "Timeout", None))
+    except(requests.exceptions.ConnectionError) as err:
+        return connectionErrHandler(args.json, "ConnectionError", err)
+    if res.status_code == 403:
+        return "The specified Interface"+"("+args.Interface+")"+\
+        " doesn't exist"
+    return res.text
+
+
+def getHostname(host, args, session):
+
+    """
+        Called by the network function. Prints out the Hostname.
+
+        @param host: string, the hostname or IP address of the bmc
+        @param args: contains additional arguments used by the ldap subcommand
+                args.json: boolean, if this flag is set to true, the output
+                will be provided in json format for programmatic consumption
+        @param session: the active session to use
+    """
+
+    url = "https://"+host+"/xyz/openbmc_project/network/config/attr/HostName"
+    httpHeader = {'Content-Type': 'application/json'}
+
+    try:
+        res = session.get(url, headers=httpHeader, verify=False, timeout=40)
+    except(requests.exceptions.Timeout):
+        return(connectionErrHandler(args.json, "Timeout", None))
+    except(requests.exceptions.ConnectionError) as err:
+        return connectionErrHandler(args.json, "ConnectionError", err)
+
+    return res.text
+
+
+def setHostname(host, args, session):
+    """
+        Called by the network function. Sets the Hostname.
+
+        @param host: string, the hostname or IP address of the bmc
+        @param args: contains additional arguments used by the ldap subcommand
+                args.json: boolean, if this flag is set to true, the output
+                will be provided in json format for programmatic consumption
+        @param session: the active session to use
+    """
+
+    url = "https://"+host+"/xyz/openbmc_project/network/config/attr/HostName"
+    httpHeader = {'Content-Type': 'application/json'}
+
+    data = {"data": args.HostName}
+
+    try:
+        res = session.put(url, headers=httpHeader, json=data, verify=False,
+                          timeout=30)
+    except(requests.exceptions.Timeout):
+        return(connectionErrHandler(args.json, "Timeout", None))
+    except(requests.exceptions.ConnectionError) as err:
+        return connectionErrHandler(args.json, "ConnectionError", err)
+
+    return res.text
+
+
+def getDomainName(host, args, session):
+
+    """
+        Called by the network function. Prints out the DomainName.
+
+        @param host: string, the hostname or IP address of the bmc
+        @param args: contains additional arguments used by the ldap subcommand
+                args.json: boolean, if this flag is set to true, the output
+                will be provided in json format for programmatic consumption
+        @param session: the active session to use
+    """
+
+    url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\
+    "/attr/DomainName"
+    httpHeader = {'Content-Type': 'application/json'}
+
+    try:
+        res = session.get(url, headers=httpHeader, verify=False, timeout=40)
+    except(requests.exceptions.Timeout):
+        return(connectionErrHandler(args.json, "Timeout", None))
+    except(requests.exceptions.ConnectionError) as err:
+        return connectionErrHandler(args.json, "ConnectionError", err)
+    if res.status_code == 404:
+        return "The specified Interface"+"("+args.Interface+")"+\
+        " doesn't exist"
+
+    return res.text
+
+
+def setDomainName(host, args, session):
+    """
+        Called by the network function. Sets the DomainName.
+
+        @param host: string, the hostname or IP address of the bmc
+        @param args: contains additional arguments used by the ldap subcommand
+                args.json: boolean, if this flag is set to true, the output
+                will be provided in json format for programmatic consumption
+        @param session: the active session to use
+    """
+
+    url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\
+    "/attr/DomainName"
+    httpHeader = {'Content-Type': 'application/json'}
+
+    data = {"data": args.DomainName.split(",")}
+
+    try:
+        res = session.put(url, headers=httpHeader, json=data, verify=False,
+                          timeout=30)
+    except(requests.exceptions.Timeout):
+        return(connectionErrHandler(args.json, "Timeout", None))
+    except(requests.exceptions.ConnectionError) as err:
+        return connectionErrHandler(args.json, "ConnectionError", err)
+    if res.status_code == 403:
+        return "The specified Interface"+"("+args.Interface+")"+\
+        " doesn't exist"
+
+    return res.text
+
+
+def getMACAddress(host, args, session):
+
+    """
+        Called by the network function. Prints out the MACAddress.
+
+        @param host: string, the hostname or IP address of the bmc
+        @param args: contains additional arguments used by the ldap subcommand
+                args.json: boolean, if this flag is set to true, the output
+                will be provided in json format for programmatic consumption
+        @param session: the active session to use
+    """
+
+    url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\
+    "/attr/MACAddress"
+    httpHeader = {'Content-Type': 'application/json'}
+
+    try:
+        res = session.get(url, headers=httpHeader, verify=False, timeout=40)
+    except(requests.exceptions.Timeout):
+        return(connectionErrHandler(args.json, "Timeout", None))
+    except(requests.exceptions.ConnectionError) as err:
+        return connectionErrHandler(args.json, "ConnectionError", err)
+    if res.status_code == 404:
+        return "The specified Interface"+"("+args.Interface+")"+\
+        " doesn't exist"
+
+    return res.text
+
+
+def setMACAddress(host, args, session):
+    """
+        Called by the network function. Sets the MACAddress.
+
+        @param host: string, the hostname or IP address of the bmc
+        @param args: contains additional arguments used by the ldap subcommand
+                args.json: boolean, if this flag is set to true, the output
+                will be provided in json format for programmatic consumption
+        @param session: the active session to use
+    """
+
+    url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\
+    "/attr/MACAddress"
+    httpHeader = {'Content-Type': 'application/json'}
+
+    data = {"data": args.MACAddress}
+
+    try:
+        res = session.put(url, headers=httpHeader, json=data, verify=False,
+                          timeout=30)
+    except(requests.exceptions.Timeout):
+        return(connectionErrHandler(args.json, "Timeout", None))
+    except(requests.exceptions.ConnectionError) as err:
+        return connectionErrHandler(args.json, "ConnectionError", err)
+    if res.status_code == 403:
+        return "The specified Interface"+"("+args.Interface+")"+\
+        " doesn't exist"
+
+    return res.text
+
+
+def getDefaultGateway(host, args, session):
+
+    """
+        Called by the network function. Prints out the DefaultGateway.
+
+        @param host: string, the hostname or IP address of the bmc
+        @param args: contains additional arguments used by the ldap subcommand
+                args.json: boolean, if this flag is set to true, the output
+                will be provided in json format for programmatic consumption
+        @param session: the active session to use
+    """
+
+    url = "https://"+host+"/xyz/openbmc_project/network/config/attr/DefaultGateway"
+    httpHeader = {'Content-Type': 'application/json'}
+
+    try:
+        res = session.get(url, headers=httpHeader, verify=False, timeout=40)
+    except(requests.exceptions.Timeout):
+        return(connectionErrHandler(args.json, "Timeout", None))
+    except(requests.exceptions.ConnectionError) as err:
+        return connectionErrHandler(args.json, "ConnectionError", err)
+    if res.status_code == 404:
+        return "Failed to get Default Gateway info!!"
+
+    return res.text
+
+
+def setDefaultGateway(host, args, session):
+    """
+        Called by the network function. Sets the DefaultGateway.
+
+        @param host: string, the hostname or IP address of the bmc
+        @param args: contains additional arguments used by the ldap subcommand
+                args.json: boolean, if this flag is set to true, the output
+                will be provided in json format for programmatic consumption
+        @param session: the active session to use
+    """
+
+    url = "https://"+host+"/xyz/openbmc_project/network/config/attr/DefaultGateway"
+    httpHeader = {'Content-Type': 'application/json'}
+
+    data = {"data": args.DefaultGW}
+
+    try:
+        res = session.put(url, headers=httpHeader, json=data, verify=False,
+                          timeout=30)
+    except(requests.exceptions.Timeout):
+        return(connectionErrHandler(args.json, "Timeout", None))
+    except(requests.exceptions.ConnectionError) as err:
+        return connectionErrHandler(args.json, "ConnectionError", err)
+    if res.status_code == 403:
+        return "Failed to set Default Gateway!!"
+
+    return res.text
+
+
+def viewNWConfig(host, args, session):
+    """
+         Called by the ldap function. Prints out network configured properties
+
+         @param host: string, the hostname or IP address of the bmc
+         @param args: contains additional arguments used by the ldap subcommand
+                args.json: boolean, if this flag is set to true, the output
+                will be provided in json format for programmatic consumption
+         @param session: the active session to use
+         @return returns LDAP's configured properties.
+    """
+    url = "https://"+host+"/xyz/openbmc_project/network/enumerate"
+    httpHeader = {'Content-Type': 'application/json'}
+    try:
+        res = session.get(url, headers=httpHeader, verify=False, timeout=40)
+    except(requests.exceptions.Timeout):
+        return(connectionErrHandler(args.json, "Timeout", None))
+    except(requests.exceptions.ConnectionError) as err:
+        return connectionErrHandler(args.json, "ConnectionError", err)
+    except(requests.exceptions.RequestException) as err:
+        return connectionErrHandler(args.json, "RequestException", err)
+    if res.status_code == 404:
+        return "LDAP server config has not been created"
+    return res.text
+
+
+def getDNS(host, args, session):
+
+    """
+        Called by the network function. Prints out DNS servers on the interface
+
+        @param host: string, the hostname or IP address of the bmc
+        @param args: contains additional arguments used by the ldap subcommand
+                args.json: boolean, if this flag is set to true, the output
+                will be provided in json format for programmatic consumption
+        @param session: the active session to use
+    """
+
+    url = "https://" + host + "/xyz/openbmc_project/network/" + args.Interface\
+        + "/attr/Nameservers"
+    httpHeader = {'Content-Type': 'application/json'}
+
+    try:
+        res = session.get(url, headers=httpHeader, verify=False, timeout=40)
+    except(requests.exceptions.Timeout):
+        return(connectionErrHandler(args.json, "Timeout", None))
+    except(requests.exceptions.ConnectionError) as err:
+        return connectionErrHandler(args.json, "ConnectionError", err)
+    if res.status_code == 404:
+        return "The specified Interface"+"("+args.Interface+")" + \
+            " doesn't exist"
+
+    return res.text
+
+
+def setDNS(host, args, session):
+    """
+        Called by the network function. Sets DNS servers on the interface.
+
+        @param host: string, the hostname or IP address of the bmc
+        @param args: contains additional arguments used by the ldap subcommand
+                args.json: boolean, if this flag is set to true, the output
+                will be provided in json format for programmatic consumption
+        @param session: the active session to use
+    """
+
+    url = "https://" + host + "/xyz/openbmc_project/network/" + args.Interface\
+        + "/attr/Nameservers"
+    httpHeader = {'Content-Type': 'application/json'}
+
+    data = {"data": args.DNSServers.split(",")}
+
+    try:
+        res = session.put(url, headers=httpHeader, json=data, verify=False,
+                          timeout=30)
+    except(requests.exceptions.Timeout):
+        return(connectionErrHandler(args.json, "Timeout", None))
+    except(requests.exceptions.ConnectionError) as err:
+        return connectionErrHandler(args.json, "ConnectionError", err)
+    if res.status_code == 403:
+        return "The specified Interface"+"("+args.Interface+")" +\
+            " doesn't exist"
+
+    return res.text
+
+
+def getNTP(host, args, session):
+
+    """
+        Called by the network function. Prints out NTP servers on the interface
+
+        @param host: string, the hostname or IP address of the bmc
+        @param args: contains additional arguments used by the ldap subcommand
+                args.json: boolean, if this flag is set to true, the output
+                will be provided in json format for programmatic consumption
+        @param session: the active session to use
+    """
+
+    url = "https://" + host + "/xyz/openbmc_project/network/" + args.Interface\
+        + "/attr/NTPServers"
+    httpHeader = {'Content-Type': 'application/json'}
+
+    try:
+        res = session.get(url, headers=httpHeader, verify=False, timeout=40)
+    except(requests.exceptions.Timeout):
+        return(connectionErrHandler(args.json, "Timeout", None))
+    except(requests.exceptions.ConnectionError) as err:
+        return connectionErrHandler(args.json, "ConnectionError", err)
+    if res.status_code == 404:
+        return "The specified Interface"+"("+args.Interface+")" + \
+            " doesn't exist"
+
+    return res.text
+
+
+def setNTP(host, args, session):
+    """
+        Called by the network function. Sets NTP servers on the interface.
+
+        @param host: string, the hostname or IP address of the bmc
+        @param args: contains additional arguments used by the ldap subcommand
+                args.json: boolean, if this flag is set to true, the output
+                will be provided in json format for programmatic consumption
+        @param session: the active session to use
+    """
+
+    url = "https://" + host + "/xyz/openbmc_project/network/" + args.Interface\
+        + "/attr/NTPServers"
+    httpHeader = {'Content-Type': 'application/json'}
+
+    data = {"data": args.NTPServers.split(",")}
+
+    try:
+        res = session.put(url, headers=httpHeader, json=data, verify=False,
+                          timeout=30)
+    except(requests.exceptions.Timeout):
+        return(connectionErrHandler(args.json, "Timeout", None))
+    except(requests.exceptions.ConnectionError) as err:
+        return connectionErrHandler(args.json, "ConnectionError", err)
+    if res.status_code == 403:
+        return "The specified Interface"+"("+args.Interface+")" +\
+            " doesn't exist"
+
+    return res.text
+
+
 def createPrivilegeMapping(host, args, session):
     """
          Called by the ldap function. Creates the group and the privilege mapping.
@@ -2737,6 +3171,157 @@
         help="Password of local user")
     parser_set_password.set_defaults(func=setPassword)
 
+    # network
+    parser_nw = subparsers.add_parser("network", help="network controls")
+    nw_sub = parser_nw.add_subparsers(title='subcommands',
+                                      description='valid subcommands',
+                                      help="sub-command help",
+                                      dest='command')
+
+    # enable DHCP
+    parser_enable_dhcp = nw_sub.add_parser("enableDHCP",
+                                           help="enables the DHCP on given "
+                                           "Interface")
+    parser_enable_dhcp.add_argument("-I", "--Interface", required=True,
+                                    choices=['eth0', 'eth1'],
+                                    help="Name of the ethernet interface"
+                                    "(it can be obtained by the command:"
+                                    "network view-config)")
+    parser_enable_dhcp.set_defaults(func=enableDHCP)
+
+    # disable DHCP
+    parser_disable_dhcp = nw_sub.add_parser("disableDHCP",
+                                            help="disables the DHCP on given "
+                                            "Interface")
+    parser_disable_dhcp.add_argument("-I", "--Interface", required=True,
+                                     choices=['eth0', 'eth1'],
+                                     help="Name of the ethernet interface"
+                                     "(it can be obtained by the command:"
+                                     "network view-config)")
+    parser_disable_dhcp.set_defaults(func=disableDHCP)
+
+    # get HostName
+    parser_gethostname = nw_sub.add_parser("getHostName",
+                                           help="prints out HostName")
+    parser_gethostname.set_defaults(func=getHostname)
+
+    # set HostName
+    parser_sethostname = nw_sub.add_parser("setHostName", help="sets HostName")
+    parser_sethostname.add_argument("-H", "--HostName", required=True,
+                                    help="A HostName for the BMC")
+    parser_sethostname.set_defaults(func=setHostname)
+
+    # get domainname
+    parser_getdomainname = nw_sub.add_parser("getDomainName",
+                                             help="prints out DomainName of "
+                                             "given Interface")
+    parser_getdomainname.add_argument("-I", "--Interface", required=True,
+                                      choices=['eth0', 'eth1'],
+                                      help="Name of the ethernet interface"
+                                      "(it can be obtained by the command:"
+                                      "network view-config)")
+    parser_getdomainname.set_defaults(func=getDomainName)
+
+    # set domainname
+    parser_setdomainname = nw_sub.add_parser("setDomainName",
+                                             help="sets DomainName of given "
+                                             "Interface")
+    parser_setdomainname.add_argument("-D", "--DomainName", required=True,
+                                      help="Ex: DomainName=Domain1,Domain2,...")
+    parser_setdomainname.add_argument("-I", "--Interface", required=True,
+                                      choices=['eth0', 'eth1'],
+                                      help="Name of the ethernet interface"
+                                      "(it can be obtained by the command:"
+                                      "network view-config)")
+    parser_setdomainname.set_defaults(func=setDomainName)
+
+    # get MACAddress
+    parser_getmacaddress = nw_sub.add_parser("getMACAddress",
+                                             help="prints out MACAddress the "
+                                             "given Interface")
+    parser_getmacaddress.add_argument("-I", "--Interface", required=True,
+                                      choices=['eth0', 'eth1'],
+                                      help="Name of the ethernet interface"
+                                      "(it can be obtained by the command:"
+                                      "network view-config)")
+    parser_getmacaddress.set_defaults(func=getMACAddress)
+
+    # set MACAddress
+    parser_setmacaddress = nw_sub.add_parser("setMACAddress",
+                                             help="sets MACAddress")
+    parser_setmacaddress.add_argument("-MA", "--MACAddress", required=True,
+                                      help="A MACAddress for the given "
+                                      "Interface")
+    parser_setmacaddress.add_argument("-I", "--Interface", required=True,
+                                      choices=['eth0', 'eth1'],
+                                      help="Name of the ethernet interface"
+                                      "(it can be obtained by the command:"
+                                      "network view-config)")
+    parser_setmacaddress.set_defaults(func=setMACAddress)
+
+    # get DefaultGW
+    parser_getdefaultgw = nw_sub.add_parser("getDefaultGW",
+                                            help="prints out DefaultGateway "
+                                            "the BMC")
+    parser_getdefaultgw.set_defaults(func=getDefaultGateway)
+
+    # set DefaultGW
+    parser_setdefaultgw = nw_sub.add_parser("setDefaultGW",
+                                             help="sets DefaultGW")
+    parser_setdefaultgw.add_argument("-GW", "--DefaultGW", required=True,
+                                      help="A DefaultGateway for the BMC")
+    parser_setdefaultgw.set_defaults(func=setDefaultGateway)
+
+    # view network Config
+    parser_ldap_config = nw_sub.add_parser("view-config", help="prints out a "
+                                           "list of all network's configured "
+                                           "properties")
+    parser_ldap_config.set_defaults(func=viewNWConfig)
+
+    # get DNS
+    parser_getDNS = nw_sub.add_parser("getDNS",
+                                      help="prints out DNS servers on the "
+                                      "given interface")
+    parser_getDNS.add_argument("-I", "--Interface", required=True,
+                               choices=['eth0', 'eth1'],
+                               help="Name of the ethernet interface(it can be "
+                               "obtained by the command:network view-config)")
+    parser_getDNS.set_defaults(func=getDNS)
+
+    # set DNS
+    parser_setDNS = nw_sub.add_parser("setDNS",
+                                      help="sets DNS servers on the given "
+                                      "interface")
+    parser_setDNS.add_argument("-d", "--DNSServers", required=True,
+                               help="Ex: DNSSERVERS=DNS1,DNS2,...")
+    parser_setDNS.add_argument("-I", "--Interface", required=True,
+                               choices=['eth0', 'eth1'],
+                               help="Name of the ethernet interface(it can be "
+                               "obtained by the command:network view-config)")
+    parser_setDNS.set_defaults(func=setDNS)
+
+    # get NTP
+    parser_getNTP = nw_sub.add_parser("getNTP",
+                                      help="prints out NTP servers on the "
+                                      "given interface")
+    parser_getNTP.add_argument("-I", "--Interface", required=True,
+                               choices=['eth0', 'eth1'],
+                               help="Name of the ethernet interface(it can be "
+                               "obtained by the command:network view-config)")
+    parser_getNTP.set_defaults(func=getNTP)
+
+    # set NTP
+    parser_setNTP = nw_sub.add_parser("setNTP",
+                                      help="sets NTP servers on the given "
+                                      "interface")
+    parser_setNTP.add_argument("-N", "--NTPServers", required=True,
+                               help="Ex: NTPSERVERS=NTP1,NTP2,...")
+    parser_setNTP.add_argument("-I", "--Interface", required=True,
+                               choices=['eth0', 'eth1'],
+                               help="Name of the ethernet interface(it can be "
+                               "obtained by the command:network view-config)")
+    parser_setNTP.set_defaults(func=setNTP)
+
     return parser
 
 def main(argv=None):