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 | |
| 21 | import org.onosproject.bgpio.exceptions.BGPParseException; |
| 22 | import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4; |
| 23 | import org.onosproject.bgpio.protocol.linkstate.BGPNodeLSNlriVer4; |
| 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) { |
| 49 | this.stringBuilder.append(Long.toString(linkNlri.getRouteDistinguisher().getRouteDistinguisher())) |
| 50 | .append(':'); |
| 51 | } |
| 52 | |
| 53 | try { |
| 54 | this.stringBuilder.append(linkNlri.getProtocolId().toString()) |
| 55 | .append(':') |
| 56 | .append(Long.toString(linkNlri.getIdentifier())) |
| 57 | .append('/'); |
| 58 | |
| 59 | if (nodeDescriptorType == NODE_DESCRIPTOR_LOCAL) { |
| 60 | add(linkNlri.localNodeDescriptors().toString()); |
| 61 | } else if (nodeDescriptorType == NODE_DESCRIPTOR_REMOTE) { |
| 62 | add(linkNlri.remoteNodeDescriptors().toString()); |
| 63 | } |
| 64 | } catch (BGPParseException e) { |
| 65 | log.info("Exception BgpId string: " + e.toString()); |
| 66 | } |
| 67 | |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Initialize bgp id to generate URI. |
| 72 | * |
| 73 | * @param nodeNlri node Nlri. |
| 74 | */ |
| 75 | public BgpDpid(final BGPNodeLSNlriVer4 nodeNlri) { |
| 76 | this.stringBuilder = new StringBuilder("bgpls://"); |
| 77 | |
| 78 | if (nodeNlri.getRouteDistinguisher() != null) { |
| 79 | this.stringBuilder.append(Long.toString(nodeNlri.getRouteDistinguisher().getRouteDistinguisher())) |
| 80 | .append(':'); |
| 81 | } |
| 82 | |
| 83 | try { |
| 84 | |
| 85 | this.stringBuilder.append(nodeNlri.getProtocolId().toString()) |
| 86 | .append(':') |
| 87 | .append(Long.toString(nodeNlri.getIdentifier())) |
| 88 | .append('/'); |
| 89 | |
| 90 | add(nodeNlri.getLocalNodeDescriptors().toString()); |
| 91 | |
| 92 | } catch (BGPParseException e) { |
| 93 | log.info("Exception node string: " + e.toString()); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | BgpDpid add(final Object value) { |
| 98 | if (value != null) { |
| 99 | this.stringBuilder.append('&').append('=').append(value.toString()); |
| 100 | } |
| 101 | return this; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public String toString() { |
| 106 | return this.stringBuilder.toString(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Produces bgp URI. |
| 111 | * |
| 112 | * @param value string to get URI |
| 113 | * @return bgp URI, otherwise null |
| 114 | */ |
| 115 | public static URI uri(String value) { |
| 116 | try { |
| 117 | return new URI(SCHEME, value, null); |
| 118 | } catch (URISyntaxException e) { |
| 119 | log.info("Exception BgpId URI: " + e.toString()); |
| 120 | } |
| 121 | return null; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns bgpDpid created from the given device URI. |
| 126 | * |
| 127 | * @param uri device URI |
| 128 | * @return object of BgpDpid |
| 129 | */ |
| 130 | public static BgpDpid bgpDpid(URI uri) { |
| 131 | checkArgument(uri.getScheme().equals(SCHEME), "Unsupported URI scheme"); |
| 132 | |
| 133 | // TODO: return BgpDpid generated from uri |
| 134 | return null; |
| 135 | } |
| 136 | } |