4b704fb02ae4c53b6017a6db31f7d57d37562bca
[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.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 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import com.google.common.base.MoreObjects;
31
32 /**
33  * BGP Multi-Topology ID of the LS attribute.
34  */
35 public class BgpAttrNodeMultiTopologyId implements BGPValueType {
36
37     private static final Logger log = LoggerFactory
38             .getLogger(BgpAttrNodeMultiTopologyId.class);
39
40     public static final int ATTRNODE_MULTITOPOLOGY = 263;
41
42     /* Opaque Node Attribute */
43     private List<Short> multiTopologyId = new ArrayList<Short>();
44
45     /**
46      * Constructor to initialize the Node attribute multi-topology ID.
47      *
48      * @param multiTopologyId multi-topology ID
49      */
50     public BgpAttrNodeMultiTopologyId(List<Short> multiTopologyId) {
51         this.multiTopologyId = multiTopologyId;
52     }
53
54     /**
55      * Returns object of this class with specified values.
56      *
57      * @param multiTopologyId Prefix Metric
58      * @return object of BgpAttrNodeMultiTopologyId
59      */
60     public static BgpAttrNodeMultiTopologyId of(ArrayList<Short> multiTopologyId) {
61         return new BgpAttrNodeMultiTopologyId(multiTopologyId);
62     }
63
64     /**
65      * Reads the Multi-topology ID of Node attribute.
66      *
67      * @param cb ChannelBuffer
68      * @return Constructor of BgpAttrNodeMultiTopologyId
69      * @throws BGPParseException while parsing BgpAttrNodeMultiTopologyId
70      */
71     public static BgpAttrNodeMultiTopologyId read(ChannelBuffer cb)
72             throws BGPParseException {
73         ArrayList<Short> multiTopologyId = new ArrayList<Short>();
74         short tempMultiTopologyId;
75         short lsAttrLength = cb.readShort();
76         int len = lsAttrLength / 2; // Length is 2*n and n is the number of MT-IDs
77
78         if (cb.readableBytes() < lsAttrLength) {
79             Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR,
80                                    BGPErrorType.ATTRIBUTE_LENGTH_ERROR,
81                                    lsAttrLength);
82         }
83
84         for (int i = 0; i < len; i++) {
85             tempMultiTopologyId = cb.readShort();
86             multiTopologyId.add(new Short(tempMultiTopologyId));
87         }
88
89         return new BgpAttrNodeMultiTopologyId(multiTopologyId);
90     }
91
92     /**
93      * to get the multi-topology ID.
94      *
95      * @return multitopology ID
96      */
97     public List<Short> attrMultiTopologyId() {
98         return multiTopologyId;
99     }
100
101     @Override
102     public short getType() {
103         return ATTRNODE_MULTITOPOLOGY;
104     }
105
106     @Override
107     public int hashCode() {
108         return Objects.hash(multiTopologyId);
109     }
110
111     @Override
112     public boolean equals(Object obj) {
113         if (this == obj) {
114             return true;
115         }
116
117         if (obj instanceof BgpAttrNodeMultiTopologyId) {
118             BgpAttrNodeMultiTopologyId other = (BgpAttrNodeMultiTopologyId) obj;
119             return Objects.equals(multiTopologyId, other.multiTopologyId);
120         }
121         return false;
122     }
123
124     @Override
125     public int write(ChannelBuffer cb) {
126         // TODO This will be implemented in the next version
127         return 0;
128     }
129
130     @Override
131     public String toString() {
132         return MoreObjects.toStringHelper(getClass())
133                 .omitNullValues()
134                 .add("multiTopologyId", multiTopologyId)
135                 .toString();
136     }
137 }