Populate data/ hierarchy
[yangtools.git] / data / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / DatastoreIdentifier.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech s.r.o. 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.yangtools.yang.data.api;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
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;
25
26 /**
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}.
30  */
31 @Beta
32 @NonNullByDefault
33 public final class DatastoreIdentifier extends AbstractIdentifier<QName> implements WritableObject {
34     private static final long serialVersionUID = 1L;
35
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");
39
40     private static final LoadingCache<QName, DatastoreIdentifier> CACHE = CacheBuilder.newBuilder().weakValues()
41             .build(new CacheLoader<QName, DatastoreIdentifier>() {
42                 @Override
43                 public DatastoreIdentifier load(final QName key) {
44                     return of(key);
45                 }
46             });
47
48     private DatastoreIdentifier(final QName qname) {
49         super(qname);
50         if (IETF_DATASTORES_NAMESPACE.equals(qname.getNamespace())) {
51             checkArgument(!KNOWN_ABSTRACTS.contains(qname.getLocalName()), "%s refers to a known-abstract datastore",
52                 qname);
53         }
54     }
55
56     public static DatastoreIdentifier of(final QName qname) {
57         return new DatastoreIdentifier(qname);
58     }
59
60     public static DatastoreIdentifier create(final QName qname) {
61         final DatastoreIdentifier existing = CACHE.getIfPresent(qname);
62         return existing != null ? existing : CACHE.getUnchecked(qname.intern());
63     }
64
65     public static DatastoreIdentifier readFrom(final DataInput in) throws IOException {
66         return create(QName.readFrom(in));
67     }
68
69     @Override
70     public void writeTo(final DataOutput out) throws IOException {
71         getValue().writeTo(out);
72     }
73
74     private Object writeReplace() {
75         return new DSIv1(getValue());
76     }
77 }