YANG revision dates mass-update
[bgpcep.git] / bgp / topology-provider / src / main / java / org / opendaylight / bgpcep / bgp / topology / provider / UriBuilder.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.bgpcep.bgp.topology.provider;
9
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;
27
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;
33
34     UriBuilder(final UriBuilder base, final String type) {
35         this.sb = new StringBuilder(base.sb);
36         this.sb.append("type=").append(type);
37     }
38
39     UriBuilder(final LinkstateRoute route) {
40         this.sb = new StringBuilder("bgpls://");
41
42         if (route.getRouteDistinguisher() != null) {
43             String rd;
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();
48             } else  {
49                 rd = route.getRouteDistinguisher().getRdTwoOctetAs().getValue();
50             }
51             this.sb.append(rd).append(':');
52         }
53
54         this.sb.append(route.getProtocolId().toString()).append(':')
55                 .append(route.getIdentifier().getValue().toString()).append('/');
56     }
57
58     UriBuilder add(final String name, final Object value) {
59         if (value != null) {
60             this.sb.append('&').append(name).append('=').append(value.toString());
61         }
62         return this;
63     }
64
65     UriBuilder add(final LinkCase link) {
66         addPrefix("local-", link.getLocalNodeDescriptors());
67         addPrefix("remote-", link.getRemoteNodeDescriptors());
68
69         final LinkDescriptors ld = link.getLinkDescriptors();
70         if (ld.getIpv4InterfaceAddress() != null) {
71             add("ipv4-iface", ld.getIpv4InterfaceAddress().getValue());
72         }
73         if (ld.getIpv4NeighborAddress() != null) {
74             add("ipv4-neigh", ld.getIpv4NeighborAddress().getValue());
75         }
76         if (ld.getIpv6InterfaceAddress() != null) {
77             add("ipv6-iface", ld.getIpv6InterfaceAddress().getValue());
78         }
79         if (ld.getIpv6NeighborAddress() != null) {
80             add("ipv6-neigh", ld.getIpv6NeighborAddress().getValue());
81         }
82         if (ld.getMultiTopologyId() != null) {
83             add("mt", ld.getMultiTopologyId().getValue());
84         }
85         add("local-id", ld.getLinkLocalIdentifier());
86         add("remote-id", ld.getLinkRemoteIdentifier());
87         return this;
88     }
89
90     private static String isoId(final byte[] bytes) {
91         final StringBuilder sBuilder = new StringBuilder();
92         int id = 0;
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);
97             }
98         }
99         return sBuilder.toString();
100     }
101
102     /**
103      * Creates a String representation of ISO system identifier
104      * in format XX.XX.XX where X is one byte.
105      *
106      * @param systemId IsoSystemIdentifier object
107      * @return String representation of ISO Identifier
108      */
109     public static String isoId(final IsoSystemIdentifier systemId) {
110         return isoId(systemId.getValue());
111     }
112
113     private static String formatRouterIdentifier(final CRouterIdentifier routerIdentifier) {
114         if (routerIdentifier == null) {
115             return null;
116         }
117         if (routerIdentifier instanceof IsisNodeCase) {
118             return isoId(((IsisNodeCase) routerIdentifier).getIsisNode().getIsoSystemId());
119         }
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() });
124         }
125         if (routerIdentifier instanceof OspfNodeCase) {
126             return ((OspfNodeCase) routerIdentifier).getOspfNode().getOspfRouterId().toString();
127         }
128         if (routerIdentifier instanceof OspfPseudonodeCase) {
129             final OspfPseudonode r = ((OspfPseudonodeCase) routerIdentifier).getOspfPseudonode();
130             return r.getOspfRouterId().toString() + ':' + r.getLanInterface().getValue();
131         }
132         LOG.warn("Unhandled router identifier type {}, fallback to toString()",
133                 routerIdentifier.implementedInterface());
134         return routerIdentifier.toString();
135     }
136
137     UriBuilder addPrefix(final String prefix, final NodeIdentifier node) {
138         if (node.getAsNumber() != null) {
139             add(prefix + "as", node.getAsNumber().getValue());
140         }
141         if (node.getDomainId() != null) {
142             add(prefix + "domain", node.getDomainId().getValue());
143         }
144         if (node.getAreaId() != null) {
145             add(prefix + "area", node.getAreaId().getValue());
146         }
147         add(prefix + "router", formatRouterIdentifier(node.getCRouterIdentifier()));
148         return this;
149     }
150
151     @Override
152     public String toString() {
153         final String ret = this.sb.toString();
154         LOG.trace("New URI {}", ret);
155         return ret;
156     }
157
158     /**
159      * Creates string representation of IS-IS Network Entity Title,
160      * based on Area Identifier and System Identifier.
161      *
162      * @param areaId IS-IS Area Identifier
163      * @param systemId string representation of ISO SYSTEM-ID
164      * @return ISO NET ID
165      */
166     public static String toIsoNetId(final IsisAreaIdentifier areaId, final String systemId) {
167         final byte[] value = areaId.getValue();
168         //first byte is AFI
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;
172     }
173 }