2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.bgpcep.bgp.topology.provider;
10 import com.google.common.io.BaseEncoding;
11 import java.util.Arrays;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.IsisAreaIdentifier;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.NodeIdentifier;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.object.type.LinkCase;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.object.type.link._case.LinkDescriptors;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.routes.linkstate.routes.LinkstateRoute;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.node.identifier.CRouterIdentifier;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.node.identifier.c.router.identifier.IsisNodeCase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.node.identifier.c.router.identifier.IsisPseudonodeCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.node.identifier.c.router.identifier.OspfNodeCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.node.identifier.c.router.identifier.OspfPseudonodeCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.node.identifier.c.router.identifier.isis.pseudonode._case.IsisPseudonode;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.node.identifier.c.router.identifier.ospf.pseudonode._case.OspfPseudonode;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.IsoSystemIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 final class UriBuilder {
29 private static final Logger LOG = LoggerFactory.getLogger(UriBuilder.class);
30 private static final char HEX_SEPARATOR = '.';
31 private static final int AREA_ID_MAX_SIZE = 7;
32 private final StringBuilder sb;
34 UriBuilder(final UriBuilder base, final String type) {
35 this.sb = new StringBuilder(base.sb);
36 this.sb.append("type=").append(type);
39 UriBuilder(final LinkstateRoute route) {
40 this.sb = new StringBuilder("bgpls://");
42 if (route.getRouteDistinguisher() != null) {
44 if (route.getRouteDistinguisher().getRdAs() != null) {
45 rd = route.getRouteDistinguisher().getRdAs().getValue();
46 } else if (route.getRouteDistinguisher().getRdIpv4() != null) {
47 rd = route.getRouteDistinguisher().getRdIpv4().getValue();
49 rd = route.getRouteDistinguisher().getRdTwoOctetAs().getValue();
51 this.sb.append(rd).append(':');
54 this.sb.append(route.getProtocolId().toString()).append(':')
55 .append(route.getIdentifier().getValue().toString()).append('/');
58 UriBuilder add(final String name, final Object value) {
60 this.sb.append('&').append(name).append('=').append(value.toString());
65 UriBuilder add(final LinkCase link) {
66 addPrefix("local-", link.getLocalNodeDescriptors());
67 addPrefix("remote-", link.getRemoteNodeDescriptors());
69 final LinkDescriptors ld = link.getLinkDescriptors();
70 if (ld.getIpv4InterfaceAddress() != null) {
71 add("ipv4-iface", ld.getIpv4InterfaceAddress().getValue());
73 if (ld.getIpv4NeighborAddress() != null) {
74 add("ipv4-neigh", ld.getIpv4NeighborAddress().getValue());
76 if (ld.getIpv6InterfaceAddress() != null) {
77 add("ipv6-iface", ld.getIpv6InterfaceAddress().getValue());
79 if (ld.getIpv6NeighborAddress() != null) {
80 add("ipv6-neigh", ld.getIpv6NeighborAddress().getValue());
82 if (ld.getMultiTopologyId() != null) {
83 add("mt", ld.getMultiTopologyId().getValue());
85 add("local-id", ld.getLinkLocalIdentifier());
86 add("remote-id", ld.getLinkRemoteIdentifier());
90 private static String isoId(final byte[] bytes) {
91 final StringBuilder sBuilder = new StringBuilder();
93 while (id < bytes.length) {
94 sBuilder.append(BaseEncoding.base16().encode(new byte[] { bytes[id++], bytes[id++] }));
95 if (id != bytes.length) {
96 sBuilder.append(HEX_SEPARATOR);
99 return sBuilder.toString();
103 * Creates a String representation of ISO system identifier
104 * in format XX.XX.XX where X is one byte.
106 * @param systemId IsoSystemIdentifier object
107 * @return String representation of ISO Identifier
109 public static String isoId(final IsoSystemIdentifier systemId) {
110 return isoId(systemId.getValue());
113 private static String formatRouterIdentifier(final CRouterIdentifier routerIdentifier) {
114 if (routerIdentifier == null) {
117 if (routerIdentifier instanceof IsisNodeCase) {
118 return isoId(((IsisNodeCase) routerIdentifier).getIsisNode().getIsoSystemId());
120 if (routerIdentifier instanceof IsisPseudonodeCase) {
121 final IsisPseudonode r = ((IsisPseudonodeCase) routerIdentifier).getIsisPseudonode();
122 return isoId(r.getIsIsRouterIdentifier().getIsoSystemId().getValue()) + '.'
123 + BaseEncoding.base16().encode(new byte[] { r.getPsn().byteValue() });
125 if (routerIdentifier instanceof OspfNodeCase) {
126 return ((OspfNodeCase) routerIdentifier).getOspfNode().getOspfRouterId().toString();
128 if (routerIdentifier instanceof OspfPseudonodeCase) {
129 final OspfPseudonode r = ((OspfPseudonodeCase) routerIdentifier).getOspfPseudonode();
130 return r.getOspfRouterId().toString() + ':' + r.getLanInterface().getValue();
132 LOG.warn("Unhandled router identifier type {}, fallback to toString()",
133 routerIdentifier.implementedInterface());
134 return routerIdentifier.toString();
137 UriBuilder addPrefix(final String prefix, final NodeIdentifier node) {
138 if (node.getAsNumber() != null) {
139 add(prefix + "as", node.getAsNumber().getValue());
141 if (node.getDomainId() != null) {
142 add(prefix + "domain", node.getDomainId().getValue());
144 if (node.getAreaId() != null) {
145 add(prefix + "area", node.getAreaId().getValue());
147 add(prefix + "router", formatRouterIdentifier(node.getCRouterIdentifier()));
152 public String toString() {
153 final String ret = this.sb.toString();
154 LOG.trace("New URI {}", ret);
159 * Creates string representation of IS-IS Network Entity Title,
160 * based on Area Identifier and System Identifier.
162 * @param areaId IS-IS Area Identifier
163 * @param systemId string representation of ISO SYSTEM-ID
166 public static String toIsoNetId(final IsisAreaIdentifier areaId, final String systemId) {
167 final byte[] value = areaId.getValue();
169 //ISIS area identifier might have variable length, but need to fit the IsoNetId pattern
170 return BaseEncoding.base16().encode(value, 0, 1) + HEX_SEPARATOR
171 + UriBuilder.isoId(Arrays.copyOfRange(value, 1, AREA_ID_MAX_SIZE)) + HEX_SEPARATOR + systemId;