Merge branch 'master' of ../controller
[yangtools.git] / yang / 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 java.net.URI;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.opendaylight.yangtools.concepts.WritableObject;
23 import org.opendaylight.yangtools.util.AbstractIdentifier;
24 import org.opendaylight.yangtools.yang.common.QName;
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 URI IETF_DATASTORES_NAMESPACE = URI.create("urn:ietf:params:xml:ns:yang:ietf-datastores");
37     private static final ImmutableSet<String> KNOWN_ABSTRACTS = ImmutableSet.of("datastore", "conventional", "dynamic");
38
39     private static final LoadingCache<QName, DatastoreIdentifier> CACHE = CacheBuilder.newBuilder().weakValues()
40             .build(new CacheLoader<QName, DatastoreIdentifier>() {
41                 @Override
42                 public DatastoreIdentifier load(final QName key) {
43                     return of(key);
44                 }
45             });
46
47     private DatastoreIdentifier(final QName qname) {
48         super(qname);
49         if (IETF_DATASTORES_NAMESPACE.equals(qname.getNamespace())) {
50             checkArgument(!KNOWN_ABSTRACTS.contains(qname.getLocalName()), "%s refers to a known-abstract datastore",
51                 qname);
52         }
53     }
54
55     public static DatastoreIdentifier of(final QName qname) {
56         return new DatastoreIdentifier(qname);
57     }
58
59     public static DatastoreIdentifier create(final QName qname) {
60         final DatastoreIdentifier existing = CACHE.getIfPresent(qname);
61         return existing != null ? existing : CACHE.getUnchecked(qname.intern());
62     }
63
64     public static DatastoreIdentifier readFrom(final DataInput in) throws IOException {
65         return create(QName.readFrom(in));
66     }
67
68     @Override
69     public void writeTo(final DataOutput out) throws IOException {
70         getValue().writeTo(out);
71     }
72
73     private Object writeReplace() {
74         return new DSIv1(getValue());
75     }
76 }