blob: 8ad7d7be392e543060323791d65917be316f065e [file] [log] [blame]
Satish Kf6d87cb2015-11-30 19:59:22 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Satish Kf6d87cb2015-11-30 19:59:22 +05303 *
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 */
16package org.onosproject.iptopology.api;
17
18import static com.google.common.base.MoreObjects.toStringHelper;
19
20import java.util.Iterator;
21import java.util.List;
22import java.util.Objects;
23
24import org.onlab.packet.Ip4Address;
25import org.onlab.packet.Ip6Address;
26
27/**
28 * Represents Device Traffic Engineering parameters.
29 */
30public class DeviceTed {
31 private final List<Ip4Address> ipv4RouterIds;
32 private final List<Ip6Address> ipv6RouterIds;
33 private final List<TopologyId> topologyIds;
34 private final Position position;
35
36 /**
37 * Constructor to initialize the parameter fields.
38 *
39 * @param ipv4RouterIds Router ids of Ipv4
40 * @param ipv6RouterIds Router ids of Ipv6
41 * @param topologyIds list of multi-topology IDs of the node
42 * @param position of router whether it is ABR or ASBR
43 */
44 public DeviceTed(List<Ip4Address> ipv4RouterIds, List<Ip6Address> ipv6RouterIds,
45 List<TopologyId> topologyIds, Position position) {
46 this.ipv4RouterIds = ipv4RouterIds;
47 this.ipv6RouterIds = ipv6RouterIds;
48 this.topologyIds = topologyIds;
49 this.position = position;
50 }
51
52 /**
53 * Obtain list of Ipv4 Router id.
54 *
55 * @return Ipv4 Router ids
56 */
57 public List<Ip4Address> ipv4RouterIds() {
58 return ipv4RouterIds;
59 }
60
61 /**
62 * Obtain list of Ipv6 Router id.
63 *
64 * @return Ipv6 Router ids
65 */
66 public List<Ip6Address> ipv6RouterIds() {
67 return ipv6RouterIds;
68 }
69
70 /**
71 * Obtain the list of topology ID's.
72 *
73 * @return list of topology id's
74 */
75 public List<TopologyId> topologyIds() {
76 return topologyIds;
77 }
78
79
80 /**
81 * Obtain position of device in the network.
82 *
83 * @return position of device in the network
84 */
85 public Position position() {
86 return position;
87 }
88
89 @Override
90 public int hashCode() {
91 return Objects.hash(ipv4RouterIds, ipv6RouterIds, topologyIds, position);
92 }
93
94 @Override
95 public boolean equals(Object obj) {
96 if (this == obj) {
97 return true;
98 }
99
100 if (obj instanceof DeviceTed) {
101 int countObjSubTlv = 0;
102 int countOtherSubTlv = 0;
103 int countObjTopologyId = 0;
104 int countOtherTopologyId = 0;
105 boolean isCommonSubTlv = true;
106 boolean isCommonSubTlv6 = true;
107 boolean isCommonTopology = true;
108 DeviceTed other = (DeviceTed) obj;
109 Iterator<Ip4Address> objListIterator = other.ipv4RouterIds.iterator();
110 countOtherSubTlv = other.ipv4RouterIds.size();
111 countObjSubTlv = ipv4RouterIds.size();
112
113 Iterator<Ip6Address> objListIteratorIpv6 = other.ipv6RouterIds.iterator();
114 int countOtherSubTlv6 = other.ipv6RouterIds.size();
115 int countObjSubTlv6 = ipv6RouterIds.size();
116
117 Iterator<TopologyId> topologyId = other.topologyIds.iterator();
118 countOtherTopologyId = other.topologyIds.size();
119 countObjTopologyId = topologyIds.size();
120
121 if (countObjSubTlv != countOtherSubTlv || countOtherSubTlv6 != countObjSubTlv6
122 || countObjTopologyId != countOtherTopologyId) {
123 return false;
124 } else {
125 while (objListIterator.hasNext() && isCommonSubTlv) {
126 Ip4Address subTlv = objListIterator.next();
127 //find index of that element and then get that from the list and then compare
128 if (ipv4RouterIds.contains(subTlv) && other.ipv4RouterIds.contains(subTlv)) {
129 isCommonSubTlv = Objects.equals(ipv4RouterIds.get(ipv4RouterIds.indexOf(subTlv)),
130 other.ipv4RouterIds.get(other.ipv4RouterIds.indexOf(subTlv)));
131 } else {
132 isCommonSubTlv = false;
133 }
134 }
135 while (objListIteratorIpv6.hasNext() && isCommonSubTlv6) {
136 Ip6Address subTlv = objListIteratorIpv6.next();
137 //find index of that element and then get that from the list and then compare
138 if (ipv6RouterIds.contains(subTlv) && other.ipv6RouterIds.contains(subTlv)) {
139 isCommonSubTlv6 = Objects.equals(ipv6RouterIds.get(ipv6RouterIds.indexOf(subTlv)),
140 other.ipv6RouterIds.get(other.ipv6RouterIds.indexOf(subTlv)));
141 } else {
142 isCommonSubTlv6 = false;
143 }
144 }
145 while (topologyId.hasNext() && isCommonTopology) {
146 TopologyId subTlv = topologyId.next();
147 //find index of that element and then get that from the list and then compare
148 if (topologyIds.contains(subTlv) && other.topologyIds.contains(subTlv)) {
149 isCommonTopology = Objects.equals(topologyIds.get(topologyIds.indexOf(subTlv)),
150 other.topologyIds.get(other.topologyIds.indexOf(subTlv)));
151 } else {
152 isCommonTopology = false;
153 }
154 }
155 return isCommonSubTlv && isCommonSubTlv6 && isCommonTopology
156 && Objects.equals(position, other.position);
157 }
158 }
159 return false;
160 }
161
162 @Override
163 public String toString() {
164 return toStringHelper(this)
165 .omitNullValues()
166 .add("ipv6RouterIds", ipv6RouterIds)
167 .add("ipv4RouterIds", ipv4RouterIds)
168 .add("topologyIds", topologyIds)
169 .add("position", position)
170 .toString();
171 }
172
173}