blob: aadbee1cb6906e7e38efc7763b56100f888618c5 [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
42 private static final String SCHEME = "bgp";
43 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) {
64 add(linkNlri.localNodeDescriptors());
65 } else if (nodeDescriptorType == NODE_DESCRIPTOR_REMOTE) {
66 add(linkNlri.remoteNodeDescriptors());
Shashikanth VHc06002c2015-11-18 18:45:39 +053067 }
Shashikanth VHc06002c2015-11-18 18:45:39 +053068 }
69
Shashikanth VHe68bb5e2016-02-18 23:44:50 +053070 /*
71 * Get iso node ID in specified string format.
72 */
73 private String isoNodeIdString(byte[] isoNodeId) {
74 if (isoNodeId != null) {
Shashikanth VHabef1bf2016-03-09 15:54:45 +053075 return String.format("%02x%02x.%02x%02x.%02x%02x", isoNodeId[0], isoNodeId[1],
76 isoNodeId[2], isoNodeId[3],
77 isoNodeId[4], isoNodeId[5]);
Shashikanth VHe68bb5e2016-02-18 23:44:50 +053078 }
79 return null;
80 }
81
Shashikanth VHc06002c2015-11-18 18:45:39 +053082 /**
Priyanka Bfc51c952016-03-26 14:30:33 +053083 * Initialize BGP id to generate URI.
Shashikanth VHc06002c2015-11-18 18:45:39 +053084 *
Priyanka Bfc51c952016-03-26 14:30:33 +053085 * @param nlri node NLRI.
Shashikanth VHc06002c2015-11-18 18:45:39 +053086 */
Priyanka Bfc51c952016-03-26 14:30:33 +053087 public BgpDpid(final BgpNodeLSNlriVer4 nlri) {
Shashikanth VHe68bb5e2016-02-18 23:44:50 +053088 this.stringBuilder = new StringBuilder("");
Priyanka Bfc51c952016-03-26 14:30:33 +053089 if (((BgpNodeLSNlriVer4) nlri).getRouteDistinguisher() != null) {
90 this.stringBuilder.append("RD=")
91 .append(((BgpNodeLSNlriVer4) nlri).getRouteDistinguisher().getRouteDistinguisher()).append(":");
Shashikanth VHc06002c2015-11-18 18:45:39 +053092 }
93
Priyanka Bfc51c952016-03-26 14:30:33 +053094 this.stringBuilder.append(":ROUTINGUNIVERSE=").append(((BgpNodeLSNlriVer4) nlri).getIdentifier());
95 add(((BgpNodeLSNlriVer4) nlri).getLocalNodeDescriptors().getNodedescriptors());
96 log.info("BgpDpid :: add");
Shashikanth VHc06002c2015-11-18 18:45:39 +053097 }
98
Priyanka Bfc51c952016-03-26 14:30:33 +053099 /**
100 * Obtains instance of this class by appending stringBuilder with node descriptor value.
101 *
102 * @param value node descriptor
103 * @return instance of this class
104 */
105 public BgpDpid add(final NodeDescriptors value) {
106 log.info("BgpDpid :: add function");
107 if (value != null) {
108 List<BgpValueType> subTlvs = value.getSubTlvs();
Shashikanth VHe68bb5e2016-02-18 23:44:50 +0530109 ListIterator<BgpValueType> listIterator = subTlvs.listIterator();
110 while (listIterator.hasNext()) {
111 BgpValueType tlv = listIterator.next();
112 if (tlv.getType() == AutonomousSystemTlv.TYPE) {
Priyanka Bfc51c952016-03-26 14:30:33 +0530113 this.stringBuilder.append(":ASN=").append(((AutonomousSystemTlv) tlv).getAsNum());
Shashikanth VHe68bb5e2016-02-18 23:44:50 +0530114 } else if (tlv.getType() == BgpLSIdentifierTlv.TYPE) {
Priyanka Bfc51c952016-03-26 14:30:33 +0530115 this.stringBuilder.append(":DOMAINID=").append(((BgpLSIdentifierTlv) tlv).getBgpLsIdentifier());
Shashikanth VHe68bb5e2016-02-18 23:44:50 +0530116 } else if (tlv.getType() == NodeDescriptors.IGP_ROUTERID_TYPE) {
117 if (tlv instanceof IsIsNonPseudonode) {
Priyanka Bfc51c952016-03-26 14:30:33 +0530118 this.stringBuilder.append(":ISOID=").append(
119 isoNodeIdString(((IsIsNonPseudonode) tlv).getIsoNodeId()));
Shashikanth VHe68bb5e2016-02-18 23:44:50 +0530120 } else if (tlv instanceof IsIsPseudonode) {
Priyanka Bfc51c952016-03-26 14:30:33 +0530121 IsIsPseudonode isisPseudonode = ((IsIsPseudonode) tlv);
122 this.stringBuilder.append(":ISOID=").append(
123 isoNodeIdString(((IsIsPseudonode) tlv).getIsoNodeId()));
Shashikanth VHe68bb5e2016-02-18 23:44:50 +0530124 this.stringBuilder.append(":PSN=").append(isisPseudonode.getPsnIdentifier());
125 } else if (tlv instanceof OspfNonPseudonode) {
Priyanka Bfc51c952016-03-26 14:30:33 +0530126 this.stringBuilder.append(":RID=").append(((OspfNonPseudonode) tlv).getrouterID());
Shashikanth VHe68bb5e2016-02-18 23:44:50 +0530127 } else if (tlv instanceof OspfPseudonode) {
Priyanka Bfc51c952016-03-26 14:30:33 +0530128 this.stringBuilder.append(":RID=").append(((OspfPseudonode) tlv).getrouterID());
Shashikanth VHe68bb5e2016-02-18 23:44:50 +0530129 }
130 }
131 }
Shashikanth VHc06002c2015-11-18 18:45:39 +0530132 }
133 return this;
134 }
135
136 @Override
137 public String toString() {
138 return this.stringBuilder.toString();
139 }
140
141 /**
142 * Produces bgp URI.
143 *
144 * @param value string to get URI
145 * @return bgp URI, otherwise null
146 */
147 public static URI uri(String value) {
148 try {
149 return new URI(SCHEME, value, null);
150 } catch (URISyntaxException e) {
151 log.info("Exception BgpId URI: " + e.toString());
152 }
153 return null;
154 }
155
156 /**
157 * Returns bgpDpid created from the given device URI.
158 *
159 * @param uri device URI
160 * @return object of BgpDpid
161 */
162 public static BgpDpid bgpDpid(URI uri) {
163 checkArgument(uri.getScheme().equals(SCHEME), "Unsupported URI scheme");
164
165 // TODO: return BgpDpid generated from uri
166 return null;
167 }
168}