Mohamed Rahil | a3b9e99 | 2016-02-16 20:26:49 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Foundation |
Mohamed Rahil | a3b9e99 | 2016-02-16 20:26:49 +0530 | [diff] [blame] | 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 | package org.onosproject.ospf.protocol.lsa.types; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.base.Objects; |
| 20 | import com.google.common.primitives.Bytes; |
| 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| 22 | import org.onlab.packet.Ip4Address; |
| 23 | import org.onosproject.ospf.exceptions.OspfErrorType; |
| 24 | import org.onosproject.ospf.exceptions.OspfParseException; |
| 25 | import org.onosproject.ospf.protocol.lsa.LsaHeader; |
| 26 | import org.onosproject.ospf.protocol.util.OspfUtil; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import java.util.ArrayList; |
| 31 | import java.util.List; |
| 32 | |
| 33 | /** |
| 34 | * Representation of a Network LSA and fields and methods to access them. |
| 35 | */ |
| 36 | public 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 | /** |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 106 | * Gets the list of attached routers. |
| 107 | * |
| 108 | * @return list of attached routers |
| 109 | */ |
| 110 | public List<Ip4Address> attachedRouters() { |
| 111 | |
| 112 | return attachedRouters; |
| 113 | } |
| 114 | |
| 115 | /** |
Mohamed Rahil | a3b9e99 | 2016-02-16 20:26:49 +0530 | [diff] [blame] | 116 | * Reads from channel buffer and populate instance. |
| 117 | * |
| 118 | * @param channelBuffer channel buffer instance |
| 119 | * @throws OspfParseException might throws exception while parsing buffer |
| 120 | */ |
| 121 | public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException { |
| 122 | |
| 123 | try { |
| 124 | byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES]; |
| 125 | channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); |
| 126 | this.setNetworkMask(Ip4Address.valueOf(tempByteArray)); |
| 127 | //add all the attached routers |
| 128 | while (channelBuffer.readableBytes() > 0) { |
| 129 | tempByteArray = new byte[OspfUtil.FOUR_BYTES]; |
| 130 | channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); |
| 131 | this.addAttachedRouter(Ip4Address.valueOf(tempByteArray)); |
| 132 | } |
| 133 | } catch (Exception e) { |
| 134 | log.debug("Error::NetworkLSA::readFrom:: {}", e.getMessage()); |
| 135 | throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR, OspfErrorType.BAD_MESSAGE); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Gets LSA as bytes. |
| 141 | * |
| 142 | * @return LSA as byte array |
| 143 | * @throws OspfParseException might throws exception while parsing packet |
| 144 | */ |
| 145 | public byte[] asBytes() throws OspfParseException { |
| 146 | byte[] lsaMessage = null; |
| 147 | |
| 148 | byte[] lsaHeader = getLsaHeaderAsByteArray(); |
sunish vk | aa48da8 | 2016-03-02 23:17:06 +0530 | [diff] [blame] | 149 | byte[] lsaBody = getLsaBodyAsByteArray(); |
Mohamed Rahil | a3b9e99 | 2016-02-16 20:26:49 +0530 | [diff] [blame] | 150 | lsaMessage = Bytes.concat(lsaHeader, lsaBody); |
| 151 | |
| 152 | return lsaMessage; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Gets LSA body as byte array. |
| 157 | * |
| 158 | * @return LSA body as byte array |
| 159 | * @throws OspfParseException might throws exception while parsing packet |
| 160 | */ |
sunish vk | aa48da8 | 2016-03-02 23:17:06 +0530 | [diff] [blame] | 161 | public byte[] getLsaBodyAsByteArray() throws OspfParseException { |
Mohamed Rahil | a3b9e99 | 2016-02-16 20:26:49 +0530 | [diff] [blame] | 162 | List<Byte> bodyLst = new ArrayList<>(); |
| 163 | |
| 164 | try { |
| 165 | bodyLst.addAll(Bytes.asList(this.networkMask().toOctets())); |
| 166 | //add each attachedRouters details |
| 167 | for (Ip4Address attachedRouter : attachedRouters) { |
| 168 | //attached router |
| 169 | bodyLst.addAll(Bytes.asList(attachedRouter.toOctets())); |
| 170 | } |
| 171 | } catch (Exception e) { |
| 172 | log.debug("Error::NetworkLSA::getLsrBodyAsByteArray {}", e.getMessage()); |
| 173 | throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR, OspfErrorType.BAD_MESSAGE); |
| 174 | } |
| 175 | |
| 176 | return Bytes.toArray(bodyLst); |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public boolean equals(Object o) { |
| 181 | if (this == o) { |
| 182 | return true; |
| 183 | } |
| 184 | if (o == null || getClass() != o.getClass()) { |
| 185 | return false; |
| 186 | } |
| 187 | NetworkLsa that = (NetworkLsa) o; |
| 188 | return Objects.equal(networkMask, that.networkMask) && |
| 189 | Objects.equal(attachedRouters, that.attachedRouters); |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public int hashCode() { |
| 194 | return Objects.hashCode(networkMask, attachedRouters); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public String toString() { |
| 199 | return MoreObjects.toStringHelper(getClass()) |
| 200 | .omitNullValues() |
| 201 | .add("networkMask", networkMask) |
| 202 | .add("attachedRouters", attachedRouters) |
| 203 | .toString(); |
| 204 | } |
| 205 | } |