blob: fb9ca1d1f50b7866b7cacb7be976bb0f3cf62bcb [file] [log] [blame]
Shashikanth VHc06002c2015-11-18 18:45:39 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Shashikanth VHc06002c2015-11-18 18:45:39 +05303 *
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;
Shashikanth VHe68bb5e2016-02-18 23:44:50 +053020import java.util.List;
21import java.util.ListIterator;
Shashikanth VHc06002c2015-11-18 18:45:39 +053022
Shashikanth VHc06002c2015-11-18 18:45:39 +053023import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053024import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4;
Shashikanth VHe68bb5e2016-02-18 23:44:50 +053025import org.onosproject.bgpio.protocol.linkstate.NodeDescriptors;
Shashikanth VHe68bb5e2016-02-18 23:44:50 +053026import org.onosproject.bgpio.types.AutonomousSystemTlv;
27import org.onosproject.bgpio.types.BgpLSIdentifierTlv;
28import org.onosproject.bgpio.types.BgpValueType;
29import org.onosproject.bgpio.types.IsIsNonPseudonode;
30import org.onosproject.bgpio.types.IsIsPseudonode;
31import org.onosproject.bgpio.types.OspfNonPseudonode;
32import org.onosproject.bgpio.types.OspfPseudonode;
Shashikanth VHc06002c2015-11-18 18:45:39 +053033import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
36/**
Shashikanth VHe68bb5e2016-02-18 23:44:50 +053037 * The class representing a network bgp device id.
Shashikanth VHc06002c2015-11-18 18:45:39 +053038 */
39public final class BgpDpid {
40 private static final Logger log = LoggerFactory.getLogger(BgpDpid.class);
41
Priyanka B9bee0802016-04-27 22:06:02 +053042 private static final String SCHEME = "l3";
Shashikanth VHc06002c2015-11-18 18:45:39 +053043 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 Bfc51c952016-03-26 14:30:33 +053049 * Initialize BGP id to generate URI.
Shashikanth VHc06002c2015-11-18 18:45:39 +053050 *
Priyanka Bfc51c952016-03-26 14:30:33 +053051 * @param linkNlri node NLRI.
Shashikanth VHc06002c2015-11-18 18:45:39 +053052 * @param nodeDescriptorType node descriptor type, local/remote
53 */
54 public BgpDpid(final BgpLinkLsNlriVer4 linkNlri, int nodeDescriptorType) {
Shashikanth VHe68bb5e2016-02-18 23:44:50 +053055 this.stringBuilder = new StringBuilder("");
Shashikanth VHc06002c2015-11-18 18:45:39 +053056
57 if (linkNlri.getRouteDistinguisher() != null) {
Shashikanth VHe68bb5e2016-02-18 23:44:50 +053058 this.stringBuilder.append("RD=").append(linkNlri.getRouteDistinguisher()
59 .getRouteDistinguisher()).append(":");
Shashikanth VHc06002c2015-11-18 18:45:39 +053060 }
Priyanka Bfc51c952016-03-26 14:30:33 +053061 this.stringBuilder.append(":ROUTINGUNIVERSE=").append(((BgpLinkLsNlriVer4) linkNlri).getIdentifier());
Shashikanth VHc06002c2015-11-18 18:45:39 +053062
Priyanka Bfc51c952016-03-26 14:30:33 +053063 if (nodeDescriptorType == NODE_DESCRIPTOR_LOCAL) {
mohamedrahil00f6f262016-11-24 20:20:41 +053064 log.debug("Local node descriptor added");
Priyanka Bfc51c952016-03-26 14:30:33 +053065 add(linkNlri.localNodeDescriptors());
66 } else if (nodeDescriptorType == NODE_DESCRIPTOR_REMOTE) {
mohamedrahil00f6f262016-11-24 20:20:41 +053067 log.debug("Remote node descriptor added");
Priyanka Bfc51c952016-03-26 14:30:33 +053068 add(linkNlri.remoteNodeDescriptors());
Shashikanth VHc06002c2015-11-18 18:45:39 +053069 }
Shashikanth VHc06002c2015-11-18 18:45:39 +053070 }
71
Shashikanth VHe68bb5e2016-02-18 23:44:50 +053072 /*
73 * Get iso node ID in specified string format.
74 */
Priyanka B19c08732016-06-04 21:11:19 +053075 public String isoNodeIdString(byte[] isoNodeId) {
Shashikanth VHe68bb5e2016-02-18 23:44:50 +053076 if (isoNodeId != null) {
Shashikanth VHabef1bf2016-03-09 15:54:45 +053077 return String.format("%02x%02x.%02x%02x.%02x%02x", isoNodeId[0], isoNodeId[1],
78 isoNodeId[2], isoNodeId[3],
79 isoNodeId[4], isoNodeId[5]);
Shashikanth VHe68bb5e2016-02-18 23:44:50 +053080 }
81 return null;
82 }
83
Shashikanth VHc06002c2015-11-18 18:45:39 +053084 /**
Priyanka Bfc51c952016-03-26 14:30:33 +053085 * Initialize BGP id to generate URI.
Shashikanth VHc06002c2015-11-18 18:45:39 +053086 *
Priyanka Bfc51c952016-03-26 14:30:33 +053087 * @param nlri node NLRI.
Shashikanth VHc06002c2015-11-18 18:45:39 +053088 */
Priyanka Bfc51c952016-03-26 14:30:33 +053089 public BgpDpid(final BgpNodeLSNlriVer4 nlri) {
Shashikanth VHe68bb5e2016-02-18 23:44:50 +053090 this.stringBuilder = new StringBuilder("");
Priyanka Bfc51c952016-03-26 14:30:33 +053091 if (((BgpNodeLSNlriVer4) nlri).getRouteDistinguisher() != null) {
92 this.stringBuilder.append("RD=")
93 .append(((BgpNodeLSNlriVer4) nlri).getRouteDistinguisher().getRouteDistinguisher()).append(":");
Shashikanth VHc06002c2015-11-18 18:45:39 +053094 }
95
Priyanka Bfc51c952016-03-26 14:30:33 +053096 this.stringBuilder.append(":ROUTINGUNIVERSE=").append(((BgpNodeLSNlriVer4) nlri).getIdentifier());
97 add(((BgpNodeLSNlriVer4) nlri).getLocalNodeDescriptors().getNodedescriptors());
mohamedrahil00f6f262016-11-24 20:20:41 +053098 log.debug("BgpDpid :: add");
Shashikanth VHc06002c2015-11-18 18:45:39 +053099 }
100
Priyanka Bfc51c952016-03-26 14:30:33 +0530101 /**
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) {
mohamedrahil00f6f262016-11-24 20:20:41 +0530108 log.debug("BgpDpid :: add function");
Priyanka Bfc51c952016-03-26 14:30:33 +0530109 if (value != null) {
110 List<BgpValueType> subTlvs = value.getSubTlvs();
Shashikanth VHe68bb5e2016-02-18 23:44:50 +0530111 ListIterator<BgpValueType> listIterator = subTlvs.listIterator();
112 while (listIterator.hasNext()) {
113 BgpValueType tlv = listIterator.next();
114 if (tlv.getType() == AutonomousSystemTlv.TYPE) {
Priyanka Bfc51c952016-03-26 14:30:33 +0530115 this.stringBuilder.append(":ASN=").append(((AutonomousSystemTlv) tlv).getAsNum());
Shashikanth VHe68bb5e2016-02-18 23:44:50 +0530116 } else if (tlv.getType() == BgpLSIdentifierTlv.TYPE) {
Priyanka Bfc51c952016-03-26 14:30:33 +0530117 this.stringBuilder.append(":DOMAINID=").append(((BgpLSIdentifierTlv) tlv).getBgpLsIdentifier());
Shashikanth VHe68bb5e2016-02-18 23:44:50 +0530118 } else if (tlv.getType() == NodeDescriptors.IGP_ROUTERID_TYPE) {
119 if (tlv instanceof IsIsNonPseudonode) {
Priyanka Bfc51c952016-03-26 14:30:33 +0530120 this.stringBuilder.append(":ISOID=").append(
121 isoNodeIdString(((IsIsNonPseudonode) tlv).getIsoNodeId()));
Shashikanth VHe68bb5e2016-02-18 23:44:50 +0530122 } else if (tlv instanceof IsIsPseudonode) {
Priyanka Bfc51c952016-03-26 14:30:33 +0530123 IsIsPseudonode isisPseudonode = ((IsIsPseudonode) tlv);
124 this.stringBuilder.append(":ISOID=").append(
125 isoNodeIdString(((IsIsPseudonode) tlv).getIsoNodeId()));
Shashikanth VHe68bb5e2016-02-18 23:44:50 +0530126 this.stringBuilder.append(":PSN=").append(isisPseudonode.getPsnIdentifier());
127 } else if (tlv instanceof OspfNonPseudonode) {
Priyanka Bfc51c952016-03-26 14:30:33 +0530128 this.stringBuilder.append(":RID=").append(((OspfNonPseudonode) tlv).getrouterID());
Shashikanth VHe68bb5e2016-02-18 23:44:50 +0530129 } else if (tlv instanceof OspfPseudonode) {
Priyanka Bfc51c952016-03-26 14:30:33 +0530130 this.stringBuilder.append(":RID=").append(((OspfPseudonode) tlv).getrouterID());
Shashikanth VHe68bb5e2016-02-18 23:44:50 +0530131 }
132 }
133 }
Shashikanth VHc06002c2015-11-18 18:45:39 +0530134 }
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) {
mohamedrahil00f6f262016-11-24 20:20:41 +0530153 log.debug("Exception BgpId URI: " + e.toString());
Shashikanth VHc06002c2015-11-18 18:45:39 +0530154 }
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}