Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 14 | package org.onosproject.bgp.controller; |
| 15 | |
| 16 | import static com.google.common.base.Preconditions.checkArgument; |
| 17 | |
| 18 | import java.net.URI; |
| 19 | import java.net.URISyntaxException; |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 20 | import java.util.List; |
| 21 | import java.util.ListIterator; |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 22 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 23 | import org.onosproject.bgpio.exceptions.BgpParseException; |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 24 | import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4; |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 25 | import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSIdentifier; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 26 | import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4; |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 27 | import org.onosproject.bgpio.protocol.linkstate.NodeDescriptors; |
| 28 | import org.onosproject.bgpio.types.AreaIDTlv; |
| 29 | import org.onosproject.bgpio.types.AutonomousSystemTlv; |
| 30 | import org.onosproject.bgpio.types.BgpLSIdentifierTlv; |
| 31 | import org.onosproject.bgpio.types.BgpValueType; |
| 32 | import org.onosproject.bgpio.types.IsIsNonPseudonode; |
| 33 | import org.onosproject.bgpio.types.IsIsPseudonode; |
| 34 | import org.onosproject.bgpio.types.OspfNonPseudonode; |
| 35 | import org.onosproject.bgpio.types.OspfPseudonode; |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
| 39 | /** |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 40 | * The class representing a network bgp device id. |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 41 | */ |
| 42 | public 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 VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 58 | this.stringBuilder = new StringBuilder(""); |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 59 | |
| 60 | if (linkNlri.getRouteDistinguisher() != null) { |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 61 | this.stringBuilder.append("RD=").append(linkNlri.getRouteDistinguisher() |
| 62 | .getRouteDistinguisher()).append(":"); |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | try { |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 66 | 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 VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 74 | |
| 75 | if (nodeDescriptorType == NODE_DESCRIPTOR_LOCAL) { |
Shashikanth VH | 00af33f | 2015-11-29 03:27:26 +0530 | [diff] [blame] | 76 | add(linkNlri.localNodeDescriptors()); |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 77 | } else if (nodeDescriptorType == NODE_DESCRIPTOR_REMOTE) { |
Shashikanth VH | 00af33f | 2015-11-29 03:27:26 +0530 | [diff] [blame] | 78 | add(linkNlri.remoteNodeDescriptors()); |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 79 | } |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 80 | } catch (BgpParseException e) { |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 81 | log.info("Exception BgpId string: " + e.toString()); |
| 82 | } |
| 83 | |
| 84 | } |
| 85 | |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 86 | /* |
| 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 VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 100 | /** |
| 101 | * Initialize bgp id to generate URI. |
| 102 | * |
| 103 | * @param nodeNlri node Nlri. |
| 104 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 105 | public BgpDpid(final BgpNodeLSNlriVer4 nodeNlri) { |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 106 | this.stringBuilder = new StringBuilder(""); |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 107 | |
| 108 | if (nodeNlri.getRouteDistinguisher() != null) { |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 109 | this.stringBuilder.append("RD=").append(nodeNlri.getRouteDistinguisher() |
| 110 | .getRouteDistinguisher()).append(":"); |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | try { |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 114 | if ((nodeNlri.getProtocolId() == BgpNodeLSNlriVer4.ProtocolType.ISIS_LEVEL_ONE) |
| 115 | || (nodeNlri.getProtocolId() == BgpNodeLSNlriVer4.ProtocolType.ISIS_LEVEL_TWO)) { |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 116 | |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 117 | 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 VH | 00af33f | 2015-11-29 03:27:26 +0530 | [diff] [blame] | 123 | add(nodeNlri.getLocalNodeDescriptors()); |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 124 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 125 | } catch (BgpParseException e) { |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 126 | log.info("Exception node string: " + e.toString()); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | BgpDpid add(final Object value) { |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 131 | 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 VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 170 | } |
| 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 | } |