blob: 1dfb1a4ac8897299be03068558d3364747681072 [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;
29import org.onosproject.isis.io.isispacket.tlv.IpInterfaceAddressTlv;
30import org.onosproject.isis.io.isispacket.tlv.IpInternalReachabilityTlv;
31import org.onosproject.isis.io.isispacket.tlv.IsReachabilityTlv;
32import org.onosproject.isis.io.isispacket.tlv.MetricOfInternalReachability;
33import org.onosproject.isis.io.isispacket.tlv.MetricsOfReachability;
34import org.onosproject.isis.io.isispacket.tlv.ProtocolSupportedTlv;
35import org.onosproject.isis.io.isispacket.tlv.TlvHeader;
36import org.onosproject.isis.io.isispacket.tlv.TlvType;
37
38import java.util.List;
39
40/**
41 * Representation of link state PDU generator.
42 */
43public class LspGenerator {
44
45 public LsPdu getLsp(IsisInterface isisInterface, String lspId, IsisPduType isisPduType,
46 List<Ip4Address> allConfiguredInterfaceIps) {
47 IsisHeader header = getHeader(isisPduType);
48 LsPdu lsp = new LsPdu(header);
49
50 lsp.setPduLength(0);
51 lsp.setRemainingLifeTime(IsisConstants.LSPMAXAGE);
52 lsp.setLspId(lspId);
53 lsp.setSequenceNumber(isisInterface.isisLsdb().lsSequenceNumber(isisPduType));
54 lsp.setCheckSum(0);
55 if (isisPduType == IsisPduType.L1LSPDU) {
56 lsp.setTypeBlock((byte) 1);
57 lsp.setIntermediateSystemType((byte) 1);
58 } else if (isisPduType == IsisPduType.L2LSPDU) {
59 lsp.setTypeBlock((byte) 3);
60 lsp.setIntermediateSystemType((byte) 3);
61 }
62 lsp.setAttachedToOtherAreas(AttachedToOtherAreas.NONE);
63 lsp.setPartitionRepair(false);
64 lsp.setLspDbol(false);
65
66 TlvHeader tlvHeader = new TlvHeader();
67 tlvHeader.setTlvType(TlvType.AREAADDRESS.value());
68 tlvHeader.setTlvLength(0);
69 AreaAddressTlv areaAddressTlv = new AreaAddressTlv(tlvHeader);
70 areaAddressTlv.addAddress(isisInterface.areaAddress());
71 lsp.addTlv(areaAddressTlv);
72
73 tlvHeader.setTlvType(TlvType.PROTOCOLSUPPORTED.value());
74 tlvHeader.setTlvLength(0);
75 ProtocolSupportedTlv protocolSupportedTlv = new ProtocolSupportedTlv(tlvHeader);
76 protocolSupportedTlv.addProtocolSupported((byte) IsisConstants.PROTOCOLSUPPORTED);
77 lsp.addTlv(protocolSupportedTlv);
78
79 tlvHeader.setTlvType(TlvType.IPINTERFACEADDRESS.value());
80 tlvHeader.setTlvLength(0);
81 IpInterfaceAddressTlv ipInterfaceAddressTlv = new IpInterfaceAddressTlv(tlvHeader);
82 for (Ip4Address ipaddress : allConfiguredInterfaceIps) {
83 ipInterfaceAddressTlv.addInterfaceAddres(ipaddress);
84 }
85 lsp.addTlv(ipInterfaceAddressTlv);
86
87 tlvHeader.setTlvType(TlvType.HOSTNAME.value());
88 tlvHeader.setTlvLength(0);
89 HostNameTlv hostNameTlv = new HostNameTlv(tlvHeader);
90 hostNameTlv.setHostName(isisInterface.intermediateSystemName());
91 lsp.addTlv(hostNameTlv);
92
93 tlvHeader.setTlvType(TlvType.ISREACHABILITY.value());
94 tlvHeader.setTlvLength(0);
95 IsReachabilityTlv isReachabilityTlv = new IsReachabilityTlv(tlvHeader);
96 isReachabilityTlv.setReserved(0);
97 MetricsOfReachability metricsOfReachability = new MetricsOfReachability();
98 metricsOfReachability.setDefaultMetric((byte) 10);
99 metricsOfReachability.setDefaultIsInternal(true);
100 metricsOfReachability.setDelayMetric((byte) 10);
101 metricsOfReachability.setDelayIsInternal(true);
102 metricsOfReachability.setDelayMetricSupported(true);
103 metricsOfReachability.setExpenseMetric((byte) 10);
104 metricsOfReachability.setExpenseIsInternal(true);
105 metricsOfReachability.setExpenseMetricSupported(true);
106 metricsOfReachability.setErrorMetric((byte) 10);
107 metricsOfReachability.setErrorIsInternal(true);
108 metricsOfReachability.setErrorMetricSupported(true);
109 if (isisInterface.networkType() == IsisNetworkType.BROADCAST) {
110 if (isisPduType == IsisPduType.L1LSPDU) {
111 metricsOfReachability.setNeighborId(isisInterface.l1LanId());
112 } else if (isisPduType == IsisPduType.L2LSPDU) {
113 metricsOfReachability.setNeighborId(isisInterface.l2LanId());
114 }
115 } else if (isisInterface.networkType() == IsisNetworkType.P2P) {
116 MacAddress neighborMac = isisInterface.neighbors().iterator().next();
117 IsisNeighbor neighbor = isisInterface.lookup(neighborMac);
118 metricsOfReachability.setNeighborId(neighbor.neighborSystemId());
119 }
120
121 isReachabilityTlv.addMeticsOfReachability(metricsOfReachability);
122 lsp.addTlv(isReachabilityTlv);
123
124 tlvHeader.setTlvType(TlvType.IPINTERNALREACHABILITY.value());
125 tlvHeader.setTlvLength(0);
126 IpInternalReachabilityTlv ipInterReacTlv = new IpInternalReachabilityTlv(tlvHeader);
127 MetricOfInternalReachability metricOfIntRea = new MetricOfInternalReachability();
128 metricOfIntRea.setDefaultMetric((byte) 10);
129 metricOfIntRea.setDefaultIsInternal(true);
130 metricOfIntRea.setDefaultDistributionDown(true);
131 metricOfIntRea.setDelayMetric((byte) 0);
132 metricOfIntRea.setDelayMetricSupported(false);
133 metricOfIntRea.setDelayIsInternal(true);
134 metricOfIntRea.setExpenseMetric((byte) 0);
135 metricOfIntRea.setExpenseMetricSupported(false);
136 metricOfIntRea.setExpenseIsInternal(true);
137 metricOfIntRea.setErrorMetric((byte) 0);
138 metricOfIntRea.setErrorMetricSupported(false);
139 metricOfIntRea.setExpenseIsInternal(true);
140 metricOfIntRea.setIpAddress(isisInterface.interfaceIpAddress());
141 metricOfIntRea.setSubnetAddres(Ip4Address.valueOf(isisInterface.networkMask()));
142 ipInterReacTlv.addInternalReachabilityMetric(metricOfIntRea);
143 lsp.addTlv(ipInterReacTlv);
144 return lsp;
145 }
146
147 public IsisHeader getHeader(IsisPduType pduType) {
148 IsisHeader isisHeader = new IsisHeader();
149 isisHeader.setIrpDiscriminator((byte) IsisConstants.IRPDISCRIMINATOR);
150 isisHeader.setPduHeaderLength((byte) IsisUtil.getPduHeaderLength(pduType.value()));
151 isisHeader.setVersion((byte) IsisConstants.ISISVERSION);
152 isisHeader.setIdLength((byte) IsisConstants.IDLENGTH);
153 isisHeader.setIsisPduType(pduType.value());
154 isisHeader.setVersion2((byte) IsisConstants.ISISVERSION);
155 isisHeader.setReserved((byte) IsisConstants.RESERVED);
156 isisHeader.setMaximumAreaAddresses((byte) IsisConstants.MAXAREAADDRESS);
157 return isisHeader;
158 }
159}