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