Priyanka B
Committed by Gerrit Code Review

[Emu] [ONOS-2601] Implement BGP Update protocol message and parse all basic path attributes.

Change-Id: I85f912eeebacf46d699fdcf5549e06bae702823e
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;
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.util.Validation;
25 +import org.slf4j.Logger;
26 +import org.slf4j.LoggerFactory;
27 +
28 +import com.google.common.base.MoreObjects;
29 +
30 +/**
31 + * Provides Implementation of As4Path BGP Path Attribute.
32 + */
33 +public class As4Path implements BGPValueType {
34 + private static final Logger log = LoggerFactory.getLogger(AsPath.class);
35 + public static final byte AS4PATH_TYPE = 17;
36 + public static final byte ASNUM_SIZE = 4;
37 + public static final int TYPE_AND_LEN_AS_SHORT = 4;
38 + public static final int TYPE_AND_LEN_AS_BYTE = 3;
39 +
40 + private List<Integer> as4pathSet;
41 + private List<Integer> as4pathSeq;
42 +
43 + /**
44 + * Initialize fields.
45 + */
46 + public As4Path() {
47 + this.as4pathSeq = null;
48 + this.as4pathSet = null;
49 + }
50 +
51 + /**
52 + * Constructor to initialize parameters.
53 + *
54 + * @param as4pathSet AS4path Set
55 + * @param as4pathSeq AS4path Sequence
56 + */
57 + public As4Path(List<Integer> as4pathSet, List<Integer> as4pathSeq) {
58 + this.as4pathSeq = as4pathSeq;
59 + this.as4pathSet = as4pathSet;
60 + }
61 +
62 + /**
63 + * Reads from the channel buffer and parses As4Path.
64 + *
65 + * @param cb ChannelBuffer
66 + * @return object of As4Path
67 + * @throws BGPParseException while parsing As4Path
68 + */
69 + public static As4Path read(ChannelBuffer cb) throws BGPParseException {
70 + List<Integer> as4pathSet = new ArrayList<>();
71 + List<Integer> as4pathSeq = new ArrayList<>();
72 + ChannelBuffer tempCb = cb.copy();
73 + Validation validation = Validation.parseAttributeHeader(cb);
74 +
75 + if (cb.readableBytes() < validation.getLength()) {
76 + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.ATTRIBUTE_LENGTH_ERROR,
77 + validation.getLength());
78 + }
79 + //if fourth bit is set length is read as short otherwise as byte , len includes type, length and value
80 + int len = validation.isShort() ? validation.getLength() + TYPE_AND_LEN_AS_SHORT : validation
81 + .getLength() + TYPE_AND_LEN_AS_BYTE;
82 + ChannelBuffer data = tempCb.readBytes(len);
83 + if (validation.getFirstBit() && !validation.getSecondBit() && validation.getThirdBit()) {
84 + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.ATTRIBUTE_FLAGS_ERROR, data);
85 + }
86 +
87 + ChannelBuffer tempBuf = cb.readBytes(validation.getLength());
88 + while (tempBuf.readableBytes() > 0) {
89 + byte pathSegType = tempBuf.readByte();
90 + //no of ASes
91 + byte pathSegLen = tempBuf.readByte();
92 + //length = no of Ases * ASnum size (4 bytes)
93 + int length = pathSegLen * ASNUM_SIZE;
94 + if (tempBuf.readableBytes() < length) {
95 + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR,
96 + BGPErrorType.ATTRIBUTE_LENGTH_ERROR, length);
97 + }
98 + ChannelBuffer aspathBuf = tempBuf.readBytes(length);
99 + while (aspathBuf.readableBytes() > 0) {
100 + int asNum;
101 + asNum = aspathBuf.readInt();
102 + switch (pathSegType) {
103 + case AsPath.ASPATH_SET_TYPE:
104 + as4pathSet.add(asNum);
105 + break;
106 + case AsPath.ASPATH_SEQ_TYPE:
107 + as4pathSeq.add(asNum);
108 + break;
109 + default: log.debug("Other type Not Supported:" + pathSegType);
110 + }
111 + }
112 + }
113 + return new As4Path(as4pathSet, as4pathSeq);
114 + }
115 +
116 + @Override
117 + public short getType() {
118 + return AS4PATH_TYPE;
119 + }
120 +
121 + /**
122 + * Returns list of ASNum in AS4path Sequence.
123 + *
124 + * @return list of ASNum in AS4path Sequence
125 + */
126 + public List<Integer> as4PathSEQ() {
127 + return this.as4pathSeq;
128 + }
129 +
130 + /**
131 + * Returns list of ASNum in AS4path Set.
132 + *
133 + * @return list of ASNum in AS4path Set
134 + */
135 + public List<Integer> as4PathSET() {
136 + return this.as4pathSet;
137 + }
138 +
139 + @Override
140 + public int hashCode() {
141 + return Objects.hash(as4pathSet, as4pathSeq);
142 + }
143 +
144 + @Override
145 + public boolean equals(Object obj) {
146 + if (this == obj) {
147 + return true;
148 + }
149 + if (obj instanceof As4Path) {
150 + As4Path other = (As4Path) obj;
151 + return Objects.equals(as4pathSet, other.as4pathSet) && Objects.equals(as4pathSeq, other.as4pathSeq);
152 + }
153 + return false;
154 + }
155 +
156 + @Override
157 + public String toString() {
158 + return MoreObjects.toStringHelper(getClass())
159 + .omitNullValues()
160 + .add("as4pathSet", as4pathSet)
161 + .add("as4pathSeq", as4pathSeq)
162 + .toString();
163 + }
164 +
165 + @Override
166 + public int write(ChannelBuffer cb) {
167 + //Not required to Implement as of now
168 + return 0;
169 + }
170 +}
...\ No newline at end of file ...\ No newline at end of file
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 +
17 +package org.onosproject.bgpio.types;
18 +
19 +import java.util.ArrayList;
20 +import java.util.List;
21 +import java.util.Objects;
22 +
23 +import org.jboss.netty.buffer.ChannelBuffer;
24 +import org.onosproject.bgpio.exceptions.BGPParseException;
25 +import org.onosproject.bgpio.util.Validation;
26 +import org.slf4j.Logger;
27 +import org.slf4j.LoggerFactory;
28 +
29 +import com.google.common.base.MoreObjects;
30 +
31 +/**
32 + * Provides Implementation of AsPath mandatory BGP Path Attribute.
33 + */
34 +public class AsPath implements BGPValueType {
35 + /**
36 + * Enum to provide AS types.
37 + */
38 + public enum ASTYPE {
39 + AS_SET(1), AS_SEQUENCE(2), AS_CONFED_SEQUENCE(3), AS_CONFED_SET(4);
40 + int value;
41 +
42 + /**
43 + * Assign val with the value as the AS type.
44 + *
45 + * @param val AS type
46 + */
47 + ASTYPE(int val) {
48 + value = val;
49 + }
50 +
51 + /**
52 + * Returns value of AS type.
53 + *
54 + * @return AS type
55 + */
56 + public byte getType() {
57 + return (byte) value;
58 + }
59 + }
60 +
61 + private static final Logger log = LoggerFactory.getLogger(AsPath.class);
62 + public static final byte ASPATH_TYPE = 2;
63 + public static final byte ASPATH_SET_TYPE = 1;
64 + public static final byte ASPATH_SEQ_TYPE = 2;
65 + public static final byte ASNUM_SIZE = 2;
66 + public static final int TYPE_AND_LEN_AS_SHORT = 4;
67 + public static final int TYPE_AND_LEN_AS_BYTE = 3;
68 +
69 + private boolean isAsPath = false;
70 + private List<Short> aspathSet;
71 + private List<Short> aspathSeq;
72 +
73 + /**
74 + * Initialize Fields.
75 + */
76 + public AsPath() {
77 + this.aspathSeq = null;
78 + this.aspathSet = null;
79 + }
80 +
81 + /**
82 + * Constructor to initialize parameters.
83 + *
84 + * @param aspathSet ASpath Set type
85 + * @param aspathSeq ASpath Sequence type
86 + */
87 + public AsPath(List<Short> aspathSet, List<Short> aspathSeq) {
88 + this.aspathSeq = aspathSeq;
89 + this.aspathSet = aspathSet;
90 + this.isAsPath = true;
91 + }
92 +
93 + /**
94 + * Reads from the channel buffer and parses AsPath.
95 + *
96 + * @param cb ChannelBuffer
97 + * @return object of AsPath
98 + * @throws BGPParseException while parsing AsPath
99 + */
100 + public static AsPath read(ChannelBuffer cb) throws BGPParseException {
101 + List<Short> aspathSet = new ArrayList<>();
102 + List<Short> aspathSeq = new ArrayList<>();
103 + ChannelBuffer tempCb = cb.copy();
104 + Validation validation = Validation.parseAttributeHeader(cb);
105 +
106 + if (cb.readableBytes() < validation.getLength()) {
107 + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.ATTRIBUTE_LENGTH_ERROR,
108 + validation.getLength());
109 + }
110 + //if fourth bit is set, length is read as short otherwise as byte , len includes type, length and value
111 + int len = validation.isShort() ? validation.getLength() + TYPE_AND_LEN_AS_SHORT : validation
112 + .getLength() + TYPE_AND_LEN_AS_BYTE;
113 + ChannelBuffer data = tempCb.readBytes(len);
114 + if (validation.getFirstBit() && !validation.getSecondBit() && validation.getThirdBit()) {
115 + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.ATTRIBUTE_FLAGS_ERROR, data);
116 + }
117 +
118 + ChannelBuffer tempBuf = cb.readBytes(validation.getLength());
119 + while (tempBuf.readableBytes() > 0) {
120 + byte pathSegType = tempBuf.readByte();
121 + //no of ASes
122 + byte pathSegLen = tempBuf.readByte();
123 + int length = pathSegLen * ASNUM_SIZE;
124 + if (tempBuf.readableBytes() < length) {
125 + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR,
126 + BGPErrorType.ATTRIBUTE_LENGTH_ERROR, length);
127 + }
128 + ChannelBuffer aspathBuf = tempBuf.readBytes(length);
129 + while (aspathBuf.readableBytes() > 0) {
130 + short asNum;
131 + asNum = aspathBuf.readShort();
132 + switch (pathSegType) {
133 + case ASPATH_SET_TYPE:
134 + aspathSet.add(asNum);
135 + break;
136 + case ASPATH_SEQ_TYPE:
137 + aspathSeq.add(asNum);
138 + break;
139 + default: log.debug("Other type Not Supported:" + pathSegType);
140 + }
141 + }
142 + }
143 + return new AsPath(aspathSet, aspathSeq);
144 + }
145 +
146 + @Override
147 + public short getType() {
148 + return ASPATH_TYPE;
149 + }
150 +
151 + /**
152 + * Returns whether ASpath path attribute is present.
153 + *
154 + * @return whether ASpath path attribute is present
155 + */
156 + public boolean isaspathSet() {
157 + return this.isAsPath;
158 + }
159 +
160 + /**
161 + * Returns list of ASNum in ASpath Sequence.
162 + *
163 + * @return list of ASNum in ASpath Sequence
164 + */
165 + public List<Short> asPathSeq() {
166 + return this.aspathSeq;
167 + }
168 +
169 + /**
170 + * Returns list of ASNum in ASpath SET.
171 + *
172 + * @return list of ASNum in ASpath SET
173 + */
174 + public List<Short> asPathSet() {
175 + return this.aspathSet;
176 + }
177 +
178 + @Override
179 + public int hashCode() {
180 + return Objects.hash(aspathSet, aspathSeq);
181 + }
182 +
183 + @Override
184 + public boolean equals(Object obj) {
185 + if (this == obj) {
186 + return true;
187 + }
188 + if (obj instanceof AsPath) {
189 + AsPath other = (AsPath) obj;
190 + return Objects.equals(aspathSet, other.aspathSet) && Objects.equals(aspathSeq, other.aspathSeq);
191 + }
192 + return false;
193 + }
194 +
195 + @Override
196 + public String toString() {
197 + return MoreObjects.toStringHelper(getClass())
198 + .omitNullValues()
199 + .add("aspathSet", aspathSet)
200 + .add("aspathSeq", aspathSeq)
201 + .toString();
202 + }
203 +
204 + @Override
205 + public int write(ChannelBuffer cb) {
206 + //Not required to Implement as of now
207 + return 0;
208 + }
209 +}
...\ No newline at end of file ...\ No newline at end of file
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;
17 +
18 +import java.util.Objects;
19 +
20 +import org.jboss.netty.buffer.ChannelBuffer;
21 +import org.onosproject.bgpio.exceptions.BGPParseException;
22 +import org.onosproject.bgpio.util.Validation;
23 +import org.slf4j.Logger;
24 +import org.slf4j.LoggerFactory;
25 +
26 +import com.google.common.base.MoreObjects;
27 +
28 +/**
29 + * Provides implementation of LocalPref BGP Path Attribute.
30 + */
31 +public class LocalPref implements BGPValueType {
32 +
33 + private static final Logger log = LoggerFactory.getLogger(LocalPref.class);
34 + public static final byte LOCAL_PREF_TYPE = 5;
35 + public static final int TYPE_AND_LEN_AS_SHORT = 4;
36 + public static final int TYPE_AND_LEN_AS_BYTE = 3;
37 + public static final byte LOCAL_PREF_MAX_LEN = 4;
38 +
39 + private int localPref;
40 +
41 + /**
42 + * Constructor to initialize LocalPref.
43 + *
44 + * @param localPref local preference
45 + */
46 + public LocalPref(int localPref) {
47 + this.localPref = localPref;
48 + }
49 +
50 + /**
51 + * Returns local preference value.
52 + *
53 + * @return local preference value
54 + */
55 + public int localPref() {
56 + return this.localPref;
57 + }
58 +
59 + /**
60 + * Reads the channel buffer and returns object of LocalPref.
61 + *
62 + * @param cb channelBuffer
63 + * @return object of LocalPref
64 + * @throws BGPParseException while parsing localPref attribute
65 + */
66 + public static LocalPref read(ChannelBuffer cb) throws BGPParseException {
67 + int localPref;
68 + ChannelBuffer tempCb = cb.copy();
69 + Validation parseFlags = Validation.parseAttributeHeader(cb);
70 + if ((parseFlags.getLength() > LOCAL_PREF_MAX_LEN) || cb.readableBytes() < parseFlags.getLength()) {
71 + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.ATTRIBUTE_LENGTH_ERROR,
72 + parseFlags.getLength());
73 + }
74 +
75 + int len = parseFlags.isShort() ? parseFlags.getLength() + TYPE_AND_LEN_AS_SHORT : parseFlags.getLength()
76 + + TYPE_AND_LEN_AS_BYTE;
77 + ChannelBuffer data = tempCb.readBytes(len);
78 + if (parseFlags.getFirstBit()) {
79 + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.ATTRIBUTE_FLAGS_ERROR, data);
80 + }
81 +
82 + localPref = cb.readInt();
83 + return new LocalPref(localPref);
84 + }
85 +
86 + @Override
87 + public short getType() {
88 + return LOCAL_PREF_TYPE;
89 + }
90 +
91 + @Override
92 + public int hashCode() {
93 + return Objects.hash(localPref);
94 + }
95 +
96 + @Override
97 + public boolean equals(Object obj) {
98 + if (this == obj) {
99 + return true;
100 + }
101 + if (obj instanceof LocalPref) {
102 + LocalPref other = (LocalPref) obj;
103 + return Objects.equals(localPref, other.localPref);
104 + }
105 + return false;
106 + }
107 +
108 + @Override
109 + public String toString() {
110 + return MoreObjects.toStringHelper(getClass())
111 + .add("localPref", localPref)
112 + .toString();
113 + }
114 +
115 + @Override
116 + public int write(ChannelBuffer cb) {
117 + //Not to implement as of now
118 + return 0;
119 + }
120 +}
...\ No newline at end of file ...\ No newline at end of file
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;
17 +
18 +import java.util.Objects;
19 +
20 +import org.jboss.netty.buffer.ChannelBuffer;
21 +import org.onosproject.bgpio.exceptions.BGPParseException;
22 +import org.onosproject.bgpio.util.Validation;
23 +import org.slf4j.Logger;
24 +import org.slf4j.LoggerFactory;
25 +
26 +import com.google.common.base.MoreObjects;
27 +
28 +/**
29 + * Provides Implementation of Med BGP Path Attribute.
30 + */
31 +public class Med implements BGPValueType {
32 + private static final Logger log = LoggerFactory.getLogger(Med.class);
33 + public static final byte MED_TYPE = 4;
34 + public static final int TYPE_AND_LEN_AS_SHORT = 4;
35 + public static final int TYPE_AND_LEN_AS_BYTE = 3;
36 + public static final byte MED_MAX_LEN = 4;
37 +
38 + private int med;
39 +
40 + /**
41 + * Constructor to initialize med.
42 + *
43 + * @param med MULTI_EXIT_DISC value
44 + */
45 + public Med(int med) {
46 + this.med = med;
47 + }
48 +
49 + /**
50 + * Returns Med value.
51 + *
52 + * @return Med value
53 + */
54 + public int med() {
55 + return this.med;
56 + }
57 +
58 + /**
59 + * Reads the channel buffer and returns object of Med.
60 + *
61 + * @param cb ChannelBuffer
62 + * @return object of Med
63 + * @throws BGPParseException while parsing Med path attribute
64 + */
65 + public static Med read(ChannelBuffer cb) throws BGPParseException {
66 + int med;
67 + ChannelBuffer tempCb = cb.copy();
68 + Validation parseFlags = Validation.parseAttributeHeader(cb);
69 +
70 + if ((parseFlags.getLength() > MED_MAX_LEN) || cb.readableBytes() < parseFlags.getLength()) {
71 + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.ATTRIBUTE_LENGTH_ERROR,
72 + parseFlags.getLength());
73 + }
74 + int len = parseFlags.isShort() ? parseFlags.getLength() + TYPE_AND_LEN_AS_SHORT : parseFlags
75 + .getLength() + TYPE_AND_LEN_AS_BYTE;
76 + ChannelBuffer data = tempCb.readBytes(len);
77 + if (!parseFlags.getFirstBit() && parseFlags.getSecondBit() && parseFlags.getThirdBit()) {
78 + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.ATTRIBUTE_FLAGS_ERROR, data);
79 + }
80 +
81 + med = cb.readInt();
82 + return new Med(med);
83 + }
84 +
85 + @Override
86 + public short getType() {
87 + return MED_TYPE;
88 + }
89 +
90 + @Override
91 + public int hashCode() {
92 + return Objects.hash(med);
93 + }
94 +
95 + @Override
96 + public boolean equals(Object obj) {
97 + if (this == obj) {
98 + return true;
99 + }
100 + if (obj instanceof Med) {
101 + Med other = (Med) obj;
102 + return Objects.equals(med, other.med);
103 + }
104 + return false;
105 + }
106 +
107 + @Override
108 + public String toString() {
109 + return MoreObjects.toStringHelper(getClass())
110 + .add("med", med)
111 + .toString();
112 + }
113 +
114 + @Override
115 + public int write(ChannelBuffer cb) {
116 + //Not to implement as of now
117 + return 0;
118 + }
119 +}
...\ No newline at end of file ...\ No newline at end of file
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;
17 +
18 +import java.net.InetAddress;
19 +import java.util.Objects;
20 +
21 +import org.jboss.netty.buffer.ChannelBuffer;
22 +import org.onlab.packet.Ip4Address;
23 +import org.onosproject.bgpio.exceptions.BGPParseException;
24 +import org.onosproject.bgpio.util.Validation;
25 +import org.slf4j.Logger;
26 +import org.slf4j.LoggerFactory;
27 +
28 +import com.google.common.base.MoreObjects;
29 +import com.google.common.base.Preconditions;
30 +
31 +/**
32 + * Implementation of NextHop BGP Path Attribute.
33 + */
34 +public class NextHop implements BGPValueType {
35 + private static final Logger log = LoggerFactory.getLogger(NextHop.class);
36 + public static final byte NEXTHOP_TYPE = 3;
37 + public static final int TYPE_AND_LEN_AS_SHORT = 4;
38 + public static final int TYPE_AND_LEN_AS_BYTE = 3;
39 +
40 + private boolean isNextHop = false;
41 + private Ip4Address nextHop;
42 +
43 + /**
44 + * Constructor to initialize parameters.
45 + *
46 + * @param nextHop nextHop address
47 + */
48 + public NextHop(Ip4Address nextHop) {
49 + this.nextHop = Preconditions.checkNotNull(nextHop);
50 + this.isNextHop = true;
51 + }
52 +
53 + /**
54 + * Returns whether next hop is present.
55 + *
56 + * @return whether next hop is present
57 + */
58 + public boolean isNextHopSet() {
59 + return this.isNextHop;
60 + }
61 +
62 + /**
63 + * Reads from ChannelBuffer and parses NextHop.
64 + *
65 + * @param cb ChannelBuffer
66 + * @return object of NextHop
67 + * @throws BGPParseException while parsing nexthop attribute
68 + */
69 + public static NextHop read(ChannelBuffer cb) throws BGPParseException {
70 + Ip4Address nextHop;
71 + ChannelBuffer tempCb = cb.copy();
72 + Validation parseFlags = Validation.parseAttributeHeader(cb);
73 +
74 + if (cb.readableBytes() < parseFlags.getLength()) {
75 + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.ATTRIBUTE_LENGTH_ERROR,
76 + parseFlags.getLength());
77 + }
78 + int len = parseFlags.isShort() ? parseFlags.getLength() + TYPE_AND_LEN_AS_SHORT : parseFlags
79 + .getLength() + TYPE_AND_LEN_AS_BYTE;
80 + ChannelBuffer data = tempCb.readBytes(len);
81 + if (parseFlags.getFirstBit() && !parseFlags.getSecondBit() && parseFlags.getThirdBit()) {
82 + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.ATTRIBUTE_FLAGS_ERROR, data);
83 + }
84 +
85 + //TODO: use Validation.toInetAddress once Validation is merged
86 + InetAddress ipAddress = (InetAddress) cb.readBytes(parseFlags.getLength());
87 + if (ipAddress.isMulticastAddress()) {
88 + throw new BGPParseException("Multicast address is not supported");
89 + }
90 +
91 + nextHop = Ip4Address.valueOf(ipAddress);
92 + return new NextHop(nextHop);
93 + }
94 +
95 + /**
96 + * Return nexthop address.
97 + *
98 + * @return nexthop address
99 + */
100 + public Ip4Address nextHop() {
101 + return nextHop;
102 + }
103 +
104 + @Override
105 + public short getType() {
106 + return NEXTHOP_TYPE;
107 + }
108 +
109 + @Override
110 + public int write(ChannelBuffer cb) {
111 + //Not required to be implemented now
112 + return 0;
113 + }
114 +
115 + @Override
116 + public int hashCode() {
117 + return Objects.hash(nextHop);
118 + }
119 +
120 + @Override
121 + public boolean equals(Object obj) {
122 + if (this == obj) {
123 + return true;
124 + }
125 + if (obj instanceof NextHop) {
126 + NextHop other = (NextHop) obj;
127 + return Objects.equals(nextHop, other.nextHop);
128 + }
129 + return false;
130 + }
131 +
132 + @Override
133 + public String toString() {
134 + return MoreObjects.toStringHelper(getClass())
135 + .add("nextHop", nextHop)
136 + .toString();
137 + }
138 +}
...\ No newline at end of file ...\ No newline at end of file
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;
17 +
18 +import java.util.Objects;
19 +
20 +import org.jboss.netty.buffer.ChannelBuffer;
21 +import org.onosproject.bgpio.exceptions.BGPParseException;
22 +import org.onosproject.bgpio.util.Validation;
23 +import org.slf4j.Logger;
24 +import org.slf4j.LoggerFactory;
25 +
26 +import com.google.common.base.MoreObjects;
27 +
28 +/**
29 + * Provides Implementation of mandatory BGP Origin path attribute.
30 + */
31 +public class Origin implements BGPValueType {
32 + private static final Logger log = LoggerFactory.getLogger(Origin.class);
33 +
34 + /**
35 + * Enum to provide ORIGIN types.
36 + */
37 + public enum ORIGINTYPE {
38 + IGP(0), EGP(1), INCOMPLETE(2);
39 + int value;
40 + /**
41 + * Assign val with the value as the ORIGIN type.
42 + *
43 + * @param val ORIGIN type
44 + */
45 + ORIGINTYPE(int val) {
46 + value = val;
47 + }
48 +
49 + /**
50 + * Returns value of ORIGIN type.
51 + *
52 + * @return ORIGIN type
53 + */
54 + public byte getType() {
55 + return (byte) value;
56 + }
57 + }
58 +
59 + public static final byte ORIGIN_TYPE = 1;
60 + public static final byte ORIGIN_VALUE_LEN = 1;
61 + public static final int TYPE_AND_LEN_AS_SHORT = 4;
62 + public static final int TYPE_AND_LEN_AS_BYTE = 3;
63 +
64 + private boolean isOrigin = false;
65 + private byte origin;
66 +
67 + /**
68 + * Constructor to initialize parameters.
69 + *
70 + * @param origin origin value
71 + */
72 + public Origin(byte origin) {
73 + this.origin = origin;
74 + this.isOrigin = true;
75 + }
76 +
77 + /**
78 + * Returns true if origin attribute is present otherwise false.
79 + *
80 + * @return whether origin is present or not
81 + */
82 + public boolean isOriginSet() {
83 + return this.isOrigin;
84 + }
85 +
86 + /**
87 + * Returns type of Origin in Enum values.
88 + *
89 + * @return type of Origin in Enum values
90 + */
91 + public ORIGINTYPE origin() {
92 + if (this.origin == 0) {
93 + return ORIGINTYPE.IGP;
94 + } else if (this.origin == 1) {
95 + return ORIGINTYPE.EGP;
96 + } else {
97 + return ORIGINTYPE.INCOMPLETE;
98 + }
99 + }
100 +
101 + /**
102 + * Reads from ChannelBuffer and parses Origin.
103 + *
104 + * @param cb ChannelBuffer
105 + * @return object of Origin
106 + * @throws BGPParseException while parsing Origin path attribute
107 + */
108 + public static Origin read(ChannelBuffer cb) throws BGPParseException {
109 + ChannelBuffer tempCb = cb.copy();
110 + Validation parseFlags = Validation.parseAttributeHeader(cb);
111 +
112 + int len = parseFlags.isShort() ? parseFlags.getLength() + TYPE_AND_LEN_AS_SHORT : parseFlags
113 + .getLength() + TYPE_AND_LEN_AS_BYTE;
114 + ChannelBuffer data = tempCb.readBytes(len);
115 + if ((parseFlags.getLength() > ORIGIN_VALUE_LEN) || (cb.readableBytes() < parseFlags.getLength())) {
116 + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.ATTRIBUTE_LENGTH_ERROR,
117 + parseFlags.getLength());
118 + }
119 + if (parseFlags.getFirstBit() && !parseFlags.getSecondBit() && parseFlags.getThirdBit()) {
120 + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.ATTRIBUTE_FLAGS_ERROR, data);
121 + }
122 +
123 + byte originValue;
124 + originValue = cb.readByte();
125 + if ((originValue != ORIGINTYPE.INCOMPLETE.value) || (originValue != ORIGINTYPE.IGP.value) ||
126 + (originValue != ORIGINTYPE.EGP.value)) {
127 + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.INVALID_ORIGIN_ATTRIBUTE, data);
128 + }
129 + return new Origin(originValue);
130 + }
131 +
132 + @Override
133 + public short getType() {
134 + return ORIGIN_TYPE;
135 + }
136 +
137 + @Override
138 + public int write(ChannelBuffer cb) {
139 + //Not required to Implement as of now
140 + return 0;
141 + }
142 +
143 + @Override
144 + public int hashCode() {
145 + return Objects.hash(origin);
146 + }
147 +
148 + @Override
149 + public boolean equals(Object obj) {
150 + if (this == obj) {
151 + return true;
152 + }
153 + if (obj instanceof Origin) {
154 + Origin other = (Origin) obj;
155 + return Objects.equals(origin, other.origin);
156 + }
157 + return false;
158 + }
159 +
160 + @Override
161 + public String toString() {
162 + return MoreObjects.toStringHelper(getClass())
163 + .add("origin", origin)
164 + .toString();
165 + }
166 +}
...\ No newline at end of file ...\ No newline at end of file