1#!/usr/bin/env python3
2#
3#  Copyright (c) 2019, The OpenThread Authors.
4#  All rights reserved.
5#
6#  Redistribution and use in source and binary forms, with or without
7#  modification, are permitted provided that the following conditions are met:
8#  1. Redistributions of source code must retain the above copyright
9#     notice, this list of conditions and the following disclaimer.
10#  2. Redistributions in binary form must reproduce the above copyright
11#     notice, this list of conditions and the following disclaimer in the
12#     documentation and/or other materials provided with the distribution.
13#  3. Neither the name of the copyright holder nor the
14#     names of its contributors may be used to endorse or promote products
15#     derived from this software without specific prior written permission.
16#
17#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27#  POSSIBILITY OF SUCH DAMAGE.
28#
29
30import json
31import os
32from typing import Dict
33
34from pktverify.addrs import EthAddr, ExtAddr, Ipv6Addr
35
36
37class TestInfo(object):
38    """
39    Represents the test information.
40    """
41
42    def __init__(self, filename):
43        self.filename = filename
44        with open(filename, 'rt') as fd:
45            test_info = json.loads(fd.read())
46
47            self.testcase = test_info.get('testcase', '')
48            self._pcap = test_info.get('pcap', 'current.pcap')
49            self.topology = self._convert_keys_to_ints(test_info['topology'])
50            self.extaddrs = {int(k): ExtAddr(v) for k, v in test_info.get('extaddrs', {}).items()}
51            self.ethaddrs = {int(k): EthAddr(v) for k, v in test_info.get('ethaddrs', {}).items()}
52            self.ipaddrs = {int(k): [Ipv6Addr(x) for x in l] for k, l in test_info.get('ipaddrs', {}).items()}
53            self.mleids = {int(k): Ipv6Addr(v) for k, v in test_info.get('mleids', {}).items()}
54            self.rlocs = {int(k): Ipv6Addr(v) for k, v in test_info.get('rlocs', {}).items()}
55            self.rloc16s = self._convert_hex_values(self._convert_keys_to_ints(test_info.get('rloc16s', {})))
56            self.omrs = {int(k): [Ipv6Addr(x) for x in l] for k, l in test_info.get('omrs', {}).items()}
57            self.duas = {int(k): Ipv6Addr(v) for k, v in test_info.get('duas', {}).items()}
58            self.extra_vars = test_info.get('extra_vars', {})
59            self.leader_aloc = Ipv6Addr(test_info.get('leader_aloc')) if 'leader_aloc' in test_info else ''
60
61    def __str__(self):
62        macs = dict(self.extaddrs)
63        macs.update({k + '_ETH': v for k, v in self.ethaddrs.items()})
64        macs = ",\n\t".join("%s=%s" % (k, v) for k, v in macs.items())
65
66        return "TestInfo<{case}|{pcap}|\n\t{macs}>".format(case=self.testcase, pcap=self._pcap, macs=macs)
67
68    @property
69    def pcap_path(self) -> str:
70        """
71        :return: The path to the Pcap file.
72        """
73        dir = os.path.dirname(self.filename)
74        return os.path.join(dir, self._pcap)
75
76    @staticmethod
77    def _convert_keys_to_ints(d: Dict[str, any]) -> Dict[int, any]:
78        return {int(k): v for k, v in d.items()}
79
80    @staticmethod
81    def _convert_hex_values(d: Dict[any, str]) -> Dict[any, int]:
82        return {k: int(v, 16) for k, v in d.items()}
83
84    def get_node_name(self, node_id: int) -> str:
85        """
86        Gets the name of the device.
87
88        :param node_id: The device ID.
89        :return: The name of the device.
90        """
91        return self.topology[node_id].get('name', 'Node%d' % node_id)
92