Bug-5588: Missing topology-types in BGP-provided topologies
[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 com.google.common.base.Function;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Collections2;
13 import com.google.common.collect.Lists;
14 import com.google.common.io.BaseEncoding;
15 import java.math.BigDecimal;
16 import java.nio.ByteBuffer;
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Set;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
25 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.protocol.bgp.rib.RibReference;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.DomainName;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Ipv4InterfaceIdentifier;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Ipv6InterfaceIdentifier;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.IsisAreaIdentifier;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeFlagBits;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeIdentifier;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.TopologyIdentifier;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.ObjectType;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.LinkCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.NodeCase;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.PrefixCase;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.link._case.LinkDescriptors;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.LinkStateAttribute;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.NodeAttributesCase;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.PrefixAttributesCase;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributes;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.node.attributes._case.NodeAttributes;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.prefix.attributes._case.PrefixAttributes;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.LinkstateRoutes;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.LinkstateRoute;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.linkstate.route.Attributes1;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.CRouterIdentifier;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.IsisNodeCase;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.IsisPseudonodeCase;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.OspfNodeCase;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.OspfPseudonodeCase;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.isis.node._case.IsisNode;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.isis.pseudonode._case.IsisPseudonode;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.ospf.pseudonode._case.OspfPseudonode;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.topology.types.rev160524.TopologyTypes1;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.topology.types.rev160524.TopologyTypes1Builder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.topology.types.rev160524.bgp.linkstate.topology.type.BgpLinkstateTopologyBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgId;
67 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoNetId;
68 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoPseudonodeId;
69 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoSystemId;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.link.attributes.IsisLinkAttributesBuilder;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.IsisNodeAttributesBuilder;
72 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.isis.node.attributes.IsoBuilder;
73 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.srlg.attributes.SrlgValues;
74 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.srlg.attributes.SrlgValuesBuilder;
75 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.SrlgBuilder;
76 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidth;
77 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidthBuilder;
78 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidthKey;
79 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
80 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
81 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
82 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
83 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.DestinationBuilder;
84 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.SourceBuilder;
85 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
86 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder;
87 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
88 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
89 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
90 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
91 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypes;
92 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypesBuilder;
93 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
94 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
95 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
96 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1;
97 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1Builder;
98 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1;
99 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1Builder;
100 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1;
101 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1Builder;
102 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.link.attributes.IgpLinkAttributesBuilder;
103 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributesBuilder;
104 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.igp.node.attributes.Prefix;
105 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;
106 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;
107 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.IgpTerminationPointAttributesBuilder;
108 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;
109 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;
110 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;
111 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.Prefix1;
112 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.Prefix1Builder;
113 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.link.attributes.OspfLinkAttributesBuilder;
114 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.OspfNodeAttributesBuilder;
115 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.AbrBuilder;
116 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.InternalBuilder;
117 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.PseudonodeBuilder;
118 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.prefix.attributes.OspfPrefixAttributesBuilder;
119 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
120 import org.slf4j.Logger;
121 import org.slf4j.LoggerFactory;
122
123 public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<LinkstateRoute> {
124     private static final TopologyTypes LINKSTATE_TOPOLOGY_TYPE = new TopologyTypesBuilder()
125             .addAugmentation(TopologyTypes1.class,
126                     new TopologyTypes1Builder()
127                             .setBgpLinkstateTopology(new BgpLinkstateTopologyBuilder().build()).build())
128             .build();
129
130     private static final String UNHANDLED_OBJECT_CLASS = "Unhandled object class {}";
131
132     private static final class TpHolder {
133         private final Set<LinkId> local = new HashSet<>();
134         private final Set<LinkId> remote = new HashSet<>();
135
136         private final TerminationPoint tp;
137
138         private TpHolder(final TerminationPoint tp) {
139             this.tp = Preconditions.checkNotNull(tp);
140         }
141
142         private synchronized void addLink(final LinkId id, final boolean isRemote) {
143             if (isRemote) {
144                 this.remote.add(id);
145             } else {
146                 this.local.add(id);
147             }
148         }
149
150         private synchronized boolean removeLink(final LinkId id, final boolean isRemote) {
151             final boolean removed;
152             if (isRemote) {
153                 removed = this.remote.remove(id);
154             } else {
155                 removed = this.local.remove(id);
156             }
157             if (!removed) {
158                 LOG.warn("Removed non-reference link {} from TP {} isRemote {}", this.tp.getTpId(), id, isRemote);
159             }
160
161             return this.local.isEmpty() && this.remote.isEmpty();
162         }
163
164         private TerminationPoint getTp() {
165             return this.tp;
166         }
167     }
168
169     private final class NodeHolder {
170         private final Map<PrefixKey, Prefix> prefixes = new HashMap<>();
171         private final Map<TpId, TpHolder> tps = new HashMap<>();
172         private boolean advertized = false;
173         private IgpNodeAttributesBuilder inab;
174         private NodeBuilder nb;
175
176         private NodeHolder(final NodeId id) {
177             this.inab = new IgpNodeAttributesBuilder();
178             this.nb = new NodeBuilder().setKey(new NodeKey(id)).setNodeId(id);
179         }
180
181         /**
182          * Synchronized in-core state of a node into the backing store using the transaction
183          *
184          * @param trans data modification transaction which to use
185          * @return True if the node has been purged, false otherwise.
186          */
187         private boolean syncState(final WriteTransaction trans) {
188             final InstanceIdentifier<Node> nid = getInstanceIdentifier().child(
189                 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node.class,
190                 this.nb.getKey());
191
192             /*
193              * Transaction's putOperationalData() does a merge. Force it onto a replace
194              * by removing the data. If we decide to remove the node -- we just skip the put.
195              */
196             trans.delete(LogicalDatastoreType.OPERATIONAL, nid);
197
198             if (!this.advertized) {
199                 if (this.tps.isEmpty() && this.prefixes.isEmpty()) {
200                     LOG.debug("Removing unadvertized unused node {}", this.nb.getNodeId());
201                     return true;
202                 }
203
204                 LOG.debug("Node {} is still implied by {} TPs and {} prefixes", this.nb.getNodeId(), this.tps.size(), this.prefixes.size());
205             }
206
207             // Re-generate termination points
208             this.nb.setTerminationPoint(Lists.newArrayList(Collections2.transform(this.tps.values(), new Function<TpHolder, TerminationPoint>() {
209                 @Override
210                 public TerminationPoint apply(final TpHolder input) {
211                     return input.getTp();
212                 }
213             })));
214
215             // Re-generate prefixes
216             this.inab.setPrefix(Lists.newArrayList(this.prefixes.values()));
217
218             // Write the node out
219             final Node n = this.nb.addAugmentation(Node1.class, new Node1Builder().setIgpNodeAttributes(this.inab.build()).build()).build();
220             trans.put(LogicalDatastoreType.OPERATIONAL, nid, n);
221             LOG.debug("Created node {} at {}", n, nid);
222             return false;
223         }
224
225         private synchronized void removeTp(final TpId tp, final LinkId link, final boolean isRemote) {
226             final TpHolder h = this.tps.get(tp);
227             if (h != null) {
228                 if (h.removeLink(link, isRemote)) {
229                     this.tps.remove(tp);
230                     LOG.debug("Removed TP {}", tp);
231                 }
232             } else {
233                 LOG.warn("Removed non-present TP {} by link {}", tp, link);
234             }
235         }
236
237         private void addTp(final TerminationPoint tp, final LinkId link, final boolean isRemote) {
238             TpHolder h = this.tps.get(tp.getTpId());
239             if (h == null) {
240                 h = new TpHolder(tp);
241                 this.tps.put(tp.getTpId(), h);
242             }
243
244             h.addLink(link, isRemote);
245         }
246
247         private void addPrefix(final Prefix pfx) {
248             this.prefixes.put(pfx.getKey(), pfx);
249         }
250
251         private void removePrefix(final PrefixCase p) {
252             this.prefixes.remove(new PrefixKey(p.getPrefixDescriptors().getIpReachabilityInformation()));
253         }
254
255         private void unadvertized() {
256             this.inab = new IgpNodeAttributesBuilder();
257             this.nb = new NodeBuilder().setKey(this.nb.getKey()).setNodeId(this.nb.getNodeId());
258             this.advertized = false;
259             LOG.debug("Node {} is unadvertized", this.nb.getNodeId());
260         }
261
262         private void advertized(final NodeBuilder nb, final IgpNodeAttributesBuilder inab) {
263             this.nb = Preconditions.checkNotNull(nb);
264             this.inab = Preconditions.checkNotNull(inab);
265             this.advertized = true;
266             LOG.debug("Node {} is advertized", nb.getNodeId());
267         }
268
269         private Object getNodeId() {
270             return this.nb.getNodeId();
271         }
272     }
273
274     private static final Logger LOG = LoggerFactory.getLogger(LinkstateTopologyBuilder.class);
275     private final Map<NodeId, NodeHolder> nodes = new HashMap<>();
276
277     public LinkstateTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference, final TopologyId topologyId) {
278         super(dataProvider, locRibReference, topologyId, LINKSTATE_TOPOLOGY_TYPE);
279     }
280
281     private static LinkId buildLinkId(final UriBuilder base, final LinkCase link) {
282         return new LinkId(new UriBuilder(base, "link").add(link).toString());
283     }
284
285     private static NodeId buildNodeId(final UriBuilder base,
286             final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeIdentifier node) {
287         return new NodeId(new UriBuilder(base, "node").add("", node).toString());
288     }
289
290     private static TpId buildTpId(final UriBuilder base, final TopologyIdentifier topologyIdentifier,
291         final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier, final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
292         final UriBuilder b = new UriBuilder(base, "tp");
293         if (topologyIdentifier != null) {
294             b.add("mt", topologyIdentifier.getValue());
295         }
296         if (ipv4InterfaceIdentifier != null) {
297             b.add("ipv4", ipv4InterfaceIdentifier.getValue());
298         }
299         if (ipv6InterfaceIdentifier != null) {
300             b.add("ipv6", ipv6InterfaceIdentifier.getValue());
301         }
302
303         return new TpId(b.add("id", id).toString());
304     }
305
306     private static TpId buildLocalTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
307         return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4InterfaceAddress(),
308             linkDescriptors.getIpv6InterfaceAddress(), linkDescriptors.getLinkLocalIdentifier());
309     }
310
311     private static TerminationPoint buildTp(final TpId id, final TerminationPointType type) {
312         final TerminationPointBuilder stpb = new TerminationPointBuilder();
313         stpb.setKey(new TerminationPointKey(id));
314         stpb.setTpId(id);
315
316         if (type != null) {
317             stpb.addAugmentation(TerminationPoint1.class, new TerminationPoint1Builder().setIgpTerminationPointAttributes(
318                 new IgpTerminationPointAttributesBuilder().setTerminationPointType(null).build()).build());
319         }
320
321         return stpb.build();
322     }
323
324     private static TerminationPointType getTpType(final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier,
325         final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
326         // Order of preference: Unnumbered first, then IP
327         if (id != null) {
328             LOG.debug("Unnumbered termination point type: {}", id);
329             return new UnnumberedBuilder().setUnnumberedId(id).build();
330         }
331
332         final IpAddress ip;
333         if (ipv6InterfaceIdentifier != null) {
334             ip = new IpAddress(ipv6InterfaceIdentifier);
335         } else if (ipv4InterfaceIdentifier != null) {
336             ip = new IpAddress(ipv4InterfaceIdentifier);
337         } else {
338             ip = null;
339         }
340
341         if (ip != null) {
342             LOG.debug("IP termination point type: {}", ip);
343             return new IpBuilder().setIpAddress(Lists.newArrayList(ip)).build();
344         }
345
346         return null;
347     }
348
349     private static TerminationPoint buildLocalTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
350         final TpId id = buildLocalTpId(base, linkDescriptors);
351         final TerminationPointType t = getTpType(linkDescriptors.getIpv4InterfaceAddress(), linkDescriptors.getIpv6InterfaceAddress(),
352             linkDescriptors.getLinkLocalIdentifier());
353
354         return buildTp(id, t);
355     }
356
357     private static TpId buildRemoteTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
358         return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4NeighborAddress(),
359             linkDescriptors.getIpv6NeighborAddress(), linkDescriptors.getLinkRemoteIdentifier());
360     }
361
362     private static TerminationPoint buildRemoteTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
363         final TpId id = buildRemoteTpId(base, linkDescriptors);
364         final TerminationPointType t = getTpType(linkDescriptors.getIpv4NeighborAddress(), linkDescriptors.getIpv6NeighborAddress(),
365             linkDescriptors.getLinkRemoteIdentifier());
366
367         return buildTp(id, t);
368     }
369
370     private InstanceIdentifier<Link> buildLinkIdentifier(final LinkId id) {
371         return getInstanceIdentifier().child(
372             org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link.class,
373             new LinkKey(id));
374     }
375
376     private static Float bandwidthToFloat(final Bandwidth bandwidth) {
377         return ByteBuffer.wrap(bandwidth.getValue()).getFloat();
378     }
379
380     private static BigDecimal bandwidthToBigDecimal(final Bandwidth bandwidth) {
381         return BigDecimal.valueOf(bandwidthToFloat(bandwidth));
382     }
383
384     private static List<UnreservedBandwidth> unreservedBandwidthList(
385         final List<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.UnreservedBandwidth> input) {
386         final List<UnreservedBandwidth> ret = new ArrayList<>(input.size());
387
388         for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.UnreservedBandwidth i : input) {
389             ret.add(new UnreservedBandwidthBuilder().setBandwidth(bandwidthToBigDecimal(i.getBandwidth())).setKey(
390                 new UnreservedBandwidthKey(i.getPriority())).build());
391         }
392
393         return ret;
394     }
395
396     private static org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1 isisLinkAttributes(
397         final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
398         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();
399
400         if (la != null) {
401             if (la.getAdminGroup() != null) {
402                 tb.setColor(la.getAdminGroup().getValue());
403             }
404             if (la.getTeMetric() != null) {
405                 tb.setTeDefaultMetric(la.getTeMetric().getValue());
406             }
407             if (la.getUnreservedBandwidth() != null) {
408                 tb.setUnreservedBandwidth(unreservedBandwidthList(la.getUnreservedBandwidth()));
409             }
410             if (la.getMaxLinkBandwidth() != null) {
411                 tb.setMaxLinkBandwidth(bandwidthToBigDecimal(la.getMaxLinkBandwidth()));
412             }
413             if (la.getMaxReservableBandwidth() != null) {
414                 tb.setMaxResvLinkBandwidth(bandwidthToBigDecimal(la.getMaxReservableBandwidth()));
415             }
416             if (la.getSharedRiskLinkGroups() != null) {
417                 final List<SrlgValues> srlgs = new ArrayList<>();
418                 for (final SrlgId id : la.getSharedRiskLinkGroups()) {
419                     srlgs.add(new SrlgValuesBuilder().setSrlgValue(id.getValue()).build());
420                 }
421                 tb.setSrlg(new SrlgBuilder().setSrlgValues(srlgs).build());
422             }
423         }
424
425         final IsisLinkAttributesBuilder ilab = new IsisLinkAttributesBuilder();
426         ilab.setTed(tb.build());
427         if (topologyIdentifier != null) {
428             ilab.setMultiTopologyId(topologyIdentifier.getValue().shortValue());
429         }
430
431         return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1Builder().setIsisLinkAttributes(
432             ilab.build()).build();
433     }
434
435     private static org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1 ospfLinkAttributes(
436         final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
437         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();
438
439         if (la != null) {
440             if (la.getAdminGroup() != null) {
441                 tb.setColor(la.getAdminGroup().getValue());
442             }
443             if (la.getTeMetric() != null) {
444                 tb.setTeDefaultMetric(la.getTeMetric().getValue());
445             }
446             if (la.getUnreservedBandwidth() != null) {
447                 tb.setUnreservedBandwidth(unreservedBandwidthList(la.getUnreservedBandwidth()));
448             }
449             if (la.getMaxLinkBandwidth() != null) {
450                 tb.setMaxLinkBandwidth(bandwidthToBigDecimal(la.getMaxLinkBandwidth()));
451             }
452             if (la.getMaxReservableBandwidth() != null) {
453                 tb.setMaxResvLinkBandwidth(bandwidthToBigDecimal(la.getMaxReservableBandwidth()));
454             }
455             if (la.getSharedRiskLinkGroups() != null) {
456                 final List<SrlgValues> srlgs = new ArrayList<>();
457                 for (final SrlgId id : la.getSharedRiskLinkGroups()) {
458                     srlgs.add(new SrlgValuesBuilder().setSrlgValue(id.getValue()).build());
459                 }
460                 tb.setSrlg(new SrlgBuilder().setSrlgValues(srlgs).build());
461             }
462         }
463
464         final OspfLinkAttributesBuilder ilab = new OspfLinkAttributesBuilder();
465         ilab.setTed(tb.build());
466         if (topologyIdentifier != null) {
467             ilab.setMultiTopologyId(topologyIdentifier.getValue().shortValue());
468         }
469
470         return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1Builder().setOspfLinkAttributes(
471             ilab.build()).build();
472     }
473
474     private NodeHolder getNode(final NodeId id) {
475         if (this.nodes.containsKey(id)) {
476             LOG.debug("Node {} is already present", id);
477             return this.nodes.get(id);
478         }
479
480         final NodeHolder ret = new NodeHolder(id);
481         this.nodes.put(id, ret);
482         return ret;
483     }
484
485     private void putNode(final WriteTransaction trans, final NodeHolder holder) {
486         if (holder.syncState(trans)) {
487             this.nodes.remove(holder.getNodeId());
488         }
489     }
490
491     private static void augmentProtocolId(final LinkstateRoute value, final IgpLinkAttributesBuilder ilab,
492             final LinkAttributes la, final LinkDescriptors ld) {
493         switch (value.getProtocolId()) {
494         case Direct:
495         case Static:
496         case Unknown:
497             break;
498         case IsisLevel1:
499         case IsisLevel2:
500             ilab.addAugmentation(
501                 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1.class,
502                 isisLinkAttributes(ld.getMultiTopologyId(), la));
503             break;
504         case Ospf:
505             ilab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1.class,
506                 ospfLinkAttributes(ld.getMultiTopologyId(), la));
507             break;
508         default:
509             break;
510         }
511     }
512
513     private void createLink(final WriteTransaction trans, final UriBuilder base,
514         final LinkstateRoute value, final LinkCase l, final Attributes attributes) {
515         // defensive lookup
516         final LinkAttributes la;
517         final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
518         if (attr != null) {
519             final LinkStateAttribute attrType = attr.getLinkStateAttribute();
520             if (attrType != null) {
521                 la = ((LinkAttributesCase)attrType).getLinkAttributes();
522             } else {
523                 LOG.debug("Missing attribute type in link {} route {}, skipping it", l, value);
524                 la = null;
525             }
526         } else {
527             LOG.debug("Missing attributes in link {} route {}, skipping it", l, value);
528             la = null;
529         }
530
531         final IgpLinkAttributesBuilder ilab = new IgpLinkAttributesBuilder();
532         if (la != null) {
533             if (la.getMetric() != null) {
534                 ilab.setMetric(la.getMetric().getValue());
535             }
536             ilab.setName(la.getLinkName());
537         }
538         augmentProtocolId(value, ilab, la, l.getLinkDescriptors());
539
540         final LinkBuilder lb = new LinkBuilder();
541         lb.setLinkId(buildLinkId(base, l));
542         lb.addAugmentation(Link1.class, new Link1Builder().setIgpLinkAttributes(ilab.build()).build());
543
544         final NodeId srcNode = buildNodeId(base, l.getLocalNodeDescriptors());
545         LOG.debug("Link {} implies source node {}", l, srcNode);
546
547         final NodeId dstNode = buildNodeId(base, l.getRemoteNodeDescriptors());
548         LOG.debug("Link {} implies destination node {}", l, dstNode);
549
550         final TerminationPoint srcTp = buildLocalTp(base, l.getLinkDescriptors());
551         LOG.debug("Link {} implies source TP {}", l, srcTp);
552
553         final TerminationPoint dstTp = buildRemoteTp(base, l.getLinkDescriptors());
554         LOG.debug("Link {} implies destination TP {}", l, dstTp);
555
556         lb.setSource(new SourceBuilder().setSourceNode(srcNode).setSourceTp(srcTp.getTpId()).build());
557         lb.setDestination(new DestinationBuilder().setDestNode(dstNode).setDestTp(dstTp.getTpId()).build());
558
559         final NodeHolder snh = getNode(srcNode);
560         snh.addTp(srcTp, lb.getLinkId(), false);
561         LOG.debug("Created TP {} as link source", srcTp);
562         putNode(trans, snh);
563
564         final NodeHolder dnh = getNode(dstNode);
565         dnh.addTp(dstTp, lb.getLinkId(), true);
566         LOG.debug("Created TP {} as link destination", dstTp);
567         putNode(trans, dnh);
568
569         final InstanceIdentifier<Link> lid = buildLinkIdentifier(lb.getLinkId());
570         final Link link = lb.build();
571
572         trans.put(LogicalDatastoreType.OPERATIONAL, lid, link);
573         LOG.debug("Created link {} at {} for {}", link, lid, l);
574     }
575
576     private void removeTp(final WriteTransaction trans, final NodeId node, final TpId tp,
577         final LinkId link, final boolean isRemote) {
578         final NodeHolder nh = this.nodes.get(node);
579         if (nh != null) {
580             nh.removeTp(tp, link, isRemote);
581             putNode(trans, nh);
582         } else {
583             LOG.warn("Removed non-existent node {}", node);
584         }
585     }
586
587     private void removeLink(final WriteTransaction trans, final UriBuilder base, final LinkCase l) {
588         final LinkId id = buildLinkId(base, l);
589         final InstanceIdentifier<?> lid = buildLinkIdentifier(id);
590         trans.delete(LogicalDatastoreType.OPERATIONAL, lid);
591         LOG.debug("Removed link {}", lid);
592
593         removeTp(trans, buildNodeId(base, l.getLocalNodeDescriptors()), buildLocalTpId(base, l.getLinkDescriptors()), id, false);
594         removeTp(trans, buildNodeId(base, l.getRemoteNodeDescriptors()), buildRemoteTpId(base, l.getLinkDescriptors()), id, true);
595     }
596
597     private static List<Short> nodeMultiTopology(final List<TopologyIdentifier> list) {
598         final List<Short> ret = new ArrayList<>(list.size());
599         for (final TopologyIdentifier id : list) {
600             ret.add(id.getValue().shortValue());
601         }
602         return ret;
603     }
604
605     private static org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1 isisNodeAttributes(
606         final NodeIdentifier node, final NodeAttributes na) {
607         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();
608         final IsisNodeAttributesBuilder ab = new IsisNodeAttributesBuilder();
609
610         if (na != null) {
611             if (na.getIpv4RouterId() != null) {
612                 tb.setTeRouterIdIpv4(na.getIpv4RouterId());
613             }
614             if (na.getIpv6RouterId() != null) {
615                 tb.setTeRouterIdIpv6(na.getIpv6RouterId());
616             }
617             if (na.getTopologyIdentifier() != null) {
618                 ab.setMultiTopologyId(nodeMultiTopology(na.getTopologyIdentifier()));
619             }
620         }
621
622         final CRouterIdentifier ri = node.getCRouterIdentifier();
623         if (ri instanceof IsisPseudonodeCase) {
624             final IsisPseudonode pn = ((IsisPseudonodeCase) ri).getIsisPseudonode();
625             final IsoBuilder b = new IsoBuilder();
626             final String systemId = UriBuilder.isoId(pn.getIsIsRouterIdentifier().getIsoSystemId());
627             b.setIsoSystemId(new IsoSystemId(systemId));
628             b.setIsoPseudonodeId(new IsoPseudonodeId(BaseEncoding.base16().encode(new byte[] {pn.getPsn().byteValue()})));
629             ab.setIso(b.build());
630             if (na != null) {
631                 ab.setNet(toIsoNetIds(na.getIsisAreaId(), systemId));
632             }
633         } else if (ri instanceof IsisNodeCase) {
634             final IsisNode in = ((IsisNodeCase) ri).getIsisNode();
635             final String systemId = UriBuilder.isoId(in.getIsoSystemId());
636             ab.setIso(new IsoBuilder().setIsoSystemId(new IsoSystemId(systemId)).build());
637             if (na != null) {
638                 ab.setNet(toIsoNetIds(na.getIsisAreaId(), systemId));
639             }
640         }
641
642         ab.setTed(tb.build());
643
644         return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1Builder().setIsisNodeAttributes(
645             ab.build()).build();
646     }
647
648     private static List<IsoNetId> toIsoNetIds(final List<IsisAreaIdentifier> areaIds, final String systemId) {
649         return Lists.transform(areaIds, new Function<IsisAreaIdentifier, IsoNetId>() {
650             @Override
651             public IsoNetId apply(final IsisAreaIdentifier input) {
652                 return new IsoNetId(UriBuilder.toIsoNetId(input, systemId));
653             }
654         });
655     }
656
657     private static org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1 ospfNodeAttributes(
658         final NodeIdentifier node, final NodeAttributes na) {
659         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();
660         final OspfNodeAttributesBuilder ab = new OspfNodeAttributesBuilder();
661         if (na != null) {
662             if (na.getIpv4RouterId() != null) {
663                 tb.setTeRouterIdIpv4(na.getIpv4RouterId());
664             }
665             if (na.getIpv6RouterId() != null) {
666                 tb.setTeRouterIdIpv6(na.getIpv6RouterId());
667             }
668             if (na.getTopologyIdentifier() != null) {
669                 ab.setMultiTopologyId(nodeMultiTopology(na.getTopologyIdentifier()));
670             }
671             final CRouterIdentifier ri = node.getCRouterIdentifier();
672             if (ri instanceof OspfPseudonodeCase) {
673                 final OspfPseudonode pn = ((OspfPseudonodeCase) ri).getOspfPseudonode();
674
675                 ab.setRouterType(new PseudonodeBuilder().setPseudonode(Boolean.TRUE).build());
676                 ab.setDrInterfaceId(pn.getLanInterface().getValue());
677             } else if (ri instanceof OspfNodeCase && na.getNodeFlags() != null) {
678                 // TODO: what should we do with in.getOspfRouterId()?
679
680                 final NodeFlagBits nf = na.getNodeFlags();
681                 if (nf.isAbr() != null) {
682                     ab.setRouterType(new AbrBuilder().setAbr(nf.isAbr()).build());
683                 } else if (nf.isExternal() != null) {
684                     ab.setRouterType(new InternalBuilder().setInternal(!nf.isExternal()).build());
685                 }
686             }
687         }
688         ab.setTed(tb.build());
689         return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1Builder().setOspfNodeAttributes(
690             ab.build()).build();
691     }
692
693     private static void augmentProtocolId(final LinkstateRoute value, final IgpNodeAttributesBuilder inab,
694             final NodeAttributes na, final NodeIdentifier nd) {
695         switch (value.getProtocolId()) {
696         case Direct:
697         case Static:
698         case Unknown:
699             break;
700         case IsisLevel1:
701         case IsisLevel2:
702             inab.addAugmentation(
703                 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1.class,
704                 isisNodeAttributes(nd, na));
705             break;
706         case Ospf:
707             inab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1.class,
708                 ospfNodeAttributes(nd, na));
709             break;
710         default:
711             break;
712         }
713     }
714
715     private void createNode(final WriteTransaction trans, final UriBuilder base,
716         final LinkstateRoute value, final NodeCase n, final Attributes attributes) {
717         final NodeAttributes na;
718         //defensive lookup
719         final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
720         if (attr != null) {
721             final LinkStateAttribute attrType = attr.getLinkStateAttribute();
722             if (attrType != null) {
723                 na = ((NodeAttributesCase)attrType).getNodeAttributes();
724             } else {
725                 LOG.debug("Missing attribute type in node {} route {}, skipping it", n, value);
726                 na = null;
727             }
728         } else {
729             LOG.debug("Missing attributes in node {} route {}, skipping it", n, value);
730             na = null;
731         }
732         final IgpNodeAttributesBuilder inab = new IgpNodeAttributesBuilder();
733         final List<IpAddress> ids = new ArrayList<>();
734         if (na != null) {
735             if (na.getIpv4RouterId() != null) {
736                 ids.add(new IpAddress(na.getIpv4RouterId()));
737             }
738             if (na.getIpv6RouterId() != null) {
739                 ids.add(new IpAddress(na.getIpv6RouterId()));
740             }
741             if (na.getDynamicHostname() != null) {
742                 inab.setName(new DomainName(na.getDynamicHostname()));
743             }
744         }
745         if (!ids.isEmpty()) {
746             inab.setRouterId(ids);
747         }
748         augmentProtocolId(value, inab, na, n.getNodeDescriptors());
749
750         final NodeId nid = buildNodeId(base, n.getNodeDescriptors());
751         final NodeHolder nh = getNode(nid);
752         /*
753          *  Eventhough the the holder creates a dummy structure, we need to duplicate it here,
754          *  as that is the API requirement. The reason for it is the possible presence of supporting
755          *  node -- something which the holder does not track.
756          */
757         final NodeBuilder nb = new NodeBuilder();
758         nb.setNodeId(nid);
759         nb.setKey(new NodeKey(nb.getNodeId()));
760
761         nh.advertized(nb, inab);
762         putNode(trans, nh);
763     }
764
765     private void removeNode(final WriteTransaction trans, final UriBuilder base, final NodeCase n) {
766         final NodeId id = buildNodeId(base, n.getNodeDescriptors());
767         final NodeHolder nh = this.nodes.get(id);
768         if (nh != null) {
769             nh.unadvertized();
770             putNode(trans, nh);
771         } else {
772             LOG.warn("Node {} does not have a holder", id);
773         }
774     }
775
776     private static void augmentProtocolId(final LinkstateRoute value, final PrefixAttributes pa, final PrefixBuilder pb) {
777         switch (value.getProtocolId()) {
778         case Direct:
779         case IsisLevel1:
780         case IsisLevel2:
781         case Static:
782         case Unknown:
783             break;
784         case Ospf:
785             if (pa != null && pa.getOspfForwardingAddress() != null) {
786                 pb.addAugmentation(
787                     Prefix1.class,
788                     new Prefix1Builder().setOspfPrefixAttributes(
789                         new OspfPrefixAttributesBuilder().setForwardingAddress(pa.getOspfForwardingAddress().getIpv4Address()).build()).build());
790             }
791             break;
792         default:
793             break;
794         }
795     }
796
797     private void createPrefix(final WriteTransaction trans, final UriBuilder base,
798         final LinkstateRoute value, final PrefixCase p, final Attributes attributes) {
799         final IpPrefix ippfx = p.getPrefixDescriptors().getIpReachabilityInformation();
800         if (ippfx == null) {
801             LOG.warn("IP reachability not present in prefix {} route {}, skipping it", p, value);
802             return;
803         }
804         final PrefixBuilder pb = new PrefixBuilder();
805         pb.setKey(new PrefixKey(ippfx));
806         pb.setPrefix(ippfx);
807
808         final PrefixAttributes pa;
809         // Very defensive lookup
810         final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
811         if (attr != null) {
812             final LinkStateAttribute attrType = attr.getLinkStateAttribute();
813             if (attrType != null) {
814                 pa = ((PrefixAttributesCase)attrType).getPrefixAttributes();
815             } else {
816                 LOG.debug("Missing attribute type in IP {} prefix {} route {}, skipping it", ippfx, p, value);
817                 pa = null;
818             }
819         } else {
820             LOG.debug("Missing attributes in IP {} prefix {} route {}, skipping it", ippfx, p, value);
821             pa = null;
822         }
823         if (pa != null) {
824             pb.setMetric(pa.getPrefixMetric().getValue());
825         }
826         augmentProtocolId(value, pa, pb);
827
828         final Prefix pfx = pb.build();
829
830         /*
831          * All set, but... the hosting node may not exist, we may need to fake it.
832          */
833         final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
834         final NodeHolder nh = getNode(node);
835         nh.addPrefix(pfx);
836         LOG.debug("Created prefix {} for {}", pfx, p);
837         putNode(trans, nh);
838     }
839
840     private void removePrefix(final WriteTransaction trans, final UriBuilder base, final PrefixCase p) {
841         final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
842         final NodeHolder nh = this.nodes.get(node);
843         if (nh != null) {
844             nh.removePrefix(p);
845             LOG.debug("Removed prefix {}", p);
846             putNode(trans, nh);
847         } else {
848             LOG.warn("Removing prefix from non-existing node {}", node);
849         }
850     }
851
852     @Override
853     protected void createObject(final ReadWriteTransaction trans,
854         final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
855         final UriBuilder base = new UriBuilder(value);
856
857         final ObjectType t = value.getObjectType();
858         Preconditions.checkArgument(t != null, "Route %s value %s has null object type", id, value);
859
860         if (t instanceof LinkCase) {
861             createLink(trans, base, value, (LinkCase) t, value.getAttributes());
862         } else if (t instanceof NodeCase) {
863             createNode(trans, base, value, (NodeCase) t, value.getAttributes());
864         } else if (t instanceof PrefixCase) {
865             createPrefix(trans, base, value, (PrefixCase) t, value.getAttributes());
866         } else {
867             LOG.debug(UNHANDLED_OBJECT_CLASS, t.getImplementedInterface());
868         }
869     }
870
871     @Override
872     protected void removeObject(final ReadWriteTransaction trans,
873         final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
874         final UriBuilder base = new UriBuilder(value);
875
876         final ObjectType t = value.getObjectType();
877         if (t instanceof LinkCase) {
878             removeLink(trans, base, (LinkCase) t);
879         } else if (t instanceof NodeCase) {
880             removeNode(trans, base, (NodeCase) t);
881         } else if (t instanceof PrefixCase) {
882             removePrefix(trans, base, (PrefixCase) t);
883         } else {
884             LOG.debug(UNHANDLED_OBJECT_CLASS, t.getImplementedInterface());
885         }
886     }
887
888     @SuppressWarnings({ "rawtypes", "unchecked" })
889     @Override
890     protected InstanceIdentifier<LinkstateRoute> getRouteWildcard(final InstanceIdentifier<Tables> tablesId) {
891         return tablesId.child((Class)LinkstateRoutes.class).child(LinkstateRoute.class);
892     }
893 }