blob: c6ae96273c103e6fec9af8ba62e98c03f5ad384b [file] [log] [blame]
Dhruv Dhodye64b93e2016-04-20 19:26:55 +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.util;
17
18import org.onlab.packet.Ip4Address;
19import org.onlab.packet.MacAddress;
20import org.onosproject.isis.controller.IsisInterface;
21import org.onosproject.isis.controller.IsisNeighbor;
22import org.onosproject.isis.controller.IsisNetworkType;
23import org.onosproject.isis.controller.IsisPduType;
24import org.onosproject.isis.io.isispacket.IsisHeader;
25import org.onosproject.isis.io.isispacket.pdu.AttachedToOtherAreas;
26import org.onosproject.isis.io.isispacket.pdu.LsPdu;
27import org.onosproject.isis.io.isispacket.tlv.AreaAddressTlv;
28import org.onosproject.isis.io.isispacket.tlv.HostNameTlv;
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053029import org.onosproject.isis.io.isispacket.tlv.IpExtendedReachabilityTlv;
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053030import org.onosproject.isis.io.isispacket.tlv.IpInterfaceAddressTlv;
31import org.onosproject.isis.io.isispacket.tlv.IpInternalReachabilityTlv;
32import org.onosproject.isis.io.isispacket.tlv.IsReachabilityTlv;
33import org.onosproject.isis.io.isispacket.tlv.MetricOfInternalReachability;
34import org.onosproject.isis.io.isispacket.tlv.MetricsOfReachability;
35import org.onosproject.isis.io.isispacket.tlv.ProtocolSupportedTlv;
36import org.onosproject.isis.io.isispacket.tlv.TlvHeader;
37import org.onosproject.isis.io.isispacket.tlv.TlvType;
38
39import java.util.List;
40
41/**
42 * Representation of link state PDU generator.
43 */
44public class LspGenerator {
45
46 public LsPdu getLsp(IsisInterface isisInterface, String lspId, IsisPduType isisPduType,
47 List<Ip4Address> allConfiguredInterfaceIps) {
48 IsisHeader header = getHeader(isisPduType);
49 LsPdu lsp = new LsPdu(header);
50
51 lsp.setPduLength(0);
52 lsp.setRemainingLifeTime(IsisConstants.LSPMAXAGE);
53 lsp.setLspId(lspId);
54 lsp.setSequenceNumber(isisInterface.isisLsdb().lsSequenceNumber(isisPduType));
55 lsp.setCheckSum(0);
56 if (isisPduType == IsisPduType.L1LSPDU) {
57 lsp.setTypeBlock((byte) 1);
58 lsp.setIntermediateSystemType((byte) 1);
59 } else if (isisPduType == IsisPduType.L2LSPDU) {
60 lsp.setTypeBlock((byte) 3);
61 lsp.setIntermediateSystemType((byte) 3);
62 }
63 lsp.setAttachedToOtherAreas(AttachedToOtherAreas.NONE);
64 lsp.setPartitionRepair(false);
65 lsp.setLspDbol(false);
66
67 TlvHeader tlvHeader = new TlvHeader();
68 tlvHeader.setTlvType(TlvType.AREAADDRESS.value());
69 tlvHeader.setTlvLength(0);
70 AreaAddressTlv areaAddressTlv = new AreaAddressTlv(tlvHeader);
71 areaAddressTlv.addAddress(isisInterface.areaAddress());
72 lsp.addTlv(areaAddressTlv);
73
74 tlvHeader.setTlvType(TlvType.PROTOCOLSUPPORTED.value());
75 tlvHeader.setTlvLength(0);
76 ProtocolSupportedTlv protocolSupportedTlv = new ProtocolSupportedTlv(tlvHeader);
77 protocolSupportedTlv.addProtocolSupported((byte) IsisConstants.PROTOCOLSUPPORTED);
78 lsp.addTlv(protocolSupportedTlv);
79
80 tlvHeader.setTlvType(TlvType.IPINTERFACEADDRESS.value());
81 tlvHeader.setTlvLength(0);
82 IpInterfaceAddressTlv ipInterfaceAddressTlv = new IpInterfaceAddressTlv(tlvHeader);
83 for (Ip4Address ipaddress : allConfiguredInterfaceIps) {
84 ipInterfaceAddressTlv.addInterfaceAddres(ipaddress);
85 }
86 lsp.addTlv(ipInterfaceAddressTlv);
87
88 tlvHeader.setTlvType(TlvType.HOSTNAME.value());
89 tlvHeader.setTlvLength(0);
90 HostNameTlv hostNameTlv = new HostNameTlv(tlvHeader);
91 hostNameTlv.setHostName(isisInterface.intermediateSystemName());
92 lsp.addTlv(hostNameTlv);
93
94 tlvHeader.setTlvType(TlvType.ISREACHABILITY.value());
95 tlvHeader.setTlvLength(0);
96 IsReachabilityTlv isReachabilityTlv = new IsReachabilityTlv(tlvHeader);
97 isReachabilityTlv.setReserved(0);
98 MetricsOfReachability metricsOfReachability = new MetricsOfReachability();
99 metricsOfReachability.setDefaultMetric((byte) 10);
100 metricsOfReachability.setDefaultIsInternal(true);
101 metricsOfReachability.setDelayMetric((byte) 10);
102 metricsOfReachability.setDelayIsInternal(true);
103 metricsOfReachability.setDelayMetricSupported(true);
104 metricsOfReachability.setExpenseMetric((byte) 10);
105 metricsOfReachability.setExpenseIsInternal(true);
106 metricsOfReachability.setExpenseMetricSupported(true);
107 metricsOfReachability.setErrorMetric((byte) 10);
108 metricsOfReachability.setErrorIsInternal(true);
109 metricsOfReachability.setErrorMetricSupported(true);
110 if (isisInterface.networkType() == IsisNetworkType.BROADCAST) {
111 if (isisPduType == IsisPduType.L1LSPDU) {
112 metricsOfReachability.setNeighborId(isisInterface.l1LanId());
113 } else if (isisPduType == IsisPduType.L2LSPDU) {
114 metricsOfReachability.setNeighborId(isisInterface.l2LanId());
115 }
116 } else if (isisInterface.networkType() == IsisNetworkType.P2P) {
117 MacAddress neighborMac = isisInterface.neighbors().iterator().next();
118 IsisNeighbor neighbor = isisInterface.lookup(neighborMac);
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530119 metricsOfReachability.setNeighborId(neighbor.neighborSystemId() + ".00");
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530120 }
121
122 isReachabilityTlv.addMeticsOfReachability(metricsOfReachability);
123 lsp.addTlv(isReachabilityTlv);
124
125 tlvHeader.setTlvType(TlvType.IPINTERNALREACHABILITY.value());
126 tlvHeader.setTlvLength(0);
127 IpInternalReachabilityTlv ipInterReacTlv = new IpInternalReachabilityTlv(tlvHeader);
128 MetricOfInternalReachability metricOfIntRea = new MetricOfInternalReachability();
129 metricOfIntRea.setDefaultMetric((byte) 10);
130 metricOfIntRea.setDefaultIsInternal(true);
131 metricOfIntRea.setDefaultDistributionDown(true);
132 metricOfIntRea.setDelayMetric((byte) 0);
133 metricOfIntRea.setDelayMetricSupported(false);
134 metricOfIntRea.setDelayIsInternal(true);
135 metricOfIntRea.setExpenseMetric((byte) 0);
136 metricOfIntRea.setExpenseMetricSupported(false);
137 metricOfIntRea.setExpenseIsInternal(true);
138 metricOfIntRea.setErrorMetric((byte) 0);
139 metricOfIntRea.setErrorMetricSupported(false);
140 metricOfIntRea.setExpenseIsInternal(true);
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530141 Ip4Address ip4Address = isisInterface.interfaceIpAddress();
142 byte[] ipAddress = ip4Address.toOctets();
143 ipAddress[ipAddress.length - 1] = 0;
144 metricOfIntRea.setIpAddress(Ip4Address.valueOf(ipAddress));
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530145 metricOfIntRea.setSubnetAddres(Ip4Address.valueOf(isisInterface.networkMask()));
146 ipInterReacTlv.addInternalReachabilityMetric(metricOfIntRea);
147 lsp.addTlv(ipInterReacTlv);
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530148
149 tlvHeader.setTlvType(TlvType.IPEXTENDEDREACHABILITY.value());
150 tlvHeader.setTlvLength(0);
151 IpExtendedReachabilityTlv extendedTlv = new IpExtendedReachabilityTlv(tlvHeader);
152 extendedTlv.setDown(false);
153 extendedTlv.setMetric(10);
154 extendedTlv.setPrefix("192.168.7");
155 extendedTlv.setPrefixLength(24);
156 extendedTlv.setSubTlvLength((byte) 0);
157 extendedTlv.setSubTlvPresence(false);
158 lsp.addTlv(extendedTlv);
159
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530160 return lsp;
161 }
162
163 public IsisHeader getHeader(IsisPduType pduType) {
164 IsisHeader isisHeader = new IsisHeader();
165 isisHeader.setIrpDiscriminator((byte) IsisConstants.IRPDISCRIMINATOR);
166 isisHeader.setPduHeaderLength((byte) IsisUtil.getPduHeaderLength(pduType.value()));
167 isisHeader.setVersion((byte) IsisConstants.ISISVERSION);
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530168 isisHeader.setIdLength((byte) IsisConstants.SYSTEMIDLENGTH);
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530169 isisHeader.setIsisPduType(pduType.value());
170 isisHeader.setVersion2((byte) IsisConstants.ISISVERSION);
171 isisHeader.setReserved((byte) IsisConstants.RESERVED);
172 isisHeader.setMaximumAreaAddresses((byte) IsisConstants.MAXAREAADDRESS);
173 return isisHeader;
174 }
175}