blob: 435c4237056a1755d78b89f0e9d48159cc4861df [file] [log] [blame]
Mohamed Rahila3b9e992016-02-16 20:26:49 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Mohamed Rahila3b9e992016-02-16 20:26:49 +05303 *
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.ospf.protocol.lsa.types;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.base.Objects;
20import com.google.common.primitives.Bytes;
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.onlab.packet.Ip4Address;
23import org.onosproject.ospf.controller.OspfLsaType;
24import org.onosproject.ospf.exceptions.OspfErrorType;
25import org.onosproject.ospf.exceptions.OspfParseException;
26import org.onosproject.ospf.protocol.lsa.LsaHeader;
27import org.onosproject.ospf.protocol.util.OspfUtil;
28
29import java.util.ArrayList;
30import java.util.List;
31
32/**
33 * Representation of an ASBR Summary LSA and methods to access them.
34 */
35public class AsbrSummaryLsa extends LsaHeader {
36
37 /*
38 0 1 2 3
39 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
40 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 | LS age | Options | 3 or 4 |
42 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 | Link State ID |
44 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 | Advertising Router |
46 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 | LS sequence number |
48 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 | LS checksum | length |
50 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 | Network Mask |
52 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 | 0 | metric |
54 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55 | TOS | TOS metric |
56 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57 | ... |
58
59 Summary LSA format
60 REFERENCE : RFC 2328
61 */
62 private Ip4Address networkMask;
63 private int metric;
64
65 /**
66 * Creates an instance of ASBR Summary LSA.
67 *
68 * @param lsaHeader lsa header instance.
69 */
70 public AsbrSummaryLsa(LsaHeader lsaHeader) {
71 populateHeader(lsaHeader);
72 }
73
74 /**
75 * Gets network mask.
76 *
77 * @return networkMask network mask
78 */
79 public Ip4Address networkMask() {
80 return networkMask;
81 }
82
83 /**
84 * Sets network mask.
85 *
86 * @param networkMask network mask
87 */
88 public void setNetworkMask(Ip4Address networkMask) {
89 this.networkMask = networkMask;
90 }
91
92 /**
93 * Gets metric value.
94 *
95 * @return metric value
96 */
97 public int metric() {
98 return metric;
99 }
100
101 /**
102 * Sets metric value.
103 *
104 * @param metric metric value
105 */
106 public void setMetric(int metric) {
107 this.metric = metric;
108 }
109
110 /**
111 * Reads from channel buffer and populate instance.
112 *
113 * @param channelBuffer channelBuffer instance.
114 * @throws OspfParseException might throws exception while parsing buffer
115 */
116 public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {
117 try {
118 byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES];
119 channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
120 this.setNetworkMask(Ip4Address.valueOf(tempByteArray));
121 int unusedByte = channelBuffer.readByte();
122 this.setMetric(channelBuffer.readUnsignedMedium());
123 } catch (Exception e) {
124 log.debug("Error::AsbrSummaryLsa:: {}", e.getMessage());
125 throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR, OspfErrorType.BAD_MESSAGE);
126 }
127 }
128
129 /**
130 * Gets LSA bytes as array.
131 *
132 * @return LSA message as bytes
133 * @throws OspfParseException might throws exception while parsing packet
134 */
135 public byte[] asBytes() throws OspfParseException {
136 byte[] lsaMessage = null;
137
138 byte[] lsaHeader = getLsaHeaderAsByteArray();
139 byte[] lsaBody = getLsaBodyAsByteArray();
140 lsaMessage = Bytes.concat(lsaHeader, lsaBody);
141
142 return lsaMessage;
143 }
144
145 /**
146 * Get LSA body as byte array.
147 *
148 * @return byte array contains lsa body
149 * @throws OspfParseException might throws exception while parsing packet
150 */
151 public byte[] getLsaBodyAsByteArray() throws OspfParseException {
152 List<Byte> bodyLst = new ArrayList<>();
153
154 try {
155 bodyLst.addAll(Bytes.asList(this.networkMask().toOctets()));
156 bodyLst.addAll(Bytes.asList(OspfUtil.convertToFourBytes(this.metric())));
157 } catch (Exception e) {
158 log.debug("Error::getLsrBodyAsByteArray {}", e.getMessage());
159 throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR, OspfErrorType.BAD_MESSAGE);
160 }
161
162 return Bytes.toArray(bodyLst);
163 }
164
165 @Override
166 public OspfLsaType getOspfLsaType() {
167 return OspfLsaType.ASBR_SUMMARY;
168 }
169
170 @Override
171 public boolean equals(Object o) {
172 if (this == o) {
173 return true;
174 }
175 if (o == null || getClass() != o.getClass()) {
176 return false;
177 }
178 AsbrSummaryLsa that = (AsbrSummaryLsa) o;
179 return Objects.equal(networkMask, that.networkMask) &&
180 Objects.equal(metric, that.metric);
181 }
182
183 @Override
184 public int hashCode() {
185 return Objects.hashCode(networkMask, metric);
186 }
187
188 @Override
189 public String toString() {
190 return MoreObjects.toStringHelper(getClass())
191 .omitNullValues()
192 .add("networkMask", networkMask)
193 .add("metric", metric)
194 .toString();
195 }
196}