blob: 017dedac0f691c55e5fee1f3f84f25b43f045ed0 [file] [log] [blame]
Thejaswi NKcd4a90e2015-10-14 19:08:46 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thejaswi NKcd4a90e2015-10-14 19:08:46 +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.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;
Priyanka B0ab34b92015-12-03 21:13:52 +053036 public static final short SIZE = 4;
Thejaswi NKcd4a90e2015-10-14 19:08:46 +053037
38 /* Shared Risk Link Group */
39 private List<Integer> sRlg = new ArrayList<Integer>();
40
41 /**
42 * Constructor to initialize the date.
43 *
44 * @param sRlg Shared Risk link group data
45 */
46 public BgpLinkAttrSrlg(List<Integer> sRlg) {
47 this.sRlg = sRlg;
48 }
49
50 /**
51 * Returns object of this class with specified values.
52 *
53 * @param sRlg Shared Risk link group data
54 * @return object of BgpLinkAttrSrlg
55 */
56 public static BgpLinkAttrSrlg of(ArrayList<Integer> sRlg) {
57 return new BgpLinkAttrSrlg(sRlg);
58 }
59
60 /**
61 * Reads the BGP link attributes Shared Risk link group data.
62 *
63 * @param cb Channel buffer
64 * @return object of type BgpLinkAttrSrlg
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053065 * @throws BgpParseException while parsing BgpLinkAttrSrlg
Thejaswi NKcd4a90e2015-10-14 19:08:46 +053066 */
67 public static BgpLinkAttrSrlg read(ChannelBuffer cb)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053068 throws BgpParseException {
Thejaswi NKcd4a90e2015-10-14 19:08:46 +053069 int tempSrlg;
70 ArrayList<Integer> sRlg = new ArrayList<Integer>();
71
72 short lsAttrLength = cb.readShort();
Priyanka B0ab34b92015-12-03 21:13:52 +053073 int len = lsAttrLength / SIZE; // each element is of 4 octets
Thejaswi NKcd4a90e2015-10-14 19:08:46 +053074
75 if (cb.readableBytes() < lsAttrLength) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053076 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
77 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Thejaswi NKcd4a90e2015-10-14 19:08:46 +053078 lsAttrLength);
79 }
80
81 for (int i = 0; i < len; i++) {
82 tempSrlg = cb.readInt();
83 sRlg.add(new Integer(tempSrlg));
84 }
85
86 return BgpLinkAttrSrlg.of(sRlg);
87 }
88
89 /**
90 * Returns the Shared Risk link group data.
91 *
92 * @return array of Shared Risk link group data
93 */
94 public List<Integer> attrSrlg() {
95 return sRlg;
96 }
97
98 @Override
99 public short getType() {
100 return ATTRNODE_SRLG;
101 }
102
103 @Override
104 public int hashCode() {
105 return Objects.hash(sRlg);
106 }
107
108 @Override
109 public boolean equals(Object obj) {
110 if (this == obj) {
111 return true;
112 }
113
114 if (obj instanceof BgpLinkAttrSrlg) {
115 BgpLinkAttrSrlg other = (BgpLinkAttrSrlg) obj;
116 return Objects.equals(sRlg, other.sRlg);
117 }
118 return false;
119 }
120
121 @Override
122 public int write(ChannelBuffer cb) {
123 // TODO This will be implemented in the next version
124 return 0;
125 }
126
127 @Override
128 public String toString() {
129 return MoreObjects.toStringHelper(getClass()).omitNullValues().add("sRlg", sRlg).toString();
130 }
Priyanka B02040732015-11-29 11:30:29 +0530131
132 @Override
133 public int compareTo(Object o) {
134 // TODO Auto-generated method stub
135 return 0;
136 }
Thejaswi NKcd4a90e2015-10-14 19:08:46 +0530137}