blob: ec0ffe74dc2837c7cd3710ae5b1fc41612e2d65b [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;
28import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
31import java.util.ArrayList;
32import java.util.List;
33
34/**
35 * Representation of a Summary LSA, fields and methods to access them.
36 */
37public class SummaryLsa extends LsaHeader {
38 /*
39 0 1 2 3
40 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
41 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 | LS age | Options | 3 or 4 |
43 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 | Link State ID |
45 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 | Advertising Router |
47 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 | LS sequence number |
49 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 | LS checksum | length |
51 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 | Network Mask |
53 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 | 0 | metric |
55 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56 | TOS | TOS metric |
57 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 | ... |
59
60 */
61 private static final Logger log = LoggerFactory.getLogger(SummaryLsa.class);
62 private Ip4Address networkMask;
63 private int metric;
64
65 /**
66 * Creates an instance of Summary LSA.
67 *
68 * @param lsaHeader LSA header instance
69 */
70 public SummaryLsa(LsaHeader lsaHeader) {
71 populateHeader(lsaHeader);
72 }
73
74 /**
75 * Gets network mask.
76 *
77 * @return 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
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
118 try {
119 byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES];
120 channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
121 this.setNetworkMask(Ip4Address.valueOf(tempByteArray));
122 int unsedByte = channelBuffer.readByte();
123 this.setMetric(channelBuffer.readUnsignedMedium());
124 } catch (Exception e) {
125 log.debug("Error::SummaryLsa:: {}", e.getMessage());
126 throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR, OspfErrorType.BAD_MESSAGE);
127 }
128 }
129
130 /**
131 * Returns instance as bytes.
132 *
133 * @return instance as bytes
134 */
135 public byte[] asBytes() {
136 byte[] lsaMessage = null;
137 byte[] lsaHeader = getLsaHeaderAsByteArray();
138 byte[] lsaBody = getLsaBodyAsByteArray();
139 lsaMessage = Bytes.concat(lsaHeader, lsaBody);
140
141 return lsaMessage;
142 }
143
144 /**
145 * Get the LSA body.
146 *
147 * @return LSA body
148 */
149 public byte[] getLsaBodyAsByteArray() {
150 List<Byte> bodyLst = new ArrayList<>();
151
152 try {
153 bodyLst.addAll(Bytes.asList(this.networkMask().toOctets()));
154 bodyLst.add((byte) 0);
155 bodyLst.addAll(Bytes.asList(OspfUtil.convertToThreeBytes(this.metric())));
156 } catch (Exception e) {
157 log.debug("Error::getLsrBodyAsByteArray {}", e.getMessage());
158 return Bytes.toArray(bodyLst);
159 }
160
161 return Bytes.toArray(bodyLst);
162 }
163
164 @Override
165 public OspfLsaType getOspfLsaType() {
166 return OspfLsaType.SUMMARY;
167 }
168
169 @Override
170 public String toString() {
171 return MoreObjects.toStringHelper(getClass())
172 .omitNullValues()
173 .add("networkMask", networkMask)
174 .add("metric", metric)
175 .toString();
176 }
177
178 @Override
179 public boolean equals(Object o) {
180 if (this == o) {
181 return true;
182 }
183 if (o == null || getClass() != o.getClass()) {
184 return false;
185 }
186 SummaryLsa that = (SummaryLsa) o;
187 return Objects.equal(networkMask, that.networkMask) &&
188 Objects.equal(metric, that.metric);
189 }
190
191 @Override
192 public int hashCode() {
193 return Objects.hashCode(networkMask, metric);
194 }
195}