blob: 71117c2a479c1e142ece88218d74312763587545 [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) {
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 }
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053064 } catch (BgpParseException e) {
Shashikanth VHc06002c2015-11-18 18:45:39 +053065 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 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053075 public BgpDpid(final BgpNodeLSNlriVer4 nodeNlri) {
Shashikanth VHc06002c2015-11-18 18:45:39 +053076 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
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053092 } catch (BgpParseException e) {
Shashikanth VHc06002c2015-11-18 18:45:39 +053093 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}