d6f33dde27e3070cca6c62abdc015561935b0f41
[bgpcep.git] / bgp / topology-provider / src / main / java / org / opendaylight / bgpcep / bgp / topology / provider / LinkstateTopologyBuilder.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 java.math.BigDecimal;
11 import java.nio.ByteBuffer;
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
16 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
17 import org.opendaylight.protocol.bgp.rib.LocRibReference;
18 import org.opendaylight.protocol.util.ByteArray;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.Ipv4InterfaceIdentifier;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.Ipv6InterfaceIdentifier;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.NodeIdentifier;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.TopologyIdentifier;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.LinkstateRoute;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.Attributes1;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.ObjectType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.object.type.LinkCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.object.type.NodeCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.object.type.PrefixCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.object.type.link._case.LinkDescriptors;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.CRouterIdentifier;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.isis.node._case.IsisNode;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.isis.pseudonode._case.IsisPseudonode;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.node.attributes._case.NodeAttributes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.prefix.attributes._case.PrefixAttributes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.route.Attributes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoPseudonodeId;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoSystemId;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.link.attributes.IsisLinkAttributesBuilder;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.IsisNodeAttributesBuilder;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.isis.node.attributes.IsoBuilder;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidth;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidthBuilder;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidthKey;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.DestinationBuilder;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.SourceBuilder;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1;
61 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1Builder;
62 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1;
63 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1Builder;
64 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1;
65 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1Builder;
66 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.link.attributes.IgpLinkAttributesBuilder;
67 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributesBuilder;
68 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.igp.node.attributes.PrefixBuilder;
69 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.igp.node.attributes.PrefixKey;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.IgpTerminationPointAttributesBuilder;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.igp.termination.point.attributes.TerminationPointType;
72 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.igp.termination.point.attributes.termination.point.type.IpBuilder;
73 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.igp.termination.point.attributes.termination.point.type.UnnumberedBuilder;
74 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.Prefix1;
75 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.Prefix1Builder;
76 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.link.attributes.OspfLinkAttributesBuilder;
77 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.OspfNodeAttributesBuilder;
78 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.prefix.attributes.OspfPrefixAttributesBuilder;
79 import org.opendaylight.yangtools.yang.binding.DataObject;
80 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
81 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
82 import org.slf4j.Logger;
83 import org.slf4j.LoggerFactory;
84
85 import com.google.common.collect.Lists;
86 import com.google.common.primitives.UnsignedInteger;
87
88 public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<LinkstateRoute> {
89         private static final Logger LOG = LoggerFactory.getLogger(LinkstateTopologyBuilder.class);
90
91         public LinkstateTopologyBuilder(final DataProviderService dataProvider, final LocRibReference locRibReference, final TopologyId topologyId) {
92                 super(dataProvider, locRibReference, topologyId, LinkstateRoute.class);
93         }
94
95         private LinkId buildLinkId(final UriBuilder base, final LinkCase link) {
96                 return new LinkId(new UriBuilder(base, "link").add(link).toString());
97         }
98
99         private NodeId buildNodeId(final UriBuilder base, final NodeIdentifier node) {
100                 return new NodeId(new UriBuilder(base, "node").add("", node).toString());
101         }
102
103         private TpId buildTpId(final UriBuilder base, final TopologyIdentifier topologyIdentifier, final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier, final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final byte[] bs) {
104                 return new TpId(new UriBuilder(base, "tp").add("mt", topologyIdentifier).add("ipv4", ipv4InterfaceIdentifier).add("ipv6", ipv6InterfaceIdentifier).add("id", bs).toString());
105         }
106
107         private TpId buildLocalTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
108                 return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4InterfaceAddress(), linkDescriptors.getIpv6InterfaceAddress(), linkDescriptors.getLinkLocalIdentifier());
109         }
110
111         private TerminationPoint buildTp(final TpId id, final TerminationPointType type) {
112                 final TerminationPointBuilder stpb = new TerminationPointBuilder();
113                 stpb.setKey(new TerminationPointKey(id));
114                 stpb.setTpId(id);
115
116                 if (type != null) {
117                         stpb.addAugmentation(TerminationPoint1.class, new TerminationPoint1Builder().setIgpTerminationPointAttributes(new IgpTerminationPointAttributesBuilder().setTerminationPointType(null).build()).build());
118                 }
119
120                 return stpb.build();
121         }
122
123         private final TerminationPointType getTpType(final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier, final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final byte[] bs) {
124                 // Order of preference: Unnumbered first, then IP
125                 if (bs != null) {
126                         final long id = UnsignedInteger.fromIntBits(ByteArray.bytesToInt(bs)).longValue();
127                         LOG.debug("Unnumbered termination point type: {}", id);
128                         return new UnnumberedBuilder().setUnnumberedId(id).build();
129                 }
130
131                 final IpAddress ip;
132                 if (ipv6InterfaceIdentifier != null) {
133                         ip = new IpAddress(ipv6InterfaceIdentifier);
134                 } else if (ipv4InterfaceIdentifier != null) {
135                         ip = new IpAddress(ipv4InterfaceIdentifier);
136                 } else {
137                         ip = null;
138                 }
139
140                 if (ip != null) {
141                         LOG.debug("IP termination point type: {}", ip);
142                         return new IpBuilder().setIpAddress(Lists.newArrayList(ip)).build();
143                 }
144
145                 return null;
146         }
147
148         private TerminationPoint buildLocalTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
149                 final TpId id = buildLocalTpId(base, linkDescriptors);
150                 final TerminationPointType t = getTpType(linkDescriptors.getIpv4InterfaceAddress(), linkDescriptors.getIpv6InterfaceAddress(), linkDescriptors.getLinkLocalIdentifier());
151
152                 return buildTp(id, t);
153         }
154
155         private TpId buildRemoteTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
156                 return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4NeighborAddress(), linkDescriptors.getIpv6NeighborAddress(), linkDescriptors.getLinkRemoteIdentifier());
157         }
158
159         private TerminationPoint buildRemoteTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
160                 final TpId id = buildRemoteTpId(base, linkDescriptors);
161                 final TerminationPointType t = getTpType(linkDescriptors.getIpv4NeighborAddress(), linkDescriptors.getIpv6NeighborAddress(), linkDescriptors.getLinkRemoteIdentifier());
162
163                 return buildTp(id, t);
164         }
165
166         private InstanceIdentifier<?> buildLinkIdentifier(final UriBuilder base, final LinkCase l) {
167                 return InstanceIdentifier.builder(getInstanceIdentifier()).child(
168                                 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link.class,
169                                 new LinkKey(buildLinkId(base, l))).toInstance();
170         }
171
172         private static Float bandwidthToFloat(final Bandwidth bandwidth) {
173                 return ByteBuffer.wrap(bandwidth.getValue()).getFloat();
174         }
175
176         private static BigDecimal bandwidthToBigDecimal(final Bandwidth bandwidth) {
177                 return BigDecimal.valueOf(bandwidthToFloat(bandwidth));
178         }
179
180         private static List<UnreservedBandwidth> unreservedBandwidthList(
181                         final List<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.UnreservedBandwidth> input) {
182                 final List<UnreservedBandwidth> ret = new ArrayList<>(input.size());
183
184                 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.UnreservedBandwidth i : input) {
185                         ret.add(new UnreservedBandwidthBuilder().setBandwidth(bandwidthToBigDecimal(i.getBandwidth())).setKey(
186                                         new UnreservedBandwidthKey(i.getPriority())).build());
187                 }
188
189                 return ret;
190         }
191
192         private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1 isisLinkAttributes(
193                         final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
194                 final org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.link.attributes.isis.link.attributes.TedBuilder tb = new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.link.attributes.isis.link.attributes.TedBuilder();
195
196                 tb.setColor(la.getAdminGroup().getValue());
197                 tb.setTeDefaultMetric(la.getTeMetric().getValue());
198                 tb.setUnreservedBandwidth(unreservedBandwidthList(la.getUnreservedBandwidth()));
199                 tb.setMaxLinkBandwidth(bandwidthToBigDecimal(la.getMaxLinkBandwidth()));
200                 tb.setMaxResvLinkBandwidth(bandwidthToBigDecimal(la.getMaxReservableBandwidth()));
201
202                 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1Builder().setIsisLinkAttributes(
203                                 new IsisLinkAttributesBuilder().setMultiTopologyId(topologyIdentifier.getValue().shortValue()).setTed(tb.build()).build()).build();
204         }
205
206         private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1 ospfLinkAttributes(
207                         final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
208                 final org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.link.attributes.ospf.link.attributes.TedBuilder tb = new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.link.attributes.ospf.link.attributes.TedBuilder();
209
210                 tb.setColor(la.getAdminGroup().getValue());
211                 tb.setTeDefaultMetric(la.getTeMetric().getValue());
212                 tb.setUnreservedBandwidth(unreservedBandwidthList(la.getUnreservedBandwidth()));
213                 tb.setMaxLinkBandwidth(bandwidthToBigDecimal(la.getMaxLinkBandwidth()));
214                 tb.setMaxResvLinkBandwidth(bandwidthToBigDecimal(la.getMaxReservableBandwidth()));
215
216                 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1Builder().setOspfLinkAttributes(
217                                 new OspfLinkAttributesBuilder().setMultiTopologyId(topologyIdentifier.getValue().shortValue()).setTed(tb.build()).build()).build();
218         }
219
220         private void createLink(final DataModification<InstanceIdentifier<?>, DataObject> trans, final UriBuilder base, final LinkstateRoute value,
221                         final LinkCase l, final Attributes attributes) {
222                 final LinkAttributes la = (LinkAttributes) attributes.getAugmentation(Attributes1.class).getAttributeType();
223
224                 final IgpLinkAttributesBuilder ilab = new IgpLinkAttributesBuilder();
225                 ilab.setMetric(la.getMetric().getValue());
226                 ilab.setName(la.getLinkName());
227
228                 switch (value.getProtocolId()) {
229                 case Direct:
230                 case Static:
231                 case Unknown:
232                         break;
233                 case IsisLevel1:
234                 case IsisLevel2:
235                         ilab.addAugmentation(
236                                         org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1.class,
237                                         isisLinkAttributes(l.getLinkDescriptors().getMultiTopologyId(), la));
238                         break;
239                 case Ospf:
240                         ilab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1.class,
241                                         ospfLinkAttributes(l.getLinkDescriptors().getMultiTopologyId(), la));
242                         break;
243                 }
244
245                 final LinkBuilder lb = new LinkBuilder();
246                 lb.setLinkId(buildLinkId(base, l));
247                 lb.addAugmentation(Link1.class, new Link1Builder().setIgpLinkAttributes(ilab.build()).build());
248
249                 final NodeId srcNode = buildNodeId(base, l.getLocalNodeDescriptors());
250                 final NodeId dstNode = buildNodeId(base, l.getRemoteNodeDescriptors());
251                 final TerminationPoint srcTp = buildLocalTp(base, l.getLinkDescriptors());
252                 final TerminationPoint dstTp = buildRemoteTp(base, l.getLinkDescriptors());
253
254                 lb.setSource(new SourceBuilder().setSourceNode(srcNode).setSourceTp(srcTp.getTpId()).build());
255                 lb.setDestination(new DestinationBuilder().setDestNode(dstNode).setDestTp(dstTp.getTpId()).build());
256
257                 trans.putOperationalData(buildTpIdentifier(srcNode, srcTp.getKey()), srcTp);
258                 trans.putOperationalData(buildTpIdentifier(dstNode, dstTp.getKey()), dstTp);
259                 trans.putOperationalData(buildLinkIdentifier(base, l), lb.build());
260                 LOG.debug("Created link {}", l);
261         }
262
263         private void removeLink(final DataModification<InstanceIdentifier<?>, DataObject> trans, final UriBuilder base, final LinkCase l) {
264                 trans.removeOperationalData(buildLinkIdentifier(base, l));
265         }
266
267         private InstanceIdentifierBuilder<org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node> nodeIdentifierBuilder(
268                         final UriBuilder base, final NodeIdentifier node) {
269                 return InstanceIdentifier.builder(getInstanceIdentifier()).child(
270                                 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node.class,
271                                 new NodeKey(buildNodeId(base, node)));
272         }
273
274         private InstanceIdentifier<TerminationPoint> buildTpIdentifier(final NodeId node, final TerminationPointKey key) {
275                 return InstanceIdentifier.builder(getInstanceIdentifier()).child(
276                                 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node.class,
277                                 new NodeKey(node)).child(TerminationPoint.class, key).build();
278         }
279
280         private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1 isisNodeAttributes(
281                         final NodeIdentifier node, final NodeAttributes na) {
282                 final org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.isis.node.attributes.TedBuilder tb = new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.isis.node.attributes.TedBuilder();
283                 if (na.getIpv4RouterId() != null) {
284                         tb.setTeRouterIdIpv4(na.getIpv4RouterId());
285                 }
286                 if (na.getIpv6RouterId() != null) {
287                         tb.setTeRouterIdIpv6(na.getIpv6RouterId());
288                 }
289
290                 final IsisNodeAttributesBuilder ab = new IsisNodeAttributesBuilder();
291                 final CRouterIdentifier ri = node.getCRouterIdentifier();
292
293                 if (ri instanceof IsisPseudonode) {
294                         final IsisPseudonode pn = (IsisPseudonode) ri;
295                         ab.setIso(new IsoBuilder().setIsoPseudonodeId(new IsoPseudonodeId(pn.toString())).build());
296                 } else if (ri instanceof IsisNode) {
297                         final IsisNode in = (IsisNode) ri;
298                         ab.setIso(new IsoBuilder().setIsoSystemId(new IsoSystemId(in.getIsoSystemId().toString())).build());
299                 }
300
301                 ab.setTed(tb.build());
302
303                 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1Builder().setIsisNodeAttributes(
304                                 ab.build()).build();
305         }
306
307         private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1 ospfNodeAttributes(
308                         final NodeAttributes na) {
309                 final org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.TedBuilder tb = new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.TedBuilder();
310
311                 final OspfNodeAttributesBuilder ab = new OspfNodeAttributesBuilder();
312
313                 ab.setTed(tb.build());
314
315                 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1Builder().setOspfNodeAttributes(
316                                 ab.build()).build();
317         }
318
319         private void createNode(final DataModification<InstanceIdentifier<?>, DataObject> trans, final UriBuilder base, final LinkstateRoute value,
320                         final NodeCase n, final Attributes attributes) {
321                 final NodeAttributes na = (NodeAttributes) attributes.getAugmentation(Attributes1.class).getAttributeType();
322
323                 final List<IpAddress> ids = new ArrayList<>();
324                 if (na.getIpv4RouterId() != null) {
325                         ids.add(new IpAddress(na.getIpv4RouterId()));
326                 }
327                 if (na.getIpv6RouterId() != null) {
328                         ids.add(new IpAddress(na.getIpv6RouterId()));
329                 }
330
331                 final IgpNodeAttributesBuilder inab = new IgpNodeAttributesBuilder();
332                 if (!ids.isEmpty()) {
333                         inab.setRouterId(ids);
334                 }
335
336                 switch (value.getProtocolId()) {
337                 case Direct:
338                 case Static:
339                 case Unknown:
340                         break;
341                 case IsisLevel1:
342                 case IsisLevel2:
343                         inab.addAugmentation(
344                                         org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1.class,
345                                         isisNodeAttributes(n.getNodeDescriptors(), na));
346                         break;
347                 case Ospf:
348                         inab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1.class,
349                                         ospfNodeAttributes(na));
350                         break;
351                 }
352
353                 final NodeBuilder nb = new NodeBuilder();
354                 nb.setNodeId(buildNodeId(base, n.getNodeDescriptors()));
355                 nb.addAugmentation(Node1.class, new Node1Builder().setIgpNodeAttributes(inab.build()).build());
356
357                 trans.putOperationalData(nodeIdentifierBuilder(base, n.getNodeDescriptors()).toInstance(), nb.build());
358                 LOG.debug("Created node {}", n);
359         }
360
361         private void removeNode(final DataModification<InstanceIdentifier<?>, DataObject> trans, final UriBuilder base, final NodeCase n) {
362                 trans.removeOperationalData(nodeIdentifierBuilder(base, n.getNodeDescriptors()).toInstance());
363         }
364
365         private InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.igp.node.attributes.Prefix> prefixIdentifier(
366                         final UriBuilder base, final PrefixCase p) {
367                 return nodeIdentifierBuilder(base, p.getAdvertisingNodeDescriptors()).augmentation(Node1.class).child(
368                                 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributes.class).child(
369                                                 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.igp.node.attributes.Prefix.class,
370                                                 new PrefixKey(p.getIpReachabilityInformation())).toInstance();
371         }
372
373         private void createPrefix(final DataModification<InstanceIdentifier<?>, DataObject> trans, final UriBuilder base,
374                         final LinkstateRoute value, final PrefixCase p, final Attributes attributes) {
375                 final PrefixAttributes pa = (PrefixAttributes) attributes.getAugmentation(Attributes1.class).getAttributeType();
376
377                 final PrefixBuilder pb = new PrefixBuilder();
378                 pb.setPrefix(p.getIpReachabilityInformation());
379                 pb.setMetric(pa.getPrefixMetric().getValue());
380
381                 switch (value.getProtocolId()) {
382                 case Direct:
383                 case IsisLevel1:
384                 case IsisLevel2:
385                 case Static:
386                 case Unknown:
387                         break;
388                 case Ospf:
389                         pb.addAugmentation(
390                                         Prefix1.class,
391                                         new Prefix1Builder().setOspfPrefixAttributes(
392                                                         new OspfPrefixAttributesBuilder().setForwardingAddress(pa.getOspfForwardingAddress()).build()).build());
393                         break;
394                 }
395
396                 trans.putOperationalData(prefixIdentifier(base, p), pb.build());
397                 LOG.debug("Created prefix {}", p);
398         }
399
400         private void removePrefix(final DataModification<InstanceIdentifier<?>, DataObject> trans, final UriBuilder base, final PrefixCase p) {
401                 trans.removeOperationalData(prefixIdentifier(base, p));
402         }
403
404         @Override
405         protected void createObject(final DataModification<InstanceIdentifier<?>, DataObject> trans,
406                         final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
407                 final UriBuilder base = new UriBuilder(value);
408
409                 final ObjectType t = value.getObjectType();
410                 if (t instanceof LinkCase) {
411                         createLink(trans, base, value, (LinkCase) t, value.getAttributes());
412                 } else if (t instanceof NodeCase) {
413                         createNode(trans, base, value, (NodeCase) t, value.getAttributes());
414                 } else if (t instanceof PrefixCase) {
415                         createPrefix(trans, base, value, (PrefixCase) t, value.getAttributes());
416                 } else {
417                         throw new IllegalStateException("Unhandled object class " + t.getImplementedInterface());
418                 }
419         }
420
421         @Override
422         protected void removeObject(final DataModification<InstanceIdentifier<?>, DataObject> trans,
423                         final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
424                 final UriBuilder base = new UriBuilder(value);
425
426                 final ObjectType t = value.getObjectType();
427                 if (t instanceof LinkCase) {
428                         removeLink(trans, base, (LinkCase) t);
429                 } else if (t instanceof NodeCase) {
430                         removeNode(trans, base, (NodeCase) t);
431                 } else if (t instanceof PrefixCase) {
432                         removePrefix(trans, base, (PrefixCase) t);
433                 } else {
434                         throw new IllegalStateException("Unhandled object class " + t.getImplementedInterface());
435                 }
436         }
437 }