blob: 621cb2d211abf22dc2656c47b8ccd3cafa4f8098 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net;
tom64b7aac2014-08-26 00:18:21 -070017
tombb58c202014-09-07 22:51:50 -070018import java.util.Objects;
19
alshabibdf652ad2014-09-09 11:53:19 -070020import com.google.common.base.MoreObjects;
tombb58c202014-09-07 22:51:50 -070021
tom64b7aac2014-08-26 00:18:21 -070022/**
23 * Abstraction of a network connection point expressed as a pair of the
tombb58c202014-09-07 22:51:50 -070024 * network element identifier and port number.
tom64b7aac2014-08-26 00:18:21 -070025 */
tombb58c202014-09-07 22:51:50 -070026public class ConnectPoint {
27
28 private final ElementId elementId;
29 private final PortNumber portNumber;
tom64b7aac2014-08-26 00:18:21 -070030
31 /**
tombb58c202014-09-07 22:51:50 -070032 * Creates a new connection point.
tom64b7aac2014-08-26 00:18:21 -070033 *
tombb58c202014-09-07 22:51:50 -070034 * @param elementId network element identifier
35 * @param portNumber port number
tom64b7aac2014-08-26 00:18:21 -070036 */
tombb58c202014-09-07 22:51:50 -070037 public ConnectPoint(ElementId elementId, PortNumber portNumber) {
38 this.elementId = elementId;
39 this.portNumber = portNumber;
40 }
41
42 /**
43 * Returns the network element identifier.
44 *
45 * @return element identifier
46 */
47 public ElementId elementId() {
48 return elementId;
49 }
50
51 /**
52 * Returns the identifier of the infrastructure device if the connection
53 * point belongs to a network element which is indeed an infrastructure
54 * device.
55 *
56 * @return network element identifier as a device identifier
57 * @throws java.lang.IllegalStateException if connection point is not
58 * associated with a device
59 */
tombb58c202014-09-07 22:51:50 -070060 public DeviceId deviceId() {
61 if (elementId instanceof DeviceId) {
62 return (DeviceId) elementId;
63 }
64 throw new IllegalStateException("Connection point not associated " +
tomf5c9d922014-10-03 15:22:03 -070065 "with an infrastructure device");
66 }
67
68 /**
69 * Returns the identifier of the infrastructure device if the connection
70 * point belongs to a network element which is indeed an end-station host.
71 *
72 * @return network element identifier as a host identifier
73 * @throws java.lang.IllegalStateException if connection point is not
74 * associated with a host
75 */
76 public HostId hostId() {
77 if (elementId instanceof HostId) {
78 return (HostId) elementId;
79 }
80 throw new IllegalStateException("Connection point not associated " +
81 "with an end-station host");
tombb58c202014-09-07 22:51:50 -070082 }
tom64b7aac2014-08-26 00:18:21 -070083
84 /**
85 * Returns the connection port number.
86 *
87 * @return port number
88 */
tombb58c202014-09-07 22:51:50 -070089 public PortNumber port() {
90 return portNumber;
91 }
92
93 @Override
94 public int hashCode() {
95 return Objects.hash(elementId, portNumber);
96 }
97
98 @Override
99 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700100 if (this == obj) {
101 return true;
102 }
tombb58c202014-09-07 22:51:50 -0700103 if (obj instanceof ConnectPoint) {
104 final ConnectPoint other = (ConnectPoint) obj;
105 return Objects.equals(this.elementId, other.elementId) &&
106 Objects.equals(this.portNumber, other.portNumber);
107 }
108 return false;
109 }
110
111 @Override
112 public String toString() {
alshabibdf652ad2014-09-09 11:53:19 -0700113 return MoreObjects.toStringHelper(this)
tombb58c202014-09-07 22:51:50 -0700114 .add("elementId", elementId)
115 .add("portNumber", portNumber)
116 .toString();
117 }
tom64b7aac2014-08-26 00:18:21 -0700118
119}