b95ce5c0963f3bf2922258e27cd9bd679906d510
[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 com.google.common.annotations.Beta;
11 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
12
13 /**
14  * Strongly-typed representation of a datastore (configuration or operational).
15  */
16 @Beta
17 // FIXME Base this on ietf-datastores.yang (RFC 8342)
18 public abstract sealed class Datastore {
19
20     /** Class representing the configuration datastore. */
21     public static final Class<Configuration> CONFIGURATION = Configuration.class;
22
23     /** Class representing the operational datastore. */
24     public static final Class<Operational> OPERATIONAL = Operational.class;
25
26     public static final class Configuration extends Datastore {
27
28     }
29
30     public static final class Operational extends Datastore {
31
32     }
33
34     private Datastore() {
35         // Hidden on purpose
36     }
37
38     /**
39      * Returns the logical datastore type corresponding to the given datastore class.
40      *
41      * @param datastoreClass The datastore class to convert.
42      * @return The corresponding logical datastore type.
43      * @throws IllegalArgumentException if the provided datastore class isn’t handled.
44      */
45     public static LogicalDatastoreType toType(final Class<? extends Datastore> datastoreClass) {
46         if (datastoreClass.equals(Configuration.class)) {
47             return LogicalDatastoreType.CONFIGURATION;
48         } else if (Operational.class.equals(datastoreClass)) {
49             return LogicalDatastoreType.OPERATIONAL;
50         } else {
51             throw new IllegalArgumentException("Unknown datastore class " + datastoreClass);
52         }
53     }
54
55     /**
56      * Returns the datastore class corresponding to the given logical datastore type.
57      * @param datastoreType The logical datastore type to convert.
58      * @return The corresponding datastore class.
59      * @throws IllegalArgumentException if the provided logical datastore type isn’t handled.
60      */
61     public static Class<? extends Datastore> toClass(final LogicalDatastoreType datastoreType) {
62         return switch (datastoreType) {
63             case CONFIGURATION -> CONFIGURATION;
64             case OPERATIONAL -> OPERATIONAL;
65         };
66     }
67 }