blob: 2d48788505af0aa699653a747474260f88a04c4c [file] [log] [blame]
Mohammad Shahid30fedc52017-08-09 11:49:40 +05301/*
2 * Copyright 2017-present Open Networking Foundation
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
17package org.onosproject.bgpio.types;
18
19import com.google.common.base.MoreObjects;
Yuta HIGUCHI1edc36b2018-01-24 23:39:06 -080020
21import java.util.Arrays;
22import java.util.Objects;
23
Mohammad Shahid30fedc52017-08-09 11:49:40 +053024import org.jboss.netty.buffer.ChannelBuffer;
25
26/**
27 * Implementation of RouteTarget.
28 */
29public class RouteTarget implements BgpValueType {
30
31 /*
32 * Type 0x00: Local Administrator sub-field uses 2 octets with AS number and
33 * Assigned number uses 4 octests Type 0x01: Local Administrator sub-field
34 * uses 4 octets with IP address and Assigned number uses 2 octests Type
35 * 0x02: Local Administrator sub-field uses 4 octets with AS number and
36 * Assigned number uses 2 octests
37 */
38 private byte[] routeTarget;
39 private short type;
40
41 public enum RouteTargetype {
42
43 AS((short) 0x0002), IP((short) 0x0102), LARGEAS((short) 0x0202);
44 short value;
45
46 /**
47 * Assign val with the value as the tunnel type.
48 *
49 * @param val tunnel type
50 */
51 RouteTargetype(short val) {
52 value = val;
53 }
54
55 /**
56 * Returns value of route type.
57 *
58 * @return route type
59 */
60 public short getType() {
61 return value;
62 }
63 }
64
65 /**
66 * Resets fields.
67 */
68 public RouteTarget() {
69 this.type = 0;
70 this.routeTarget = null;
71 }
72
73 /**
74 * Constructor to initialize parameters.
75 *
76 * @param type type
77 * @param routeTarget route target
78 */
79 public RouteTarget(short type, byte[] routeTarget) {
80 this.type = type;
81 this.routeTarget = routeTarget;
82 }
83
84 /**
85 * Reads route target from channelBuffer.
86 *
87 * @param type type
88 * @param cb channelBuffer
89 * @return object of RouteTarget
90 */
91 public static RouteTarget read(short type, ChannelBuffer cb) {
92 return new RouteTarget(type, cb.readBytes(6).array());
93 }
94
95 /**
96 * Returns route target.
97 *
98 * @return route target
99 */
100 public byte[] getRouteTarget() {
101 return this.routeTarget;
102 }
103
104 @Override
105 public boolean equals(Object obj) {
106 if (this == obj) {
107 return true;
108 }
109
110 if (obj instanceof RouteTarget) {
111 RouteTarget that = (RouteTarget) obj;
112 if (this.type == that.type
Yuta HIGUCHI1edc36b2018-01-24 23:39:06 -0800113 && Arrays.equals(this.routeTarget, that.routeTarget)) {
Mohammad Shahid30fedc52017-08-09 11:49:40 +0530114 return true;
115 }
116 }
117 return false;
118 }
119
120 @Override
121 public int hashCode() {
Yuta HIGUCHI1edc36b2018-01-24 23:39:06 -0800122 return Objects.hash(type, Arrays.hashCode(routeTarget));
Mohammad Shahid30fedc52017-08-09 11:49:40 +0530123 }
124
125 @Override
126 public String toString() {
127 return MoreObjects.toStringHelper(getClass()).add("type", type)
128 .add("routeTarget", routeTarget).toString();
129 }
130
131 @Override
132 public short getType() {
133 return type;
134 }
135
136 @Override
137 public int write(ChannelBuffer cb) {
138 int iLenStartIndex = cb.writerIndex();
139 cb.writeShort(type);
140 cb.writeBytes(routeTarget);
141 return cb.writerIndex() - iLenStartIndex;
142 }
143
144 @Override
145 public int compareTo(Object rd) {
146 return 0;
147 }
148}