blob: ec57f954a197e540e405053899cae5ef47882e30 [file] [log] [blame]
Thejaswi N K65800e12015-10-15 17:31:30 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thejaswi N K65800e12015-10-15 17:31:30 +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.Objects;
19
20import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053021import org.onosproject.bgpio.exceptions.BgpParseException;
22import org.onosproject.bgpio.types.BgpErrorType;
23import org.onosproject.bgpio.types.BgpValueType;
Priyanka B0ab34b92015-12-03 21:13:52 +053024import org.onosproject.bgpio.util.Constants;
Thejaswi N K65800e12015-10-15 17:31:30 +053025import org.onosproject.bgpio.util.Validation;
Thejaswi N K65800e12015-10-15 17:31:30 +053026
27import com.google.common.base.MoreObjects;
28
29/**
30 * Implements BGP link protection type attribute.
31 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053032public final class BgpLinkAttrProtectionType implements BgpValueType {
Thejaswi N K65800e12015-10-15 17:31:30 +053033 public static final int ATTRLINK_PROTECTIONTYPE = 1093;
34 public static final int LINK_PROTECTION_LEN = 2;
35
Priyanka B0ab34b92015-12-03 21:13:52 +053036 private byte linkProtectionType;
Thejaswi N K65800e12015-10-15 17:31:30 +053037
Priyanka B0ab34b92015-12-03 21:13:52 +053038 /**
39 * Enum to provide Link protection types.
40 */
41 public enum ProtectionType {
42 EXTRA_TRAFFIC(1), UNPROTECTED(2), SHARED(4), DEDICATED_ONE_ISTO_ONE(8),
43 DEDICATED_ONE_PLUS_ONE(0x10), ENHANCED(0x20), RESERVED(0x40);
44 int value;
45
46 /**
47 * Assign val with the value as the link protection type.
48 *
49 * @param val link protection
50 */
51 ProtectionType(int val) {
52 value = val;
53 }
54
55 /**
56 * Returns value of link protection type.
57 *
58 * @return link protection type
59 */
60 public byte type() {
61 return (byte) value;
62 }
63 }
Thejaswi N K65800e12015-10-15 17:31:30 +053064
65 /**
66 * Constructor to initialize the value.
67 *
Priyanka B0ab34b92015-12-03 21:13:52 +053068 * @param linkProtectionType link protection type
Thejaswi N K65800e12015-10-15 17:31:30 +053069 */
Priyanka B0ab34b92015-12-03 21:13:52 +053070 public BgpLinkAttrProtectionType(byte linkProtectionType) {
71 this.linkProtectionType = linkProtectionType;
Thejaswi N K65800e12015-10-15 17:31:30 +053072 }
73
74 /**
75 * Returns object of this class with specified values.
76 *
Priyanka B0ab34b92015-12-03 21:13:52 +053077 * @param linkProtectionType link protection type
Thejaswi N K65800e12015-10-15 17:31:30 +053078 * @return object of BgpLinkAttrProtectionType
79 */
Priyanka B0ab34b92015-12-03 21:13:52 +053080 public static BgpLinkAttrProtectionType of(byte linkProtectionType) {
81 return new BgpLinkAttrProtectionType(linkProtectionType);
Thejaswi N K65800e12015-10-15 17:31:30 +053082 }
83
84 /**
85 * Reads the BGP link attributes protection type.
86 *
87 * @param cb Channel buffer
88 * @return object of type BgpLinkAttrProtectionType
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053089 * @throws BgpParseException while parsing BgpLinkAttrProtectionType
Thejaswi N K65800e12015-10-15 17:31:30 +053090 */
91 public static BgpLinkAttrProtectionType read(ChannelBuffer cb)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053092 throws BgpParseException {
Thejaswi N K65800e12015-10-15 17:31:30 +053093 short lsAttrLength = cb.readShort();
94
Priyanka B0ab34b92015-12-03 21:13:52 +053095 if ((lsAttrLength != LINK_PROTECTION_LEN) || (cb.readableBytes() < lsAttrLength)) {
96 Validation
97 .validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, lsAttrLength);
Thejaswi N K65800e12015-10-15 17:31:30 +053098 }
99
Priyanka B0ab34b92015-12-03 21:13:52 +0530100 byte linkProtectionType = cb.readByte();
101 byte reserved = cb.readByte();
Thejaswi N K65800e12015-10-15 17:31:30 +0530102
Priyanka B0ab34b92015-12-03 21:13:52 +0530103 return BgpLinkAttrProtectionType.of(linkProtectionType);
Thejaswi N K65800e12015-10-15 17:31:30 +0530104 }
105
106 /**
Priyanka B0ab34b92015-12-03 21:13:52 +0530107 * Returns Link Protection Type.
Thejaswi N K65800e12015-10-15 17:31:30 +0530108 *
Priyanka B0ab34b92015-12-03 21:13:52 +0530109 * @return Link Protection Type
Bharat saraswal6c886952015-12-11 01:43:11 +0530110 * @throws BgpParseException when failed to parse link protection type
Thejaswi N K65800e12015-10-15 17:31:30 +0530111 */
Priyanka B0ab34b92015-12-03 21:13:52 +0530112 public ProtectionType protectionType() throws BgpParseException {
113 switch (linkProtectionType) {
114 case Constants.EXTRA_TRAFFIC:
115 return ProtectionType.EXTRA_TRAFFIC;
116 case Constants.UNPROTECTED:
117 return ProtectionType.UNPROTECTED;
118 case Constants.SHARED:
119 return ProtectionType.SHARED;
120 case Constants.DEDICATED_ONE_ISTO_ONE:
121 return ProtectionType.DEDICATED_ONE_ISTO_ONE;
122 case Constants.DEDICATED_ONE_PLUS_ONE:
123 return ProtectionType.DEDICATED_ONE_PLUS_ONE;
124 case Constants.ENHANCED:
125 return ProtectionType.ENHANCED;
126 case Constants.RESERVED:
127 return ProtectionType.RESERVED;
128 default:
129 throw new BgpParseException("Got another type " + linkProtectionType);
130 }
Thejaswi N K65800e12015-10-15 17:31:30 +0530131 }
132
133 @Override
134 public short getType() {
135 return ATTRLINK_PROTECTIONTYPE;
136 }
137
138 @Override
139 public int hashCode() {
Priyanka B0ab34b92015-12-03 21:13:52 +0530140 return Objects.hash(linkProtectionType);
Thejaswi N K65800e12015-10-15 17:31:30 +0530141 }
142
143 @Override
144 public boolean equals(Object obj) {
145 if (this == obj) {
146 return true;
147 }
148
149 if (obj instanceof BgpLinkAttrProtectionType) {
150 BgpLinkAttrProtectionType other = (BgpLinkAttrProtectionType) obj;
Priyanka B0ab34b92015-12-03 21:13:52 +0530151 return Objects.equals(linkProtectionType, other.linkProtectionType);
Thejaswi N K65800e12015-10-15 17:31:30 +0530152 }
153 return false;
154 }
155
156 @Override
157 public int write(ChannelBuffer cb) {
158 // TODO This will be implemented in the next version
159 return 0;
160 }
161
162 @Override
163 public String toString() {
164 return MoreObjects.toStringHelper(getClass())
Priyanka B0ab34b92015-12-03 21:13:52 +0530165 .add("linkProtectionType", linkProtectionType)
166 .toString();
Thejaswi N K65800e12015-10-15 17:31:30 +0530167 }
Priyanka B02040732015-11-29 11:30:29 +0530168
169 @Override
170 public int compareTo(Object o) {
171 // TODO Auto-generated method stub
172 return 0;
173 }
Thejaswi N K65800e12015-10-15 17:31:30 +0530174}