ffa7eaa8e2ffc174593876ce20753be69749d180
[vtn.git] /
1 /*
2  * Copyright (c) 2015 NEC Corporation
3  * All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this
7  * distribution, and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.vtn.manager.internal.util.inventory;
11
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.impl.topology.rev150209.vtn.topology.VtnLink;
13
14 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
15
16 /**
17  * {@code LinkEdge} describes an edge of network topology.
18  */
19 public final class LinkEdge {
20     /**
21      * The source switch port of this edge.
22      */
23     private final SalPort  sourcePort;
24
25     /**
26      * The destination switch port of this edge.
27      */
28     private final SalPort  destinationPort;
29
30     /**
31      * Construct a new instance.
32      *
33      * @param vlink  A {@link VtnLink} instance.
34      * @throws NullPointerException
35      *    {@code vlink} is {@code null}.
36      * @throws IllegalArgumentException
37      *    An invalid instance is specified to {@code vlink}.
38      */
39     public LinkEdge(VtnLink vlink) {
40         sourcePort = SalPort.create(vlink.getSource());
41         if (sourcePort == null) {
42             throw new IllegalArgumentException(
43                 "Source port is not configured: " + vlink);
44         }
45
46         destinationPort = SalPort.create(vlink.getDestination());
47         if (destinationPort == null) {
48             throw new IllegalArgumentException(
49                 "Destination port is not configured: " + vlink);
50         }
51     }
52
53     /**
54      * Construct a new instance.
55      *
56      * @param link  A {@link Link} instance.
57      * @throws NullPointerException
58      *    {@code link} is {@code null}.
59      * @throws IllegalArgumentException
60      *    An invalid instance is specified to {@code link}.
61      */
62     public LinkEdge(Link link) {
63         sourcePort = SalPort.create(link.getSource());
64         if (sourcePort == null) {
65             throw new IllegalArgumentException(
66                 "Source port is not configured: " + link);
67         }
68
69         destinationPort = SalPort.create(link.getDestination());
70         if (destinationPort == null) {
71             throw new IllegalArgumentException(
72                 "Destination port is not configured: " + link);
73         }
74     }
75
76     /**
77      * Return the source port of this edge.
78      *
79      * @return  A {@link SalPort} instance which represents the source
80      *          switch port of the edge.
81      */
82     public SalPort getSourcePort() {
83         return sourcePort;
84     }
85
86     /**
87      * Return the destination port of this edge.
88      *
89      * @return  A {@link SalPort} instance which represents the destination
90      *          switch port of the edge.
91      */
92     public SalPort getDestinationPort() {
93         return destinationPort;
94     }
95
96     /**
97      * Determine whether the given object is identical to this object.
98      *
99      * @param o  An object to be compared.
100      * @return   {@code true} if identical. Otherwise {@code false}.
101      */
102     @Override
103     public boolean equals(Object o) {
104         if (o == this) {
105             return true;
106         }
107         if (o == null || !getClass().equals(o.getClass())) {
108             return false;
109         }
110
111         LinkEdge le = (LinkEdge)o;
112         return (sourcePort.equals(le.sourcePort) &&
113                 destinationPort.equals(le.destinationPort));
114     }
115
116     /**
117      * Return the hash code of this object.
118      *
119      * @return  The hash code.
120      */
121     @Override
122     public int hashCode() {
123         return sourcePort.hashCode() + destinationPort.hashCode() * 31;
124     }
125
126     /**
127      * Return a string representation of this object.
128      *
129      * @return  A string representation of this object.
130      */
131     @Override
132     public String toString() {
133         StringBuilder builder = new StringBuilder("LinkEdge[");
134         builder.append(sourcePort).append(" -> ").append(destinationPort).
135             append(']');
136
137         return builder.toString();
138     }
139 }