/**
 * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.controller.md.compatibility.topology;

import java.util.Iterator;

import org.opendaylight.controller.sal.compatibility.InventoryMapping;
import org.opendaylight.controller.sal.core.ConstructionException;
import org.opendaylight.controller.sal.core.Edge;
import org.opendaylight.controller.sal.core.NodeConnector;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;

import com.google.common.base.Splitter;

public class TopologyMapping {
    private static final Splitter QUAD_COLON_SPLITTER = Splitter.on("::::");

    public TopologyMapping(final TopologyKey path, final InstanceIdentifier<Topology> key) {
        // No-op for now. Multi-instance will require fixing InventoryMapping first.
    }

    public Edge toAdTopologyEdge(final InstanceIdentifier<Link> identifier) throws ConstructionException {
        @SuppressWarnings("unchecked")
        final LinkKey linkKey = ((KeyedInstanceIdentifier<Link, LinkKey>)identifier).getKey();

        final Iterator<String> it = QUAD_COLON_SPLITTER.split(linkKey.getLinkId().getValue()).iterator();
        final NodeConnector tail = InventoryMapping.nodeConnectorFromId(it.next());
        final NodeConnector head = InventoryMapping.nodeConnectorFromId(it.next());
        return new Edge(tail, head);
    }

    public NodeConnector toAdTopologyNodeConnector(final InstanceIdentifier<TerminationPoint> identifier) {
        @SuppressWarnings("unchecked")
        final TerminationPointKey tpKey = ((KeyedInstanceIdentifier<TerminationPoint, TerminationPointKey>)identifier).getKey();

        return InventoryMapping.nodeConnectorFromId(tpKey.getTpId().getValue());
    }

    public org.opendaylight.controller.sal.core.Node toAdTopologyNode(final InstanceIdentifier<Node> identifier) {
        @SuppressWarnings("unchecked")
        final NodeKey nodeKey = ((KeyedInstanceIdentifier<Node, NodeKey>)identifier).getKey();

        return InventoryMapping.nodeFromNodeId(nodeKey.getNodeId().getValue());
    }

    public NodeKey toTopologyNodeKey(final org.opendaylight.controller.sal.core.Node node) {
        return new NodeKey(new NodeId(InventoryMapping.toNodeId(node)));
    }

    public TerminationPointKey toTopologyTerminationPointKey(final NodeConnector nc) {
        return new TerminationPointKey(new TpId(InventoryMapping.toNodeConnectorId(nc)));
    }

    public LinkKey toTopologyLinkKey(final Edge edge) {
        final TerminationPointKey sourceTp = toTopologyTerminationPointKey(edge.getTailNodeConnector());
        final TerminationPointKey destTp = toTopologyTerminationPointKey(edge.getHeadNodeConnector());

        final StringBuilder sb = new StringBuilder();
        sb.append(sourceTp.getTpId().toString());
        sb.append("::::");
        sb.append(destTp.getTpId().toString());
        return new LinkKey(new LinkId(sb.toString()));
    }
}

@@ -2157,16 +2157,75 @@
 pping %7B%0A
+    private static final String HEAD_TAIL_STRING = %22::::%22;%0A
     priv
@@ -2250,26 +2250,25 @@
 plitter 
-QUAD_COLON
+HEAD_TAIL
 _SPLITTE
@@ -2283,22 +2283,32 @@
 tter.on(
-%22::::%22
+HEAD_TAIL_STRING
 );%0A%0A    
@@ -2770,18 +2770,17 @@
 t = 
-QUAD_COLON
+HEAD_TAIL
 _SPL
@@ -4493,22 +4493,32 @@
 .append(
-%22::::%22
+HEAD_TAIL_STRING
 );%0A     

use a string constant for "::::"