openbmctool: add network related commands #2

added below givem commands:
-setIP
-getIP
-rmIP

Tested: used below given commands in testing
1.python openbmctool.py -H <BMC_IP> -U root -P <root password> network getIP -I <Interface name>
2.python openbmctool.py -H <BMC_IP> -U root -P <root password> network setIP -a <ADDRESS> \
	-gw <GATEWAY> -l <PREFIXLENGTH> -p <protocol type> -I <Interface name>
3.python openbmctool.py -H <BMC_IP> -U root -P <root password> network rmIP -I  <Interface name> -a <ADDRESS>

Change-Id: Icea1ac73c966eca0815f4b35054ad30a1c9ef5a5
Signed-off-by: Nagaraju Goruganti <ngorugan@in.ibm.com>
diff --git a/thalerj/openbmctool.py b/thalerj/openbmctool.py
index 9a09ddd..88c7d3a 100755
--- a/thalerj/openbmctool.py
+++ b/thalerj/openbmctool.py
@@ -2709,6 +2709,113 @@
     return res.text
 
 
+def addIP(host, args, session):
+    """
+        Called by the network function. Configures IP address 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/IP"
+    httpHeader = {'Content-Type': 'application/json'}
+    protocol = {
+             'ipv4': 'xyz.openbmc_project.Network.IP.Protocol.IPv4',
+             'ipv6': 'xyz.openbmc_project.Network.IP.Protocol.IPv6'
+            }
+
+    data = {"data": [protocol[args.type], args.address, int(args.prefixLength),
+        args.gateway]}
+
+    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 Interface" + "(" + args.Interface + ")" +\
+            " doesn't exist"
+
+    return res.text
+
+
+def getIP(host, args, session):
+    """
+        Called by the network function. Prints out IP address of 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 +\
+        "/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)
+    if res.status_code == 404:
+        return "The specified Interface" + "(" + args.Interface + ")" +\
+            " doesn't exist"
+
+    return res.text
+
+
+def deleteIP(host, args, session):
+    """
+        Called by the network function. Deletes the IP address from given Interface
+
+        @param host: string, the hostname or IP address of the bmc
+        @param args: contains additional arguments used by the ldap subcommand
+        @param session: the active session to use
+        @param args.json: boolean, if this flag is set to true, the output
+            will be provided in json format for programmatic consumption
+    """
+
+    url = "https://"+host+"/xyz/openbmc_project/network/" + args.Interface+\
+        "/enumerate"
+    httpHeader = {'Content-Type':'application/json'}
+    data = {"data": []}
+    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"
+    objDict = json.loads(res.text)
+    if not objDict['data']:
+        return "No object found for given address on given Interface"
+
+    for obj in objDict['data']:
+        if args.address in objDict['data'][obj]['Address']:
+            url = "https://"+host+obj+"/action/delete"
+            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)
+            return res.text
+        else:
+            continue
+    return "No object found for given address on given Interface"
+
+
 def createPrivilegeMapping(host, args, session):
     """
          Called by the ldap function. Creates the group and the privilege mapping.
@@ -3183,10 +3290,10 @@
                                            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)")
+                                    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_enable_dhcp.set_defaults(func=enableDHCP)
 
     # disable DHCP
@@ -3194,10 +3301,10 @@
                                             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)")
+                                     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_disable_dhcp.set_defaults(func=disableDHCP)
 
     # get HostName
@@ -3216,10 +3323,10 @@
                                              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)")
+                                      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_getdomainname.set_defaults(func=getDomainName)
 
     # set domainname
@@ -3229,10 +3336,10 @@
     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)")
+                                      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_setdomainname.set_defaults(func=setDomainName)
 
     # get MACAddress
@@ -3240,10 +3347,10 @@
                                              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)")
+                                      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_getmacaddress.set_defaults(func=getMACAddress)
 
     # set MACAddress
@@ -3253,10 +3360,10 @@
                                       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)")
+                                    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_setmacaddress.set_defaults(func=setMACAddress)
 
     # get DefaultGW
@@ -3283,9 +3390,10 @@
                                       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)")
+                               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_getDNS.set_defaults(func=getDNS)
 
     # set DNS
@@ -3295,9 +3403,10 @@
     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)")
+                               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_setDNS.set_defaults(func=setDNS)
 
     # get NTP
@@ -3305,9 +3414,10 @@
                                       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)")
+                               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_getNTP.set_defaults(func=getNTP)
 
     # set NTP
@@ -3317,11 +3427,51 @@
     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)")
+                               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_setNTP.set_defaults(func=setNTP)
 
+    # configure IP
+    parser_ip_config = nw_sub.add_parser("addIP", help="Sets IP address to"
+                                         "given interface")
+    parser_ip_config.add_argument("-a", "--address", required=True,
+                                  help="IP address of given interface")
+    parser_ip_config.add_argument("-gw", "--gateway", required=False, default='',
+                                  help="The gateway for given interface")
+    parser_ip_config.add_argument("-l", "--prefixLength", required=True,
+                                  help="The prefixLength of IP address")
+    parser_ip_config.add_argument("-p", "--type", choices=['ipv4', 'ipv6'],
+                                  help="The protocol type of the given"
+                                  "IP address")
+    parser_ip_config.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_ip_config.set_defaults(func=addIP)
+
+    # getIP
+    parser_getIP = nw_sub.add_parser("getIP", help="prints out IP address"
+                                     "of given interface")
+    parser_getIP.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_getIP.set_defaults(func=getIP)
+
+    # rmIP
+    parser_rmIP = nw_sub.add_parser("rmIP", help="deletes IP address"
+                                     "of given interface")
+    parser_rmIP.add_argument("-a", "--address", required=True,
+                                  help="IP address to remove form given Interface")
+    parser_rmIP.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_rmIP.set_defaults(func=deleteIP)
+
     return parser
 
 def main(argv=None):