blob: caf73a0b243d1d07fd6d4d2cde1d66eb3dfafcc7 [file] [log] [blame]
Priyanka Bb2988fa2015-10-09 12:45:36 +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 */
Priyanka Bb2988fa2015-10-09 12:45:36 +053016package org.onosproject.bgpio.types.attr;
17
Thejaswi NK380fa1b2015-11-18 17:50:51 +053018import java.util.ArrayList;
19import java.util.List;
Priyanka Bb2988fa2015-10-09 12:45:36 +053020import java.util.Objects;
21
22import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053023import org.onosproject.bgpio.exceptions.BgpParseException;
24import org.onosproject.bgpio.types.BgpErrorType;
25import org.onosproject.bgpio.types.BgpValueType;
Priyanka Bb2988fa2015-10-09 12:45:36 +053026import org.onosproject.bgpio.util.Validation;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import com.google.common.base.MoreObjects;
31
32/**
33 * BGP Multi-Topology ID of the LS attribute.
34 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053035public class BgpAttrNodeMultiTopologyId implements BgpValueType {
Priyanka Bb2988fa2015-10-09 12:45:36 +053036
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 */
Thejaswi NK380fa1b2015-11-18 17:50:51 +053043 private List<Short> multiTopologyId = new ArrayList<Short>();
Priyanka Bb2988fa2015-10-09 12:45:36 +053044
45 /**
46 * Constructor to initialize the Node attribute multi-topology ID.
47 *
48 * @param multiTopologyId multi-topology ID
49 */
Thejaswi NK380fa1b2015-11-18 17:50:51 +053050 public BgpAttrNodeMultiTopologyId(List<Short> multiTopologyId) {
Priyanka Bb2988fa2015-10-09 12:45:36 +053051 this.multiTopologyId = multiTopologyId;
52 }
53
54 /**
Thejaswi NK380fa1b2015-11-18 17:50:51 +053055 * 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 /**
Priyanka Bb2988fa2015-10-09 12:45:36 +053065 * Reads the Multi-topology ID of Node attribute.
66 *
67 * @param cb ChannelBuffer
68 * @return Constructor of BgpAttrNodeMultiTopologyId
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053069 * @throws BgpParseException while parsing BgpAttrNodeMultiTopologyId
Priyanka Bb2988fa2015-10-09 12:45:36 +053070 */
71 public static BgpAttrNodeMultiTopologyId read(ChannelBuffer cb)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053072 throws BgpParseException {
Thejaswi NK380fa1b2015-11-18 17:50:51 +053073 ArrayList<Short> multiTopologyId = new ArrayList<Short>();
74 short tempMultiTopologyId;
Priyanka Bb2988fa2015-10-09 12:45:36 +053075 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) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053079 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
80 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Thejaswi NK380fa1b2015-11-18 17:50:51 +053081 lsAttrLength);
Priyanka Bb2988fa2015-10-09 12:45:36 +053082 }
83
Priyanka Bb2988fa2015-10-09 12:45:36 +053084 for (int i = 0; i < len; i++) {
Thejaswi NK380fa1b2015-11-18 17:50:51 +053085 tempMultiTopologyId = cb.readShort();
86 multiTopologyId.add(new Short(tempMultiTopologyId));
Priyanka Bb2988fa2015-10-09 12:45:36 +053087 }
88
89 return new BgpAttrNodeMultiTopologyId(multiTopologyId);
90 }
91
92 /**
93 * to get the multi-topology ID.
94 *
95 * @return multitopology ID
96 */
Thejaswi NK380fa1b2015-11-18 17:50:51 +053097 public List<Short> attrMultiTopologyId() {
Priyanka Bb2988fa2015-10-09 12:45:36 +053098 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) {
Thejaswi NK380fa1b2015-11-18 17:50:51 +0530126 // TODO This will be implemented in the next version
Priyanka Bb2988fa2015-10-09 12:45:36 +0530127 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}