blob: 8dbe3a31ad49d80b6e58dd56a0a500d7354bf659 [file] [log] [blame]
Mohamed Rahila3b9e992016-02-16 20:26:49 +05301/*
2 * Copyright 2016 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.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.exceptions.OspfErrorType;
24import org.onosproject.ospf.exceptions.OspfParseException;
25import org.onosproject.ospf.protocol.lsa.LsaHeader;
26import org.onosproject.ospf.protocol.util.OspfUtil;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import java.util.ArrayList;
31import java.util.List;
32
33/**
34 * Representation of a Network LSA and fields and methods to access them.
35 */
36public class NetworkLsa extends LsaHeader {
37
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 | 2 |
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 | Attached Router |
55 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56 | ... |
57 */
58 private static final Logger log =
59 LoggerFactory.getLogger(NetworkLsa.class);
60 private Ip4Address networkMask;
61 private List<Ip4Address> attachedRouters = new ArrayList<>();
62
63 /**
64 * Creates an instance of Network LSA.
65 */
66 public NetworkLsa() {
67 }
68
69 /**
70 * Creates an instance of Network LSA.
71 *
72 * @param lsaHeader lsa header instance.
73 */
74 public NetworkLsa(LsaHeader lsaHeader) {
75 populateHeader(lsaHeader);
76 }
77
78 /**
79 * Gets network mask.
80 *
81 * @return network mask
82 */
83 public Ip4Address networkMask() {
84 return networkMask;
85 }
86
87 /**
88 * Sets network mask.
89 *
90 * @param networkMask network mask
91 */
92 public void setNetworkMask(Ip4Address networkMask) {
93 this.networkMask = networkMask;
94 }
95
96 /**
97 * Adds attached router to list.
98 *
99 * @param attachedRouter attached router ip address.
100 */
101 public void addAttachedRouter(Ip4Address attachedRouter) {
102 attachedRouters.add(attachedRouter);
103 }
104
105 /**
106 * Reads from channel buffer and populate instance.
107 *
108 * @param channelBuffer channel buffer instance
109 * @throws OspfParseException might throws exception while parsing buffer
110 */
111 public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {
112
113 try {
114 byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES];
115 channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
116 this.setNetworkMask(Ip4Address.valueOf(tempByteArray));
117 //add all the attached routers
118 while (channelBuffer.readableBytes() > 0) {
119 tempByteArray = new byte[OspfUtil.FOUR_BYTES];
120 channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES);
121 this.addAttachedRouter(Ip4Address.valueOf(tempByteArray));
122 }
123 } catch (Exception e) {
124 log.debug("Error::NetworkLSA::readFrom:: {}", e.getMessage());
125 throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR, OspfErrorType.BAD_MESSAGE);
126 }
127 }
128
129 /**
130 * Gets LSA as bytes.
131 *
132 * @return LSA as byte array
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();
sunish vkaa48da82016-03-02 23:17:06 +0530139 byte[] lsaBody = getLsaBodyAsByteArray();
Mohamed Rahila3b9e992016-02-16 20:26:49 +0530140 lsaMessage = Bytes.concat(lsaHeader, lsaBody);
141
142 return lsaMessage;
143 }
144
145 /**
146 * Gets LSA body as byte array.
147 *
148 * @return LSA body as byte array
149 * @throws OspfParseException might throws exception while parsing packet
150 */
sunish vkaa48da82016-03-02 23:17:06 +0530151 public byte[] getLsaBodyAsByteArray() throws OspfParseException {
Mohamed Rahila3b9e992016-02-16 20:26:49 +0530152 List<Byte> bodyLst = new ArrayList<>();
153
154 try {
155 bodyLst.addAll(Bytes.asList(this.networkMask().toOctets()));
156 //add each attachedRouters details
157 for (Ip4Address attachedRouter : attachedRouters) {
158 //attached router
159 bodyLst.addAll(Bytes.asList(attachedRouter.toOctets()));
160 }
161 } catch (Exception e) {
162 log.debug("Error::NetworkLSA::getLsrBodyAsByteArray {}", e.getMessage());
163 throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR, OspfErrorType.BAD_MESSAGE);
164 }
165
166 return Bytes.toArray(bodyLst);
167 }
168
169 @Override
170 public boolean equals(Object o) {
171 if (this == o) {
172 return true;
173 }
174 if (o == null || getClass() != o.getClass()) {
175 return false;
176 }
177 NetworkLsa that = (NetworkLsa) o;
178 return Objects.equal(networkMask, that.networkMask) &&
179 Objects.equal(attachedRouters, that.attachedRouters);
180 }
181
182 @Override
183 public int hashCode() {
184 return Objects.hashCode(networkMask, attachedRouters);
185 }
186
187 @Override
188 public String toString() {
189 return MoreObjects.toStringHelper(getClass())
190 .omitNullValues()
191 .add("networkMask", networkMask)
192 .add("attachedRouters", attachedRouters)
193 .toString();
194 }
195}