blob: 8882936bbb8ef430f8337a98e1db6349d68374d8 [file] [log] [blame]
Thejaswi N K65800e12015-10-15 17:31:30 +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.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
Thejaswi N K65800e12015-10-15 17:31:30 +0530110 */
Priyanka B0ab34b92015-12-03 21:13:52 +0530111 public ProtectionType protectionType() throws BgpParseException {
112 switch (linkProtectionType) {
113 case Constants.EXTRA_TRAFFIC:
114 return ProtectionType.EXTRA_TRAFFIC;
115 case Constants.UNPROTECTED:
116 return ProtectionType.UNPROTECTED;
117 case Constants.SHARED:
118 return ProtectionType.SHARED;
119 case Constants.DEDICATED_ONE_ISTO_ONE:
120 return ProtectionType.DEDICATED_ONE_ISTO_ONE;
121 case Constants.DEDICATED_ONE_PLUS_ONE:
122 return ProtectionType.DEDICATED_ONE_PLUS_ONE;
123 case Constants.ENHANCED:
124 return ProtectionType.ENHANCED;
125 case Constants.RESERVED:
126 return ProtectionType.RESERVED;
127 default:
128 throw new BgpParseException("Got another type " + linkProtectionType);
129 }
Thejaswi N K65800e12015-10-15 17:31:30 +0530130 }
131
132 @Override
133 public short getType() {
134 return ATTRLINK_PROTECTIONTYPE;
135 }
136
137 @Override
138 public int hashCode() {
Priyanka B0ab34b92015-12-03 21:13:52 +0530139 return Objects.hash(linkProtectionType);
Thejaswi N K65800e12015-10-15 17:31:30 +0530140 }
141
142 @Override
143 public boolean equals(Object obj) {
144 if (this == obj) {
145 return true;
146 }
147
148 if (obj instanceof BgpLinkAttrProtectionType) {
149 BgpLinkAttrProtectionType other = (BgpLinkAttrProtectionType) obj;
Priyanka B0ab34b92015-12-03 21:13:52 +0530150 return Objects.equals(linkProtectionType, other.linkProtectionType);
Thejaswi N K65800e12015-10-15 17:31:30 +0530151 }
152 return false;
153 }
154
155 @Override
156 public int write(ChannelBuffer cb) {
157 // TODO This will be implemented in the next version
158 return 0;
159 }
160
161 @Override
162 public String toString() {
163 return MoreObjects.toStringHelper(getClass())
Priyanka B0ab34b92015-12-03 21:13:52 +0530164 .add("linkProtectionType", linkProtectionType)
165 .toString();
Thejaswi N K65800e12015-10-15 17:31:30 +0530166 }
Priyanka B02040732015-11-29 11:30:29 +0530167
168 @Override
169 public int compareTo(Object o) {
170 // TODO Auto-generated method stub
171 return 0;
172 }
Thejaswi N K65800e12015-10-15 17:31:30 +0530173}