aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/apps/iptopology-api/src/main/java/org/onosproject/iptopology/api/DeviceTed.java
blob: 4d9da55d8199a2387c864422a2f9ec857b046626 (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onosproject.iptopology.api;

import static com.google.common.base.MoreObjects.toStringHelper;

import java.util.Iterator;
import java.util.List;
import java.util.Objects;

import org.onlab.packet.Ip4Address;
import org.onlab.packet.Ip6Address;

/**
 * Represents Device Traffic Engineering parameters.
 */
public class DeviceTed {
    private final List<Ip4Address> ipv4RouterIds;
    private final List<Ip6Address> ipv6RouterIds;
    private final List<TopologyId> topologyIds;
    private final Position position;

    /**
     * Constructor to initialize the parameter fields.
     *
     * @param ipv4RouterIds Router ids of Ipv4
     * @param ipv6RouterIds Router ids of Ipv6
     * @param topologyIds list of multi-topology IDs of the node
     * @param position of router whether it is ABR or ASBR
     */
    public DeviceTed(List<Ip4Address> ipv4RouterIds, List<Ip6Address> ipv6RouterIds,
                     List<TopologyId> topologyIds, Position position) {
        this.ipv4RouterIds = ipv4RouterIds;
        this.ipv6RouterIds = ipv6RouterIds;
        this.topologyIds = topologyIds;
        this.position = position;
    }

    /**
     * Obtain list of Ipv4 Router id.
     *
     * @return Ipv4 Router ids
     */
    public List<Ip4Address> ipv4RouterIds() {
        return ipv4RouterIds;
    }

    /**
     * Obtain list of Ipv6 Router id.
     *
     * @return Ipv6 Router ids
     */
    public List<Ip6Address> ipv6RouterIds() {
        return ipv6RouterIds;
    }

    /**
     * Obtain the list of topology ID's.
     *
     * @return list of topology id's
     */
    public List<TopologyId> topologyIds() {
        return topologyIds;
    }


    /**
     * Obtain position of device in the network.
     *
     * @return position of device in the network
     */
    public Position position() {
        return position;
    }

    @Override
    public int hashCode() {
        return Objects.hash(ipv4RouterIds, ipv6RouterIds, topologyIds, position);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (obj instanceof DeviceTed) {
            int countObjSubTlv = 0;
            int countOtherSubTlv = 0;
            int countObjTopologyId = 0;
            int countOtherTopologyId = 0;
            boolean isCommonSubTlv = true;
            boolean isCommonSubTlv6 = true;
            boolean isCommonTopology = true;
            DeviceTed other = (DeviceTed) obj;
            Iterator<Ip4Address> objListIterator = other.ipv4RouterIds.iterator();
            countOtherSubTlv = other.ipv4RouterIds.size();
            countObjSubTlv = ipv4RouterIds.size();

            Iterator<Ip6Address> objListIteratorIpv6 = other.ipv6RouterIds.iterator();
            int countOtherSubTlv6 = other.ipv6RouterIds.size();
            int countObjSubTlv6 = ipv6RouterIds.size();

            Iterator<TopologyId> topologyId = other.topologyIds.iterator();
            countOtherTopologyId = other.topologyIds.size();
            countObjTopologyId = topologyIds.size();

            if (countObjSubTlv != countOtherSubTlv || countOtherSubTlv6 != countObjSubTlv6
                    || countObjTopologyId != countOtherTopologyId) {
                return false;
            } else {
                while (objListIterator.hasNext() && isCommonSubTlv) {
                    Ip4Address subTlv = objListIterator.next();
                    //find index of that element and then get that from the list and then compare
                    if (ipv4RouterIds.contains(subTlv) && other.ipv4RouterIds.contains(subTlv)) {
                        isCommonSubTlv = Objects.equals(ipv4RouterIds.get(ipv4RouterIds.indexOf(subTlv)),
                                other.ipv4RouterIds.get(other.ipv4RouterIds.indexOf(subTlv)));
                    } else {
                        isCommonSubTlv = false;
                    }
                }
                while (objListIteratorIpv6.hasNext() && isCommonSubTlv6) {
                    Ip6Address subTlv = objListIteratorIpv6.next();
                    //find index of that element and then get that from the list and then compare
                    if (ipv6RouterIds.contains(subTlv) && other.ipv6RouterIds.contains(subTlv)) {
                        isCommonSubTlv6 = Objects.equals(ipv6RouterIds.get(ipv6RouterIds.indexOf(subTlv)),
                                other.ipv6RouterIds.get(other.ipv6RouterIds.indexOf(subTlv)));
                    } else {
                        isCommonSubTlv6 = false;
                    }
                }
                while (topologyId.hasNext() && isCommonTopology) {
                    TopologyId subTlv = topologyId.next();
                    //find index of that element and then get that from the list and then compare
                    if (topologyIds.contains(subTlv) && other.topologyIds.contains(subTlv)) {
                        isCommonTopology = Objects.equals(topologyIds.get(topologyIds.indexOf(subTlv)),
                                other.topologyIds.get(other.topologyIds.indexOf(subTlv)));
                    } else {
                        isCommonTopology = false;
                    }
                }
                return isCommonSubTlv && isCommonSubTlv6 && isCommonTopology
                        && Objects.equals(position, other.position);
            }
        }
        return false;
    }

    @Override
    public String toString() {
        return toStringHelper(this)
                .omitNullValues()
                .add("ipv6RouterIds", ipv6RouterIds)
                .add("ipv4RouterIds", ipv4RouterIds)
                .add("topologyIds", topologyIds)
                .add("position", position)
                .toString();
    }

}