Clean up Datastore addressing
[mdsal.git] / binding / mdsal-binding-util / src / main / java / org / opendaylight / mdsal / binding / util / Datastore.java
1 /*
2  * Copyright © 2018 Red Hat, Inc. and others.
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.mdsal.binding.util;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
14
15 /**
16  * Strongly-typed representation of a datastore (configuration or operational).
17  */
18 @Beta
19 // FIXME Base this on ietf-datastores.yang (RFC 8342)
20 public abstract sealed class Datastore {
21     /**
22      * Class representing the configuration datastore.
23      */
24     public static final class Configuration extends Datastore {
25         private Configuration() {
26             super(LogicalDatastoreType.CONFIGURATION);
27         }
28     }
29
30     /**
31      * Class representing the operational datastore.
32      */
33     public static final class Operational extends Datastore {
34         private Operational() {
35             super(LogicalDatastoreType.OPERATIONAL);
36         }
37     }
38
39     public static final Operational OPERATIONAL = new Operational();
40     public static final Configuration CONFIGURATION = new Configuration();
41
42     private final LogicalDatastoreType type;
43
44     private Datastore(final LogicalDatastoreType type) {
45         this.type = requireNonNull(type);
46     }
47
48     /**
49      * Returns the logical datastore type corresponding to thisclass.
50      *
51      * @return The corresponding logical datastore type.
52      */
53     public LogicalDatastoreType type() {
54         return type;
55     }
56
57     /**
58      * Returns the Datastore corresponding to the given logical datastore type.
59      *
60      * @param type The logical datastore type to convert.
61      * @return The corresponding Datastore
62      * @throws NullPointerException if {@code type} is {@code null}
63      */
64     public static Datastore ofType(final LogicalDatastoreType type) {
65         return switch (type) {
66             case CONFIGURATION -> CONFIGURATION;
67             case OPERATIONAL -> OPERATIONAL;
68         };
69     }
70 }