blob: 19a990801ea55f7c9c07424825e79aed0b47d9d5 [file] [log] [blame]
Shashikanth VHc06002c2015-11-18 18:45:39 +05301/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5 * the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11 * specific language governing permissions and limitations under the License.
12 */
13
14package org.onosproject.bgp.controller;
15
16import static com.google.common.base.Preconditions.checkArgument;
17
18import java.net.URI;
19import java.net.URISyntaxException;
Shashikanth VHe68bb5e2016-02-18 23:44:50 +053020import java.util.List;
21import java.util.ListIterator;
Shashikanth VHc06002c2015-11-18 18:45:39 +053022
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053023import org.onosproject.bgpio.exceptions.BgpParseException;
Shashikanth VHc06002c2015-11-18 18:45:39 +053024import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4;
Shashikanth VHe68bb5e2016-02-18 23:44:50 +053025import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSIdentifier;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053026import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4;
Shashikanth VHe68bb5e2016-02-18 23:44:50 +053027import org.onosproject.bgpio.protocol.linkstate.NodeDescriptors;
28import org.onosproject.bgpio.types.AreaIDTlv;
29import org.onosproject.bgpio.types.AutonomousSystemTlv;
30import org.onosproject.bgpio.types.BgpLSIdentifierTlv;
31import org.onosproject.bgpio.types.BgpValueType;
32import org.onosproject.bgpio.types.IsIsNonPseudonode;
33import org.onosproject.bgpio.types.IsIsPseudonode;
34import org.onosproject.bgpio.types.OspfNonPseudonode;
35import org.onosproject.bgpio.types.OspfPseudonode;
Shashikanth VHc06002c2015-11-18 18:45:39 +053036import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
38
39/**
Shashikanth VHe68bb5e2016-02-18 23:44:50 +053040 * The class representing a network bgp device id.
Shashikanth VHc06002c2015-11-18 18:45:39 +053041 */
42public final class BgpDpid {
43 private static final Logger log = LoggerFactory.getLogger(BgpDpid.class);
44
45 private static final String SCHEME = "bgp";
46 private static final long UNKNOWN = 0;
47 private StringBuilder stringBuilder;
48 public static final int NODE_DESCRIPTOR_LOCAL = 1;
49 public static final int NODE_DESCRIPTOR_REMOTE = 2;
50
51 /**
52 * Initialize bgp id to generate URI.
53 *
54 * @param linkNlri node Nlri.
55 * @param nodeDescriptorType node descriptor type, local/remote
56 */
57 public BgpDpid(final BgpLinkLsNlriVer4 linkNlri, int nodeDescriptorType) {
Shashikanth VHe68bb5e2016-02-18 23:44:50 +053058 this.stringBuilder = new StringBuilder("");
Shashikanth VHc06002c2015-11-18 18:45:39 +053059
60 if (linkNlri.getRouteDistinguisher() != null) {
Shashikanth VHe68bb5e2016-02-18 23:44:50 +053061 this.stringBuilder.append("RD=").append(linkNlri.getRouteDistinguisher()
62 .getRouteDistinguisher()).append(":");
Shashikanth VHc06002c2015-11-18 18:45:39 +053063 }
64
65 try {
Shashikanth VHe68bb5e2016-02-18 23:44:50 +053066 if ((linkNlri.getProtocolId() == BgpNodeLSNlriVer4.ProtocolType.ISIS_LEVEL_ONE)
67 || (linkNlri.getProtocolId() == BgpNodeLSNlriVer4.ProtocolType.ISIS_LEVEL_TWO)) {
68 this.stringBuilder.append("PROTO=").append("ISIS").append(":ID=")
69 .append(linkNlri.getIdentifier());
70 } else {
71 this.stringBuilder.append("PROTO=").append(linkNlri.getProtocolId()).append(":ID=")
72 .append(linkNlri.getIdentifier());
73 }
Shashikanth VHc06002c2015-11-18 18:45:39 +053074
75 if (nodeDescriptorType == NODE_DESCRIPTOR_LOCAL) {
Shashikanth VH00af33f2015-11-29 03:27:26 +053076 add(linkNlri.localNodeDescriptors());
Shashikanth VHc06002c2015-11-18 18:45:39 +053077 } else if (nodeDescriptorType == NODE_DESCRIPTOR_REMOTE) {
Shashikanth VH00af33f2015-11-29 03:27:26 +053078 add(linkNlri.remoteNodeDescriptors());
Shashikanth VHc06002c2015-11-18 18:45:39 +053079 }
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053080 } catch (BgpParseException e) {
Shashikanth VHc06002c2015-11-18 18:45:39 +053081 log.info("Exception BgpId string: " + e.toString());
82 }
83
84 }
85
Shashikanth VHe68bb5e2016-02-18 23:44:50 +053086 /*
87 * Get iso node ID in specified string format.
88 */
89 private String isoNodeIdString(byte[] isoNodeId) {
90 if (isoNodeId != null) {
91 int p1 = (int) isoNodeId[0] << 8 | (int) isoNodeId[1];
92 int p2 = (int) isoNodeId[2] << 8 | (int) isoNodeId[3];
93 int p3 = (int) isoNodeId[4] << 8 | (int) isoNodeId[5];
94
95 return String.format("%1$d.%2$d.%3$d", p1, p2, p3);
96 }
97 return null;
98 }
99
Shashikanth VHc06002c2015-11-18 18:45:39 +0530100 /**
101 * Initialize bgp id to generate URI.
102 *
103 * @param nodeNlri node Nlri.
104 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530105 public BgpDpid(final BgpNodeLSNlriVer4 nodeNlri) {
Shashikanth VHe68bb5e2016-02-18 23:44:50 +0530106 this.stringBuilder = new StringBuilder("");
Shashikanth VHc06002c2015-11-18 18:45:39 +0530107
108 if (nodeNlri.getRouteDistinguisher() != null) {
Shashikanth VHe68bb5e2016-02-18 23:44:50 +0530109 this.stringBuilder.append("RD=").append(nodeNlri.getRouteDistinguisher()
110 .getRouteDistinguisher()).append(":");
Shashikanth VHc06002c2015-11-18 18:45:39 +0530111 }
112
113 try {
Shashikanth VHe68bb5e2016-02-18 23:44:50 +0530114 if ((nodeNlri.getProtocolId() == BgpNodeLSNlriVer4.ProtocolType.ISIS_LEVEL_ONE)
115 || (nodeNlri.getProtocolId() == BgpNodeLSNlriVer4.ProtocolType.ISIS_LEVEL_TWO)) {
Shashikanth VHc06002c2015-11-18 18:45:39 +0530116
Shashikanth VHe68bb5e2016-02-18 23:44:50 +0530117 this.stringBuilder.append("PROTO=").append("ISIS").append(":ID=")
118 .append(nodeNlri.getIdentifier());
119 } else {
120 this.stringBuilder.append("PROTO=").append(nodeNlri.getProtocolId()).append(":ID=")
121 .append(nodeNlri.getIdentifier());
122 }
Shashikanth VH00af33f2015-11-29 03:27:26 +0530123 add(nodeNlri.getLocalNodeDescriptors());
Shashikanth VHc06002c2015-11-18 18:45:39 +0530124
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530125 } catch (BgpParseException e) {
Shashikanth VHc06002c2015-11-18 18:45:39 +0530126 log.info("Exception node string: " + e.toString());
127 }
128 }
129
130 BgpDpid add(final Object value) {
Shashikanth VHe68bb5e2016-02-18 23:44:50 +0530131 NodeDescriptors nodeDescriptors = null;
132 if (value instanceof BgpNodeLSIdentifier) {
133 BgpNodeLSIdentifier nodeLsIdentifier = (BgpNodeLSIdentifier) value;
134 nodeDescriptors = nodeLsIdentifier.getNodedescriptors();
135 } else if (value instanceof NodeDescriptors) {
136 nodeDescriptors = (NodeDescriptors) value;
137 }
138
139 if (nodeDescriptors != null) {
140 List<BgpValueType> subTlvs = nodeDescriptors.getSubTlvs();
141 ListIterator<BgpValueType> listIterator = subTlvs.listIterator();
142 while (listIterator.hasNext()) {
143 BgpValueType tlv = listIterator.next();
144 if (tlv.getType() == AutonomousSystemTlv.TYPE) {
145 AutonomousSystemTlv autonomousSystem = (AutonomousSystemTlv) tlv;
146 this.stringBuilder.append(":AS=").append(autonomousSystem.getAsNum());
147 } else if (tlv.getType() == BgpLSIdentifierTlv.TYPE) {
148 BgpLSIdentifierTlv lsIdentifierTlv = (BgpLSIdentifierTlv) tlv;
149 this.stringBuilder.append(":LSID=").append(lsIdentifierTlv.getBgpLsIdentifier());
150 } else if (tlv.getType() == AreaIDTlv.TYPE) {
151 AreaIDTlv areaIdTlv = (AreaIDTlv) tlv;
152 this.stringBuilder.append(":AREA=").append(areaIdTlv.getAreaID());
153 } else if (tlv.getType() == NodeDescriptors.IGP_ROUTERID_TYPE) {
154 if (tlv instanceof IsIsNonPseudonode) {
155 IsIsNonPseudonode isisNonPseudonode = (IsIsNonPseudonode) tlv;
156 this.stringBuilder.append(":ISOID=").append(isoNodeIdString(isisNonPseudonode.getIsoNodeId()));
157 } else if (tlv instanceof IsIsPseudonode) {
158 IsIsPseudonode isisPseudonode = (IsIsPseudonode) tlv;
159 this.stringBuilder.append(":ISOID=").append(isoNodeIdString(isisPseudonode.getIsoNodeId()));
160 this.stringBuilder.append(":PSN=").append(isisPseudonode.getPsnIdentifier());
161 } else if (tlv instanceof OspfNonPseudonode) {
162 OspfNonPseudonode ospfNonPseudonode = (OspfNonPseudonode) tlv;
163 this.stringBuilder.append(":RID=").append(ospfNonPseudonode.getrouterID());
164 } else if (tlv instanceof OspfPseudonode) {
165 OspfPseudonode ospfPseudonode = (OspfPseudonode) tlv;
166 this.stringBuilder.append(":RID=").append(ospfPseudonode.getrouterID());
167 }
168 }
169 }
Shashikanth VHc06002c2015-11-18 18:45:39 +0530170 }
171 return this;
172 }
173
174 @Override
175 public String toString() {
176 return this.stringBuilder.toString();
177 }
178
179 /**
180 * Produces bgp URI.
181 *
182 * @param value string to get URI
183 * @return bgp URI, otherwise null
184 */
185 public static URI uri(String value) {
186 try {
187 return new URI(SCHEME, value, null);
188 } catch (URISyntaxException e) {
189 log.info("Exception BgpId URI: " + e.toString());
190 }
191 return null;
192 }
193
194 /**
195 * Returns bgpDpid created from the given device URI.
196 *
197 * @param uri device URI
198 * @return object of BgpDpid
199 */
200 public static BgpDpid bgpDpid(URI uri) {
201 checkArgument(uri.getScheme().equals(SCHEME), "Unsupported URI scheme");
202
203 // TODO: return BgpDpid generated from uri
204 return null;
205 }
206}