Thejaswi NK
Committed by Gerrit Code Review

[Emu][onos-2603] - Implement LinkState Link attributes-Shared Risk Link Group

Change-Id: I11400373d6206605811cf007f534f810cee31ddc
1 +/*
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 + */
16 +package org.onosproject.bgpio.types.attr;
17 +
18 +import java.util.ArrayList;
19 +import java.util.List;
20 +import java.util.Objects;
21 +
22 +import org.jboss.netty.buffer.ChannelBuffer;
23 +import org.onosproject.bgpio.exceptions.BGPParseException;
24 +import org.onosproject.bgpio.types.BGPErrorType;
25 +import org.onosproject.bgpio.types.BGPValueType;
26 +import org.onosproject.bgpio.util.Validation;
27 +
28 +import com.google.common.base.MoreObjects;
29 +
30 +/**
31 + * Implements BGP link Shared Risk Link Group attribute.
32 + */
33 +public class BgpLinkAttrSrlg implements BGPValueType {
34 +
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
64 + * @throws BGPParseException while parsing BgpLinkAttrSrlg
65 + */
66 + public static BgpLinkAttrSrlg read(ChannelBuffer cb)
67 + throws BGPParseException {
68 + 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) {
75 + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR,
76 + BGPErrorType.ATTRIBUTE_LENGTH_ERROR,
77 + 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 + }
130 +}