blob: bb280d264cd527c4a11b32f18ce709ef85a1af06 [file] [log] [blame]
PRASHANTH SHIVANANJAPPA491b8af2016-04-27 19:23:24 +05301/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.isis.io.isispacket.tlv;
17
18import org.junit.Test;
19import org.onlab.packet.Ip4Address;
20import org.onlab.packet.MacAddress;
21import org.onosproject.isis.controller.IsisInterfaceState;
22import org.onosproject.isis.io.util.IsisConstants;
23
24import java.util.List;
25
26import static org.hamcrest.MatcherAssert.assertThat;
27import static org.hamcrest.Matchers.is;
28import static org.hamcrest.Matchers.notNullValue;
29
30/**
31 * Unit test class for TlvsToBytes.
32 */
33public class TlvsToBytesTest {
34 private final String areaAddress = "49";
35 private final Ip4Address ip4Address = Ip4Address.valueOf("10.10.10.10");
36 private final String systemName = "ROUTER";
37 private final String neighborId = "2929.2929.2929";
38 private List<Byte> tlv;
39 private MacAddress macAddress = MacAddress.valueOf("a4:23:05:00:00:00");
40 private String prefix = "192.168.7";
41
42 /**
43 * Tests TlvToBytes() method.
44 */
45 @Test
46 public void testTlvToBytes() throws Exception {
47 TlvHeader tlvHeader = new TlvHeader();
48 tlvHeader.setTlvType(TlvType.AREAADDRESS.value());
49 tlvHeader.setTlvLength(0);
50 AreaAddressTlv areaAddressTlv = new AreaAddressTlv(tlvHeader);
51 areaAddressTlv.addAddress(areaAddress);
52 tlv = TlvsToBytes.tlvToBytes(areaAddressTlv);
53 assertThat(tlv, is(notNullValue()));
54
55 tlvHeader.setTlvType(TlvType.PROTOCOLSUPPORTED.value());
56 tlvHeader.setTlvLength(0);
57 ProtocolSupportedTlv protocolSupportedTlv = new ProtocolSupportedTlv(tlvHeader);
58 protocolSupportedTlv.addProtocolSupported((byte) IsisConstants.PROTOCOLSUPPORTED);
59 tlv = TlvsToBytes.tlvToBytes(protocolSupportedTlv);
60 assertThat(tlv, is(notNullValue()));
61
62 tlvHeader.setTlvType(TlvType.IPINTERFACEADDRESS.value());
63 tlvHeader.setTlvLength(0);
64 IpInterfaceAddressTlv ipInterfaceAddressTlv = new IpInterfaceAddressTlv(tlvHeader);
65 ipInterfaceAddressTlv.addInterfaceAddres(ip4Address);
66 tlv = TlvsToBytes.tlvToBytes(ipInterfaceAddressTlv);
67 assertThat(tlv, is(notNullValue()));
68
69 tlvHeader.setTlvType(TlvType.HOSTNAME.value());
70 tlvHeader.setTlvLength(0);
71 HostNameTlv hostNameTlv = new HostNameTlv(tlvHeader);
72 hostNameTlv.setHostName(systemName);
73 tlv = TlvsToBytes.tlvToBytes(hostNameTlv);
74 assertThat(tlv, is(notNullValue()));
75
76 tlvHeader.setTlvType(TlvType.ISREACHABILITY.value());
77 tlvHeader.setTlvLength(0);
78 IsReachabilityTlv isReachabilityTlv = new IsReachabilityTlv(tlvHeader);
79 isReachabilityTlv.setReserved(0);
80 MetricsOfReachability metricsOfReachability = new MetricsOfReachability();
81 metricsOfReachability.setDefaultMetric((byte) 10);
82 metricsOfReachability.setDefaultIsInternal(true);
83 metricsOfReachability.setDelayMetric((byte) 10);
84 metricsOfReachability.setDelayIsInternal(true);
85 metricsOfReachability.setDelayMetricSupported(true);
86 metricsOfReachability.setExpenseMetric((byte) 10);
87 metricsOfReachability.setExpenseIsInternal(true);
88 metricsOfReachability.setExpenseMetricSupported(true);
89 metricsOfReachability.setErrorMetric((byte) 10);
90 metricsOfReachability.setErrorIsInternal(true);
91 metricsOfReachability.setErrorMetricSupported(true);
92 metricsOfReachability.setNeighborId(neighborId);
93 isReachabilityTlv.addMeticsOfReachability(metricsOfReachability);
94 tlv = TlvsToBytes.tlvToBytes(isReachabilityTlv);
95 assertThat(tlv, is(notNullValue()));
96
97 tlvHeader.setTlvType(TlvType.IPINTERNALREACHABILITY.value());
98 tlvHeader.setTlvLength(0);
99 IpInternalReachabilityTlv ipInterReacTlv = new IpInternalReachabilityTlv(tlvHeader);
100 MetricOfInternalReachability metricOfIntRea = new MetricOfInternalReachability();
101 metricOfIntRea.setDefaultMetric((byte) 10);
102 metricOfIntRea.setDefaultIsInternal(true);
103 metricOfIntRea.setDefaultDistributionDown(true);
104 metricOfIntRea.setDelayMetric((byte) 0);
105 metricOfIntRea.setDelayMetricSupported(false);
106 metricOfIntRea.setDelayIsInternal(true);
107 metricOfIntRea.setExpenseMetric((byte) 0);
108 metricOfIntRea.setExpenseMetricSupported(false);
109 metricOfIntRea.setExpenseIsInternal(true);
110 metricOfIntRea.setErrorMetric((byte) 0);
111 metricOfIntRea.setErrorMetricSupported(false);
112 metricOfIntRea.setExpenseIsInternal(true);
113 metricOfIntRea.setIpAddress(ip4Address);
114 metricOfIntRea.setSubnetAddres(ip4Address);
115 ipInterReacTlv.addInternalReachabilityMetric(metricOfIntRea);
116 tlv = TlvsToBytes.tlvToBytes(ipInterReacTlv);
117 assertThat(tlv, is(notNullValue()));
118
119 tlvHeader.setTlvType(TlvType.PADDING.value());
120 tlvHeader.setTlvLength(255);
121 PaddingTlv paddingTlv = new PaddingTlv(tlvHeader);
122 tlv = TlvsToBytes.tlvToBytes(paddingTlv);
123 assertThat(tlv, is(notNullValue()));
124
125 tlvHeader.setTlvType(TlvType.IPEXTENDEDREACHABILITY.value());
126 tlvHeader.setTlvLength(0);
127 IpExtendedReachabilityTlv extendedTlv = new IpExtendedReachabilityTlv(tlvHeader);
128 extendedTlv.setDown(false);
129 extendedTlv.setMetric(10);
130 extendedTlv.setPrefix(prefix);
131 extendedTlv.setPrefixLength(24);
132 extendedTlv.setSubTlvLength((byte) 0);
133 extendedTlv.setSubTlvPresence(false);
134 tlv = TlvsToBytes.tlvToBytes(extendedTlv);
135 assertThat(tlv, is(notNullValue()));
136
137 tlvHeader.setTlvType(TlvType.ADJACENCYSTATE.value());
138 tlvHeader.setTlvLength(0);
139 AdjacencyStateTlv adjacencyStateTlv = new AdjacencyStateTlv(tlvHeader);
140 adjacencyStateTlv.setAdjacencyType((byte) IsisInterfaceState.DOWN.value());
141 tlv = TlvsToBytes.tlvToBytes(adjacencyStateTlv);
142 assertThat(tlv, is(notNullValue()));
143
144 tlvHeader.setTlvType(TlvType.ISNEIGHBORS.value());
145 tlvHeader.setTlvLength(0);
146 IsisNeighborTlv isisNeighborTlv = new IsisNeighborTlv(tlvHeader);
147 isisNeighborTlv.addNeighbor(macAddress);
148 tlv = TlvsToBytes.tlvToBytes(isisNeighborTlv);
149 assertThat(tlv, is(notNullValue()));
150
151 tlvHeader.setTlvType(TlvType.EXTENDEDISREACHABILITY.value());
152 tlvHeader.setTlvLength(0);
153 IsExtendedReachability reachability = new IsExtendedReachability(tlvHeader);
154 NeighborForExtendedIs forExtendedIs = new NeighborForExtendedIs();
155 forExtendedIs.setMetric(10);
156 forExtendedIs.setNeighborId(neighborId);
157 reachability.addNeighbor(forExtendedIs);
158 tlv = TlvsToBytes.tlvToBytes(reachability);
159 assertThat(tlv, is(notNullValue()));
160 }
161}