openbmctool: add network related commands #3

added below givem commands:
-enableVLAN
-disableVLAN
-viewDHCPConfig
-configureDHCP
-factoryReset

Change-Id: Ib83eb50538ddca6d7c544e4aac3ba57acd3fe089
Signed-off-by: Nagaraju Goruganti <ngorugan@in.ibm.com>
diff --git a/thalerj/openbmctool.py b/thalerj/openbmctool.py
index 88c7d3a..babf6c6 100755
--- a/thalerj/openbmctool.py
+++ b/thalerj/openbmctool.py
@@ -2816,6 +2816,162 @@
     return "No object found for given address on given Interface"
 
 
+def addVLAN(host, args, session):
+    """
+        Called by the network function. Creates VLAN on given 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/action/VLAN"
+    httpHeader = {'Content-Type': 'application/json'}
+
+    data = {"data": [args.Interface,args.Identifier]}
+
+    try:
+        res = session.post(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 == 400:
+        return "The specified Interface" + "(" + args.Interface + ")" +\
+            " doesn't exist"
+
+    return res.text
+
+
+def deleteVLAN(host, args, session):
+    """
+        Called by the network function. Creates VLAN on given 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+"/action/delete"
+    httpHeader = {'Content-Type':'application/json'}
+    data = {"data": []}
+
+    try:
+        res = session.post(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 == 404:
+        return "The specified VLAN"+"("+args.Interface+"_"+args.Identifier\
+            +")" +" doesn't exist"
+
+    return res.text
+
+
+def viewDHCPConfig(host, args, session):
+    """
+        Called by the network function. Shows DHCP 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
+    """
+
+    url="https://"+host+"/xyz/openbmc_project/network/config/dhcp"
+    httpHeader = {'Content-Type':'application/json'}
+
+    try:
+        res = session.get(url, headers=httpHeader, 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 configureDHCP(host, args, session):
+    """
+        Called by the network function. Configures/updates DHCP 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
+    """
+
+    httpHeader = {'Content-Type': 'application/json'}
+
+    try:
+        url="https://"+host+"/xyz/openbmc_project/network/config/dhcp"
+        if(args.DNSEnabled == True):
+            data = '{"data": 1}'
+        else:
+            data = '{"data": 0}'
+        res = session.put(url + '/attr/DNSEnabled', headers=httpHeader,
+                          data=data, verify=False, timeout=30)
+        if(args.HostNameEnabled == True):
+            data = '{"data": 1}'
+        else:
+            data = '{"data": 0}'
+        res = session.put(url + '/attr/HostNameEnabled', headers=httpHeader,
+                          data=data, verify=False, timeout=30)
+        if(args.NTPEnabled == True):
+            data = '{"data": 1}'
+        else:
+            data = '{"data": 0}'
+        res = session.put(url + '/attr/NTPEnabled', headers=httpHeader,
+                          data=data, verify=False, timeout=30)
+        if(args.SendHostNameEnabled == True):
+            data = '{"data": 1}'
+        else:
+            data = '{"data": 0}'
+        res = session.put(url + '/attr/SendHostNameEnabled', 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)
+
+    return res.text
+
+
+def nwReset(host, args, session):
+
+    """
+        Called by the network function. Resets networks setting to factory defaults.
+
+        @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/action/Reset"
+    httpHeader = {'Content-Type': 'application/json'}
+    data = '{"data":[] }'
+    try:
+        res = session.post(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)
+
+    return res.text
+
+
 def createPrivilegeMapping(host, args, session):
     """
          Called by the ldap function. Creates the group and the privilege mapping.
@@ -2926,6 +3082,13 @@
         return "LDAP server config has not been created"
     return res.text
 
+def str2bool(v):
+    if v.lower() in ('yes', 'true', 't', 'y', '1'):
+        return True
+    elif v.lower() in ('no', 'false', 'f', 'n', '0'):
+        return False
+    else:
+        raise argparse.ArgumentTypeError('Boolean value expected.')
 
 def localUsers(host, args, session):
     """
@@ -3472,6 +3635,59 @@
                              "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)")
     parser_rmIP.set_defaults(func=deleteIP)
 
+    # add VLAN
+    parser_create_vlan = nw_sub.add_parser("addVLAN", help="enables VLAN "
+                                           "on given interface with given "
+                                           "VLAN Identifier")
+    parser_create_vlan.add_argument("-I", "--Interface", required=True,
+                                    choices=['eth0', 'eth1'],
+                                    help="Name of the ethernet interface")
+    parser_create_vlan.add_argument("-n", "--Identifier", required=True,
+                                  help="VLAN Identifier")
+    parser_create_vlan.set_defaults(func=addVLAN)
+
+    # delete VLAN
+    parser_delete_vlan = nw_sub.add_parser("deleteVLAN", help="disables VLAN "
+                                           "on given interface with given "
+                                           "VLAN Identifier")
+    parser_delete_vlan.add_argument("-I", "--Interface", required=True,
+                                    help="Name of the ethernet interface(it can"
+                                    "be obtained by the "
+                                    "command:network view-config)"
+                                    "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)")
+    parser_delete_vlan.set_defaults(func=deleteVLAN)
+
+    # viewDHCPConfig
+    parser_viewDHCPConfig = nw_sub.add_parser("viewDHCPConfig",
+                                              help="Shows DHCP configured "
+                                              "Properties")
+    parser_viewDHCPConfig.set_defaults(func=viewDHCPConfig)
+
+    # configureDHCP
+    parser_configDHCP = nw_sub.add_parser("configureDHCP",
+                                          help="Configures/updates DHCP "
+                                          "Properties")
+    parser_configDHCP.add_argument("-d", "--DNSEnabled", type=str2bool,
+                                   required=True, help="Sets DNSEnabled property")
+    parser_configDHCP.add_argument("-n", "--HostNameEnabled", type=str2bool,
+                                   required=True,
+                                   help="Sets HostNameEnabled property")
+    parser_configDHCP.add_argument("-t", "--NTPEnabled", type=str2bool,
+                                   required=True,
+                                   help="Sets NTPEnabled property")
+    parser_configDHCP.add_argument("-s", "--SendHostNameEnabled", type=str2bool,
+                                   required=True,
+                                   help="Sets SendHostNameEnabled property")
+    parser_configDHCP.set_defaults(func=configureDHCP)
+
+    # network factory reset
+    parser_nw_reset = nw_sub.add_parser("nwReset",
+                                        help="Resets networks setting to "
+                                        "factory defaults. "
+                                        "note:Reset settings will be applied "
+                                        "after BMC reboot")
+    parser_nw_reset.set_defaults(func=nwReset)
+
     return parser
 
 def main(argv=None):