Committed by
Gerrit Code Review
ONOS-2739 - OSPF Basic Packet Structures , which includes encoding and decoding
Change-Id: I3f09176ad4ccc330b8989f13b12f74dc86f53ae4
Showing
17 changed files
with
2832 additions
and
0 deletions
| 1 | +/* | ||
| 2 | + * Copyright 2014-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 | + */ | ||
| 16 | +package org.onosproject.ospf.protocol.lsa.subtypes; | ||
| 17 | + | ||
| 18 | +import com.google.common.base.MoreObjects; | ||
| 19 | +import org.onlab.packet.Ip4Address; | ||
| 20 | + | ||
| 21 | +/** | ||
| 22 | + * Defines the OSPF external destination. | ||
| 23 | + */ | ||
| 24 | +public class OspfExternalDestination { | ||
| 25 | + | ||
| 26 | + private boolean isType1orType2Metric; | ||
| 27 | + private int metric; | ||
| 28 | + private Ip4Address forwardingAddress; | ||
| 29 | + private int externalRouterTag; | ||
| 30 | + | ||
| 31 | + /** | ||
| 32 | + * Gets whether type1 or type 2 metric. | ||
| 33 | + * | ||
| 34 | + * @return true if Type1 or false if Type2 metric | ||
| 35 | + */ | ||
| 36 | + public boolean isType1orType2Metric() { | ||
| 37 | + return isType1orType2Metric; | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + /** | ||
| 41 | + * Sets whether type1 or Type2 metric. | ||
| 42 | + * | ||
| 43 | + * @param isType1orType2Metric is type 1 or type 2 metric | ||
| 44 | + */ | ||
| 45 | + public void setType1orType2Metric(boolean isType1orType2Metric) { | ||
| 46 | + this.isType1orType2Metric = isType1orType2Metric; | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + /** | ||
| 50 | + * Gets the metric value. | ||
| 51 | + * | ||
| 52 | + * @return metric value | ||
| 53 | + */ | ||
| 54 | + public int metric() { | ||
| 55 | + return metric; | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + /** | ||
| 59 | + * Sets the metric value. | ||
| 60 | + * | ||
| 61 | + * @param metric metric value | ||
| 62 | + */ | ||
| 63 | + public void setMetric(int metric) { | ||
| 64 | + this.metric = metric; | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + /** | ||
| 68 | + * Gets forwarding address. | ||
| 69 | + * | ||
| 70 | + * @return forwarding address | ||
| 71 | + */ | ||
| 72 | + public Ip4Address forwardingAddress() { | ||
| 73 | + return forwardingAddress; | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + /** | ||
| 77 | + * Sets forwarding address. | ||
| 78 | + * | ||
| 79 | + * @param forwardingAddress forwarding address | ||
| 80 | + */ | ||
| 81 | + public void setForwardingAddress(Ip4Address forwardingAddress) { | ||
| 82 | + this.forwardingAddress = forwardingAddress; | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + /** | ||
| 86 | + * Gets external router tag. | ||
| 87 | + * | ||
| 88 | + * @return external router tag | ||
| 89 | + */ | ||
| 90 | + public int externalRouterTag() { | ||
| 91 | + return externalRouterTag; | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + /** | ||
| 95 | + * Sets external router tag. | ||
| 96 | + * | ||
| 97 | + * @param externalRouterTag external router tag | ||
| 98 | + */ | ||
| 99 | + public void setExternalRouterTag(int externalRouterTag) { | ||
| 100 | + this.externalRouterTag = externalRouterTag; | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + @Override | ||
| 104 | + public String toString() { | ||
| 105 | + return MoreObjects.toStringHelper(getClass()) | ||
| 106 | + .omitNullValues() | ||
| 107 | + .add("isType1orType2Metric", isType1orType2Metric) | ||
| 108 | + .add("metric", metric) | ||
| 109 | + .add("forwardingAddress", forwardingAddress) | ||
| 110 | + .add("externalRouterTag", externalRouterTag) | ||
| 111 | + .toString(); | ||
| 112 | + } | ||
| 113 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/subtypes/OspfLsaLink.java
0 → 100644
| 1 | +/* | ||
| 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 | + */ | ||
| 16 | +package org.onosproject.ospf.protocol.lsa.subtypes; | ||
| 17 | + | ||
| 18 | +import com.google.common.base.MoreObjects; | ||
| 19 | + | ||
| 20 | +/** | ||
| 21 | + * Representation of an OSPF LSA link. | ||
| 22 | + */ | ||
| 23 | +public class OspfLsaLink { | ||
| 24 | + | ||
| 25 | + public String linkId; | ||
| 26 | + public String linkData; | ||
| 27 | + public int linkType; | ||
| 28 | + public int metric; | ||
| 29 | + public int tos; | ||
| 30 | + | ||
| 31 | + /** | ||
| 32 | + * Gets link id. | ||
| 33 | + * | ||
| 34 | + * @return link id | ||
| 35 | + */ | ||
| 36 | + public String linkId() { | ||
| 37 | + return linkId; | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + /** | ||
| 41 | + * Sets link id. | ||
| 42 | + * | ||
| 43 | + * @param linkId link id | ||
| 44 | + */ | ||
| 45 | + public void setLinkId(String linkId) { | ||
| 46 | + this.linkId = linkId; | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + /** | ||
| 50 | + * Gets link data. | ||
| 51 | + * | ||
| 52 | + * @return link data | ||
| 53 | + */ | ||
| 54 | + public String linkData() { | ||
| 55 | + return linkData; | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + /** | ||
| 59 | + * Sets link data. | ||
| 60 | + * | ||
| 61 | + * @param linkData link data | ||
| 62 | + */ | ||
| 63 | + public void setLinkData(String linkData) { | ||
| 64 | + this.linkData = linkData; | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + /** | ||
| 68 | + * Gets link type. | ||
| 69 | + * | ||
| 70 | + * @return link type | ||
| 71 | + */ | ||
| 72 | + public int linkType() { | ||
| 73 | + return linkType; | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + /** | ||
| 77 | + * Sets link type. | ||
| 78 | + * | ||
| 79 | + * @param linkType link type | ||
| 80 | + */ | ||
| 81 | + public void setLinkType(int linkType) { | ||
| 82 | + this.linkType = linkType; | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + /** | ||
| 86 | + * Gets metric value. | ||
| 87 | + * | ||
| 88 | + * @return metric. | ||
| 89 | + */ | ||
| 90 | + public int metric() { | ||
| 91 | + return metric; | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + /** | ||
| 95 | + * Sets metric value. | ||
| 96 | + * | ||
| 97 | + * @param metric metric | ||
| 98 | + */ | ||
| 99 | + public void setMetric(int metric) { | ||
| 100 | + this.metric = metric; | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + /** | ||
| 104 | + * Gets tos. | ||
| 105 | + * | ||
| 106 | + * @return tos | ||
| 107 | + */ | ||
| 108 | + public int tos() { | ||
| 109 | + return tos; | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + /** | ||
| 113 | + * Sets tos. | ||
| 114 | + * | ||
| 115 | + * @param tos tos | ||
| 116 | + */ | ||
| 117 | + public void setTos(int tos) { | ||
| 118 | + this.tos = tos; | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + @Override | ||
| 122 | + public String toString() { | ||
| 123 | + return MoreObjects.toStringHelper(getClass()) | ||
| 124 | + .omitNullValues() | ||
| 125 | + .add("linkID", linkId) | ||
| 126 | + .add("linkData", linkData) | ||
| 127 | + .add("linkType", linkType) | ||
| 128 | + .add("metric", metric) | ||
| 129 | + .add("tos", tos) | ||
| 130 | + .toString(); | ||
| 131 | + } | ||
| 132 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/types/AsbrSummaryLsa.java
0 → 100644
| 1 | +/* | ||
| 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 | + */ | ||
| 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.controller.OspfLsaType; | ||
| 24 | +import org.onosproject.ospf.exceptions.OspfErrorType; | ||
| 25 | +import org.onosproject.ospf.exceptions.OspfParseException; | ||
| 26 | +import org.onosproject.ospf.protocol.lsa.LsaHeader; | ||
| 27 | +import org.onosproject.ospf.protocol.util.OspfUtil; | ||
| 28 | + | ||
| 29 | +import java.util.ArrayList; | ||
| 30 | +import java.util.List; | ||
| 31 | + | ||
| 32 | +/** | ||
| 33 | + * Representation of an ASBR Summary LSA and methods to access them. | ||
| 34 | + */ | ||
| 35 | +public 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 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/types/ExternalLsa.java
0 → 100644
| 1 | +/* | ||
| 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 | + */ | ||
| 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.controller.OspfLsaType; | ||
| 24 | +import org.onosproject.ospf.exceptions.OspfErrorType; | ||
| 25 | +import org.onosproject.ospf.exceptions.OspfParseException; | ||
| 26 | +import org.onosproject.ospf.protocol.lsa.LsaHeader; | ||
| 27 | +import org.onosproject.ospf.protocol.lsa.subtypes.OspfExternalDestination; | ||
| 28 | +import org.onosproject.ospf.protocol.util.OspfUtil; | ||
| 29 | +import org.slf4j.Logger; | ||
| 30 | +import org.slf4j.LoggerFactory; | ||
| 31 | + | ||
| 32 | +import java.util.ArrayList; | ||
| 33 | +import java.util.List; | ||
| 34 | + | ||
| 35 | +/** | ||
| 36 | + * Representation of an External LSA and the fields and methods to access them. | ||
| 37 | + */ | ||
| 38 | +public class ExternalLsa extends LsaHeader { | ||
| 39 | + | ||
| 40 | + /* | ||
| 41 | + 0 1 2 3 | ||
| 42 | + 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 | ||
| 43 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 44 | + | LS age | Options | 5 | | ||
| 45 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 46 | + | Link State ID | | ||
| 47 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 48 | + | Advertising Router | | ||
| 49 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 50 | + | LS sequence number | | ||
| 51 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 52 | + | LS checksum | length | | ||
| 53 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 54 | + | Network Mask | | ||
| 55 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 56 | + |E| 0 | metric | | ||
| 57 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 58 | + | Forwarding address | | ||
| 59 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 60 | + | External Route Tag | | ||
| 61 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 62 | + |E| TOS | TOS metric | | ||
| 63 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 64 | + | Forwarding address | | ||
| 65 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 66 | + | External Route Tag | | ||
| 67 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 68 | + | ... | | ||
| 69 | + | ||
| 70 | + External LSA format | ||
| 71 | + REFERENCE : RFC 2328 | ||
| 72 | + */ | ||
| 73 | + private static final Logger log = | ||
| 74 | + LoggerFactory.getLogger(ExternalLsa.class); | ||
| 75 | + private Ip4Address networkMask; | ||
| 76 | + private List<OspfExternalDestination> externalDestinations = new ArrayList<>(); | ||
| 77 | + | ||
| 78 | + /** | ||
| 79 | + * Creates an instance of External LSA. | ||
| 80 | + * | ||
| 81 | + * @param lsaHeader lsa header instance. | ||
| 82 | + */ | ||
| 83 | + public ExternalLsa(LsaHeader lsaHeader) { | ||
| 84 | + populateHeader(lsaHeader); | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + | ||
| 88 | + /** | ||
| 89 | + * Gets the network mask. | ||
| 90 | + * | ||
| 91 | + * @return networkMask | ||
| 92 | + */ | ||
| 93 | + public Ip4Address networkMask() { | ||
| 94 | + return networkMask; | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + /** | ||
| 98 | + * Sets network mask. | ||
| 99 | + * | ||
| 100 | + * @param networkMask network mask | ||
| 101 | + */ | ||
| 102 | + public void setNetworkMask(Ip4Address networkMask) { | ||
| 103 | + this.networkMask = networkMask; | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + /** | ||
| 107 | + * Adds the external destination details to the list. | ||
| 108 | + * | ||
| 109 | + * @param externalDestination external destination details | ||
| 110 | + */ | ||
| 111 | + public void addExternalDestination(OspfExternalDestination externalDestination) { | ||
| 112 | + if (!externalDestinations.contains(externalDestination)) { | ||
| 113 | + externalDestinations.add(externalDestination); | ||
| 114 | + } | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + /** | ||
| 118 | + * Reads from channel buffer and populate instance. | ||
| 119 | + * | ||
| 120 | + * @param channelBuffer channelBuffer instance | ||
| 121 | + * @throws OspfParseException might throws exception while parsing buffer | ||
| 122 | + */ | ||
| 123 | + public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException { | ||
| 124 | + | ||
| 125 | + try { | ||
| 126 | + byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES]; | ||
| 127 | + channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); | ||
| 128 | + this.setNetworkMask(Ip4Address.valueOf(tempByteArray)); | ||
| 129 | + | ||
| 130 | + while (channelBuffer.readableBytes() >= OspfUtil.EXTERNAL_DESTINATION_LENGTH) { | ||
| 131 | + OspfExternalDestination externalDestination = new OspfExternalDestination(); | ||
| 132 | + | ||
| 133 | + //E Bit - use to find type1 or type2 | ||
| 134 | + int eIsSet = channelBuffer.readByte(); | ||
| 135 | + if (eIsSet != 0) { | ||
| 136 | + externalDestination.setType1orType2Metric(true); | ||
| 137 | + } else { | ||
| 138 | + externalDestination.setType1orType2Metric(false); | ||
| 139 | + } | ||
| 140 | + externalDestination.setMetric(channelBuffer.readUnsignedMedium()); | ||
| 141 | + tempByteArray = new byte[OspfUtil.FOUR_BYTES]; | ||
| 142 | + channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); | ||
| 143 | + externalDestination.setForwardingAddress(Ip4Address.valueOf(tempByteArray)); | ||
| 144 | + externalDestination.setExternalRouterTag(channelBuffer.readInt()); | ||
| 145 | + | ||
| 146 | + this.addExternalDestination(externalDestination); | ||
| 147 | + } | ||
| 148 | + } catch (Exception e) { | ||
| 149 | + log.debug("Error::ExternalLSA:: {}", e.getMessage()); | ||
| 150 | + throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR, OspfErrorType.BAD_MESSAGE); | ||
| 151 | + } | ||
| 152 | + } | ||
| 153 | + | ||
| 154 | + /** | ||
| 155 | + * Gets LSA as bytes. | ||
| 156 | + * | ||
| 157 | + * @return LSA as bytes. | ||
| 158 | + * @throws OspfParseException might throws exception while parsing packet | ||
| 159 | + */ | ||
| 160 | + public byte[] asBytes() throws OspfParseException { | ||
| 161 | + byte[] lsaMessage = null; | ||
| 162 | + | ||
| 163 | + byte[] lsaHeader = getLsaHeaderAsByteArray(); | ||
| 164 | + byte[] lsaBody = getLsaBodyAsByteArray(); | ||
| 165 | + lsaMessage = Bytes.concat(lsaHeader, lsaBody); | ||
| 166 | + | ||
| 167 | + return lsaMessage; | ||
| 168 | + } | ||
| 169 | + | ||
| 170 | + /** | ||
| 171 | + * Gets LSA body as byte array. | ||
| 172 | + * | ||
| 173 | + * @return byte array contains LSA body | ||
| 174 | + * @throws OspfParseException might throws exception while parsing buffer | ||
| 175 | + */ | ||
| 176 | + public byte[] getLsaBodyAsByteArray() throws OspfParseException { | ||
| 177 | + List<Byte> bodyLst = new ArrayList<>(); | ||
| 178 | + | ||
| 179 | + try { | ||
| 180 | + bodyLst.addAll(Bytes.asList(this.networkMask().toOctets())); | ||
| 181 | + | ||
| 182 | + //add each OSPFExternalDestination details | ||
| 183 | + for (OspfExternalDestination externalDest : externalDestinations) { | ||
| 184 | + if (externalDest.isType1orType2Metric()) { | ||
| 185 | + //add 1 followed by 7 zeros equals to decimal 128 | ||
| 186 | + bodyLst.add((byte) 128); | ||
| 187 | + } else { | ||
| 188 | + bodyLst.add((byte) 0); | ||
| 189 | + } | ||
| 190 | + | ||
| 191 | + bodyLst.addAll(Bytes.asList(OspfUtil.convertToThreeBytes(externalDest.metric()))); | ||
| 192 | + bodyLst.addAll(Bytes.asList(externalDest.forwardingAddress().toOctets())); | ||
| 193 | + bodyLst.addAll(Bytes.asList(OspfUtil.convertToFourBytes(externalDest.externalRouterTag()))); | ||
| 194 | + } | ||
| 195 | + } catch (Exception e) { | ||
| 196 | + log.debug("Error::getLsrBodyAsByteArray {}", e.getMessage()); | ||
| 197 | + return Bytes.toArray(bodyLst); | ||
| 198 | + } | ||
| 199 | + | ||
| 200 | + return Bytes.toArray(bodyLst); | ||
| 201 | + } | ||
| 202 | + | ||
| 203 | + @Override | ||
| 204 | + public OspfLsaType getOspfLsaType() { | ||
| 205 | + return OspfLsaType.EXTERNAL_LSA; | ||
| 206 | + } | ||
| 207 | + | ||
| 208 | + @Override | ||
| 209 | + public boolean equals(Object other) { | ||
| 210 | + if (this == other) { | ||
| 211 | + return true; | ||
| 212 | + } | ||
| 213 | + if (other == null || getClass() != other.getClass()) { | ||
| 214 | + return false; | ||
| 215 | + } | ||
| 216 | + ExternalLsa that = (ExternalLsa) other; | ||
| 217 | + return Objects.equal(networkMask, that.networkMask) && | ||
| 218 | + Objects.equal(externalDestinations, that.externalDestinations); | ||
| 219 | + } | ||
| 220 | + | ||
| 221 | + @Override | ||
| 222 | + public int hashCode() { | ||
| 223 | + return Objects.hashCode(networkMask, externalDestinations); | ||
| 224 | + } | ||
| 225 | + | ||
| 226 | + @Override | ||
| 227 | + public String toString() { | ||
| 228 | + return MoreObjects.toStringHelper(getClass()) | ||
| 229 | + .omitNullValues() | ||
| 230 | + .add("networkMask", networkMask) | ||
| 231 | + .add("externalDestinations", externalDestinations) | ||
| 232 | + .toString(); | ||
| 233 | + } | ||
| 234 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/types/NetworkLsa.java
0 → 100644
| 1 | +/* | ||
| 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 | + */ | ||
| 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 | + /** | ||
| 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(); | ||
| 139 | + byte[] lsaBody = getLSABodyAsByteArray(); | ||
| 140 | + 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 | + */ | ||
| 151 | + public byte[] getLSABodyAsByteArray() throws OspfParseException { | ||
| 152 | + 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 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/types/OpaqueLsa11.java
0 → 100644
| 1 | +/* | ||
| 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 | + */ | ||
| 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.onosproject.ospf.controller.OspfLsaType; | ||
| 23 | +import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader; | ||
| 24 | + | ||
| 25 | +/** | ||
| 26 | + * Representation of an Opaque LSA of type AS (11). | ||
| 27 | + */ | ||
| 28 | +public class OpaqueLsa11 extends OpaqueLsaHeader { | ||
| 29 | + /* | ||
| 30 | + 0 1 2 3 | ||
| 31 | + 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 | ||
| 32 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 33 | + | LS age | Options | 9, 10 or 11 | | ||
| 34 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 35 | + | Opaque Type | Opaque ID | | ||
| 36 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 37 | + | Advertising Router | | ||
| 38 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 39 | + | LS Sequence Number | | ||
| 40 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 41 | + | LS checksum | Length | | ||
| 42 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 43 | + | | | ||
| 44 | + + + | ||
| 45 | + | Opaque Information | | ||
| 46 | + + + | ||
| 47 | + | ... | | ||
| 48 | + */ | ||
| 49 | + private byte[] opaqueInfo = null; | ||
| 50 | + | ||
| 51 | + /** | ||
| 52 | + * Creates an instance of Opaque type 11 LSA. | ||
| 53 | + * | ||
| 54 | + * @param lsaHeader LSA header instance | ||
| 55 | + */ | ||
| 56 | + public OpaqueLsa11(OpaqueLsaHeader lsaHeader) { | ||
| 57 | + populateHeader(lsaHeader); | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + /** | ||
| 61 | + * Reads from channel buffer and populate this. | ||
| 62 | + * | ||
| 63 | + * @param channelBuffer channelBuffer instance. | ||
| 64 | + */ | ||
| 65 | + public void readFrom(ChannelBuffer channelBuffer) { | ||
| 66 | + int length = channelBuffer.readableBytes(); | ||
| 67 | + opaqueInfo = new byte[length]; | ||
| 68 | + channelBuffer.readBytes(opaqueInfo, 0, length); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + /** | ||
| 72 | + * Returns instance as bytes. | ||
| 73 | + * | ||
| 74 | + * @return instance as bytes | ||
| 75 | + */ | ||
| 76 | + public byte[] asBytes() { | ||
| 77 | + byte[] lsaMessage = null; | ||
| 78 | + | ||
| 79 | + byte[] lsaHeader = getOpaqueLsaHeaderAsByteArray(); | ||
| 80 | + byte[] lsaBody = getLsaBodyAsByteArray(); | ||
| 81 | + lsaMessage = Bytes.concat(lsaHeader, lsaBody); | ||
| 82 | + | ||
| 83 | + return lsaMessage; | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + /** | ||
| 87 | + * Get the LSA body as byte array. | ||
| 88 | + * | ||
| 89 | + * @return LSA body as byte array | ||
| 90 | + */ | ||
| 91 | + public byte[] getLsaBodyAsByteArray() { | ||
| 92 | + return opaqueInfo; | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + @Override | ||
| 96 | + public OspfLsaType getOspfLsaType() { | ||
| 97 | + return OspfLsaType.AS_OPAQUE_LSA; | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + @Override | ||
| 101 | + public boolean equals(Object o) { | ||
| 102 | + if (this == o) { | ||
| 103 | + return true; | ||
| 104 | + } | ||
| 105 | + if (o == null || getClass() != o.getClass()) { | ||
| 106 | + return false; | ||
| 107 | + } | ||
| 108 | + OpaqueLsa11 that = (OpaqueLsa11) o; | ||
| 109 | + return Objects.equal(opaqueInfo, that.opaqueInfo); | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + @Override | ||
| 113 | + public int hashCode() { | ||
| 114 | + return Objects.hashCode(opaqueInfo); | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + @Override | ||
| 118 | + public String toString() { | ||
| 119 | + return MoreObjects.toStringHelper(getClass()) | ||
| 120 | + .omitNullValues() | ||
| 121 | + .add("opaqueInfo", opaqueInfo) | ||
| 122 | + .toString(); | ||
| 123 | + } | ||
| 124 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/types/OpaqueLsa9.java
0 → 100644
| 1 | +/* | ||
| 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 | + */ | ||
| 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.onosproject.ospf.controller.OspfLsaType; | ||
| 23 | +import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader; | ||
| 24 | + | ||
| 25 | +/** | ||
| 26 | + * Representation of an Opaque LSA of type link local (9). | ||
| 27 | + */ | ||
| 28 | +public class OpaqueLsa9 extends OpaqueLsaHeader { | ||
| 29 | + | ||
| 30 | + /* | ||
| 31 | + 0 1 2 3 | ||
| 32 | + 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 | ||
| 33 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 34 | + | LS age | Options | 9, 10 or 11 | | ||
| 35 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 36 | + | Opaque Type | Opaque ID | | ||
| 37 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 38 | + | Advertising Router | | ||
| 39 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 40 | + | LS Sequence Number | | ||
| 41 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 42 | + | LS checksum | Length | | ||
| 43 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 44 | + | | | ||
| 45 | + + + | ||
| 46 | + | Opaque Information | | ||
| 47 | + + + | ||
| 48 | + | ... | | ||
| 49 | + */ | ||
| 50 | + private byte[] opaqueInfo = null; | ||
| 51 | + | ||
| 52 | + /** | ||
| 53 | + * Creates an instance of Opaque type 9 LSA. | ||
| 54 | + * | ||
| 55 | + * @param lsaHeader LSA header instance | ||
| 56 | + */ | ||
| 57 | + public OpaqueLsa9(OpaqueLsaHeader lsaHeader) { | ||
| 58 | + populateHeader(lsaHeader); | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + /** | ||
| 62 | + * Reads from channel buffer and populate instance. | ||
| 63 | + * | ||
| 64 | + * @param channelBuffer channelBuffer instance | ||
| 65 | + */ | ||
| 66 | + public void readFrom(ChannelBuffer channelBuffer) { | ||
| 67 | + int length = channelBuffer.readableBytes(); | ||
| 68 | + opaqueInfo = new byte[length]; | ||
| 69 | + channelBuffer.readBytes(opaqueInfo, 0, length); | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + /** | ||
| 73 | + * Returns instance as bytes. | ||
| 74 | + * | ||
| 75 | + * @return instance as bytes | ||
| 76 | + */ | ||
| 77 | + public byte[] asBytes() { | ||
| 78 | + byte[] lsaMessage = null; | ||
| 79 | + | ||
| 80 | + byte[] lsaHeader = getOpaqueLsaHeaderAsByteArray(); | ||
| 81 | + byte[] lsaBody = getLsaBodyAsByteArray(); | ||
| 82 | + lsaMessage = Bytes.concat(lsaHeader, lsaBody); | ||
| 83 | + | ||
| 84 | + return lsaMessage; | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + /** | ||
| 88 | + * Gets the LSA body. | ||
| 89 | + * | ||
| 90 | + * @return the LSA body | ||
| 91 | + */ | ||
| 92 | + public byte[] getLsaBodyAsByteArray() { | ||
| 93 | + return opaqueInfo; | ||
| 94 | + | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + @Override | ||
| 98 | + public OspfLsaType getOspfLsaType() { | ||
| 99 | + return OspfLsaType.LINK_LOCAL_OPAQUE_LSA; | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + @Override | ||
| 103 | + public boolean equals(Object o) { | ||
| 104 | + if (this == o) { | ||
| 105 | + return true; | ||
| 106 | + } | ||
| 107 | + if (o == null || getClass() != o.getClass()) { | ||
| 108 | + return false; | ||
| 109 | + } | ||
| 110 | + OpaqueLsa9 that = (OpaqueLsa9) o; | ||
| 111 | + return Objects.equal(opaqueInfo, that.opaqueInfo); | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + @Override | ||
| 115 | + public int hashCode() { | ||
| 116 | + return Objects.hashCode(opaqueInfo); | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + @Override | ||
| 120 | + public String toString() { | ||
| 121 | + return MoreObjects.toStringHelper(getClass()) | ||
| 122 | + .omitNullValues() | ||
| 123 | + .add("opaqueInfo", opaqueInfo) | ||
| 124 | + .toString(); | ||
| 125 | + } | ||
| 126 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/types/SummaryLsa.java
0 → 100644
| 1 | +/* | ||
| 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 | + */ | ||
| 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.controller.OspfLsaType; | ||
| 24 | +import org.onosproject.ospf.exceptions.OspfErrorType; | ||
| 25 | +import org.onosproject.ospf.exceptions.OspfParseException; | ||
| 26 | +import org.onosproject.ospf.protocol.lsa.LsaHeader; | ||
| 27 | +import org.onosproject.ospf.protocol.util.OspfUtil; | ||
| 28 | +import org.slf4j.Logger; | ||
| 29 | +import org.slf4j.LoggerFactory; | ||
| 30 | + | ||
| 31 | +import java.util.ArrayList; | ||
| 32 | +import java.util.List; | ||
| 33 | + | ||
| 34 | +/** | ||
| 35 | + * Representation of a Summary LSA, fields and methods to access them. | ||
| 36 | + */ | ||
| 37 | +public 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 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
protocols/ospf/protocol/src/main/java/org/onosproject/ospf/protocol/lsa/types/TopLevelTlv.java
0 → 100644
| 1 | +/* | ||
| 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 | + */ | ||
| 16 | +package org.onosproject.ospf.protocol.lsa.types; | ||
| 17 | + | ||
| 18 | +/** | ||
| 19 | + * Marker interface represents a top level TLV in an Opaque LSA. | ||
| 20 | + */ | ||
| 21 | +public interface TopLevelTlv { | ||
| 22 | +} |
| 1 | +/* | ||
| 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 | + */ | ||
| 16 | +package org.onosproject.ospf.protocol.lsa.subtypes; | ||
| 17 | + | ||
| 18 | +import org.junit.After; | ||
| 19 | +import org.junit.Before; | ||
| 20 | +import org.junit.Test; | ||
| 21 | +import org.onlab.packet.Ip4Address; | ||
| 22 | + | ||
| 23 | +import java.net.InetAddress; | ||
| 24 | + | ||
| 25 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
| 26 | +import static org.hamcrest.Matchers.is; | ||
| 27 | +import static org.hamcrest.Matchers.notNullValue; | ||
| 28 | + | ||
| 29 | +/** | ||
| 30 | + * Unit test class for OspfRouterId. | ||
| 31 | + */ | ||
| 32 | +public class OspfExternalDestinationTest { | ||
| 33 | + | ||
| 34 | + private OspfExternalDestination ospfExternalDestination; | ||
| 35 | + | ||
| 36 | + @Before | ||
| 37 | + public void setUp() throws Exception { | ||
| 38 | + ospfExternalDestination = new OspfExternalDestination(); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + @After | ||
| 42 | + public void tearDown() throws Exception { | ||
| 43 | + ospfExternalDestination = null; | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + /** | ||
| 47 | + * Tests isType1orType2Metric() getter method. | ||
| 48 | + */ | ||
| 49 | + @Test | ||
| 50 | + public void testIsType1orType2Metric() throws Exception { | ||
| 51 | + ospfExternalDestination.setType1orType2Metric(true); | ||
| 52 | + assertThat(ospfExternalDestination.isType1orType2Metric(), is(true)); | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + /** | ||
| 56 | + * Tests isType1orType2Metric() setter method. | ||
| 57 | + */ | ||
| 58 | + @Test | ||
| 59 | + public void testSetType1orType2Metric() throws Exception { | ||
| 60 | + ospfExternalDestination.setType1orType2Metric(true); | ||
| 61 | + assertThat(ospfExternalDestination.isType1orType2Metric(), is(true)); | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + /** | ||
| 65 | + * Tests metric() getter method. | ||
| 66 | + */ | ||
| 67 | + @Test | ||
| 68 | + public void testGetMetric() throws Exception { | ||
| 69 | + ospfExternalDestination.setMetric(100); | ||
| 70 | + assertThat(ospfExternalDestination.metric(), is(100)); | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + /** | ||
| 74 | + * Tests metric() setter method. | ||
| 75 | + */ | ||
| 76 | + @Test | ||
| 77 | + public void testSetMetric() throws Exception { | ||
| 78 | + ospfExternalDestination.setMetric(100); | ||
| 79 | + assertThat(ospfExternalDestination.metric(), is(100)); | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + /** | ||
| 83 | + * Tests forwardingAddress() getter method. | ||
| 84 | + */ | ||
| 85 | + @Test | ||
| 86 | + public void testGetForwardingAddress() throws Exception { | ||
| 87 | + ospfExternalDestination.setForwardingAddress(Ip4Address.valueOf(InetAddress.getLocalHost())); | ||
| 88 | + assertThat(ospfExternalDestination.forwardingAddress(), is(Ip4Address.valueOf(InetAddress.getLocalHost()))); | ||
| 89 | + | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | + /** | ||
| 93 | + * Tests forwardingAddress() setter method. | ||
| 94 | + */ | ||
| 95 | + @Test | ||
| 96 | + public void testSetForwardingAddress() throws Exception { | ||
| 97 | + ospfExternalDestination.setForwardingAddress(Ip4Address.valueOf(InetAddress.getLocalHost())); | ||
| 98 | + assertThat(ospfExternalDestination.forwardingAddress(), is(Ip4Address.valueOf(InetAddress.getLocalHost()))); | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + /** | ||
| 102 | + * Tests externalRouterTag() getter method. | ||
| 103 | + */ | ||
| 104 | + @Test | ||
| 105 | + public void testGetExternalRouterTag() throws Exception { | ||
| 106 | + ospfExternalDestination.setExternalRouterTag(100); | ||
| 107 | + assertThat(ospfExternalDestination.externalRouterTag(), is(100)); | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + /** | ||
| 111 | + * Tests externalRouterTag() setter method. | ||
| 112 | + */ | ||
| 113 | + @Test | ||
| 114 | + public void testSetExternalRouterTag() throws Exception { | ||
| 115 | + ospfExternalDestination.setExternalRouterTag(100); | ||
| 116 | + assertThat(ospfExternalDestination.externalRouterTag(), is(100)); | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + /** | ||
| 120 | + * Tests to string method. | ||
| 121 | + */ | ||
| 122 | + @Test | ||
| 123 | + public void testToString() throws Exception { | ||
| 124 | + assertThat(ospfExternalDestination.toString(), is(notNullValue())); | ||
| 125 | + } | ||
| 126 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 1 | +/* | ||
| 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 | + */ | ||
| 16 | +package org.onosproject.ospf.protocol.lsa.subtypes; | ||
| 17 | + | ||
| 18 | +import org.junit.After; | ||
| 19 | +import org.junit.Before; | ||
| 20 | +import org.junit.Test; | ||
| 21 | + | ||
| 22 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
| 23 | +import static org.hamcrest.Matchers.is; | ||
| 24 | +import static org.hamcrest.Matchers.notNullValue; | ||
| 25 | + | ||
| 26 | +/** | ||
| 27 | + * Unit test class for OspfLsaLink. | ||
| 28 | + */ | ||
| 29 | +public class OspfLsaLinkTest { | ||
| 30 | + | ||
| 31 | + private OspfLsaLink ospfLsaLink; | ||
| 32 | + | ||
| 33 | + @Before | ||
| 34 | + public void setUp() throws Exception { | ||
| 35 | + ospfLsaLink = new OspfLsaLink(); | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + @After | ||
| 39 | + public void tearDown() throws Exception { | ||
| 40 | + ospfLsaLink = null; | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + /** | ||
| 44 | + * Tests linkId() getter method. | ||
| 45 | + */ | ||
| 46 | + @Test | ||
| 47 | + public void testGetLinkID() throws Exception { | ||
| 48 | + ospfLsaLink.setLinkId("1.1.1.1"); | ||
| 49 | + assertThat(ospfLsaLink.linkId(), is("1.1.1.1")); | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + /** | ||
| 53 | + * Tests linkId() setter method. | ||
| 54 | + */ | ||
| 55 | + @Test | ||
| 56 | + public void testSetLinkID() throws Exception { | ||
| 57 | + ospfLsaLink.setLinkId("1.1.1.1"); | ||
| 58 | + assertThat(ospfLsaLink.linkId(), is("1.1.1.1")); | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + /** | ||
| 62 | + * Tests linkData() getter method. | ||
| 63 | + */ | ||
| 64 | + @Test | ||
| 65 | + public void testGetLinkData() throws Exception { | ||
| 66 | + ospfLsaLink.setLinkData("1.1.1.1"); | ||
| 67 | + assertThat(ospfLsaLink.linkData(), is("1.1.1.1")); | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + /** | ||
| 71 | + * Tests linkData() setter method. | ||
| 72 | + */ | ||
| 73 | + @Test | ||
| 74 | + public void testSetLinkData() throws Exception { | ||
| 75 | + ospfLsaLink.setLinkData("1.1.1.1"); | ||
| 76 | + assertThat(ospfLsaLink.linkData(), is("1.1.1.1")); | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + /** | ||
| 80 | + * Tests linkType() getter method. | ||
| 81 | + */ | ||
| 82 | + @Test | ||
| 83 | + public void testGetLinkType() throws Exception { | ||
| 84 | + ospfLsaLink.setLinkType(1); | ||
| 85 | + assertThat(ospfLsaLink.linkType(), is(1)); | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + /** | ||
| 89 | + * Tests linkType() setter method. | ||
| 90 | + */ | ||
| 91 | + @Test | ||
| 92 | + public void testSetLinkType() throws Exception { | ||
| 93 | + ospfLsaLink.setLinkType(1); | ||
| 94 | + assertThat(ospfLsaLink.linkType(), is(1)); | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + /** | ||
| 98 | + * Tests metric() getter method. | ||
| 99 | + */ | ||
| 100 | + @Test | ||
| 101 | + public void testGetMetric() throws Exception { | ||
| 102 | + ospfLsaLink.setMetric(100); | ||
| 103 | + assertThat(ospfLsaLink.metric(), is(100)); | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + /** | ||
| 107 | + * Tests metric() setter method. | ||
| 108 | + */ | ||
| 109 | + @Test | ||
| 110 | + public void testSetMetric() throws Exception { | ||
| 111 | + ospfLsaLink.setMetric(100); | ||
| 112 | + assertThat(ospfLsaLink.metric(), is(100)); | ||
| 113 | + } | ||
| 114 | + | ||
| 115 | + /** | ||
| 116 | + * Tests tos() getter method. | ||
| 117 | + */ | ||
| 118 | + @Test | ||
| 119 | + public void testGetTos() throws Exception { | ||
| 120 | + ospfLsaLink.setTos(100); | ||
| 121 | + assertThat(ospfLsaLink.tos(), is(100)); | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + /** | ||
| 125 | + * Tests tos() setter method. | ||
| 126 | + */ | ||
| 127 | + @Test | ||
| 128 | + public void testSetTos() throws Exception { | ||
| 129 | + ospfLsaLink.setTos(100); | ||
| 130 | + assertThat(ospfLsaLink.tos(), is(100)); | ||
| 131 | + } | ||
| 132 | + | ||
| 133 | + /** | ||
| 134 | + * Tests to string method. | ||
| 135 | + */ | ||
| 136 | + @Test | ||
| 137 | + public void testToString() throws Exception { | ||
| 138 | + assertThat(ospfLsaLink.toString(), is(notNullValue())); | ||
| 139 | + } | ||
| 140 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 1 | +/* | ||
| 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 | + */ | ||
| 16 | +package org.onosproject.ospf.protocol.lsa.types; | ||
| 17 | + | ||
| 18 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
| 19 | +import org.jboss.netty.buffer.ChannelBuffers; | ||
| 20 | +import org.junit.After; | ||
| 21 | +import org.junit.Before; | ||
| 22 | +import org.junit.Test; | ||
| 23 | +import org.onlab.packet.Ip4Address; | ||
| 24 | +import org.onosproject.ospf.controller.OspfLsaType; | ||
| 25 | +import org.onosproject.ospf.protocol.lsa.LsaHeader; | ||
| 26 | + | ||
| 27 | + | ||
| 28 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
| 29 | +import static org.hamcrest.Matchers.is; | ||
| 30 | +import static org.hamcrest.Matchers.notNullValue; | ||
| 31 | + | ||
| 32 | +/** | ||
| 33 | + * Unit test class for AsbrSummaryLsa. | ||
| 34 | + */ | ||
| 35 | +public class AsbrSummaryLsaTest { | ||
| 36 | + | ||
| 37 | + private final Ip4Address ipAddress = Ip4Address.valueOf("10.226.165.164"); | ||
| 38 | + private AsbrSummaryLsa asbrSummaryLsa; | ||
| 39 | + private Ip4Address result; | ||
| 40 | + private int result1; | ||
| 41 | + private int num; | ||
| 42 | + private byte[] inputByteArray; | ||
| 43 | + private byte[] result2; | ||
| 44 | + private ChannelBuffer channelBuffer; | ||
| 45 | + private LsaHeader lsaHeader; | ||
| 46 | + private OspfLsaType ospflsaType; | ||
| 47 | + private String result3; | ||
| 48 | + private boolean result4; | ||
| 49 | + | ||
| 50 | + @Before | ||
| 51 | + public void setUp() throws Exception { | ||
| 52 | + asbrSummaryLsa = new AsbrSummaryLsa(new LsaHeader()); | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + @After | ||
| 56 | + public void tearDown() throws Exception { | ||
| 57 | + asbrSummaryLsa = null; | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + /** | ||
| 61 | + * Tests networkMask() getter method. | ||
| 62 | + */ | ||
| 63 | + @Test | ||
| 64 | + public void testGetNetworkMask() throws Exception { | ||
| 65 | + asbrSummaryLsa.setNetworkMask(ipAddress); | ||
| 66 | + result = asbrSummaryLsa.networkMask(); | ||
| 67 | + assertThat(result, is(notNullValue())); | ||
| 68 | + assertThat(result, is(ipAddress)); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + /** | ||
| 72 | + * Tests networkMask() setter method. | ||
| 73 | + */ | ||
| 74 | + @Test | ||
| 75 | + public void testSetNetworkMask() throws Exception { | ||
| 76 | + asbrSummaryLsa.setNetworkMask(ipAddress); | ||
| 77 | + result = asbrSummaryLsa.networkMask(); | ||
| 78 | + assertThat(result, is(notNullValue())); | ||
| 79 | + assertThat(result, is(ipAddress)); | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + /** | ||
| 83 | + * Tests metric() getter method. | ||
| 84 | + */ | ||
| 85 | + @Test | ||
| 86 | + public void testGetMetric() throws Exception { | ||
| 87 | + num = 10; | ||
| 88 | + asbrSummaryLsa.setMetric(num); | ||
| 89 | + result1 = asbrSummaryLsa.metric(); | ||
| 90 | + assertThat(result1, is(notNullValue())); | ||
| 91 | + assertThat(result1, is(num)); | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + /** | ||
| 95 | + * Tests metric() setter method. | ||
| 96 | + */ | ||
| 97 | + @Test | ||
| 98 | + public void testSetMetric() throws Exception { | ||
| 99 | + num = 20; | ||
| 100 | + asbrSummaryLsa.setMetric(num); | ||
| 101 | + result1 = asbrSummaryLsa.metric(); | ||
| 102 | + assertThat(result1, is(notNullValue())); | ||
| 103 | + assertThat(result1, is(num)); | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + /** | ||
| 107 | + * Tests readFrom() method. | ||
| 108 | + */ | ||
| 109 | + @Test | ||
| 110 | + public void testReadFrom() throws Exception { | ||
| 111 | + inputByteArray = createByteForNetworkLsa(); | ||
| 112 | + lsaHeader = createLsaHeader(); | ||
| 113 | + asbrSummaryLsa = new AsbrSummaryLsa(lsaHeader); | ||
| 114 | + channelBuffer = ChannelBuffers.copiedBuffer(inputByteArray); | ||
| 115 | + asbrSummaryLsa.readFrom(channelBuffer); | ||
| 116 | + assertThat(asbrSummaryLsa, is(notNullValue())); | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + /** | ||
| 120 | + * Tests readFrom() method. | ||
| 121 | + */ | ||
| 122 | + @Test(expected = Exception.class) | ||
| 123 | + public void testReadFrom1() throws Exception { | ||
| 124 | + byte[] temp = {0, 1, 2, 3}; | ||
| 125 | + inputByteArray = temp; | ||
| 126 | + lsaHeader = createLsaHeader(); | ||
| 127 | + asbrSummaryLsa = new AsbrSummaryLsa(lsaHeader); | ||
| 128 | + channelBuffer = ChannelBuffers.copiedBuffer(inputByteArray); | ||
| 129 | + asbrSummaryLsa.readFrom(channelBuffer); | ||
| 130 | + assertThat(asbrSummaryLsa, is(notNullValue())); | ||
| 131 | + } | ||
| 132 | + | ||
| 133 | + /** | ||
| 134 | + * Tests asBytes() method. | ||
| 135 | + */ | ||
| 136 | + @Test(expected = Exception.class) | ||
| 137 | + public void testAsBytes() throws Exception { | ||
| 138 | + result2 = asbrSummaryLsa.asBytes(); | ||
| 139 | + assertThat(result2, is(notNullValue())); | ||
| 140 | + } | ||
| 141 | + | ||
| 142 | + /** | ||
| 143 | + * Tests getLsaBodyAsByteArray() method. | ||
| 144 | + */ | ||
| 145 | + @Test(expected = Exception.class) | ||
| 146 | + public void testGetLsaBodyAsByteArray() throws Exception { | ||
| 147 | + result2 = asbrSummaryLsa.getLsaBodyAsByteArray(); | ||
| 148 | + assertThat(result2, is(notNullValue())); | ||
| 149 | + } | ||
| 150 | + | ||
| 151 | + /** | ||
| 152 | + * Tests ospfLsaType() getter method. | ||
| 153 | + */ | ||
| 154 | + @Test | ||
| 155 | + public void testGetOspfLsaType() throws Exception { | ||
| 156 | + | ||
| 157 | + ospflsaType = asbrSummaryLsa.getOspfLsaType(); | ||
| 158 | + assertThat(ospflsaType, is(notNullValue())); | ||
| 159 | + assertThat(ospflsaType, is(OspfLsaType.ASBR_SUMMARY)); | ||
| 160 | + } | ||
| 161 | + | ||
| 162 | + /** | ||
| 163 | + * Tests to string method. | ||
| 164 | + */ | ||
| 165 | + @Test | ||
| 166 | + public void testToString() throws Exception { | ||
| 167 | + | ||
| 168 | + result3 = asbrSummaryLsa.toString(); | ||
| 169 | + assertThat(result3, is(notNullValue())); | ||
| 170 | + | ||
| 171 | + } | ||
| 172 | + | ||
| 173 | + /** | ||
| 174 | + * Tests hashcode() method. | ||
| 175 | + */ | ||
| 176 | + @Test | ||
| 177 | + public void testHashcode() throws Exception { | ||
| 178 | + | ||
| 179 | + result1 = asbrSummaryLsa.hashCode(); | ||
| 180 | + assertThat(result1, is(notNullValue())); | ||
| 181 | + | ||
| 182 | + } | ||
| 183 | + | ||
| 184 | + /** | ||
| 185 | + * Tests equals() method. | ||
| 186 | + */ | ||
| 187 | + @Test | ||
| 188 | + public void testEqual() throws Exception { | ||
| 189 | + | ||
| 190 | + result4 = asbrSummaryLsa.equals(new AsbrSummaryLsa(new LsaHeader())); | ||
| 191 | + assertThat(result4, is(true)); | ||
| 192 | + | ||
| 193 | + } | ||
| 194 | + | ||
| 195 | + /** | ||
| 196 | + * Utility method used by junit methods. | ||
| 197 | + */ | ||
| 198 | + private byte[] createByteForNetworkLsa() { | ||
| 199 | + byte[] packet = {2, 1, 1, 52, -64, -88, 56, 1, -64, -88, 56, 1, 0, 100, 0, 100, 0, 0, 0, 0, 0, 0, 0, 0, -64, | ||
| 200 | + -88, 56, 1, 0, 10, 1, 1, 0, 0, 0, 40, -64, -88, 56, 1, -64, -88, 56, 1, -64, -88, 56, 1, -64, -88, 56, | ||
| 201 | + 1}; | ||
| 202 | + return packet; | ||
| 203 | + } | ||
| 204 | + | ||
| 205 | + /** | ||
| 206 | + * Utility method used by junit methods. | ||
| 207 | + */ | ||
| 208 | + private LsaHeader createLsaHeader() { | ||
| 209 | + lsaHeader = new LsaHeader(); | ||
| 210 | + lsaHeader.setLsType(1); | ||
| 211 | + lsaHeader.setLsPacketLen(48); | ||
| 212 | + lsaHeader.setLsCheckSum(10); | ||
| 213 | + lsaHeader.setAge(4); | ||
| 214 | + lsaHeader.setLinkStateId("10.226.165.164"); | ||
| 215 | + lsaHeader.setLsSequenceNo(250); | ||
| 216 | + lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165")); | ||
| 217 | + lsaHeader.setOptions(2); | ||
| 218 | + return lsaHeader; | ||
| 219 | + } | ||
| 220 | +} |
protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/lsa/types/ExternalLsaTest.java
0 → 100644
| 1 | +/* | ||
| 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 | + */ | ||
| 16 | +package org.onosproject.ospf.protocol.lsa.types; | ||
| 17 | + | ||
| 18 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
| 19 | +import org.jboss.netty.buffer.ChannelBuffers; | ||
| 20 | +import org.junit.After; | ||
| 21 | +import org.junit.Before; | ||
| 22 | +import org.junit.Test; | ||
| 23 | +import org.onlab.packet.Ip4Address; | ||
| 24 | +import org.onosproject.ospf.controller.OspfLsaType; | ||
| 25 | +import org.onosproject.ospf.protocol.lsa.LsaHeader; | ||
| 26 | +import org.onosproject.ospf.protocol.lsa.subtypes.OspfExternalDestination; | ||
| 27 | + | ||
| 28 | + | ||
| 29 | +import java.net.InetAddress; | ||
| 30 | +import java.net.UnknownHostException; | ||
| 31 | +import java.util.Vector; | ||
| 32 | + | ||
| 33 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
| 34 | +import static org.hamcrest.Matchers.is; | ||
| 35 | +import static org.hamcrest.Matchers.notNullValue; | ||
| 36 | + | ||
| 37 | +/** | ||
| 38 | + * Unit test class for ExternalLsa. | ||
| 39 | + */ | ||
| 40 | +public class ExternalLsaTest { | ||
| 41 | + | ||
| 42 | + private ExternalLsa externalLsa; | ||
| 43 | + private Vector<OspfExternalDestination> externalDestinations = new Vector<OspfExternalDestination>(); | ||
| 44 | + private Ip4Address result; | ||
| 45 | + private OspfExternalDestination ospfExternalDestination; | ||
| 46 | + private OspfExternalDestination ospfExternalDestination1; | ||
| 47 | + private LsaHeader lsaHeader; | ||
| 48 | + private byte[] inputByteArray; | ||
| 49 | + private ChannelBuffer channelBuffer; | ||
| 50 | + private byte[] result1; | ||
| 51 | + private OspfLsaType ospflsaType; | ||
| 52 | + private int result2; | ||
| 53 | + | ||
| 54 | + @Before | ||
| 55 | + public void setUp() throws Exception { | ||
| 56 | + externalLsa = new ExternalLsa(new LsaHeader()); | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + @After | ||
| 60 | + public void tearDown() throws Exception { | ||
| 61 | + externalLsa = null; | ||
| 62 | + externalDestinations = null; | ||
| 63 | + result = null; | ||
| 64 | + ospfExternalDestination = null; | ||
| 65 | + ospfExternalDestination1 = null; | ||
| 66 | + lsaHeader = null; | ||
| 67 | + inputByteArray = null; | ||
| 68 | + channelBuffer = null; | ||
| 69 | + result1 = null; | ||
| 70 | + ospflsaType = null; | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + /** | ||
| 74 | + * Tests networkMask() getter method. | ||
| 75 | + */ | ||
| 76 | + @Test | ||
| 77 | + public void testGetNetworkMask() throws Exception { | ||
| 78 | + externalLsa.setNetworkMask(Ip4Address.valueOf("10.226.165.164")); | ||
| 79 | + result = externalLsa.networkMask(); | ||
| 80 | + assertThat(result, is(notNullValue())); | ||
| 81 | + assertThat(result, is(Ip4Address.valueOf("10.226.165.164"))); | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + /** | ||
| 85 | + * Tests networkMask() setter method. | ||
| 86 | + */ | ||
| 87 | + @Test | ||
| 88 | + public void testSetNetworkMask() throws Exception { | ||
| 89 | + externalLsa.setNetworkMask(Ip4Address.valueOf("10.226.165.164")); | ||
| 90 | + result = externalLsa.networkMask(); | ||
| 91 | + assertThat(result, is(notNullValue())); | ||
| 92 | + assertThat(result, is(Ip4Address.valueOf("10.226.165.164"))); | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + /** | ||
| 96 | + * Tests addExternalDesitnation() method. | ||
| 97 | + */ | ||
| 98 | + @Test | ||
| 99 | + public void testAddExternalDesitnation() throws Exception { | ||
| 100 | + externalLsa.addExternalDestination(createOspfExternalDestination()); | ||
| 101 | + assertThat(externalLsa, is(notNullValue())); | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + /** | ||
| 105 | + * Tests hashCode() method. | ||
| 106 | + */ | ||
| 107 | + @Test | ||
| 108 | + public void testHashcode() throws Exception { | ||
| 109 | + | ||
| 110 | + result2 = externalLsa.hashCode(); | ||
| 111 | + assertThat(result2, is(notNullValue())); | ||
| 112 | + | ||
| 113 | + } | ||
| 114 | + | ||
| 115 | + /** | ||
| 116 | + * Tests readFrom() method. | ||
| 117 | + */ | ||
| 118 | + @Test | ||
| 119 | + public void testReadFrom() throws Exception { | ||
| 120 | + ospfExternalDestination = new OspfExternalDestination(); | ||
| 121 | + ospfExternalDestination.setExternalRouterTag(2); | ||
| 122 | + ospfExternalDestination.setMetric(100); | ||
| 123 | + ospfExternalDestination.setType1orType2Metric(true); | ||
| 124 | + externalLsa.addExternalDestination(ospfExternalDestination); | ||
| 125 | + ospfExternalDestination1 = new OspfExternalDestination(); | ||
| 126 | + ospfExternalDestination.setExternalRouterTag(3); | ||
| 127 | + ospfExternalDestination.setMetric(50); | ||
| 128 | + ospfExternalDestination.setType1orType2Metric(true); | ||
| 129 | + externalLsa.addExternalDestination(ospfExternalDestination1); | ||
| 130 | + ospfExternalDestination.setForwardingAddress(Ip4Address.valueOf(InetAddress.getLocalHost())); | ||
| 131 | + inputByteArray = createByteForNetworkLsa(); | ||
| 132 | + lsaHeader = createLsaHeader(); | ||
| 133 | + externalLsa = new ExternalLsa(lsaHeader); | ||
| 134 | + channelBuffer = ChannelBuffers.copiedBuffer(inputByteArray); | ||
| 135 | + externalLsa.readFrom(channelBuffer); | ||
| 136 | + assertThat(externalLsa, is(notNullValue())); | ||
| 137 | + } | ||
| 138 | + | ||
| 139 | + /** | ||
| 140 | + * Tests readFrom() method. | ||
| 141 | + */ | ||
| 142 | + @Test(expected = Exception.class) | ||
| 143 | + public void testReadFrom1() throws Exception { | ||
| 144 | + ospfExternalDestination = new OspfExternalDestination(); | ||
| 145 | + ospfExternalDestination.setExternalRouterTag(2); | ||
| 146 | + ospfExternalDestination.setMetric(100); | ||
| 147 | + ospfExternalDestination.setType1orType2Metric(true); | ||
| 148 | + externalLsa.addExternalDestination(ospfExternalDestination); | ||
| 149 | + ospfExternalDestination1 = new OspfExternalDestination(); | ||
| 150 | + ospfExternalDestination.setExternalRouterTag(3); | ||
| 151 | + ospfExternalDestination.setMetric(50); | ||
| 152 | + ospfExternalDestination.setType1orType2Metric(true); | ||
| 153 | + externalLsa.addExternalDestination(ospfExternalDestination1); | ||
| 154 | + ospfExternalDestination.setForwardingAddress(Ip4Address.valueOf(InetAddress.getLocalHost())); | ||
| 155 | + byte[] temp = {0, 0, 0}; | ||
| 156 | + inputByteArray = temp; | ||
| 157 | + lsaHeader = createLsaHeader(); | ||
| 158 | + externalLsa = new ExternalLsa(lsaHeader); | ||
| 159 | + channelBuffer = ChannelBuffers.copiedBuffer(inputByteArray); | ||
| 160 | + externalLsa.readFrom(channelBuffer); | ||
| 161 | + assertThat(externalLsa, is(notNullValue())); | ||
| 162 | + } | ||
| 163 | + | ||
| 164 | + /** | ||
| 165 | + * Tests asBytes() method. | ||
| 166 | + */ | ||
| 167 | + @Test | ||
| 168 | + public void testAsBytes() throws Exception { | ||
| 169 | + result1 = externalLsa.asBytes(); | ||
| 170 | + assertThat(result1, is(notNullValue())); | ||
| 171 | + } | ||
| 172 | + | ||
| 173 | + /** | ||
| 174 | + * Tests getLsaBodyAsByteArray() method. | ||
| 175 | + */ | ||
| 176 | + @Test | ||
| 177 | + public void testGetLsaBodyAsByteArray() throws Exception { | ||
| 178 | + ospfExternalDestination = new OspfExternalDestination(); | ||
| 179 | + ospfExternalDestination.setExternalRouterTag(2); | ||
| 180 | + ospfExternalDestination.setMetric(100); | ||
| 181 | + ospfExternalDestination.setType1orType2Metric(true); | ||
| 182 | + externalLsa.addExternalDestination(ospfExternalDestination); | ||
| 183 | + ospfExternalDestination1 = new OspfExternalDestination(); | ||
| 184 | + ospfExternalDestination.setExternalRouterTag(3); | ||
| 185 | + ospfExternalDestination.setMetric(100); | ||
| 186 | + ospfExternalDestination.setType1orType2Metric(true); | ||
| 187 | + externalLsa.addExternalDestination(ospfExternalDestination1); | ||
| 188 | + result1 = externalLsa.getLsaBodyAsByteArray(); | ||
| 189 | + assertThat(result1, is(notNullValue())); | ||
| 190 | + } | ||
| 191 | + | ||
| 192 | + /** | ||
| 193 | + * Tests getLsaBodyAsByteArray() method. | ||
| 194 | + */ | ||
| 195 | + @Test | ||
| 196 | + public void testGetLsaBodyAsByteArray1() throws Exception { | ||
| 197 | + externalLsa.setNetworkMask(Ip4Address.valueOf("255.255.255.255")); | ||
| 198 | + ospfExternalDestination = new OspfExternalDestination(); | ||
| 199 | + ospfExternalDestination.setExternalRouterTag(2); | ||
| 200 | + ospfExternalDestination.setMetric(100); | ||
| 201 | + ospfExternalDestination.setType1orType2Metric(true); | ||
| 202 | + externalLsa.addExternalDestination(ospfExternalDestination); | ||
| 203 | + ospfExternalDestination1 = new OspfExternalDestination(); | ||
| 204 | + ospfExternalDestination.setExternalRouterTag(3); | ||
| 205 | + ospfExternalDestination.setMetric(100); | ||
| 206 | + ospfExternalDestination.setType1orType2Metric(true); | ||
| 207 | + externalLsa.addExternalDestination(ospfExternalDestination1); | ||
| 208 | + result1 = externalLsa.getLsaBodyAsByteArray(); | ||
| 209 | + assertThat(result1, is(notNullValue())); | ||
| 210 | + } | ||
| 211 | + | ||
| 212 | + /** | ||
| 213 | + * Tests getOspfLsaType() getter method. | ||
| 214 | + */ | ||
| 215 | + @Test | ||
| 216 | + public void testGetOspfLsaType() throws Exception { | ||
| 217 | + ospflsaType = externalLsa.getOspfLsaType(); | ||
| 218 | + assertThat(ospflsaType, is(notNullValue())); | ||
| 219 | + assertThat(ospflsaType, is(OspfLsaType.EXTERNAL_LSA)); | ||
| 220 | + } | ||
| 221 | + | ||
| 222 | + /** | ||
| 223 | + * Tests to string method. | ||
| 224 | + */ | ||
| 225 | + @Test | ||
| 226 | + public void testToString() throws Exception { | ||
| 227 | + assertThat(externalLsa.toString(), is(notNullValue())); | ||
| 228 | + } | ||
| 229 | + | ||
| 230 | + /** | ||
| 231 | + * Utility method used by junit methods. | ||
| 232 | + */ | ||
| 233 | + private byte[] createByteForNetworkLsa() { | ||
| 234 | + byte[] packet = {2, 1, 1, 52, -64, -88, 56, 1, -64, -88, 56, 1, 0, 100, 0, 100, 0, 0, 0, 0, 0, 0, 0, 0, -64, | ||
| 235 | + -88, 56, 1, 0, 10, 1, 1, 0, 0, 0, 40, -64, -88, 56, 1, -64, -88, 56, 1, -64, -88, 56, 1, -64, -88, 56, | ||
| 236 | + 1}; | ||
| 237 | + return packet; | ||
| 238 | + } | ||
| 239 | + | ||
| 240 | + /** | ||
| 241 | + * Utility method used by junit methods. | ||
| 242 | + */ | ||
| 243 | + private LsaHeader createLsaHeader() { | ||
| 244 | + lsaHeader = new LsaHeader(); | ||
| 245 | + lsaHeader.setLsType(1); | ||
| 246 | + lsaHeader.setLsPacketLen(48); | ||
| 247 | + lsaHeader.setLsCheckSum(10); | ||
| 248 | + lsaHeader.setAge(4); | ||
| 249 | + lsaHeader.setLinkStateId("10.226.165.164"); | ||
| 250 | + lsaHeader.setLsSequenceNo(250); | ||
| 251 | + lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165")); | ||
| 252 | + lsaHeader.setOptions(2); | ||
| 253 | + return lsaHeader; | ||
| 254 | + } | ||
| 255 | + | ||
| 256 | + /** | ||
| 257 | + * Utility method used by junit methods. | ||
| 258 | + */ | ||
| 259 | + private OspfExternalDestination createOspfExternalDestination() throws UnknownHostException { | ||
| 260 | + ospfExternalDestination = new OspfExternalDestination(); | ||
| 261 | + ospfExternalDestination.setExternalRouterTag(1); | ||
| 262 | + ospfExternalDestination.setMetric(10); | ||
| 263 | + ospfExternalDestination.setType1orType2Metric(true); | ||
| 264 | + ospfExternalDestination.setForwardingAddress(Ip4Address.valueOf(InetAddress.getLocalHost())); | ||
| 265 | + return ospfExternalDestination; | ||
| 266 | + } | ||
| 267 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/lsa/types/NetworkLsaTest.java
0 → 100644
| 1 | +/* | ||
| 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 | + */ | ||
| 16 | +package org.onosproject.ospf.protocol.lsa.types; | ||
| 17 | + | ||
| 18 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
| 19 | +import org.jboss.netty.buffer.ChannelBuffers; | ||
| 20 | +import org.junit.After; | ||
| 21 | +import org.junit.Before; | ||
| 22 | +import org.junit.Test; | ||
| 23 | +import org.onlab.packet.Ip4Address; | ||
| 24 | +import org.onosproject.ospf.controller.OspfLsaType; | ||
| 25 | +import org.onosproject.ospf.protocol.lsa.LsaHeader; | ||
| 26 | + | ||
| 27 | + | ||
| 28 | +import java.net.InetAddress; | ||
| 29 | +import java.util.Vector; | ||
| 30 | + | ||
| 31 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
| 32 | +import static org.hamcrest.Matchers.is; | ||
| 33 | +import static org.hamcrest.Matchers.notNullValue; | ||
| 34 | + | ||
| 35 | +/** | ||
| 36 | + * Unit test class for NetworkLsa. | ||
| 37 | + */ | ||
| 38 | +public class NetworkLsaTest { | ||
| 39 | + | ||
| 40 | + private Vector<String> attachedRouters = new Vector(); | ||
| 41 | + private NetworkLsa networkLsa; | ||
| 42 | + private Ip4Address result; | ||
| 43 | + private Ip4Address inetAddres; | ||
| 44 | + private byte[] inputByteArray; | ||
| 45 | + private LsaHeader lsaHeader; | ||
| 46 | + private ChannelBuffer channelBuffer; | ||
| 47 | + private byte[] result1; | ||
| 48 | + private OspfLsaType ospflsaType; | ||
| 49 | + private int result2; | ||
| 50 | + | ||
| 51 | + @Before | ||
| 52 | + public void setUp() throws Exception { | ||
| 53 | + networkLsa = new NetworkLsa(); | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + @After | ||
| 57 | + public void tearDown() throws Exception { | ||
| 58 | + networkLsa = null; | ||
| 59 | + attachedRouters = null; | ||
| 60 | + result = null; | ||
| 61 | + inetAddres = null; | ||
| 62 | + inputByteArray = null; | ||
| 63 | + lsaHeader = null; | ||
| 64 | + channelBuffer = null; | ||
| 65 | + result1 = null; | ||
| 66 | + ospflsaType = null; | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + /** | ||
| 70 | + * Tests networkMask() getter method. | ||
| 71 | + */ | ||
| 72 | + @Test | ||
| 73 | + public void testGetNetworkMask() throws Exception { | ||
| 74 | + networkLsa.setNetworkMask(Ip4Address.valueOf("10.226.165.164")); | ||
| 75 | + result = networkLsa.networkMask(); | ||
| 76 | + assertThat(result, is(Ip4Address.valueOf("10.226.165.164"))); | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + /** | ||
| 80 | + * Tests networkMask() setter method. | ||
| 81 | + */ | ||
| 82 | + @Test | ||
| 83 | + public void testSetNetworkMask() throws Exception { | ||
| 84 | + networkLsa.setNetworkMask(Ip4Address.valueOf("10.226.165.165")); | ||
| 85 | + result = networkLsa.networkMask(); | ||
| 86 | + result = networkLsa.networkMask(); | ||
| 87 | + assertThat(result, is(Ip4Address.valueOf("10.226.165.165"))); | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + /** | ||
| 91 | + * Tests addAttachedRouter() getter method. | ||
| 92 | + */ | ||
| 93 | + @Test | ||
| 94 | + public void testGetAttachedRouters() throws Exception { | ||
| 95 | + attachedRouters.add("1.1.1.1"); | ||
| 96 | + networkLsa.addAttachedRouter(Ip4Address.valueOf("1.1.1.1")); | ||
| 97 | + assertThat(attachedRouters, is(notNullValue())); | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + /** | ||
| 101 | + * Tests addAttachedRouter() setter method. | ||
| 102 | + */ | ||
| 103 | + @Test | ||
| 104 | + public void testSetAttachedRouters() throws Exception { | ||
| 105 | + attachedRouters.add("1.1.1.1"); | ||
| 106 | + networkLsa.addAttachedRouter(Ip4Address.valueOf("1.1.1.1")); | ||
| 107 | + assertThat(attachedRouters, is(notNullValue())); | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + /** | ||
| 111 | + * Tests addAttachedRouter() method. | ||
| 112 | + */ | ||
| 113 | + @Test | ||
| 114 | + public void testAddAttachedRouter() throws Exception { | ||
| 115 | + inetAddres = Ip4Address.valueOf(InetAddress.getLocalHost()); | ||
| 116 | + networkLsa.addAttachedRouter(inetAddres); | ||
| 117 | + inetAddres = Ip4Address.valueOf(InetAddress.getLocalHost()); | ||
| 118 | + networkLsa.addAttachedRouter(inetAddres); | ||
| 119 | + assertThat(networkLsa, is(notNullValue())); | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + /** | ||
| 123 | + * Tests readFrom() method. | ||
| 124 | + */ | ||
| 125 | + | ||
| 126 | + @Test | ||
| 127 | + public void testReadFrom() throws Exception { | ||
| 128 | + inputByteArray = createByteForNetworkLsa(); | ||
| 129 | + lsaHeader = createLsaHeader(); | ||
| 130 | + networkLsa = new NetworkLsa(lsaHeader); | ||
| 131 | + channelBuffer = ChannelBuffers.copiedBuffer(inputByteArray); | ||
| 132 | + networkLsa.readFrom(channelBuffer); | ||
| 133 | + assertThat(networkLsa, is(notNullValue())); | ||
| 134 | + } | ||
| 135 | + | ||
| 136 | + /** | ||
| 137 | + * Tests readFrom() method. | ||
| 138 | + */ | ||
| 139 | + @Test(expected = Exception.class) | ||
| 140 | + public void testReadFrom1() throws Exception { | ||
| 141 | + byte[] temp = {0, 0, 0}; | ||
| 142 | + inputByteArray = temp; | ||
| 143 | + lsaHeader = createLsaHeader(); | ||
| 144 | + networkLsa = new NetworkLsa(lsaHeader); | ||
| 145 | + channelBuffer = ChannelBuffers.copiedBuffer(inputByteArray); | ||
| 146 | + networkLsa.readFrom(channelBuffer); | ||
| 147 | + assertThat(networkLsa, is(notNullValue())); | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + /** | ||
| 151 | + * Tests asBytes() method. | ||
| 152 | + */ | ||
| 153 | + @Test(expected = Exception.class) | ||
| 154 | + public void testAsBytes() throws Exception { | ||
| 155 | + result1 = networkLsa.asBytes(); | ||
| 156 | + assertThat(result1, is(notNullValue())); | ||
| 157 | + } | ||
| 158 | + | ||
| 159 | + /** | ||
| 160 | + * Tests getLsaBodyAsByteArray() method. | ||
| 161 | + */ | ||
| 162 | + @Test(expected = Exception.class) | ||
| 163 | + public void testGetLsaBodyAsByteArray() throws Exception { | ||
| 164 | + networkLsa.addAttachedRouter(Ip4Address.valueOf("1.1.1.1")); | ||
| 165 | + networkLsa.addAttachedRouter(Ip4Address.valueOf("2.2.2.2")); | ||
| 166 | + networkLsa.addAttachedRouter(Ip4Address.valueOf("3.3.3.3")); | ||
| 167 | + result1 = networkLsa.getLSABodyAsByteArray(); | ||
| 168 | + assertThat(result1, is(notNullValue())); | ||
| 169 | + } | ||
| 170 | + | ||
| 171 | + /** | ||
| 172 | + * Tests getLsaBodyAsByteArray() method. | ||
| 173 | + */ | ||
| 174 | + @Test | ||
| 175 | + public void testGetLsaBodyAsByteArray1() throws Exception { | ||
| 176 | + networkLsa.setNetworkMask(Ip4Address.valueOf("255.255.255.255")); | ||
| 177 | + networkLsa.addAttachedRouter(Ip4Address.valueOf("1.1.1.1")); | ||
| 178 | + networkLsa.addAttachedRouter(Ip4Address.valueOf("2.2.2.2")); | ||
| 179 | + networkLsa.addAttachedRouter(Ip4Address.valueOf("3.3.3.3")); | ||
| 180 | + result1 = networkLsa.getLSABodyAsByteArray(); | ||
| 181 | + assertThat(result1, is(notNullValue())); | ||
| 182 | + } | ||
| 183 | + | ||
| 184 | + /** | ||
| 185 | + * Tests getOspfLsaType() getter method. | ||
| 186 | + */ | ||
| 187 | + @Test | ||
| 188 | + public void testGetOspfLsaType() throws Exception { | ||
| 189 | + networkLsa.setLsType(2); | ||
| 190 | + ospflsaType = networkLsa.getOspfLsaType(); | ||
| 191 | + assertThat(ospflsaType, is(OspfLsaType.NETWORK)); | ||
| 192 | + } | ||
| 193 | + | ||
| 194 | + /** | ||
| 195 | + * Tests hashCode() method. | ||
| 196 | + */ | ||
| 197 | + @Test | ||
| 198 | + public void testHashcode() throws Exception { | ||
| 199 | + | ||
| 200 | + result2 = networkLsa.hashCode(); | ||
| 201 | + assertThat(result2, is(notNullValue())); | ||
| 202 | + | ||
| 203 | + } | ||
| 204 | + | ||
| 205 | + /** | ||
| 206 | + * Utility method used by junit methods. | ||
| 207 | + */ | ||
| 208 | + private byte[] createByteForNetworkLsa() { | ||
| 209 | + byte[] packet = {2, 1, 1, 52, -64, -88, 56, 1, -64, -88, 56, 1, 0, 100, 0, 100, 0, 0, 0, 0, 0, 0, 0, 0, -64, | ||
| 210 | + -88, 56, 1, 0, 10, 1, 1, 0, 0, 0, 40, -64, -88, 56, 1, -64, -88, 56, 1, -64, -88, 56, 1, -64, -88, 56, | ||
| 211 | + 1}; | ||
| 212 | + return packet; | ||
| 213 | + } | ||
| 214 | + | ||
| 215 | + /** | ||
| 216 | + * Utility method used by junit methods. | ||
| 217 | + */ | ||
| 218 | + private LsaHeader createLsaHeader() { | ||
| 219 | + lsaHeader = new LsaHeader(); | ||
| 220 | + lsaHeader.setLsType(2); | ||
| 221 | + lsaHeader.setLsPacketLen(48); | ||
| 222 | + lsaHeader.setLsCheckSum(10); | ||
| 223 | + lsaHeader.setAge(4); | ||
| 224 | + lsaHeader.setLinkStateId("10.226.165.164"); | ||
| 225 | + lsaHeader.setLsSequenceNo(250); | ||
| 226 | + lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165")); | ||
| 227 | + lsaHeader.setOptions(2); | ||
| 228 | + return lsaHeader; | ||
| 229 | + } | ||
| 230 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/lsa/types/OpaqueLsa11Test.java
0 → 100644
| 1 | +/* | ||
| 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 | + */ | ||
| 16 | +package org.onosproject.ospf.protocol.lsa.types; | ||
| 17 | + | ||
| 18 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
| 19 | +import org.jboss.netty.buffer.ChannelBuffers; | ||
| 20 | +import org.junit.After; | ||
| 21 | +import org.junit.Before; | ||
| 22 | +import org.junit.Test; | ||
| 23 | +import org.onlab.packet.Ip4Address; | ||
| 24 | +import org.onosproject.ospf.controller.OspfLsaType; | ||
| 25 | +import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader; | ||
| 26 | + | ||
| 27 | + | ||
| 28 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
| 29 | +import static org.hamcrest.Matchers.is; | ||
| 30 | +import static org.hamcrest.Matchers.notNullValue; | ||
| 31 | + | ||
| 32 | +/** | ||
| 33 | + * Unit test class for OpaqueLsa11. | ||
| 34 | + */ | ||
| 35 | +public class OpaqueLsa11Test { | ||
| 36 | + | ||
| 37 | + private final byte[] packet = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 38 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 39 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | ||
| 40 | + private OpaqueLsa11 opaqueLsa11; | ||
| 41 | + private OpaqueLsaHeader opqueHeader; | ||
| 42 | + private ChannelBuffer channelBuffer; | ||
| 43 | + private byte[] result; | ||
| 44 | + private int result1; | ||
| 45 | + private String result2; | ||
| 46 | + private OspfLsaType ospflsaType; | ||
| 47 | + | ||
| 48 | + @Before | ||
| 49 | + public void setUp() throws Exception { | ||
| 50 | + opaqueLsa11 = new OpaqueLsa11(new OpaqueLsaHeader()); | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + @After | ||
| 54 | + public void tearDown() throws Exception { | ||
| 55 | + opaqueLsa11 = null; | ||
| 56 | + opqueHeader = null; | ||
| 57 | + channelBuffer = null; | ||
| 58 | + result = null; | ||
| 59 | + ospflsaType = null; | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + /** | ||
| 63 | + * Tests readFrom() method. | ||
| 64 | + */ | ||
| 65 | + @Test | ||
| 66 | + public void testReadFrom() throws Exception { | ||
| 67 | + opqueHeader = new OpaqueLsaHeader(); | ||
| 68 | + opqueHeader.setLsType(11); | ||
| 69 | + opqueHeader.setLsPacketLen(48); | ||
| 70 | + opqueHeader.setLsCheckSum(10); | ||
| 71 | + opqueHeader.setAge(4); | ||
| 72 | + opqueHeader.setOpaqueId(1); | ||
| 73 | + opqueHeader.setOpaqueType(11); | ||
| 74 | + opqueHeader.setLsSequenceNo(250); | ||
| 75 | + opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165")); | ||
| 76 | + opqueHeader.setOptions(2); | ||
| 77 | + opaqueLsa11 = new OpaqueLsa11(opqueHeader); | ||
| 78 | + channelBuffer = ChannelBuffers.copiedBuffer(packet); | ||
| 79 | + opaqueLsa11.readFrom(channelBuffer); | ||
| 80 | + assertThat(opaqueLsa11, is(notNullValue())); | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + /** | ||
| 84 | + * Tests asBytes() method. | ||
| 85 | + */ | ||
| 86 | + @Test | ||
| 87 | + public void testAsBytes() throws Exception { | ||
| 88 | + opqueHeader = new OpaqueLsaHeader(); | ||
| 89 | + opqueHeader.setLsType(11); | ||
| 90 | + opqueHeader.setLsPacketLen(48); | ||
| 91 | + opqueHeader.setLsCheckSum(10); | ||
| 92 | + opqueHeader.setAge(4); | ||
| 93 | + opqueHeader.setOpaqueId(1); | ||
| 94 | + opqueHeader.setOpaqueType(11); | ||
| 95 | + opqueHeader.setLsSequenceNo(250); | ||
| 96 | + opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165")); | ||
| 97 | + opqueHeader.setOptions(2); | ||
| 98 | + opaqueLsa11 = new OpaqueLsa11(opqueHeader); | ||
| 99 | + channelBuffer = ChannelBuffers.copiedBuffer(packet); | ||
| 100 | + opaqueLsa11.readFrom(channelBuffer); | ||
| 101 | + result = opaqueLsa11.asBytes(); | ||
| 102 | + assertThat(result, is(notNullValue())); | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + /** | ||
| 106 | + * Tests getLsaBodyAsByteArray() method. | ||
| 107 | + */ | ||
| 108 | + @Test | ||
| 109 | + public void testGetLsaBodyAsByteArray() throws Exception { | ||
| 110 | + opqueHeader = new OpaqueLsaHeader(); | ||
| 111 | + opqueHeader.setLsType(11); | ||
| 112 | + opqueHeader.setLsPacketLen(48); | ||
| 113 | + opqueHeader.setLsCheckSum(10); | ||
| 114 | + opqueHeader.setAge(4); | ||
| 115 | + opqueHeader.setOpaqueId(1); | ||
| 116 | + opqueHeader.setOpaqueType(11); | ||
| 117 | + opqueHeader.setLsSequenceNo(250); | ||
| 118 | + opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165")); | ||
| 119 | + opqueHeader.setOptions(2); | ||
| 120 | + opaqueLsa11 = new OpaqueLsa11(opqueHeader); | ||
| 121 | + channelBuffer = ChannelBuffers.copiedBuffer(packet); | ||
| 122 | + opaqueLsa11.readFrom(channelBuffer); | ||
| 123 | + result = opaqueLsa11.getLsaBodyAsByteArray(); | ||
| 124 | + assertThat(result, is(notNullValue())); | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + /** | ||
| 128 | + * Tests getOspfLsaType() method. | ||
| 129 | + */ | ||
| 130 | + @Test | ||
| 131 | + public void testGetOspfLsaType() throws Exception { | ||
| 132 | + opaqueLsa11.setLsType(11); | ||
| 133 | + ospflsaType = opaqueLsa11.getOspfLsaType(); | ||
| 134 | + assertThat(ospflsaType, is(notNullValue())); | ||
| 135 | + assertThat(ospflsaType, is(OspfLsaType.AS_OPAQUE_LSA)); | ||
| 136 | + } | ||
| 137 | + | ||
| 138 | + /** | ||
| 139 | + * Tests hashCode() method. | ||
| 140 | + */ | ||
| 141 | + @Test | ||
| 142 | + public void testHashcode() throws Exception { | ||
| 143 | + | ||
| 144 | + result1 = opaqueLsa11.hashCode(); | ||
| 145 | + assertThat(result1, is(notNullValue())); | ||
| 146 | + | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + /** | ||
| 150 | + * Tests to string method. | ||
| 151 | + */ | ||
| 152 | + @Test | ||
| 153 | + public void testToString() throws Exception { | ||
| 154 | + | ||
| 155 | + result2 = opaqueLsa11.toString(); | ||
| 156 | + assertThat(result2, is(notNullValue())); | ||
| 157 | + | ||
| 158 | + } | ||
| 159 | +} |
protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/lsa/types/OpaqueLsa9Test.java
0 → 100644
| 1 | +/* | ||
| 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 | + */ | ||
| 16 | +package org.onosproject.ospf.protocol.lsa.types; | ||
| 17 | + | ||
| 18 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
| 19 | +import org.jboss.netty.buffer.ChannelBuffers; | ||
| 20 | +import org.junit.After; | ||
| 21 | +import org.junit.Before; | ||
| 22 | +import org.junit.Test; | ||
| 23 | +import org.onlab.packet.Ip4Address; | ||
| 24 | +import org.onosproject.ospf.controller.OspfLsaType; | ||
| 25 | +import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader; | ||
| 26 | + | ||
| 27 | + | ||
| 28 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
| 29 | +import static org.hamcrest.Matchers.is; | ||
| 30 | +import static org.hamcrest.Matchers.notNullValue; | ||
| 31 | + | ||
| 32 | +/** | ||
| 33 | + * Unit test class for OpaqueLsa9. | ||
| 34 | + */ | ||
| 35 | +public class OpaqueLsa9Test { | ||
| 36 | + | ||
| 37 | + private final byte[] packet = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 38 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 39 | + 0, 0, 0, 0, 0, 0, 0, 0}; | ||
| 40 | + private byte[] result; | ||
| 41 | + private String result1; | ||
| 42 | + private OpaqueLsaHeader opqueHeader; | ||
| 43 | + private OpaqueLsa9 opaqueLsa9; | ||
| 44 | + private ChannelBuffer channelBuffer; | ||
| 45 | + private OspfLsaType ospflsaType; | ||
| 46 | + private int result2; | ||
| 47 | + | ||
| 48 | + @Before | ||
| 49 | + public void setUp() throws Exception { | ||
| 50 | + opaqueLsa9 = new OpaqueLsa9(new OpaqueLsaHeader()); | ||
| 51 | + | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + @After | ||
| 55 | + public void tearDown() throws Exception { | ||
| 56 | + opaqueLsa9 = null; | ||
| 57 | + opqueHeader = null; | ||
| 58 | + channelBuffer = null; | ||
| 59 | + result = null; | ||
| 60 | + ospflsaType = null; | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + /** | ||
| 64 | + * Tests readFrom() method. | ||
| 65 | + */ | ||
| 66 | + @Test | ||
| 67 | + public void testReadFrom() throws Exception { | ||
| 68 | + opqueHeader = new OpaqueLsaHeader(); | ||
| 69 | + opqueHeader.setLsType(1); | ||
| 70 | + opqueHeader.setLsPacketLen(48); | ||
| 71 | + opqueHeader.setLsCheckSum(10); | ||
| 72 | + opqueHeader.setAge(4); | ||
| 73 | + opqueHeader.setOpaqueId(9); | ||
| 74 | + opqueHeader.setOpaqueType(9); | ||
| 75 | + opqueHeader.setLsSequenceNo(250); | ||
| 76 | + opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165")); | ||
| 77 | + opqueHeader.setOptions(2); | ||
| 78 | + opaqueLsa9 = new OpaqueLsa9(opqueHeader); | ||
| 79 | + channelBuffer = ChannelBuffers.copiedBuffer(packet); | ||
| 80 | + opaqueLsa9.readFrom(channelBuffer); | ||
| 81 | + assertThat(opaqueLsa9, is(notNullValue())); | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + /** | ||
| 85 | + * Tests asBytes() method. | ||
| 86 | + */ | ||
| 87 | + @Test | ||
| 88 | + public void testAsBytes() throws Exception { | ||
| 89 | + opqueHeader = new OpaqueLsaHeader(); | ||
| 90 | + opqueHeader.setLsType(9); | ||
| 91 | + opqueHeader.setLsPacketLen(48); | ||
| 92 | + opqueHeader.setLsCheckSum(10); | ||
| 93 | + opqueHeader.setAge(4); | ||
| 94 | + opqueHeader.setOpaqueId(9); | ||
| 95 | + opqueHeader.setOpaqueType(9); | ||
| 96 | + opqueHeader.setLsSequenceNo(250); | ||
| 97 | + opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165")); | ||
| 98 | + opqueHeader.setOptions(2); | ||
| 99 | + opaqueLsa9 = new OpaqueLsa9(opqueHeader); | ||
| 100 | + channelBuffer = ChannelBuffers.copiedBuffer(packet); | ||
| 101 | + opaqueLsa9.readFrom(channelBuffer); | ||
| 102 | + result = opaqueLsa9.asBytes(); | ||
| 103 | + assertThat(result, is(notNullValue())); | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + /** | ||
| 107 | + * Tests getLsaBodyAsByteArray() method. | ||
| 108 | + */ | ||
| 109 | + @Test | ||
| 110 | + public void testGetLsaBodyAsByteArray() throws Exception { | ||
| 111 | + opqueHeader = new OpaqueLsaHeader(); | ||
| 112 | + opqueHeader.setLsType(9); | ||
| 113 | + opqueHeader.setLsPacketLen(48); | ||
| 114 | + opqueHeader.setLsCheckSum(10); | ||
| 115 | + opqueHeader.setAge(4); | ||
| 116 | + opqueHeader.setOpaqueId(9); | ||
| 117 | + opqueHeader.setOpaqueType(9); | ||
| 118 | + opqueHeader.setLsSequenceNo(250); | ||
| 119 | + opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165")); | ||
| 120 | + opqueHeader.setOptions(2); | ||
| 121 | + opaqueLsa9 = new OpaqueLsa9(opqueHeader); | ||
| 122 | + channelBuffer = ChannelBuffers.copiedBuffer(packet); | ||
| 123 | + opaqueLsa9.readFrom(channelBuffer); | ||
| 124 | + result = opaqueLsa9.getLsaBodyAsByteArray(); | ||
| 125 | + assertThat(result, is(notNullValue())); | ||
| 126 | + } | ||
| 127 | + | ||
| 128 | + /** | ||
| 129 | + * Tests hashCode() method. | ||
| 130 | + */ | ||
| 131 | + @Test | ||
| 132 | + public void testHashcode() throws Exception { | ||
| 133 | + | ||
| 134 | + result2 = opaqueLsa9.hashCode(); | ||
| 135 | + assertThat(result2, is(notNullValue())); | ||
| 136 | + | ||
| 137 | + } | ||
| 138 | + | ||
| 139 | + /** | ||
| 140 | + * Tests to string method. | ||
| 141 | + */ | ||
| 142 | + @Test | ||
| 143 | + public void testToString() throws Exception { | ||
| 144 | + | ||
| 145 | + result1 = opaqueLsa9.toString(); | ||
| 146 | + assertThat(result1, is(notNullValue())); | ||
| 147 | + | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + /** | ||
| 151 | + * Tests to getOspfLsaType() getter method. | ||
| 152 | + */ | ||
| 153 | + @Test | ||
| 154 | + public void testGetOspfLsaType() throws Exception { | ||
| 155 | + opaqueLsa9.setLsType(9); | ||
| 156 | + ospflsaType = opaqueLsa9.getOspfLsaType(); | ||
| 157 | + assertThat(ospflsaType, is(notNullValue())); | ||
| 158 | + assertThat(ospflsaType, is(OspfLsaType.LINK_LOCAL_OPAQUE_LSA)); | ||
| 159 | + } | ||
| 160 | +} |
protocols/ospf/protocol/src/test/java/org/onosproject/ospf/protocol/lsa/types/SummaryLsaTest.java
0 → 100644
| 1 | +/* | ||
| 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 | + */ | ||
| 16 | +package org.onosproject.ospf.protocol.lsa.types; | ||
| 17 | + | ||
| 18 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
| 19 | +import org.jboss.netty.buffer.ChannelBuffers; | ||
| 20 | +import org.junit.After; | ||
| 21 | +import org.junit.Before; | ||
| 22 | +import org.junit.Test; | ||
| 23 | +import org.onlab.packet.Ip4Address; | ||
| 24 | +import org.onosproject.ospf.controller.OspfLsaType; | ||
| 25 | +import org.onosproject.ospf.protocol.lsa.LsaHeader; | ||
| 26 | + | ||
| 27 | + | ||
| 28 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
| 29 | +import static org.hamcrest.Matchers.is; | ||
| 30 | +import static org.hamcrest.Matchers.notNullValue; | ||
| 31 | + | ||
| 32 | +/** | ||
| 33 | + * Unit test class for SummaryLsa. | ||
| 34 | + */ | ||
| 35 | +public class SummaryLsaTest { | ||
| 36 | + | ||
| 37 | + private SummaryLsa summaryLsa; | ||
| 38 | + private Ip4Address result; | ||
| 39 | + private int result1; | ||
| 40 | + private byte[] inputByteArray; | ||
| 41 | + private LsaHeader lsaHeader; | ||
| 42 | + private ChannelBuffer channelBuffer; | ||
| 43 | + private byte[] result2; | ||
| 44 | + private OspfLsaType ospflsaType; | ||
| 45 | + | ||
| 46 | + @Before | ||
| 47 | + public void setUp() throws Exception { | ||
| 48 | + summaryLsa = new SummaryLsa(new LsaHeader()); | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + @After | ||
| 52 | + public void tearDown() throws Exception { | ||
| 53 | + summaryLsa = null; | ||
| 54 | + result = null; | ||
| 55 | + inputByteArray = null; | ||
| 56 | + lsaHeader = null; | ||
| 57 | + channelBuffer = null; | ||
| 58 | + result2 = null; | ||
| 59 | + ospflsaType = null; | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + /** | ||
| 63 | + * Tests networkMask() getter method. | ||
| 64 | + */ | ||
| 65 | + @Test | ||
| 66 | + public void testGetNetworkMask() throws Exception { | ||
| 67 | + summaryLsa.setNetworkMask(Ip4Address.valueOf("10.226.165.164")); | ||
| 68 | + result = summaryLsa.networkMask(); | ||
| 69 | + assertThat(result, is(Ip4Address.valueOf("10.226.165.164"))); | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + /** | ||
| 73 | + * Tests networkMask() setter method. | ||
| 74 | + */ | ||
| 75 | + @Test | ||
| 76 | + public void testSetNetworkMask() throws Exception { | ||
| 77 | + summaryLsa.setNetworkMask(Ip4Address.valueOf("10.226.165.164")); | ||
| 78 | + result = summaryLsa.networkMask(); | ||
| 79 | + assertThat(result, is(Ip4Address.valueOf("10.226.165.164"))); | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + /** | ||
| 83 | + * Tests metric() getter method. | ||
| 84 | + */ | ||
| 85 | + @Test | ||
| 86 | + public void testGetMetric() throws Exception { | ||
| 87 | + summaryLsa.setMetric(10); | ||
| 88 | + result1 = summaryLsa.metric(); | ||
| 89 | + assertThat(result1, is(10)); | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | + /** | ||
| 93 | + * Tests metric() setter method. | ||
| 94 | + */ | ||
| 95 | + @Test | ||
| 96 | + public void testSetMetric() throws Exception { | ||
| 97 | + summaryLsa.setMetric(20); | ||
| 98 | + result1 = summaryLsa.metric(); | ||
| 99 | + assertThat(result1, is(20)); | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + /** | ||
| 103 | + * Tests readFrom() method. | ||
| 104 | + */ | ||
| 105 | + @Test | ||
| 106 | + public void testReadFrom() throws Exception { | ||
| 107 | + inputByteArray = createByteForNetworkLsa(); | ||
| 108 | + lsaHeader = createLsaHeader(); | ||
| 109 | + summaryLsa = new SummaryLsa(lsaHeader); | ||
| 110 | + channelBuffer = ChannelBuffers.copiedBuffer(inputByteArray); | ||
| 111 | + summaryLsa.readFrom(channelBuffer); | ||
| 112 | + assertThat(summaryLsa, is(notNullValue())); | ||
| 113 | + } | ||
| 114 | + | ||
| 115 | + /** | ||
| 116 | + * Tests readFrom() method. | ||
| 117 | + */ | ||
| 118 | + @Test(expected = Exception.class) | ||
| 119 | + public void testReadFrom1() throws Exception { | ||
| 120 | + byte[] temp = {0, 0, 0}; | ||
| 121 | + inputByteArray = temp; | ||
| 122 | + lsaHeader = createLsaHeader(); | ||
| 123 | + summaryLsa = new SummaryLsa(lsaHeader); | ||
| 124 | + channelBuffer = ChannelBuffers.copiedBuffer(inputByteArray); | ||
| 125 | + summaryLsa.readFrom(channelBuffer); | ||
| 126 | + assertThat(summaryLsa, is(notNullValue())); | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + /** | ||
| 130 | + * Tests asBytes() method. | ||
| 131 | + */ | ||
| 132 | + @Test | ||
| 133 | + public void testAsBytes() throws Exception { | ||
| 134 | + result2 = summaryLsa.asBytes(); | ||
| 135 | + assertThat(result2, is(notNullValue())); | ||
| 136 | + } | ||
| 137 | + | ||
| 138 | + /** | ||
| 139 | + * Tests getLsaBodyAsByteArray() method. | ||
| 140 | + */ | ||
| 141 | + @Test | ||
| 142 | + public void testGetLsaBodyAsByteArray() throws Exception { | ||
| 143 | + result2 = summaryLsa.getLsaBodyAsByteArray(); | ||
| 144 | + assertThat(result2, is(notNullValue())); | ||
| 145 | + } | ||
| 146 | + | ||
| 147 | + /** | ||
| 148 | + * Tests getOspfLsaType() getter method. | ||
| 149 | + */ | ||
| 150 | + @Test | ||
| 151 | + public void testGetOspfLsaType() throws Exception { | ||
| 152 | + ospflsaType = summaryLsa.getOspfLsaType(); | ||
| 153 | + assertThat(ospflsaType, is(notNullValue())); | ||
| 154 | + assertThat(ospflsaType, is(OspfLsaType.SUMMARY)); | ||
| 155 | + } | ||
| 156 | + | ||
| 157 | + /** | ||
| 158 | + * Utility method used by junit methods. | ||
| 159 | + */ | ||
| 160 | + private byte[] createByteForNetworkLsa() { | ||
| 161 | + byte[] packet = {2, 1, 1, 52, -64, -88, 56, 1, -64, -88, 56, 1, 0, 100, 0, 100, 0, 0, 0, 0, 0, 0, 0, 0, -64, | ||
| 162 | + -88, 56, 1, 0, 10, 1, 1, 0, 0, 0, 40, -64, -88, 56, 1, -64, -88, 56, 1, -64, -88, 56, 1, -64, -88, 56, | ||
| 163 | + 1}; | ||
| 164 | + return packet; | ||
| 165 | + } | ||
| 166 | + | ||
| 167 | + /** | ||
| 168 | + * Utility method used by junit methods. | ||
| 169 | + */ | ||
| 170 | + private LsaHeader createLsaHeader() { | ||
| 171 | + lsaHeader = new LsaHeader(); | ||
| 172 | + lsaHeader.setLsType(3); | ||
| 173 | + lsaHeader.setLsPacketLen(48); | ||
| 174 | + lsaHeader.setLsCheckSum(10); | ||
| 175 | + lsaHeader.setAge(4); | ||
| 176 | + lsaHeader.setLinkStateId("10.226.165.164"); | ||
| 177 | + lsaHeader.setLsSequenceNo(250); | ||
| 178 | + lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165")); | ||
| 179 | + lsaHeader.setOptions(2); | ||
| 180 | + return lsaHeader; | ||
| 181 | + } | ||
| 182 | + | ||
| 183 | + /** | ||
| 184 | + * Tests hashcode() method. | ||
| 185 | + */ | ||
| 186 | + @Test | ||
| 187 | + public void testHashcode() throws Exception { | ||
| 188 | + | ||
| 189 | + result1 = summaryLsa.hashCode(); | ||
| 190 | + assertThat(result1, is(notNullValue())); | ||
| 191 | + | ||
| 192 | + } | ||
| 193 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment