Implement managed transactions
[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 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     public static final class Operational extends Datastore {}
29
30     /**
31      * Returns the logical datastore type corresponding to the given datastore class.
32      *
33      * @param datastoreClass The datastore class to convert.
34      * @return The corresponding logical datastore type.
35      * @throws IllegalArgumentException if the provided datastore class isn’t handled.
36      */
37     public static LogicalDatastoreType toType(Class<? extends Datastore> datastoreClass) {
38         if (datastoreClass.equals(Configuration.class)) {
39             return LogicalDatastoreType.CONFIGURATION;
40         } else if (Operational.class.equals(datastoreClass)) {
41             return LogicalDatastoreType.OPERATIONAL;
42         } else {
43             throw new IllegalArgumentException("Unknown datastore class " + datastoreClass);
44         }
45     }
46
47     /**
48      * Returns the datastore class corresponding to the given logical datastore type.
49      * @param datastoreType The logical datastore type to convert.
50      * @return The corresponding datastore class.
51      * @throws IllegalArgumentException if the provided logical datastore type isn’t handled.
52      */
53     public static Class<? extends Datastore> toClass(LogicalDatastoreType datastoreType) {
54         switch (datastoreType) {
55             case CONFIGURATION:
56                 return CONFIGURATION;
57             case OPERATIONAL:
58                 return OPERATIONAL;
59             default:
60                 throw new IllegalArgumentException("Unknown datastore type " + datastoreType);
61         }
62     }
63
64     private Datastore() {}
65 }