chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace OpenTap
{
    /// <summary>
    /// Object representing a connection between two <see cref="Port"/>s. Can be extended to add properties.
    /// </summary>
    [Display("Generic Connection")]
    public abstract class Connection : ValidatingObject, ITapPlugin
    {
        // Orders has special values because they are placed first even if there are unordered items.
 
        /// <summary>
        /// A name for the connection to be displayed in the user interface.
        /// </summary>
        [Display("Name", Order: 0 - 100000)]
        [Layout(MinWidth=10)]
        public string Name { get; set; }
 
        /// <summary>
        /// The port at the first end of the connection.
        /// </summary>
        [Display("Port 1", Order: 1 - 100000)]
        [Layout(MinWidth=13)] // "Port A on DUT" : 13 characters. 
        public Port Port1 { get; set; }
 
        /// <summary> returns true if the port is used by this connection. </summary>
        internal bool HasPort(Port port) => Equals(Port1, port) || Equals(Port2, port);
 
        /// <summary>
        /// The port at the second end of the connection.
        /// </summary>
        [Display("Port 2", Order: 3 - 100000)]
        [Layout(MinWidth=13)]
        public Port Port2 { get; set; }
        
        /// <summary>
        /// Gets the list of <see cref="ViaPoint"/>s that this connection goes through. 
        /// </summary>
        [Layout(MinWidth=13)]
        [Display("Via", Order: 2 - 100000)]
        public List<ViaPoint> Via { get; set; } // ToDo: would be nice to make the setter private, but it is currently needed for deserialization 
 
        /// <summary>
        /// Returns true when a connection going through one or more switches (set using the <see cref="Via"/> property) is "Active" (all switches are in the correct position). 
        /// </summary>
        public bool IsActive
        {
            get
            {
                return Via.All(pos => pos.IsActive);
            }
        }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="Connection"/> class.
        /// </summary>
        public Connection()
        {
            Name = "Conn";
            Via = new List<ViaPoint>();
        }
 
        /// <summary>
        /// Returns the other port when given either <see cref="Port1"/> or <see cref="Port2"/>.
        /// </summary>
        public Port GetOtherPort(Port p)
        {
            if (Equals(p, Port1))
                return Port2;
            if (Equals(p, Port2))
                return Port1;
            
            throw new ArgumentException("Argument must be either Port1 or Port2");
        }
 
 
        
        /// <summary>
        /// Returns a string representation of this connection which names the ports in each end.
        /// </summary>
        public override string ToString()
        {
            if (String.IsNullOrWhiteSpace(Name))
                return String.Format("{0} <-> {1}", Port1 != null ? Port1.ToString() : "N/A", Port2 != null ? Port2.ToString() : "N/A");
            else
                return Name;
        }
    }
}