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