Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 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 | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 23 | import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 24 | import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4; |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 25 | import org.onosproject.bgpio.protocol.linkstate.NodeDescriptors; |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 26 | import org.onosproject.bgpio.types.AutonomousSystemTlv; |
| 27 | import org.onosproject.bgpio.types.BgpLSIdentifierTlv; |
| 28 | import org.onosproject.bgpio.types.BgpValueType; |
| 29 | import org.onosproject.bgpio.types.IsIsNonPseudonode; |
| 30 | import org.onosproject.bgpio.types.IsIsPseudonode; |
| 31 | import org.onosproject.bgpio.types.OspfNonPseudonode; |
| 32 | import org.onosproject.bgpio.types.OspfPseudonode; |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
| 36 | /** |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 37 | * The class representing a network bgp device id. |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 38 | */ |
| 39 | public final class BgpDpid { |
| 40 | private static final Logger log = LoggerFactory.getLogger(BgpDpid.class); |
| 41 | |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 42 | private static final String SCHEME = "l3"; |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 43 | private static final long UNKNOWN = 0; |
| 44 | private StringBuilder stringBuilder; |
| 45 | public static final int NODE_DESCRIPTOR_LOCAL = 1; |
| 46 | public static final int NODE_DESCRIPTOR_REMOTE = 2; |
| 47 | |
| 48 | /** |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 49 | * Initialize BGP id to generate URI. |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 50 | * |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 51 | * @param linkNlri node NLRI. |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 52 | * @param nodeDescriptorType node descriptor type, local/remote |
| 53 | */ |
| 54 | public BgpDpid(final BgpLinkLsNlriVer4 linkNlri, int nodeDescriptorType) { |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 55 | this.stringBuilder = new StringBuilder(""); |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 56 | |
| 57 | if (linkNlri.getRouteDistinguisher() != null) { |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 58 | this.stringBuilder.append("RD=").append(linkNlri.getRouteDistinguisher() |
| 59 | .getRouteDistinguisher()).append(":"); |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 60 | } |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 61 | this.stringBuilder.append(":ROUTINGUNIVERSE=").append(((BgpLinkLsNlriVer4) linkNlri).getIdentifier()); |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 62 | |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 63 | if (nodeDescriptorType == NODE_DESCRIPTOR_LOCAL) { |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 64 | log.debug("Local node descriptor added"); |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 65 | add(linkNlri.localNodeDescriptors()); |
| 66 | } else if (nodeDescriptorType == NODE_DESCRIPTOR_REMOTE) { |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 67 | log.debug("Remote node descriptor added"); |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 68 | add(linkNlri.remoteNodeDescriptors()); |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 69 | } |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 70 | } |
| 71 | |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 72 | /* |
| 73 | * Get iso node ID in specified string format. |
| 74 | */ |
Priyanka B | 19c0873 | 2016-06-04 21:11:19 +0530 | [diff] [blame] | 75 | public String isoNodeIdString(byte[] isoNodeId) { |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 76 | if (isoNodeId != null) { |
Shashikanth VH | abef1bf | 2016-03-09 15:54:45 +0530 | [diff] [blame] | 77 | return String.format("%02x%02x.%02x%02x.%02x%02x", isoNodeId[0], isoNodeId[1], |
| 78 | isoNodeId[2], isoNodeId[3], |
| 79 | isoNodeId[4], isoNodeId[5]); |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 80 | } |
| 81 | return null; |
| 82 | } |
| 83 | |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 84 | /** |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 85 | * Initialize BGP id to generate URI. |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 86 | * |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 87 | * @param nlri node NLRI. |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 88 | */ |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 89 | public BgpDpid(final BgpNodeLSNlriVer4 nlri) { |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 90 | this.stringBuilder = new StringBuilder(""); |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 91 | if (((BgpNodeLSNlriVer4) nlri).getRouteDistinguisher() != null) { |
| 92 | this.stringBuilder.append("RD=") |
| 93 | .append(((BgpNodeLSNlriVer4) nlri).getRouteDistinguisher().getRouteDistinguisher()).append(":"); |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 94 | } |
| 95 | |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 96 | this.stringBuilder.append(":ROUTINGUNIVERSE=").append(((BgpNodeLSNlriVer4) nlri).getIdentifier()); |
| 97 | add(((BgpNodeLSNlriVer4) nlri).getLocalNodeDescriptors().getNodedescriptors()); |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 98 | log.debug("BgpDpid :: add"); |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 99 | } |
| 100 | |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 101 | /** |
| 102 | * Obtains instance of this class by appending stringBuilder with node descriptor value. |
| 103 | * |
| 104 | * @param value node descriptor |
| 105 | * @return instance of this class |
| 106 | */ |
| 107 | public BgpDpid add(final NodeDescriptors value) { |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 108 | log.debug("BgpDpid :: add function"); |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 109 | if (value != null) { |
| 110 | List<BgpValueType> subTlvs = value.getSubTlvs(); |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 111 | ListIterator<BgpValueType> listIterator = subTlvs.listIterator(); |
| 112 | while (listIterator.hasNext()) { |
| 113 | BgpValueType tlv = listIterator.next(); |
| 114 | if (tlv.getType() == AutonomousSystemTlv.TYPE) { |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 115 | this.stringBuilder.append(":ASN=").append(((AutonomousSystemTlv) tlv).getAsNum()); |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 116 | } else if (tlv.getType() == BgpLSIdentifierTlv.TYPE) { |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 117 | this.stringBuilder.append(":DOMAINID=").append(((BgpLSIdentifierTlv) tlv).getBgpLsIdentifier()); |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 118 | } else if (tlv.getType() == NodeDescriptors.IGP_ROUTERID_TYPE) { |
| 119 | if (tlv instanceof IsIsNonPseudonode) { |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 120 | this.stringBuilder.append(":ISOID=").append( |
| 121 | isoNodeIdString(((IsIsNonPseudonode) tlv).getIsoNodeId())); |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 122 | } else if (tlv instanceof IsIsPseudonode) { |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 123 | IsIsPseudonode isisPseudonode = ((IsIsPseudonode) tlv); |
| 124 | this.stringBuilder.append(":ISOID=").append( |
| 125 | isoNodeIdString(((IsIsPseudonode) tlv).getIsoNodeId())); |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 126 | this.stringBuilder.append(":PSN=").append(isisPseudonode.getPsnIdentifier()); |
| 127 | } else if (tlv instanceof OspfNonPseudonode) { |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 128 | this.stringBuilder.append(":RID=").append(((OspfNonPseudonode) tlv).getrouterID()); |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 129 | } else if (tlv instanceof OspfPseudonode) { |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 130 | this.stringBuilder.append(":RID=").append(((OspfPseudonode) tlv).getrouterID()); |
Shashikanth VH | e68bb5e | 2016-02-18 23:44:50 +0530 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | } |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 134 | } |
| 135 | return this; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public String toString() { |
| 140 | return this.stringBuilder.toString(); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Produces bgp URI. |
| 145 | * |
| 146 | * @param value string to get URI |
| 147 | * @return bgp URI, otherwise null |
| 148 | */ |
| 149 | public static URI uri(String value) { |
| 150 | try { |
| 151 | return new URI(SCHEME, value, null); |
| 152 | } catch (URISyntaxException e) { |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 153 | log.debug("Exception BgpId URI: " + e.toString()); |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 154 | } |
| 155 | return null; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Returns bgpDpid created from the given device URI. |
| 160 | * |
| 161 | * @param uri device URI |
| 162 | * @return object of BgpDpid |
| 163 | */ |
| 164 | public static BgpDpid bgpDpid(URI uri) { |
| 165 | checkArgument(uri.getScheme().equals(SCHEME), "Unsupported URI scheme"); |
| 166 | |
| 167 | // TODO: return BgpDpid generated from uri |
| 168 | return null; |
| 169 | } |
| 170 | } |