2 * Copyright (c) 2019 PANTHEON.tech s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.api;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import com.google.common.annotations.Beta;
13 import com.google.common.cache.CacheBuilder;
14 import com.google.common.cache.CacheLoader;
15 import com.google.common.cache.LoadingCache;
16 import com.google.common.collect.ImmutableSet;
17 import java.io.DataInput;
18 import java.io.DataOutput;
19 import java.io.IOException;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.opendaylight.yangtools.concepts.WritableObject;
22 import org.opendaylight.yangtools.util.AbstractIdentifier;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.XMLNamespace;
27 * Identifier of a RFC8342 (NMDA) datastore. This class is backed by the QName of the datastore, i.e.
28 * the {@code identity} which defines the datastore. This class does not allow creation of identifiers which are
29 * defined as abstract, that is "datastore", "conventional" and "dynamic" in the namespace of {@code ietf-datastores}.
33 public final class DatastoreIdentifier extends AbstractIdentifier<QName> implements WritableObject {
34 private static final long serialVersionUID = 1L;
36 private static final XMLNamespace IETF_DATASTORES_NAMESPACE =
37 XMLNamespace.of("urn:ietf:params:xml:ns:yang:ietf-datastores").intern();
38 private static final ImmutableSet<String> KNOWN_ABSTRACTS = ImmutableSet.of("datastore", "conventional", "dynamic");
40 private static final LoadingCache<QName, DatastoreIdentifier> CACHE = CacheBuilder.newBuilder().weakValues()
41 .build(new CacheLoader<QName, DatastoreIdentifier>() {
43 public DatastoreIdentifier load(final QName key) {
48 private DatastoreIdentifier(final QName qname) {
50 if (IETF_DATASTORES_NAMESPACE.equals(qname.getNamespace())) {
51 checkArgument(!KNOWN_ABSTRACTS.contains(qname.getLocalName()), "%s refers to a known-abstract datastore",
56 public static DatastoreIdentifier of(final QName qname) {
57 return new DatastoreIdentifier(qname);
60 public static DatastoreIdentifier create(final QName qname) {
61 final DatastoreIdentifier existing = CACHE.getIfPresent(qname);
62 return existing != null ? existing : CACHE.getUnchecked(qname.intern());
65 public static DatastoreIdentifier readFrom(final DataInput in) throws IOException {
66 return create(QName.readFrom(in));
70 public void writeTo(final DataOutput out) throws IOException {
71 getValue().writeTo(out);
74 private Object writeReplace() {
75 return new DSIv1(getValue());