From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id 44639A0032;
	Mon, 11 Jul 2022 16:52:13 +0200 (CEST)
Received: from [217.70.189.124] (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id CBFCF42B7D;
	Mon, 11 Jul 2022 16:51:39 +0200 (CEST)
Received: from lb.pantheon.sk (lb.pantheon.sk [46.229.239.20])
 by mails.dpdk.org (Postfix) with ESMTP id DA9E541611
 for <dev@dpdk.org>; Mon, 11 Jul 2022 16:51:36 +0200 (CEST)
Received: from localhost (localhost [127.0.0.1])
 by lb.pantheon.sk (Postfix) with ESMTP id 1D79D243CC6;
 Mon, 11 Jul 2022 16:51:36 +0200 (CEST)
X-Virus-Scanned: amavisd-new at siecit.sk
Received: from lb.pantheon.sk ([127.0.0.1])
 by localhost (lb.pantheon.sk [127.0.0.1]) (amavisd-new, port 10024)
 with ESMTP id NfdyPyYL8XmX; Mon, 11 Jul 2022 16:51:35 +0200 (CEST)
Received: from entguard.lab.pantheon.local (unknown [46.229.239.141])
 by lb.pantheon.sk (Postfix) with ESMTP id 51EDE243CCB;
 Mon, 11 Jul 2022 16:51:31 +0200 (CEST)
From: =?UTF-8?q?Juraj=20Linke=C5=A1?= <juraj.linkes@pantheon.tech>
To: thomas@monjalon.net, david.marchand@redhat.com, jerinjacobk@gmail.com,
 ronan.randles@intel.com, Honnappa.Nagarahalli@arm.com,
 ohilyard@iol.unh.edu, lijuan.tu@intel.com
Cc: dev@dpdk.org, =?UTF-8?q?Juraj=20Linke=C5=A1?= <juraj.linkes@pantheon.tech>
Subject: [PATCH v2 6/8] dts: add Node base class
Date: Mon, 11 Jul 2022 14:51:24 +0000
Message-Id: <20220711145126.295427-7-juraj.linkes@pantheon.tech>
X-Mailer: git-send-email 2.25.1
In-Reply-To: <20220711145126.295427-1-juraj.linkes@pantheon.tech>
References: <20220622121448.3304251-1-juraj.linkes@pantheon.tech>
 <20220711145126.295427-1-juraj.linkes@pantheon.tech>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org

The base class implements basic node management methods - connect and
execute commands.

Signed-off-by: Juraj Linkeš <juraj.linkes@pantheon.tech>
---
 dts/framework/node.py     | 108 ++++++++++++++++++++++++++++++++++++++
 dts/framework/settings.py |   3 ++
 2 files changed, 111 insertions(+)
 create mode 100644 dts/framework/node.py

diff --git a/dts/framework/node.py b/dts/framework/node.py
new file mode 100644
index 0000000000..09f567f32f
--- /dev/null
+++ b/dts/framework/node.py
@@ -0,0 +1,108 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2010-2014 Intel Corporation
+# Copyright(c) 2022 PANTHEON.tech s.r.o.
+# Copyright(c) 2022 University of New Hampshire
+#
+
+from typing import Optional
+
+from .config import NodeConfiguration
+from .logger import DTSLOG, getLogger
+from .settings import TIMEOUT
+from .ssh_connection import SSHConnection
+
+"""
+A node is a generic host that DTS connects to and manages.
+"""
+
+
+class Node(object):
+    """
+    Basic module for node management. This module implements methods that
+    manage a node, such as information gathering (of CPU/PCI/NIC) and
+    environment setup.
+    """
+
+    _config: NodeConfiguration
+    name: str
+    sut_id: int
+    logger: DTSLOG
+    session: SSHConnection
+
+    def __init__(self, node_config: NodeConfiguration, sut_id: int = 0):
+        self._config = node_config
+        self.name = node_config.name
+        self.sut_id = sut_id
+
+        self.logger = getLogger(self.name)
+        self.logger.info(f"Created node: {self.name}")
+        self.session = SSHConnection(
+            self.get_ip_address(),
+            self.name,
+            self.logger,
+            self.get_username(),
+            self.get_password(),
+            self.sut_id,
+        )
+
+    def get_ip_address(self) -> str:
+        """
+        Get SUT's ip address.
+        """
+        return self._config.hostname
+
+    def get_password(self) -> Optional[str]:
+        """
+        Get SUT's login password.
+        """
+        return self._config.password
+
+    def get_username(self) -> str:
+        """
+        Get SUT's login username.
+        """
+        return self._config.user
+
+    def send_expect(
+        self,
+        command: str,
+        expected: str,
+        timeout: int = TIMEOUT,
+        verify: bool = False,
+        trim_whitespace: bool = True,
+    ) -> str | int:
+        """
+        Send commands to node and return string before expected string. If
+        there's no expected string found before timeout, TimeoutException will
+        be raised.
+
+        By default, it will trim the whitespace from the expected string. This
+        behavior can be turned off via the trim_whitespace argument.
+        """
+
+        if trim_whitespace:
+            expected = expected.strip()
+
+        return self.session.send_expect(command, expected, timeout, verify)
+
+    def send_command(self, cmds: str, timeout: float = TIMEOUT) -> str:
+        """
+        Send commands to node and return string before timeout.
+        """
+
+        return self.session.send_command(cmds, timeout)
+
+    def close(self) -> None:
+        """
+        Close ssh session of SUT.
+        """
+        if self.session:
+            self.session.close()
+            self.session = None
+
+    def node_exit(self) -> None:
+        """
+        Recover all resource before node exit
+        """
+        self.close()
+        self.logger.logger_exit()
diff --git a/dts/framework/settings.py b/dts/framework/settings.py
index ca3cbd71b1..f5028e88c9 100644
--- a/dts/framework/settings.py
+++ b/dts/framework/settings.py
@@ -7,6 +7,9 @@
 import os
 import re
 
+# Default session timeout.
+TIMEOUT: int = 15
+
 DEFAULT_CONFIG_FILE_PATH: str = "./conf.yaml"
 
 # DTS global environment variables
-- 
2.30.2