blob: 745a831d2509f5471e33e3a5f09952808b36569c [file] [log] [blame]
Shashikanth VH73b12772016-04-18 14:28:45 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Shashikanth VH73b12772016-04-18 14:28:45 +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.bgpio.types;
17
18import com.google.common.base.MoreObjects;
19
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.onosproject.bgpio.exceptions.BgpParseException;
22import org.onosproject.bgpio.util.Validation;
23
24import java.util.ArrayList;
25import java.util.Iterator;
26import java.util.List;
27import java.util.Objects;
28
29/**
30 * Provides implementation of BGP wide community integer subtlv.
31 */
32public class WideCommunityInteger implements BgpValueType {
33 public static final short TYPE = 4;
34 private List<Integer> integer;
35
36 /**
37 * Creates an instance of wide community integer subtlv.
38 *
39 * @param integer integer
40 */
41 public WideCommunityInteger(List<Integer> integer) {
42 this.integer = integer;
43 }
44
45 /**
46 * Returns object of this class with specified values.
47 *
48 * @param integer wide community subtlv integer
49 * @return object of WideCommunityInteger
50 */
51 public static WideCommunityInteger of(List<Integer> integer) {
52 return new WideCommunityInteger(integer);
53 }
54
55 /**
56 * Returns wide community subtlv integer.
57 *
58 * @return wide community subtlv integer
59 */
60 public List<Integer> integer() {
61 return integer;
62 }
63
64 /**
65 * Sets wide community subtlv integer.
66 *
67 * @param integer wide community subtlv integer
68 */
69 public void setInteger(List<Integer> integer) {
70 this.integer = integer;
71 }
72
73 @Override
74 public int hashCode() {
75 return Objects.hash(integer);
76 }
77
78 @Override
79 public boolean equals(Object obj) {
80 if (this == obj) {
81 return true;
82 }
83
84 if (obj == null) {
85 return false;
86 }
87
88 if (obj instanceof WideCommunityInteger) {
89 WideCommunityInteger other = (WideCommunityInteger) obj;
90 return Objects.equals(integer, other.integer);
91 }
92 return false;
93 }
94
95 @Override
96 public int write(ChannelBuffer c) {
97 int iLenStartIndex = c.writerIndex();
98
99 Iterator<Integer> listIterator = integer.iterator();
100 c.writeByte(TYPE);
101
102 int iLengthIndex = c.writerIndex();
103 c.writeShort(0);
104
105 while (listIterator.hasNext()) {
106 Integer integer = listIterator.next();
107 if (integer instanceof Integer) {
108 c.writeInt(integer);
109 }
110 }
111
112 int length = c.writerIndex() - iLengthIndex;
113 c.setShort(iLengthIndex, (short) (length - 2));
114
115 return c.writerIndex() - iLenStartIndex;
116 }
117
118 /**
119 * Reads the channel buffer and returns object of WideCommunityInteger.
120 *
121 * @param c ChannelBuffer
122 * @return object of WideCommunityInteger
123 * @throws BgpParseException on read error
124 */
125 public static WideCommunityInteger read(ChannelBuffer c) throws BgpParseException {
126
127 List<Integer> integer = new ArrayList<>();
128 short length;
129
130 if (c.readableBytes() < 2) {
131 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
132 c.readableBytes());
133 }
134
135 length = c.readShort();
136 if (length == 0) {
137 return new WideCommunityInteger(integer);
138 }
139
140 if (c.readableBytes() < length) {
141 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
142 c.readableBytes());
143 }
144
145 while (c.readableBytes() > 0) {
146 if (c.readableBytes() < 4) {
147 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
148 c.readableBytes());
149 }
150 integer.add(c.readInt());
151 }
152
153 return new WideCommunityInteger(integer);
154 }
155
156 @Override
157 public String toString() {
158 return MoreObjects.toStringHelper(getClass())
159 .omitNullValues()
160 .add("integer", integer)
161 .toString();
162 }
163
164 @Override
165 public int compareTo(Object o) {
166 // TODO Auto-generated method stub
167 return 0;
168 }
169
170 @Override
171 public short getType() {
172 // TODO Auto-generated method stub
173 return 0;
174 }
175}