Updates according to comments for Operational and Functional tests for CORD certification

Change-Id: I3e972fe7f6f85f1b95dd729d6d41f6319a52c8b4
diff --git a/Fabric/Utilities/accton/accton_util.py b/Fabric/Utilities/accton/accton_util.py
index 0f32e76..b0752e6 100755
--- a/Fabric/Utilities/accton/accton_util.py
+++ b/Fabric/Utilities/accton/accton_util.py
@@ -21,6 +21,10 @@
 import time
 from oftest.testutils import *
 from oftest.parse import parse_ipv6
+import pexpect
+import os
+import json
+import sys
 
 from ncclient import manager
 import ncclient
@@ -84,6 +88,7 @@
 
     return switch_cpu_mac_str, switch_cpu_mac
 
+
 def DumpGroup(stats, verify_group_stats, always_show=True):
     if(len(stats) > len(verify_group_stats)):
         min_len = len(verify_group_stats)
@@ -1874,6 +1879,178 @@
         return m.get_config(source='running').data_xml
 
 
+def ofagent_restart(self):
+    log_path = (config["log_dir"])
+    log_file = "OF_agent_service_restart.log"
+    regexp = 'Setting up OFDPA running environment'
+    output = open(log_path + '/' + log_file, 'w')
+    cmd = "service ofagentd restart"
+    child = pexpect.spawn('ssh -p 22 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ' + self.switch_user +'@%s' % self.switch_ip)
+    child.expect('[pP]assword:')
+    child.sendline(self.switch_passwd)
+    child.logfile = output
+    child.expect('root@localhost:~#')
+    child.sendline(cmd)
+    if regexp:
+        child.expect(regexp)
+
+    output.close()
+    child.close()
+
+
+def switch_restart(self):
+    log_path = (config["log_dir"])
+    log_file = "Switch_restart.log"
+    regexp = 'The system is going down for reboot NOW!'
+    output = open(log_path + '/' + log_file, 'w')
+    cmd = "reboot"
+    child = pexpect.spawn('ssh -p 22 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ' + self.switch_user +'@%s' % self.switch_ip)
+    child.expect('[pP]assword:')
+    child.sendline(self.switch_passwd)
+    child.logfile = output
+    child.expect('root@localhost:~#')
+    child.sendline(cmd)
+    if regexp:
+        child.expect(regexp)
+
+    output.close()
+    child.close()
+
+
+def switch_port_enable(self, port, state):
+    # @parameters:
+    # port - port number on the switch to admin state Enable or Disable
+    # state - admin state "False" - Down or "True" - Up for switch's port
+    log_path = (config["log_dir"])
+    log_file = "Switch_port_" + str(state) + ".log"
+    regexp = 'Returned from ofdpaBcmCommand rpc with rc = 0.'
+    output = open(log_path + '/' + log_file, 'w')
+    if state == False:
+        cmd = "client_drivshell port xe{} enable=false".format(port - 1)
+    elif state == True:
+        cmd = "client_drivshell port xe{} enable=true".format(port - 1)
+    else:
+        assert False, "Incorrect port state parameter is given!"
+
+    child = pexpect.spawn('ssh -p 22 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ' + self.switch_user +'@%s' % self.switch_ip)
+    child.expect('[pP]assword:')
+    child.sendline(self.switch_passwd)
+    child.logfile = output
+    child.expect('root@localhost:~#')
+    child.sendline(cmd)
+    if regexp:
+        child.expect(regexp)
+
+    output.close()
+    child.close()
+
+
+def switch_port_set_mtu(self, port, mtu):
+    # @parameters:
+    # port - port number on the switch to admin state Enable or Disable
+    # mtu - size of MTU for switch's port in bytes
+    log_path = (config["log_dir"])
+    log_file = "Switch_port_set_mtu_" + str(mtu) + ".log"
+    regexp = 'Returned from ofdpaBcmCommand rpc with rc = 0.'
+    output = open(log_path + '/' + log_file, 'w')
+    cmd = ("client_drivshell port xe{} FrameMax=" + str(mtu)).format(port - 1)
+    child = pexpect.spawn('ssh -p 22 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ' + self.switch_user +'@%s' % self.switch_ip)
+    child.expect('[pP]assword:')
+    child.sendline(self.switch_passwd)
+    child.logfile = output
+    child.expect('root@localhost:~#')
+    child.sendline(cmd)
+    if regexp:
+        child.expect(regexp)
+
+    output.close()
+    child.close()
+
+
+def send_command_to_switch_cli(log_file, cmd, switch_ip, switch_user, switch_passwd, regexp=None):
+    log_path = (config["log_dir"])
+    output = open(log_path + '/' + log_file, 'w')
+    child = pexpect.spawn('ssh -p 22 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ' + switch_user +'@%s' % switch_ip)
+    child.expect('[pP]assword:')
+    child.sendline(switch_passwd)
+    child.logfile = output
+    child.expect('root@localhost:~#')
+    child.sendline(cmd)
+    if regexp:
+        child.expect(regexp)
+
+    output.close()
+    child.close()
+
+
+def send_command_to_onos_cli(log_file, cmd, onos_server, onos_user, onos_passwd, regexp=None):
+    log_path = (config["log_dir"])
+    output = open(log_path + '/' + log_file, 'w')
+    child = pexpect.spawn('ssh -p 30115 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ' + onos_user + '@%s' % onos_server)
+    child.expect('[pP]assword:')
+    child.sendline(onos_passwd)
+    child.logfile = output
+    child.expect('onos>')
+    child.sendline(cmd)
+    if regexp:
+        child.expect(regexp)
+
+    output.close()
+    child.close()
+
+
+def add_onos_xconnect(self,datapathid, vid, port1, port2):
+    log_path = (config["log_dir"])
+    cmd = "sr-xconnect-add of:" + datapathid + " " + str(vid) + " " + str(port1) + " " + str(port2)
+    regexp = 'onos>'
+    output = open(log_path + '/' + "add_xconnection" + str(vid) + "_" + str(port1) + "_" + str(port2), 'w')
+    child = pexpect.spawn(
+        'ssh -p 30115 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ' + self.onos_user + '@%s' % self.onos_server_ip)
+    child.expect('[pP]assword:')
+    child.sendline(self.onos_passwd)
+    child.logfile = output
+    child.expect('onos>')
+    child.sendline(cmd)
+    if regexp:
+        child.expect(regexp)
+
+    output.close()
+    child.close()
+
+def remove_onos_xconnect(self,datapathid, vid):
+    log_path = (config["log_dir"])
+    cmd = "sr-xconnect-remove of:" + datapathid + " " + str(vid)
+    regexp = 'onos>'
+    output = open(log_path + '/' + "remove_xconnection" + str(vid), 'w')
+    child = pexpect.spawn(
+        'ssh -p 30115 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ' + self.onos_user + '@%s' % self.onos_server_ip)
+    child.expect('[pP]assword:')
+    child.sendline(self.onos_passwd)
+    child.logfile = output
+    child.expect('onos>')
+    child.sendline(cmd)
+    if regexp:
+        child.expect(regexp)
+
+    output.close()
+    child.close()
+
+
+def ofagent_reconfig(self, arg):
+    # @parameters:
+    # arg - configure switch with both OFTest and ONOS (arg = yes) or only OFTest is required (arg = no)
+    log_path = (config["log_dir"])
+    if arg == "yes":
+        logging.info("Reconfigure OFAGENT to use both ONOS and OFTEST")
+        comd = "sed -i '/^OPT_ARGS=/c\OPT_ARGS=\"-d 2 -c 2 -c 4 -t " + self.controller_ip + ":" + self.controller_port + " -t " + self.onos_server_ip + ":" + self.onos_port + " -i $DPID\"' /etc/ofagent/ofagent.conf"
+        send_command_to_switch_cli("test_ofagent_reconfigure_yes", comd, self.switch_ip, self.switch_user, self.switch_passwd, regexp='root@localhost:~#')
+        time.sleep(1)
+    elif arg == "no":
+        logging.info("Reconfigure OFAGENT to use OFTEST only")
+        comd = "sed -i '/^OPT_ARGS=/c\OPT_ARGS=\"-d 2 -c 2 -c 4 -t " + self.controller_ip + ":" + self.controller_port + " -i $DPID\"' /etc/ofagent/ofagent.conf"
+        send_command_to_switch_cli("test_ofagent_reconfigure_no", comd, self.switch_ip, self.switch_user, self.switch_passwd, regexp='root@localhost:~#')
+        time.sleep(1)
+
 """
 MPLS
 """