blob: 47553e3334705be50a02d665899b407520649190 [file] [log] [blame]
Shashikanth VH73b12772016-04-18 14:28:45 +05301/*
2 * Copyright 2016-present 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 */
16package org.onosproject.bgpio.types;
17
18import com.google.common.base.MoreObjects;
19
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.onosproject.bgpio.exceptions.BgpParseException;
22import org.onosproject.bgpio.types.attr.WideCommunity;
23
24import java.util.List;
25import java.util.Objects;
26
27/**
28 * Provides implementation of BGP wide community target.
29 */
30public class WideCommunityTarget implements BgpValueType {
31
32 public static final byte TYPE = 1;
33 private List<BgpValueType> targetTlv;
34
35 /**
36 * Creates an instance of Wide community targets.
37 *
38 * @param targetTlv wide community targets to match
39 */
40 public WideCommunityTarget(List<BgpValueType> targetTlv) {
41 this.targetTlv = targetTlv;
42 }
43
44 /**
45 * Returns object of this class with specified values.
46 *
47 * @param targetTlv wide community target
48 * @return object of WideCommunityTarget
49 */
50 public static WideCommunityTarget of(List<BgpValueType> targetTlv) {
51 return new WideCommunityTarget(targetTlv);
52 }
53
54 /**
55 * Returns wide community targets.
56 *
57 * @return wide community targets
58 */
59 public List<BgpValueType> targetTlv() {
60 return targetTlv;
61 }
62
63 /**
64 * Sets wide community target.
65 *
66 * @param targetTlv wide community targets to match
67 */
68 public void setTargetTlv(List<BgpValueType> targetTlv) {
69 this.targetTlv = targetTlv;
70 }
71
72 @Override
73 public int hashCode() {
74 return Objects.hash(targetTlv);
75 }
76
77 @Override
78 public boolean equals(Object obj) {
79 if (this == obj) {
80 return true;
81 }
82
83 if (obj == null) {
84 return false;
85 }
86
87 if (obj instanceof WideCommunityTarget) {
88 WideCommunityTarget other = (WideCommunityTarget) obj;
89 return Objects.equals(targetTlv, other.targetTlv);
90 }
91 return false;
92 }
93
94 @Override
95 public int write(ChannelBuffer c) {
96 int iLenStartIndex = c.writerIndex();
97 WideCommunity.encodeWideCommunityTlv(c, targetTlv());
98 return c.writerIndex() - iLenStartIndex;
99 }
100
101 /**
102 * Reads the channel buffer and returns object of WideCommunityTarget.
103 *
104 * @param c ChannelBuffer
105 * @return object of WideCommunityTarget
106 * @throws BgpParseException on read error
107 */
108 public static WideCommunityTarget read(ChannelBuffer c) throws BgpParseException {
109 return new WideCommunityTarget(WideCommunity.decodeWideCommunityTlv(c));
110 }
111
112 @Override
113 public short getType() {
114 return TYPE;
115 }
116
117 @Override
118 public String toString() {
119 return MoreObjects.toStringHelper(getClass())
120 .add("Type", TYPE)
121 .add("targetTlv", targetTlv)
122 .toString();
123 }
124
125 @Override
126 public int compareTo(Object o) {
127 // TODO Auto-generated method stub
128 return 0;
129 }
130}