blob: ed04dc942d69cf696063af2742c21e8efe49b88f [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;
20
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053021import org.onosproject.bgpio.exceptions.BgpParseException;
Shashikanth VHc06002c2015-11-18 18:45:39 +053022import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053023import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4;
Shashikanth VHc06002c2015-11-18 18:45:39 +053024import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
27/**
28 * The class representing a network bgp device id. This class is immutable.
29 */
30public 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 VH00af33f2015-11-29 03:27:26 +053049 this.stringBuilder.append(linkNlri.getRouteDistinguisher().getRouteDistinguisher()).append(':');
Shashikanth VHc06002c2015-11-18 18:45:39 +053050 }
51
52 try {
Shashikanth VH00af33f2015-11-29 03:27:26 +053053 this.stringBuilder.append(linkNlri.getProtocolId()).append(':').append(linkNlri.getIdentifier())
54 .append('/');
Shashikanth VHc06002c2015-11-18 18:45:39 +053055
56 if (nodeDescriptorType == NODE_DESCRIPTOR_LOCAL) {
Shashikanth VH00af33f2015-11-29 03:27:26 +053057 add(linkNlri.localNodeDescriptors());
Shashikanth VHc06002c2015-11-18 18:45:39 +053058 } else if (nodeDescriptorType == NODE_DESCRIPTOR_REMOTE) {
Shashikanth VH00af33f2015-11-29 03:27:26 +053059 add(linkNlri.remoteNodeDescriptors());
Shashikanth VHc06002c2015-11-18 18:45:39 +053060 }
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053061 } catch (BgpParseException e) {
Shashikanth VHc06002c2015-11-18 18:45:39 +053062 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 VH5dd8dbe2015-11-26 13:22:18 +053072 public BgpDpid(final BgpNodeLSNlriVer4 nodeNlri) {
Shashikanth VHc06002c2015-11-18 18:45:39 +053073 this.stringBuilder = new StringBuilder("bgpls://");
74
75 if (nodeNlri.getRouteDistinguisher() != null) {
Shashikanth VH00af33f2015-11-29 03:27:26 +053076 this.stringBuilder.append(nodeNlri.getRouteDistinguisher().getRouteDistinguisher()).append(':');
Shashikanth VHc06002c2015-11-18 18:45:39 +053077 }
78
79 try {
80
Shashikanth VH00af33f2015-11-29 03:27:26 +053081 this.stringBuilder.append(nodeNlri.getProtocolId()).append(':').append(nodeNlri.getIdentifier())
82 .append('/');
Shashikanth VHc06002c2015-11-18 18:45:39 +053083
Shashikanth VH00af33f2015-11-29 03:27:26 +053084 add(nodeNlri.getLocalNodeDescriptors());
Shashikanth VHc06002c2015-11-18 18:45:39 +053085
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053086 } catch (BgpParseException e) {
Shashikanth VHc06002c2015-11-18 18:45:39 +053087 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}