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; |
| 20 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 21 | import org.onosproject.bgpio.exceptions.BgpParseException; |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 22 | import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 23 | import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4; |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | /** |
| 28 | * The class representing a network bgp device id. This class is immutable. |
| 29 | */ |
| 30 | public final class BgpDpid { |
| 31 | private static final Logger log = LoggerFactory.getLogger(BgpDpid.class); |
| 32 | |
| 33 | private static final String SCHEME = "bgp"; |
| 34 | private static final long UNKNOWN = 0; |
| 35 | private StringBuilder stringBuilder; |
| 36 | public static final int NODE_DESCRIPTOR_LOCAL = 1; |
| 37 | public static final int NODE_DESCRIPTOR_REMOTE = 2; |
| 38 | |
| 39 | /** |
| 40 | * Initialize bgp id to generate URI. |
| 41 | * |
| 42 | * @param linkNlri node Nlri. |
| 43 | * @param nodeDescriptorType node descriptor type, local/remote |
| 44 | */ |
| 45 | public BgpDpid(final BgpLinkLsNlriVer4 linkNlri, int nodeDescriptorType) { |
| 46 | this.stringBuilder = new StringBuilder("bgpls://"); |
| 47 | |
| 48 | if (linkNlri.getRouteDistinguisher() != null) { |
Shashikanth VH | 00af33f | 2015-11-29 03:27:26 +0530 | [diff] [blame] | 49 | this.stringBuilder.append(linkNlri.getRouteDistinguisher().getRouteDistinguisher()).append(':'); |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | try { |
Shashikanth VH | 00af33f | 2015-11-29 03:27:26 +0530 | [diff] [blame] | 53 | this.stringBuilder.append(linkNlri.getProtocolId()).append(':').append(linkNlri.getIdentifier()) |
| 54 | .append('/'); |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 55 | |
| 56 | if (nodeDescriptorType == NODE_DESCRIPTOR_LOCAL) { |
Shashikanth VH | 00af33f | 2015-11-29 03:27:26 +0530 | [diff] [blame] | 57 | add(linkNlri.localNodeDescriptors()); |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 58 | } else if (nodeDescriptorType == NODE_DESCRIPTOR_REMOTE) { |
Shashikanth VH | 00af33f | 2015-11-29 03:27:26 +0530 | [diff] [blame] | 59 | add(linkNlri.remoteNodeDescriptors()); |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 60 | } |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 61 | } catch (BgpParseException e) { |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 62 | log.info("Exception BgpId string: " + e.toString()); |
| 63 | } |
| 64 | |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Initialize bgp id to generate URI. |
| 69 | * |
| 70 | * @param nodeNlri node Nlri. |
| 71 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 72 | public BgpDpid(final BgpNodeLSNlriVer4 nodeNlri) { |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 73 | this.stringBuilder = new StringBuilder("bgpls://"); |
| 74 | |
| 75 | if (nodeNlri.getRouteDistinguisher() != null) { |
Shashikanth VH | 00af33f | 2015-11-29 03:27:26 +0530 | [diff] [blame] | 76 | this.stringBuilder.append(nodeNlri.getRouteDistinguisher().getRouteDistinguisher()).append(':'); |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | try { |
| 80 | |
Shashikanth VH | 00af33f | 2015-11-29 03:27:26 +0530 | [diff] [blame] | 81 | this.stringBuilder.append(nodeNlri.getProtocolId()).append(':').append(nodeNlri.getIdentifier()) |
| 82 | .append('/'); |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 83 | |
Shashikanth VH | 00af33f | 2015-11-29 03:27:26 +0530 | [diff] [blame] | 84 | add(nodeNlri.getLocalNodeDescriptors()); |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 85 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 86 | } catch (BgpParseException e) { |
Shashikanth VH | c06002c | 2015-11-18 18:45:39 +0530 | [diff] [blame] | 87 | log.info("Exception node string: " + e.toString()); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | BgpDpid add(final Object value) { |
| 92 | if (value != null) { |
| 93 | this.stringBuilder.append('&').append('=').append(value.toString()); |
| 94 | } |
| 95 | return this; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public String toString() { |
| 100 | return this.stringBuilder.toString(); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Produces bgp URI. |
| 105 | * |
| 106 | * @param value string to get URI |
| 107 | * @return bgp URI, otherwise null |
| 108 | */ |
| 109 | public static URI uri(String value) { |
| 110 | try { |
| 111 | return new URI(SCHEME, value, null); |
| 112 | } catch (URISyntaxException e) { |
| 113 | log.info("Exception BgpId URI: " + e.toString()); |
| 114 | } |
| 115 | return null; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Returns bgpDpid created from the given device URI. |
| 120 | * |
| 121 | * @param uri device URI |
| 122 | * @return object of BgpDpid |
| 123 | */ |
| 124 | public static BgpDpid bgpDpid(URI uri) { |
| 125 | checkArgument(uri.getScheme().equals(SCHEME), "Unsupported URI scheme"); |
| 126 | |
| 127 | // TODO: return BgpDpid generated from uri |
| 128 | return null; |
| 129 | } |
| 130 | } |