blob: 31d855db0557a6abcd1c00e910f05bcb5b13e049 [file] [log] [blame]
Priyanka Bb2988fa2015-10-09 12:45:36 +05301/*
2 * Copyright 2015 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 */
Priyanka Bb2988fa2015-10-09 12:45:36 +053016package org.onosproject.bgpio.types.attr;
17
Thejaswi NK380fa1b2015-11-18 17:50:51 +053018import java.util.ArrayList;
19import java.util.List;
Priyanka B02040732015-11-29 11:30:29 +053020import java.util.ListIterator;
Priyanka Bb2988fa2015-10-09 12:45:36 +053021import java.util.Objects;
22
23import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053024import org.onosproject.bgpio.exceptions.BgpParseException;
25import org.onosproject.bgpio.types.BgpErrorType;
26import org.onosproject.bgpio.types.BgpValueType;
Priyanka Bb2988fa2015-10-09 12:45:36 +053027import org.onosproject.bgpio.util.Validation;
28import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
31import com.google.common.base.MoreObjects;
32
33/**
34 * BGP Multi-Topology ID of the LS attribute.
35 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053036public class BgpAttrNodeMultiTopologyId implements BgpValueType {
Priyanka Bb2988fa2015-10-09 12:45:36 +053037
38 private static final Logger log = LoggerFactory
39 .getLogger(BgpAttrNodeMultiTopologyId.class);
40
41 public static final int ATTRNODE_MULTITOPOLOGY = 263;
42
43 /* Opaque Node Attribute */
Thejaswi NK380fa1b2015-11-18 17:50:51 +053044 private List<Short> multiTopologyId = new ArrayList<Short>();
Priyanka Bb2988fa2015-10-09 12:45:36 +053045
46 /**
47 * Constructor to initialize the Node attribute multi-topology ID.
48 *
49 * @param multiTopologyId multi-topology ID
50 */
Thejaswi NK380fa1b2015-11-18 17:50:51 +053051 public BgpAttrNodeMultiTopologyId(List<Short> multiTopologyId) {
Priyanka Bb2988fa2015-10-09 12:45:36 +053052 this.multiTopologyId = multiTopologyId;
53 }
54
55 /**
Thejaswi NK380fa1b2015-11-18 17:50:51 +053056 * Returns object of this class with specified values.
57 *
58 * @param multiTopologyId Prefix Metric
59 * @return object of BgpAttrNodeMultiTopologyId
60 */
61 public static BgpAttrNodeMultiTopologyId of(ArrayList<Short> multiTopologyId) {
62 return new BgpAttrNodeMultiTopologyId(multiTopologyId);
63 }
64
65 /**
Priyanka Bb2988fa2015-10-09 12:45:36 +053066 * Reads the Multi-topology ID of Node attribute.
67 *
68 * @param cb ChannelBuffer
69 * @return Constructor of BgpAttrNodeMultiTopologyId
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053070 * @throws BgpParseException while parsing BgpAttrNodeMultiTopologyId
Priyanka Bb2988fa2015-10-09 12:45:36 +053071 */
72 public static BgpAttrNodeMultiTopologyId read(ChannelBuffer cb)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053073 throws BgpParseException {
Thejaswi NK380fa1b2015-11-18 17:50:51 +053074 ArrayList<Short> multiTopologyId = new ArrayList<Short>();
75 short tempMultiTopologyId;
Priyanka Bb2988fa2015-10-09 12:45:36 +053076 short lsAttrLength = cb.readShort();
77 int len = lsAttrLength / 2; // Length is 2*n and n is the number of MT-IDs
78
79 if (cb.readableBytes() < lsAttrLength) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053080 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
81 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Thejaswi NK380fa1b2015-11-18 17:50:51 +053082 lsAttrLength);
Priyanka Bb2988fa2015-10-09 12:45:36 +053083 }
84
Priyanka Bb2988fa2015-10-09 12:45:36 +053085 for (int i = 0; i < len; i++) {
Thejaswi NK380fa1b2015-11-18 17:50:51 +053086 tempMultiTopologyId = cb.readShort();
87 multiTopologyId.add(new Short(tempMultiTopologyId));
Priyanka Bb2988fa2015-10-09 12:45:36 +053088 }
89
90 return new BgpAttrNodeMultiTopologyId(multiTopologyId);
91 }
92
93 /**
94 * to get the multi-topology ID.
95 *
96 * @return multitopology ID
97 */
Thejaswi NK380fa1b2015-11-18 17:50:51 +053098 public List<Short> attrMultiTopologyId() {
Priyanka Bb2988fa2015-10-09 12:45:36 +053099 return multiTopologyId;
100 }
101
102 @Override
103 public short getType() {
104 return ATTRNODE_MULTITOPOLOGY;
105 }
106
107 @Override
108 public int hashCode() {
109 return Objects.hash(multiTopologyId);
110 }
111
112 @Override
113 public boolean equals(Object obj) {
114 if (this == obj) {
115 return true;
116 }
117
118 if (obj instanceof BgpAttrNodeMultiTopologyId) {
119 BgpAttrNodeMultiTopologyId other = (BgpAttrNodeMultiTopologyId) obj;
120 return Objects.equals(multiTopologyId, other.multiTopologyId);
121 }
122 return false;
123 }
124
125 @Override
126 public int write(ChannelBuffer cb) {
Thejaswi NK380fa1b2015-11-18 17:50:51 +0530127 // TODO This will be implemented in the next version
Priyanka Bb2988fa2015-10-09 12:45:36 +0530128 return 0;
129 }
130
131 @Override
132 public String toString() {
133 return MoreObjects.toStringHelper(getClass())
134 .omitNullValues()
135 .add("multiTopologyId", multiTopologyId)
136 .toString();
137 }
Priyanka B02040732015-11-29 11:30:29 +0530138
139 @Override
140 public int compareTo(Object o) {
141 if (this.equals(o)) {
142 return 0;
143 }
144 int countOtherSubTlv = ((BgpAttrNodeMultiTopologyId) o).multiTopologyId.size();
145 int countObjSubTlv = multiTopologyId.size();
146 if (countOtherSubTlv != countObjSubTlv) {
147 if (countOtherSubTlv > countObjSubTlv) {
148 return 1;
149 } else {
150 return -1;
151 }
152 }
153 ListIterator<Short> listIterator = multiTopologyId.listIterator();
154 ListIterator<Short> listIteratorOther = ((BgpAttrNodeMultiTopologyId) o).multiTopologyId.listIterator();
155 while (listIterator.hasNext()) {
156 short id = listIterator.next();
157 short id1 = listIteratorOther.next();
158 if (((Short) id).compareTo((Short) id1) != 0) {
159 return ((Short) id).compareTo((Short) id1);
160 }
161 }
162 return 0;
163 }
Priyanka Bb2988fa2015-10-09 12:45:36 +0530164}