258598be90262b8c54d3864150431b7362c82c53
[onosfw.git] /
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.Arrays;
19
20 import org.jboss.netty.buffer.ChannelBuffer;
21 import org.onosproject.bgpio.exceptions.BGPParseException;
22 import org.onosproject.bgpio.types.BGPErrorType;
23 import org.onosproject.bgpio.types.BGPValueType;
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  * Implements BGP link opaque attribute.
32  */
33 public final class BgpLinkAttrOpaqLnkAttrib implements BGPValueType {
34
35     protected static final Logger log = LoggerFactory
36             .getLogger(BgpLinkAttrOpaqLnkAttrib.class);
37
38     public static final int ATTRNODE_OPAQUELNKATTRIB = 1097;
39
40     /* Opaque Node Attribute */
41     private final byte[] opaqueLinkAttribute;
42
43     /**
44      * Constructor to initialize the data.
45      *
46      * @param opaqueLinkAttribute opaque link attribute
47      */
48     private BgpLinkAttrOpaqLnkAttrib(byte[] opaqueLinkAttribute) {
49         this.opaqueLinkAttribute = Arrays.copyOf(opaqueLinkAttribute,
50                                                  opaqueLinkAttribute.length);
51     }
52
53     /**
54      * Returns object of this class with specified values.
55      *
56      * @param opaqueLinkAttribute opaque link attribute
57      * @return object of BgpLinkAttrOpaqLnkAttrib
58      */
59     public static BgpLinkAttrOpaqLnkAttrib of(final byte[] opaqueLinkAttribute) {
60         return new BgpLinkAttrOpaqLnkAttrib(opaqueLinkAttribute);
61     }
62
63     /**
64      * Reads the BGP link attributes Opaque link attribute.
65      *
66      * @param cb Channel buffer
67      * @return object of type BgpLinkAttrOpaqLnkAttrib
68      * @throws BGPParseException while parsing BgpLinkAttrOpaqLnkAttrib
69      */
70     public static BgpLinkAttrOpaqLnkAttrib read(ChannelBuffer cb)
71             throws BGPParseException {
72
73         byte[] opaqueLinkAttribute;
74
75         short lsAttrLength = cb.readShort();
76
77         if (cb.readableBytes() < lsAttrLength) {
78             Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR,
79                                    BGPErrorType.ATTRIBUTE_LENGTH_ERROR,
80                                    lsAttrLength);
81         }
82
83         opaqueLinkAttribute = new byte[lsAttrLength];
84         cb.readBytes(opaqueLinkAttribute);
85
86         return BgpLinkAttrOpaqLnkAttrib.of(opaqueLinkAttribute);
87     }
88
89     /**
90      * Returns the Opaque link attribute.
91      *
92      * @return byte array of opaque link attribute.
93      */
94     public byte[] attrOpaqueLnk() {
95         return opaqueLinkAttribute;
96     }
97
98     @Override
99     public short getType() {
100         return ATTRNODE_OPAQUELNKATTRIB;
101     }
102
103     @Override
104     public int hashCode() {
105         return Arrays.hashCode(opaqueLinkAttribute);
106     }
107
108     @Override
109     public boolean equals(Object obj) {
110         if (this == obj) {
111             return true;
112         }
113
114         if (obj instanceof BgpLinkAttrOpaqLnkAttrib) {
115             BgpLinkAttrOpaqLnkAttrib other = (BgpLinkAttrOpaqLnkAttrib) obj;
116             return Arrays
117                     .equals(opaqueLinkAttribute, other.opaqueLinkAttribute);
118         }
119         return false;
120     }
121
122     @Override
123     public int write(ChannelBuffer cb) {
124         // TODO This will be implemented in the next version
125         return 0;
126     }
127
128     @Override
129     public String toString() {
130         return MoreObjects.toStringHelper(getClass()).omitNullValues()
131                 .add("opaqueLinkAttribute", opaqueLinkAttribute).toString();
132     }
133 }