blob: 4a6f23f9b19bf6edfdd2debd6ca9e4d65e8ad7b3 [file] [log] [blame]
Thejaswi NKcd4a90e2015-10-14 19:08:46 +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 */
16package org.onosproject.bgpio.types.attr;
17
18import java.util.ArrayList;
19import java.util.List;
20import java.util.Objects;
21
22import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053023import org.onosproject.bgpio.exceptions.BgpParseException;
24import org.onosproject.bgpio.types.BgpErrorType;
25import org.onosproject.bgpio.types.BgpValueType;
Thejaswi NKcd4a90e2015-10-14 19:08:46 +053026import org.onosproject.bgpio.util.Validation;
27
28import com.google.common.base.MoreObjects;
29
30/**
31 * Implements BGP link Shared Risk Link Group attribute.
32 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053033public class BgpLinkAttrSrlg implements BgpValueType {
Thejaswi NKcd4a90e2015-10-14 19:08:46 +053034
35 public static final short ATTRNODE_SRLG = 1097;
36
37 /* Shared Risk Link Group */
38 private List<Integer> sRlg = new ArrayList<Integer>();
39
40 /**
41 * Constructor to initialize the date.
42 *
43 * @param sRlg Shared Risk link group data
44 */
45 public BgpLinkAttrSrlg(List<Integer> sRlg) {
46 this.sRlg = sRlg;
47 }
48
49 /**
50 * Returns object of this class with specified values.
51 *
52 * @param sRlg Shared Risk link group data
53 * @return object of BgpLinkAttrSrlg
54 */
55 public static BgpLinkAttrSrlg of(ArrayList<Integer> sRlg) {
56 return new BgpLinkAttrSrlg(sRlg);
57 }
58
59 /**
60 * Reads the BGP link attributes Shared Risk link group data.
61 *
62 * @param cb Channel buffer
63 * @return object of type BgpLinkAttrSrlg
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053064 * @throws BgpParseException while parsing BgpLinkAttrSrlg
Thejaswi NKcd4a90e2015-10-14 19:08:46 +053065 */
66 public static BgpLinkAttrSrlg read(ChannelBuffer cb)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053067 throws BgpParseException {
Thejaswi NKcd4a90e2015-10-14 19:08:46 +053068 int tempSrlg;
69 ArrayList<Integer> sRlg = new ArrayList<Integer>();
70
71 short lsAttrLength = cb.readShort();
72 int len = lsAttrLength / Integer.SIZE; // each element is of 4 octets
73
74 if (cb.readableBytes() < lsAttrLength) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053075 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
76 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Thejaswi NKcd4a90e2015-10-14 19:08:46 +053077 lsAttrLength);
78 }
79
80 for (int i = 0; i < len; i++) {
81 tempSrlg = cb.readInt();
82 sRlg.add(new Integer(tempSrlg));
83 }
84
85 return BgpLinkAttrSrlg.of(sRlg);
86 }
87
88 /**
89 * Returns the Shared Risk link group data.
90 *
91 * @return array of Shared Risk link group data
92 */
93 public List<Integer> attrSrlg() {
94 return sRlg;
95 }
96
97 @Override
98 public short getType() {
99 return ATTRNODE_SRLG;
100 }
101
102 @Override
103 public int hashCode() {
104 return Objects.hash(sRlg);
105 }
106
107 @Override
108 public boolean equals(Object obj) {
109 if (this == obj) {
110 return true;
111 }
112
113 if (obj instanceof BgpLinkAttrSrlg) {
114 BgpLinkAttrSrlg other = (BgpLinkAttrSrlg) obj;
115 return Objects.equals(sRlg, other.sRlg);
116 }
117 return false;
118 }
119
120 @Override
121 public int write(ChannelBuffer cb) {
122 // TODO This will be implemented in the next version
123 return 0;
124 }
125
126 @Override
127 public String toString() {
128 return MoreObjects.toStringHelper(getClass()).omitNullValues().add("sRlg", sRlg).toString();
129 }
Priyanka B02040732015-11-29 11:30:29 +0530130
131 @Override
132 public int compareTo(Object o) {
133 // TODO Auto-generated method stub
134 return 0;
135 }
Thejaswi NKcd4a90e2015-10-14 19:08:46 +0530136}