Mani
Committed by Gerrit Code Review

ONOS-2739 - OSPF Basic Packet Structures , which includes encoding and decoding

Change-Id: I33b48593ec38d28bdff215ce3a608a6d085a9077
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.ospfpacket.subtype;
17 +
18 +import com.google.common.base.MoreObjects;
19 +
20 +/**
21 + * Representation of an LS Request packet and fields and access methods to access it.
22 + */
23 +public class LsRequestPacket {
24 +
25 + private int lsType;
26 + private String linkStateId;
27 + private String ownRouterId;
28 +
29 + /**
30 + * Gets the LSA type.
31 + *
32 + * @return LSA type
33 + */
34 + public int lsType() {
35 + return lsType;
36 + }
37 +
38 + /**
39 + * Sets the LSA type.
40 + *
41 + * @param lsType LSA type
42 + */
43 + public void setLsType(int lsType) {
44 + this.lsType = lsType;
45 + }
46 +
47 + /**
48 + * Gets the link state id.
49 + *
50 + * @return link state id
51 + */
52 + public String linkStateId() {
53 + return linkStateId;
54 + }
55 +
56 + /**
57 + * Sets link state id.
58 + *
59 + * @param linkStateId state id
60 + */
61 + public void setLinkStateId(String linkStateId) {
62 + this.linkStateId = linkStateId;
63 + }
64 +
65 + /**
66 + * Gets the router id.
67 + *
68 + * @return router id
69 + */
70 + public String ownRouterId() {
71 + return ownRouterId;
72 + }
73 +
74 + /**
75 + * Sets the router id.
76 + *
77 + * @param ownRouterId router id
78 + */
79 + public void setOwnRouterId(String ownRouterId) {
80 + this.ownRouterId = ownRouterId;
81 + }
82 +
83 + @Override
84 + public String toString() {
85 + return MoreObjects.toStringHelper(getClass())
86 + .omitNullValues()
87 + .add("lsType", lsType)
88 + .add("linkStateId", linkStateId)
89 + .add("ownRouterId", ownRouterId)
90 + .toString();
91 + }
92 +}
...\ 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 +
17 +/**
18 + * Implementation of the OSPF packet sub types which is used in OSPF packets..
19 + */
20 +package org.onosproject.ospf.protocol.ospfpacket.subtype;
...\ 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.ospfpacket.types;
17 +
18 +import com.google.common.base.MoreObjects;
19 +import com.google.common.primitives.Bytes;
20 +import org.jboss.netty.buffer.ChannelBuffer;
21 +import org.onosproject.ospf.exceptions.OspfErrorType;
22 +import org.onosproject.ospf.exceptions.OspfParseException;
23 +import org.onosproject.ospf.protocol.lsa.LsaHeader;
24 +import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
25 +import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader;
26 +import org.onosproject.ospf.protocol.util.OspfPacketType;
27 +import org.onosproject.ospf.protocol.util.OspfParameters;
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 OSPF Link State Acknowledgment Message.
37 + * Link State Acknowledgment Packets are OSPF packet type 5.
38 + * To make the flooding of LSAs reliable, flooded LSAs are explicitly
39 + * acknowledged. This acknowledgment is accomplished through the
40 + * sending and receiving of Link State Acknowledgment packets.
41 + * Multiple LSAs can be acknowledged in a single Link State Acknowledgment packet.
42 + */
43 +public class LsAcknowledge extends OspfPacketHeader {
44 + /*
45 + 0 1 2 3
46 + 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
47 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 + | Version # | 5 | Packet length |
49 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 + | Router ID |
51 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 + | Area ID |
53 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 + | Checksum | AuType |
55 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56 + | Authentication |
57 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 + | Authentication |
59 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60 + | |
61 + +- -+
62 + | |
63 + +- An LSA Header -+
64 + | |
65 + +- -+
66 + | |
67 + +- -+
68 + | |
69 + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
70 + | ... |
71 + */
72 + private static final Logger log = LoggerFactory.getLogger(LsAcknowledge.class);
73 + private List<LsaHeader> linkStateHeaders = new ArrayList<>();
74 +
75 + /**
76 + * Creates an instance of Link State Acknowledgment instance.
77 + */
78 + public LsAcknowledge() {
79 + }
80 +
81 + /**
82 + * Creates an instance of Link State Acknowledgment instance.
83 + *
84 + * @param ospfHeader OSPF header instance.
85 + */
86 + public LsAcknowledge(OspfPacketHeader ospfHeader) {
87 + populateHeader(ospfHeader);
88 + }
89 +
90 + /**
91 + * Gets ls headers.
92 + *
93 + * @return ls headers
94 + */
95 + public List<LsaHeader> getLinkStateHeaders() {
96 + return linkStateHeaders;
97 + }
98 +
99 + /**
100 + * Adds link state header to list.
101 + *
102 + * @param lsaHeader LSA header
103 + */
104 + public void addLinkStateHeader(LsaHeader lsaHeader) {
105 + if (!linkStateHeaders.contains(lsaHeader)) {
106 + linkStateHeaders.add(lsaHeader);
107 + }
108 + }
109 +
110 + @Override
111 + public OspfPacketType ospfMessageType() {
112 + return OspfPacketType.LSAACK;
113 + }
114 +
115 + @Override
116 + public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException {
117 + try {
118 + //add all the LSA Headers - one header is of 20 bytes
119 + while (channelBuffer.readableBytes() >= OspfUtil.LSA_HEADER_LENGTH) {
120 + LsaHeader header = OspfUtil.readLsaHeader(channelBuffer);
121 + //add the LSAHeader to acknowledge
122 + addLinkStateHeader(header);
123 + }
124 +
125 + } catch (Exception e) {
126 + log.debug("Error::LsAckPacket:: {}", e.getMessage());
127 + throw new OspfParseException(OspfErrorType.MESSAGE_HEADER_ERROR, OspfErrorType.BAD_MESSAGE_LENGTH);
128 + }
129 + }
130 +
131 + @Override
132 + public byte[] asBytes() {
133 + byte[] lsAckMessage = null;
134 +
135 + byte[] lsAckHeader = getLsAckAsByteArray();
136 + byte[] lsAckBody = getLsAckBodyAsByteArray();
137 + lsAckMessage = Bytes.concat(lsAckHeader, lsAckBody);
138 +
139 + return lsAckMessage;
140 + }
141 +
142 + /**
143 + * Gets LSAcknowledge as byte array.
144 + *
145 + * @return byte array
146 + */
147 + public byte[] getLsAckAsByteArray() {
148 + List<Byte> headerLst = new ArrayList<>();
149 + try {
150 + headerLst.add((byte) this.ospfVersion());
151 + headerLst.add((byte) this.ospfType());
152 + headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.ospfPacLength())));
153 + headerLst.addAll(Bytes.asList(this.routerId().toOctets()));
154 + headerLst.addAll(Bytes.asList(this.areaId().toOctets()));
155 + headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.checksum())));
156 + headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.authType())));
157 + //Authentication is 0 always. Total 8 bytes consist of zero
158 + byte[] auth = new byte[OspfUtil.EIGHT_BYTES];
159 + headerLst.addAll(Bytes.asList(auth));
160 + } catch (Exception e) {
161 + log.debug("Error::LsAckPacket:: {}", e.getMessage());
162 + return Bytes.toArray(headerLst);
163 + }
164 +
165 + return Bytes.toArray(headerLst);
166 + }
167 +
168 + /**
169 + * Gets LsAck body as byte array.
170 + *
171 + * @return byte array
172 + */
173 + public byte[] getLsAckBodyAsByteArray() {
174 + List<Byte> bodyLst = new ArrayList<>();
175 +
176 + try {
177 + for (LsaHeader lsaHeader : linkStateHeaders) {
178 + if (lsaHeader.lsType() == OspfParameters.LINK_LOCAL_OPAQUE_LSA ||
179 + lsaHeader.lsType() == OspfParameters.AREA_LOCAL_OPAQUE_LSA ||
180 + lsaHeader.lsType() == OspfParameters.AS_OPAQUE_LSA) {
181 + OpaqueLsaHeader header = (OpaqueLsaHeader) lsaHeader;
182 + bodyLst.addAll(Bytes.asList(header.getOpaqueLsaHeaderAsByteArray()));
183 + } else {
184 + bodyLst.addAll(Bytes.asList(lsaHeader.getLsaHeaderAsByteArray()));
185 + }
186 + }
187 + } catch (Exception e) {
188 + log.debug("Error::getLsAckBodyAsByteArray {}", e.getMessage());
189 + return Bytes.toArray(bodyLst);
190 + }
191 +
192 + return Bytes.toArray(bodyLst);
193 + }
194 +
195 + @Override
196 + public String toString() {
197 + return MoreObjects.toStringHelper(getClass())
198 + .omitNullValues()
199 + .add("linkStateHeaders", linkStateHeaders)
200 + .toString();
201 + }
202 +}
...\ 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.util;
17 +
18 +/**
19 + * Representation of different OSPF packet types.
20 + */
21 +public enum OspfPacketType {
22 +
23 + HELLO(1),
24 + DD(2),
25 + LSREQUEST(3),
26 + LSUPDATE(4),
27 + LSAACK(5);
28 +
29 + private int value;
30 +
31 + /**
32 + * Creates instance of OSPF packet types.
33 + *
34 + * @param value
35 + */
36 + OspfPacketType(int value) {
37 + this.value = value;
38 + }
39 +
40 + /**
41 + * Gets the value.
42 + *
43 + * @return value
44 + */
45 + public int value() {
46 + return value;
47 + }
48 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2014 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 +
17 +/**
18 + * Implementation of the OSPF Packet.
19 + */
20 +package org.onosproject.ospf.protocol.ospfpacket.types;
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.ospfpacket.subtype;
17 +
18 +import org.junit.After;
19 +import org.junit.Before;
20 +import org.junit.Test;
21 +
22 +import static org.hamcrest.CoreMatchers.is;
23 +import static org.hamcrest.CoreMatchers.notNullValue;
24 +import static org.hamcrest.MatcherAssert.assertThat;
25 +
26 +/**
27 + * Unit test class for LsRequestPacket.
28 + */
29 +public class LsRequestPacketTest {
30 +
31 + private LsRequestPacket lsrPacket;
32 + private int result;
33 +
34 + @Before
35 + public void setUp() throws Exception {
36 + lsrPacket = new LsRequestPacket();
37 + }
38 +
39 + @After
40 + public void tearDown() throws Exception {
41 + lsrPacket = null;
42 + }
43 +
44 + /**
45 + * Tests lsType() getter method.
46 + */
47 + @Test
48 + public void testGetLsType() throws Exception {
49 + lsrPacket.setLsType(1);
50 + assertThat(lsrPacket.lsType(), is(1));
51 + }
52 +
53 + /**
54 + * Tests lsType() setter method.
55 + */
56 + @Test
57 + public void testSetLsType() throws Exception {
58 + lsrPacket.setLsType(1);
59 + assertThat(lsrPacket.lsType(), is(1));
60 + }
61 +
62 + /**
63 + * Tests linkStateId() getter method.
64 + */
65 + @Test
66 + public void testGetLinkStateId() throws Exception {
67 + lsrPacket.setLinkStateId("1.1.1.1");
68 + assertThat(lsrPacket.linkStateId(), is("1.1.1.1"));
69 + }
70 +
71 + /**
72 + * Tests linkStateId() setter method.
73 + */
74 + @Test
75 + public void testSetLinkStateId() throws Exception {
76 + lsrPacket.setLinkStateId("1.1.1.1");
77 + assertThat(lsrPacket.linkStateId(), is("1.1.1.1"));
78 + }
79 +
80 + /**
81 + * Tests ownRouterId() getter method.
82 + */
83 + @Test
84 + public void testGetOwnRouterId() throws Exception {
85 + lsrPacket.setOwnRouterId("1.1.1.1");
86 + assertThat(lsrPacket.ownRouterId(), is("1.1.1.1"));
87 + }
88 +
89 + /**
90 + * Tests ownRouterId() setter method.
91 + */
92 + @Test
93 + public void testSetOwnRouterId() throws Exception {
94 + lsrPacket.setOwnRouterId("1.1.1.1");
95 + assertThat(lsrPacket.ownRouterId(), is("1.1.1.1"));
96 + }
97 +
98 + /**
99 + * Tests to string method.
100 + */
101 + @Test
102 + public void testToString() throws Exception {
103 + assertThat(lsrPacket.toString(), is(notNullValue()));
104 + }
105 +
106 + /**
107 + * Tests equals() method.
108 + */
109 + @Test
110 + public void testEquals() throws Exception {
111 + assertThat(lsrPacket.equals(new LsRequestPacket()), is(false));
112 + }
113 +
114 + /**
115 + * Tests hashCode() method.
116 + */
117 + @Test
118 + public void testHashCode() throws Exception {
119 + result = lsrPacket.hashCode();
120 + assertThat(result, is(notNullValue()));
121 + }
122 +}
...\ 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.ospfpacket.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.protocol.ospfpacket.OspfPacketHeader;
25 +import org.onosproject.ospf.protocol.util.OspfPacketType;
26 +
27 +import java.util.Vector;
28 +
29 +import static org.hamcrest.CoreMatchers.is;
30 +import static org.hamcrest.CoreMatchers.notNullValue;
31 +import static org.junit.Assert.assertThat;
32 +
33 +/**
34 + * Unit test class for HelloPacket.
35 + */
36 +public class HelloPacketTest {
37 +
38 + private boolean result1;
39 + private OspfPacketType ospfPacketType;
40 + private OspfPacketHeader ospfPacketHeader;
41 + private HelloPacket helloPacket;
42 + private Vector<String> neighborAddress = new Vector();
43 + private Ip4Address result;
44 + private int result2;
45 + private byte[] packet;
46 + private ChannelBuffer channelBuffer;
47 + private byte[] result3;
48 +
49 + @Before
50 + public void setUp() throws Exception {
51 + helloPacket = new HelloPacket();
52 + helloPacket.setAuthType(1);
53 + helloPacket.setOspftype(2);
54 + helloPacket.setRouterId(Ip4Address.valueOf("10.226.165.164"));
55 + helloPacket.setAreaId(Ip4Address.valueOf("10.226.165.100"));
56 + helloPacket.setChecksum(201);
57 + helloPacket.setAuthentication(2);
58 + helloPacket.setOspfPacLength(48);
59 + helloPacket.setOspfVer(2);
60 +
61 + }
62 +
63 + @After
64 + public void tearDown() throws Exception {
65 + helloPacket = null;
66 + result = null;
67 + ospfPacketType = null;
68 + ospfPacketHeader = null;
69 + packet = null;
70 + channelBuffer = null;
71 + result3 = null;
72 + }
73 +
74 + /**
75 + * Tests networkMask() getter method.
76 + */
77 + @Test
78 + public void testGetNetworkMask() throws Exception {
79 + helloPacket.setNetworkMask(Ip4Address.valueOf("10.226.165.164"));
80 + result = helloPacket.networkMask();
81 + assertThat(result, is(notNullValue()));
82 + assertThat(result, is(Ip4Address.valueOf("10.226.165.164")));
83 + }
84 +
85 + /**
86 + * Tests networkMask() setter method.
87 + */
88 + @Test
89 + public void testSetNetworkMask() throws Exception {
90 + helloPacket.setNetworkMask(Ip4Address.valueOf("10.226.165.164"));
91 + result = helloPacket.networkMask();
92 + assertThat(result, is(notNullValue()));
93 + assertThat(result, is(Ip4Address.valueOf("10.226.165.164")));
94 + }
95 +
96 + /**
97 + * Tests bdr() setter method.
98 + */
99 + @Test
100 + public void testSetBdr() throws Exception {
101 + helloPacket.setBdr(Ip4Address.valueOf("10.226.165.166"));
102 + result = helloPacket.bdr();
103 + assertThat(result, is(notNullValue()));
104 + assertThat(result, is(Ip4Address.valueOf("10.226.165.166")));
105 + }
106 +
107 + /**
108 + * Tests dr() getter method.
109 + */
110 + @Test
111 + public void testGetDr() throws Exception {
112 + helloPacket.setDr(Ip4Address.valueOf("10.226.165.167"));
113 + result = helloPacket.dr();
114 + assertThat(result, is(notNullValue()));
115 + assertThat(result, is(Ip4Address.valueOf("10.226.165.167")));
116 + }
117 +
118 + /**
119 + * Tests dr() setter method.
120 + */
121 + @Test
122 + public void testSetDr() throws Exception {
123 + helloPacket.setDr(Ip4Address.valueOf("10.226.165.167"));
124 + result = helloPacket.dr();
125 + assertThat(result, is(notNullValue()));
126 + assertThat(result, is(Ip4Address.valueOf("10.226.165.167")));
127 + }
128 +
129 + /**
130 + * Tests addNeighbor() method.
131 + */
132 + @Test
133 + public void testAddNeighbor() throws Exception {
134 + helloPacket.addNeighbor(Ip4Address.valueOf("10.226.165.170"));
135 + result1 = helloPacket.containsNeighbour(Ip4Address.valueOf("10.226.165.170"));
136 + assertThat(result1, is(true));
137 + }
138 +
139 + /**
140 + * Tests containsNeighbour() method.
141 + */
142 + @Test
143 + public void testContainsNeighbour() throws Exception {
144 + helloPacket.addNeighbor(Ip4Address.valueOf("10.226.165.200"));
145 + result1 = helloPacket.containsNeighbour(Ip4Address.valueOf("10.226.165.200"));
146 + assertThat(result1, is(true));
147 + }
148 +
149 +
150 + /**
151 + * Tests options() getter method.
152 + */
153 + @Test
154 + public void testGetOptions() throws Exception {
155 + helloPacket.setOptions(10);
156 + result2 = helloPacket.options();
157 + assertThat(result2, is(notNullValue()));
158 + assertThat(result2, is(10));
159 + }
160 +
161 + /**
162 + * Tests options() setter method.
163 + */
164 + @Test
165 + public void testSetOptions() throws Exception {
166 + helloPacket.setOptions(11);
167 + result2 = helloPacket.options();
168 + assertThat(result2, is(notNullValue()));
169 + assertThat(result2, is(11));
170 + }
171 +
172 + /**
173 + * Tests routerPriority() getter method.
174 + */
175 + @Test
176 + public void testGetRouterPriority() throws Exception {
177 + helloPacket.setRouterPriority(1);
178 + result2 = helloPacket.routerPriority();
179 + assertThat(result2, is(notNullValue()));
180 + assertThat(result2, is(1));
181 + }
182 +
183 + /**
184 + * Tests routerPriority() setter method.
185 + */
186 + @Test
187 + public void testSetRouterPriority() throws Exception {
188 + helloPacket.setRouterPriority(2);
189 + result2 = helloPacket.routerPriority();
190 + assertThat(result2, is(notNullValue()));
191 + assertThat(result2, is(2));
192 + }
193 +
194 + /**
195 + * Tests helloInterval() getter method.
196 + */
197 + @Test
198 + public void testGetHelloInterval() throws Exception {
199 + helloPacket.setHelloInterval(10);
200 + result2 = helloPacket.helloInterval();
201 + assertThat(result2, is(notNullValue()));
202 + assertThat(result2, is(10));
203 + }
204 +
205 + /**
206 + * Tests helloInterval() setter method.
207 + */
208 + @Test
209 + public void testSetHelloInterval() throws Exception {
210 + helloPacket.setHelloInterval(10);
211 + result2 = helloPacket.helloInterval();
212 + assertThat(result2, is(notNullValue()));
213 + assertThat(result2, is(10));
214 + }
215 +
216 + /**
217 + * Tests routerDeadInterval() getter method.
218 + */
219 + @Test
220 + public void testGetRouterDeadInterval() throws Exception {
221 + helloPacket.setRouterDeadInterval(50);
222 + result2 = helloPacket.routerDeadInterval();
223 + assertThat(result2, is(notNullValue()));
224 + assertThat(result2, is(50));
225 + }
226 +
227 + /**
228 + * Tests routerDeadInterval() setter method.
229 + */
230 + @Test
231 + public void testSetRouterDeadInterval() throws Exception {
232 + helloPacket.setRouterDeadInterval(50);
233 + result2 = helloPacket.routerDeadInterval();
234 + assertThat(result2, is(notNullValue()));
235 + assertThat(result2, is(50));
236 + }
237 +
238 + /**
239 + * Tests ospfMessageType() getter method.
240 + */
241 + @Test
242 + public void testGetOspfMessageType() throws Exception {
243 + ospfPacketType = helloPacket.ospfMessageType();
244 + assertThat(ospfPacketType, is(notNullValue()));
245 + assertThat(ospfPacketType, is(OspfPacketType.HELLO));
246 + }
247 +
248 + /**
249 + * Tests readFrom() method.
250 + */
251 + @Test
252 + public void testReadFrom() throws Exception {
253 + ospfPacketHeader = new OspfPacketHeader();
254 + ospfPacketHeader.setAreaId(Ip4Address.valueOf("1.1.1.1"));
255 + ospfPacketHeader.setAuthentication(0);
256 + ospfPacketHeader.setAuthType(0);
257 + ospfPacketHeader.setChecksum(12345);
258 + ospfPacketHeader.setDestinationIp(Ip4Address.valueOf("10.10.10.10"));
259 + ospfPacketHeader.setOspfPacLength(56);
260 + ospfPacketHeader.setOspftype(1);
261 + ospfPacketHeader.setOspfVer(2);
262 + ospfPacketHeader.setRouterId(Ip4Address.valueOf("2.2.2.2"));
263 + ospfPacketHeader.setSourceIp(Ip4Address.valueOf("3.3.3.3"));
264 + packet = createByteForHelloPacket();
265 + channelBuffer = ChannelBuffers.copiedBuffer(packet);
266 + helloPacket.readFrom(channelBuffer);
267 + assertThat(helloPacket, is(notNullValue()));
268 + assertThat(helloPacket.ospfMessageType(), is(OspfPacketType.HELLO));
269 + }
270 +
271 + /**
272 + * Tests asBytes() method.
273 + */
274 + @Test
275 + public void testAsBytes() throws Exception {
276 + result3 = helloPacket.asBytes();
277 + assertThat(result3, is(notNullValue()));
278 + }
279 +
280 + /**
281 + * Tests getHelloHeaderAsByteArray() method.
282 + */
283 + @Test
284 + public void testGetHelloHeaderAsByteArray() throws Exception {
285 + result3 = helloPacket.getHelloHeaderAsByteArray();
286 + assertThat(result3, is(notNullValue()));
287 + }
288 +
289 + /**
290 + * Tests getHelloBodyAsByteArray() method.
291 + */
292 + @Test
293 + public void testGetHelloBodyAsByteArray() throws Exception {
294 + neighborAddress.add("10.226.165.100");
295 + result3 = helloPacket.getHelloBodyAsByteArray();
296 + assertThat(result3, is(notNullValue()));
297 + }
298 +
299 + /**
300 + * Tests getHelloBodyAsByteArray() method.
301 + */
302 + @Test
303 + public void testReadHelloBody() throws Exception {
304 + helloPacket.getHelloBodyAsByteArray();
305 + assertThat(helloPacket, is(notNullValue()));
306 + }
307 +
308 + /**
309 + * Tests to string method.
310 + */
311 + @Test
312 + public void testToString() throws Exception {
313 + assertThat(helloPacket.toString(), is(notNullValue()));
314 + }
315 +
316 + /**
317 + * Utility method used by junit methods.
318 + */
319 + private byte[] createByteForHelloPacket() {
320 + byte[] helloPacket = {2, 1, 0, 44, -64, -88, -86, 8, 0, 0, 0, 1, 39, 59, 0, 0, 0, 0,
321 + 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, 10, 2, 1, 0, 0, 0, 40, -64, -88, -86, 8, 0, 0, 0, 0};
322 +
323 + return helloPacket;
324 + }
325 +}
...\ 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.ospfpacket.types;
17 +
18 +import org.hamcrest.MatcherAssert;
19 +import org.jboss.netty.buffer.ChannelBuffer;
20 +import org.jboss.netty.buffer.ChannelBuffers;
21 +import org.junit.After;
22 +import org.junit.Before;
23 +import org.junit.Test;
24 +import org.onlab.packet.Ip4Address;
25 +import org.onosproject.ospf.protocol.lsa.LsaHeader;
26 +import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
27 +import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader;
28 +import org.onosproject.ospf.protocol.util.OspfPacketType;
29 +
30 +import java.util.List;
31 +
32 +import static org.hamcrest.CoreMatchers.is;
33 +import static org.hamcrest.CoreMatchers.notNullValue;
34 +import static org.junit.Assert.assertThat;
35 +
36 +/**
37 + * Unit test class for LsAck.
38 + */
39 +public class LsAcknowledgeTest {
40 +
41 + private LsaHeader lsaHeader;
42 + private LsAcknowledge lsAck;
43 + private OspfPacketType ospfPacketType;
44 + private OspfPacketHeader ospfPacketHeader;
45 + private byte[] result;
46 + private ChannelBuffer channelBuffer;
47 + private OpaqueLsaHeader opaqueLsaHeader;
48 +
49 + @Before
50 + public void setUp() throws Exception {
51 + lsaHeader = new LsaHeader();
52 + lsAck = new LsAcknowledge();
53 + lsAck.setAuthType(1);
54 + lsAck.setOspftype(5);
55 + lsAck.setRouterId(Ip4Address.valueOf("10.226.165.164"));
56 + lsAck.setAreaId(Ip4Address.valueOf("10.226.165.100"));
57 + lsAck.setChecksum(201);
58 + lsAck.setAuthentication(2);
59 + lsAck.setOspfPacLength(48);
60 + lsAck.setOspfVer(2);
61 + }
62 +
63 + @After
64 + public void tearDown() throws Exception {
65 + lsaHeader = null;
66 + lsAck = null;
67 + ospfPacketType = null;
68 + ospfPacketHeader = null;
69 + result = null;
70 + channelBuffer = null;
71 + opaqueLsaHeader = null;
72 + }
73 +
74 + /**
75 + * Tests getLinkStateHeaders() getter method.
76 + */
77 + @Test
78 + public void testGetLinkStateHeaders() throws Exception {
79 + lsaHeader = createLsaHeader();
80 + lsAck.addLinkStateHeader(lsaHeader);
81 + lsAck.addLinkStateHeader(lsaHeader);
82 + List headers = lsAck.getLinkStateHeaders();
83 + assertThat(headers.size(), is(1));
84 +
85 + }
86 +
87 + /**
88 + * Tests addLinkStateHeader() method.
89 + */
90 + @Test
91 + public void testAddLinkStateHeader() throws Exception {
92 + lsaHeader = createLsaHeader();
93 + lsAck.addLinkStateHeader(lsaHeader);
94 + lsAck.addLinkStateHeader(lsaHeader);
95 + assertThat(lsAck, is(notNullValue()));
96 + }
97 +
98 +
99 + /**
100 + * Tests ospfMessageType() getter method.
101 + */
102 + @Test
103 + public void testGetOSPFMessageType() throws Exception {
104 + ospfPacketType = lsAck.ospfMessageType();
105 + assertThat(ospfPacketType, is(notNullValue()));
106 + assertThat(ospfPacketType, is(OspfPacketType.LSAACK));
107 + }
108 +
109 + /**
110 + * Tests ospfMessageType() getter method.
111 + */
112 + @Test
113 + public void testGetOspfMessageType() throws Exception {
114 + ospfPacketType = lsAck.ospfMessageType();
115 + assertThat(ospfPacketType, is(notNullValue()));
116 + assertThat(ospfPacketType, is(OspfPacketType.LSAACK));
117 + }
118 +
119 + /**
120 + * Tests readFrom() method.
121 + */
122 + @Test
123 + public void testReadFrom() throws Exception {
124 + ospfPacketHeader = new OspfPacketHeader();
125 + ospfPacketHeader.setAreaId(Ip4Address.valueOf("1.1.1.1"));
126 + ospfPacketHeader.setAuthentication(0);
127 + ospfPacketHeader.setAuthType(0);
128 + ospfPacketHeader.setChecksum(12345);
129 + ospfPacketHeader.setDestinationIp(Ip4Address.valueOf("10.10.10.10"));
130 + ospfPacketHeader.setOspfPacLength(56);
131 + ospfPacketHeader.setOspftype(5);
132 + ospfPacketHeader.setOspfVer(2);
133 + ospfPacketHeader.setRouterId(Ip4Address.valueOf("2.2.2.2"));
134 + ospfPacketHeader.setSourceIp(Ip4Address.valueOf("3.3.3.3"));
135 + result = createByteForLSAck();
136 + lsAck = new LsAcknowledge(ospfPacketHeader);
137 + channelBuffer = ChannelBuffers.copiedBuffer(result);
138 + lsAck.readFrom(channelBuffer);
139 + assertThat(lsAck, is(notNullValue()));
140 + assertThat(lsAck.ospfMessageType(), is(OspfPacketType.LSAACK));
141 + }
142 +
143 + /**
144 + * Tests asBytes() method.
145 + */
146 + @Test
147 + public void testAsBytes() throws Exception {
148 + result = lsAck.asBytes();
149 + assertThat(result, is(notNullValue()));
150 + }
151 +
152 + /**
153 + * Tests getLsAckAsByteArray() method.
154 + */
155 + @Test
156 + public void testGetLsAckAsByteArray() throws Exception {
157 + result = lsAck.getLsAckAsByteArray();
158 + assertThat(result, is(notNullValue()));
159 + }
160 +
161 + /**
162 + * Tests getLsAckBodyAsByteArray() method.
163 + */
164 + @Test
165 + public void testGetLsAckBodyAsByteArray() throws Exception {
166 + lsaHeader = createLsaHeader();
167 + opaqueLsaHeader = new OpaqueLsaHeader();
168 + lsAck.addLinkStateHeader(lsaHeader);
169 + lsAck.addLinkStateHeader(opaqueLsaHeader);
170 + result = lsAck.getLsAckBodyAsByteArray();
171 + assertThat(result, is(notNullValue()));
172 + }
173 +
174 + /**
175 + * Tests to string method.
176 + */
177 + @Test
178 + public void testToString() throws Exception {
179 + MatcherAssert.assertThat(lsAck.toString(), is(notNullValue()));
180 + }
181 +
182 + /**
183 + * Utility method used by junit methods.
184 + */
185 + private LsaHeader createLsaHeader() {
186 + lsaHeader = new LsaHeader();
187 + lsaHeader.setAge(10);
188 + lsaHeader.setLinkStateId("10.226.165.164");
189 + lsaHeader.setLsCheckSum(222);
190 + lsaHeader.setLsPacketLen(48);
191 + lsaHeader.setLsSequenceNo(2020);
192 + lsaHeader.setLsType(5);
193 + lsaHeader.setOptions(2);
194 + lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("10.226.165.165"));
195 + return lsaHeader;
196 + }
197 +
198 + /**
199 + * Utility method used by junit methods.
200 + */
201 + private byte[] createByteForLSAck() {
202 + byte[] lsAckPacket = {2, 5, 0, 44, -64, -88, -86, 8, 0, 0, 0, 1, -30,
203 + -12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 16, 2, 1, -64, -88, -86,
204 + 2, -64, -88, -86, 2, -128, 0, 0, 1, 74, -114, 0, 48};
205 +
206 + return lsAckPacket;
207 + }
208 +}
...\ 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.ospfpacket.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.protocol.ospfpacket.OspfPacketHeader;
25 +import org.onosproject.ospf.protocol.ospfpacket.subtype.LsRequestPacket;
26 +import org.onosproject.ospf.protocol.util.OspfPacketType;
27 +
28 +import java.net.UnknownHostException;
29 +import java.util.List;
30 +
31 +import static org.hamcrest.CoreMatchers.is;
32 +import static org.hamcrest.CoreMatchers.notNullValue;
33 +import static org.junit.Assert.assertThat;
34 +
35 +/**
36 + * Unit test class for LsRequest.
37 + */
38 +public class LsRequestTest {
39 +
40 + private LsRequest lsRequest;
41 + private List<LsRequestPacket> result;
42 + private OspfPacketType ospfMessageType;
43 + private OspfPacketHeader ospfPacketHeader;
44 + private byte[] result1;
45 + private String result2;
46 + private ChannelBuffer channelBuffer;
47 + private LsRequestPacket lsRequestPacket;
48 +
49 + @Before
50 + public void setUp() throws Exception {
51 + lsRequest = new LsRequest();
52 + lsRequest.setAuthType(1);
53 + lsRequest.setOspftype(3);
54 + lsRequest.setRouterId(Ip4Address.valueOf("10.226.165.164"));
55 + lsRequest.setAreaId(Ip4Address.valueOf("10.226.165.163"));
56 + lsRequest.setChecksum(201);
57 + lsRequest.setAuthentication(2);
58 + lsRequest.setOspfPacLength(48);
59 + lsRequest.setOspfVer(2);
60 + }
61 +
62 + @After
63 + public void tearDown() throws Exception {
64 + lsRequest = null;
65 + result = null;
66 + ospfMessageType = null;
67 + ospfPacketHeader = null;
68 + result1 = null;
69 + channelBuffer = null;
70 + lsRequestPacket = null;
71 + }
72 +
73 + /**
74 + * Tests addLinkStateRequests() method.
75 + */
76 + @Test
77 + public void testAddLinkStateRequests() throws Exception {
78 + lsRequest.addLinkStateRequests(createLsRequestPacket());
79 + result = lsRequest.getLinkStateRequests();
80 + assertThat(result, is(notNullValue()));
81 + assertThat(result.size(), is(1));
82 + }
83 +
84 + /**
85 + * Tests getLinkStateRequests() method.
86 + */
87 + @Test
88 + public void testGetLinkStateRequests() throws Exception {
89 + lsRequest.addLinkStateRequests(createLsRequestPacket());
90 + lsRequest.addLinkStateRequests(new LsRequestPacket());
91 + result = lsRequest.getLinkStateRequests();
92 + assertThat(result, is(notNullValue()));
93 + assertThat(result.size(), is(2));
94 + }
95 +
96 + /**
97 + * Tests ospfMessageType()getter method.
98 + */
99 + @Test
100 + public void testGetOspfMessageType() throws Exception {
101 + ospfMessageType = lsRequest.ospfMessageType();
102 + assertThat(ospfMessageType, is(notNullValue()));
103 + assertThat(ospfMessageType, is(OspfPacketType.LSREQUEST));
104 + }
105 +
106 + /**
107 + * Tests readFrom() method.
108 + */
109 + @Test
110 + public void testReadFrom() throws Exception {
111 + ospfPacketHeader = new OspfPacketHeader();
112 + ospfPacketHeader.setAreaId(Ip4Address.valueOf("1.1.1.1"));
113 + ospfPacketHeader.setAuthentication(0);
114 + ospfPacketHeader.setAuthType(0);
115 + ospfPacketHeader.setChecksum(12345);
116 + ospfPacketHeader.setDestinationIp(Ip4Address.valueOf("10.10.10.10"));
117 + ospfPacketHeader.setOspfPacLength(56);
118 + ospfPacketHeader.setOspftype(3);
119 + ospfPacketHeader.setOspfVer(2);
120 + ospfPacketHeader.setRouterId(Ip4Address.valueOf("2.2.2.2"));
121 + ospfPacketHeader.setSourceIp(Ip4Address.valueOf("3.3.3.3"));
122 + lsRequest = new LsRequest(ospfPacketHeader);
123 + result1 = createByteLsReqestPacket();
124 + channelBuffer = ChannelBuffers.copiedBuffer(result1);
125 + lsRequest.readFrom(channelBuffer);
126 + assertThat(lsRequest, is(notNullValue()));
127 + assertThat(lsRequest.ospfMessageType(), is(OspfPacketType.LSREQUEST));
128 + }
129 +
130 + /**
131 + * Tests asBytes() method.
132 + */
133 + @Test
134 + public void testAsBytes() throws Exception {
135 + result1 = lsRequest.asBytes();
136 + assertThat(result1, is(notNullValue()));
137 + }
138 +
139 + /**
140 + * Tests getLsrHeaderAsByteArray() method.
141 + */
142 + @Test
143 + public void testGetLsrHeaderAsByteArray() throws Exception {
144 + result1 = lsRequest.getLsrHeaderAsByteArray();
145 + assertThat(result1, is(notNullValue()));
146 + }
147 +
148 + /**
149 + * Tests getLsrBodyAsByteArray() method.
150 + */
151 + @Test
152 + public void testGetLsrBodyAsByteArray() throws Exception {
153 + lsRequest.addLinkStateRequests(createLsRequestPacket());
154 + lsRequest.addLinkStateRequests(new LsRequestPacket());
155 + result1 = lsRequest.getLsrBodyAsByteArray();
156 + assertThat(result1, is(notNullValue()));
157 + }
158 +
159 + /**
160 + * Tests to string method.
161 + */
162 + @Test
163 + public void testToString() throws Exception {
164 + result2 = lsRequest.toString();
165 + assertThat(result2, is(notNullValue()));
166 + }
167 +
168 + private LsRequestPacket createLsRequestPacket() throws UnknownHostException {
169 + lsRequestPacket = new LsRequestPacket();
170 + lsRequestPacket.setOwnRouterId("165");
171 + lsRequestPacket.setLinkStateId("10.226.165.164");
172 + lsRequestPacket.setLsType(2);
173 + return lsRequestPacket;
174 + }
175 +
176 + private byte[] createByteLsReqestPacket() {
177 + byte[] lsRequestPacket = {2, 3, 0, 36, -64, -88, -86, 3, 0, 0, 0, 1, -67,
178 + -57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -64, -88, -86, 8,
179 + -64, -88, -86, 8};
180 + return lsRequestPacket;
181 + }
182 +}
...\ No newline at end of file ...\ No newline at end of file