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