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