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