Clean up ModifyAction
[netconf.git] / netconf / netconf-api / src / main / java / org / opendaylight / netconf / api / ModifyAction.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.netconf.api;
9
10 import static java.util.Objects.requireNonNull;
11
12 import org.eclipse.jdt.annotation.NonNull;
13
14 /**
15  * NETCONF modification actions, as allowed for in {@code operation} and {@code default-operation} attributes of
16  * {@code <edit-config>} operation, as defined in
17  * <a href="https://www.rfc-editor.org/rfc/rfc6241#section-7.2">RFC6241 section 7.2</a>.
18  *
19  * <p>
20  * This concept is uncharacteristically bound to two separate semantics, but for a good reason: at the end of the day we
21  * want to know what the effective operation is.
22  */
23 public enum ModifyAction {
24     // operation and default-operation
25     MERGE("merge",     true,  true),
26     REPLACE("replace", true,  true),
27     // operation only
28     CREATE("create",   true,  false),
29     DELETE("delete",   true,  false),
30     REMOVE("remove",   true,  false),
31
32     // default-operation-only
33     NONE("none",       false, true);
34
35     private final @NonNull String xmlValue;
36     private final boolean isDefaultOperation;
37     private final boolean isOperation;
38
39     ModifyAction(final String xmlValue, final boolean isOperation, final boolean isDefaultOperation) {
40         this.xmlValue = requireNonNull(xmlValue);
41         this.isDefaultOperation = isDefaultOperation;
42         this.isOperation = isOperation;
43     }
44
45     /**
46      * Return the {@link ModifyAction} corresponding to a {@link #xmlValue}.
47      *
48      * @param xmlValue XML attribute or element value
49      * @return A {@link ModifyAction}
50      * @throws NullPointerException if {@code xmlValue} is {@code null}
51      * @throws IllegalArgumentException if {@code xmlValue} is not recognized
52      */
53     public static @NonNull ModifyAction ofXmlValue(final String xmlValue) {
54         return switch (xmlValue) {
55             case "merge" -> MERGE;
56             case "replace" -> REPLACE;
57             case "remove" -> REMOVE;
58             case "delete" -> DELETE;
59             case "create" -> CREATE;
60             case "none" -> NONE;
61             default -> throw new IllegalArgumentException("Unknown operation " + xmlValue);
62         };
63     }
64
65     /**
66      * Return an XML string literal corresponding to this {@link ModifyAction}.
67      *
68      * @return An XML string literal
69      */
70     public @NonNull String xmlValue() {
71         return xmlValue;
72     }
73
74     /**
75      * Check if this operation is a candidate for {@code default-operation} argument.
76      *
77      * @return {@code true} if this operation can be used as {@code default-operation}, {@code false} otherwise.
78      * @deprecated Use {@link #isDefaultOperation()} instead
79      */
80     @Deprecated(since = "5.0.0", forRemoval = true)
81     public boolean isAsDefaultPermitted() {
82         return isDefaultOperation;
83     }
84
85     /**
86      * Check if this operation is a candidate for {@code default-operation} argument.
87      *
88      * @return {@code true} if this operation can be used as {@code default-operation}, {@code false} otherwise.
89      */
90     public boolean isDefaultOperation() {
91         return isDefaultOperation;
92     }
93
94     @Deprecated(since = "5.0.0", forRemoval = true)
95     public boolean isOnElementPermitted() {
96         return isOperation;
97     }
98
99     /**
100      * Check if this operation is a candidate for {@code operation} attribute.
101      *
102      * @return {@code true} if this operation can be used as {@code operation}, {@code false} otherwise.
103      */
104     public boolean isOperation() {
105         return isOperation;
106     }
107 }