public static String toInstanceIdentifier(ResourceId rid) {
        StringBuilder s = new StringBuilder();

        String lastNamespace = null;
        for (NodeKey nk : rid.nodeKeys()) {
            if (nk.schemaId().name().equals("/")) {
                // special handling for root nodeKey: skip it
                // YANG runtime root: null:/
                // DCS root: org.onosproject.dcs:/
                continue;
            }

            s.append('/');

            if (!Objects.equals(lastNamespace, nk.schemaId().namespace())) {
                s.append(nk.schemaId().namespace());
                s.append(':');
                lastNamespace = nk.schemaId().namespace();
            }
            s.append(nk.schemaId().name());

            if (nk instanceof LeafListKey) {
                LeafListKey llk = (LeafListKey) nk;
                s.append('[');
                s.append('.');

                s.append('=');

                s.append('"')
                 .append(StringEscapeUtils.escapeJson(llk.asString()))
                 .append('"');
                s.append(']');

            } else if (nk instanceof ListKey) {
                ListKey lk = (ListKey) nk;

                for (KeyLeaf kl : lk.keyLeafs()) {
                    s.append('[');

                    if (!Objects.equals(kl.leafSchema().namespace(), lastNamespace)) {
                        s.append(kl.leafSchema().namespace());
                        s.append(':');
                    }
                    s.append(kl.leafSchema().name());

                    s.append('=');

                    s.append('"')
                     .append(StringEscapeUtils.escapeJson(kl.leafValAsString()))
                     .append('"');
                    s.append(']');
                }
            } else {
                // normal NodeKey
                // nothing to do
            }
        }
        if (s.length() == 0) {
            return "/";
        }
        return s.toString();
    }
@Test
    public void testToInstanceIdentifier() {

        assertThat(ResourceIds.toInstanceIdentifier(ResourceIds.ROOT_ID),
                   is("/"));
        assertThat(ResourceIds.toInstanceIdentifier(DEVICES_ID),
                   is("/org.onosproject.dcs:devices"));

        DeviceId deviceId = DeviceId.deviceId("test:device-identifier");
        assertThat(ResourceIds.toInstanceIdentifier(toResourceId(deviceId)),
                   is("/org.onosproject.dcs:devices/device[device-id=\"test:device-identifier\"]"));

        assertThat(ResourceIds.toInstanceIdentifier(ResourceIds.relativize(DEVICES_ID, toResourceId(deviceId))),
                   is("/org.onosproject.dcs:device[device-id=\"test:device-identifier\"]"));

        ResourceId eth0 = ResourceId.builder()
                .addBranchPointSchema("interfaces", "ietf-interfaces")
                .addBranchPointSchema("interface", "ietf-interfaces")
                .addKeyLeaf("name", "ietf-interfaces", "eth0")
                .build();
        assertThat(ResourceIds.toInstanceIdentifier(eth0),
                   is("/ietf-interfaces:interfaces/interface[name=\"eth0\"]"));


        assertThat(ResourceIds.toInstanceIdentifier(ResourceIds.concat(toResourceId(deviceId), eth0)),
                   is("/org.onosproject.dcs:devices/device[device-id=\"test:device-identifier\"]"
                           + "/ietf-interfaces:interfaces/interface[name=\"eth0\"]"));
    }