Rework body formatting wiring
[netconf.git] / protocol / restconf-api / src / main / java / org / opendaylight / restconf / api / query / WithDefaultsParam.java
1 /*
2  * Copyright (c) 2021 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.restconf.api.query;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.net.URI;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.with.defaults.rev110601.WithDefaultsMode;
15
16 /**
17  * Enumeration of possible {@code with-defaults} parameter values as defined by
18  * <a href="https://www.rfc-editor.org/rfc/rfc8040#section-4.8.9">RFC8040, section 4.8.9</a>. This is an equivalent
19  * of with-defaults retrieval mode as defined by
20  * <a href="https://www.rfc-editor.org/rfc/rfc6243#section-3">RFC6243 section 3</a> and expressed as the
21  * {@code typedef with-defaults-mode}} in the corresponding YANG model.
22  */
23 public enum WithDefaultsParam implements RestconfQueryParam<WithDefaultsParam> {
24     /**
25      * Data nodes set to the YANG default by the client are reported.
26      */
27     EXPLICIT(WithDefaultsMode.Explicit),
28     /**
29      * All data nodes are reported.
30      */
31     REPORT_ALL(WithDefaultsMode.ReportAll),
32     /**
33      * All data nodes are reported, and defaults are tagged.
34      */
35     REPORT_ALL_TAGGED(WithDefaultsMode.ReportAllTagged),
36     /**
37      * Data nodes set to the YANG default are not reported.
38      */
39     TRIM(WithDefaultsMode.Trim);
40
41     // API consistency: must not be confused with enum constants
42     @SuppressWarnings("checkstyle:ConstantName")
43     public static final @NonNull String uriName = "with-defaults";
44
45     private static final @NonNull URI CAPABILITY = URI.create("urn:ietf:params:restconf:capability:with-defaults:1.0");
46
47     private final @NonNull WithDefaultsMode mode;
48
49     WithDefaultsParam(final WithDefaultsMode mode) {
50         this.mode = requireNonNull(mode);
51     }
52
53     public static @NonNull WithDefaultsParam of(final WithDefaultsMode mode) {
54         return switch (mode) {
55             case Explicit -> EXPLICIT;
56             case ReportAll -> REPORT_ALL;
57             case ReportAllTagged -> REPORT_ALL_TAGGED;
58             case Trim -> TRIM;
59         };
60     }
61
62     public static @NonNull WithDefaultsParam forUriValue(final String uriValue) {
63         try {
64             return of(WithDefaultsMode.ofName(uriValue));
65         } catch (IllegalArgumentException e) {
66             throw new IllegalArgumentException("Invalid " + uriName + " value: " + e.getMessage(), e);
67         }
68     }
69
70     @Override
71     public Class<WithDefaultsParam> javaClass() {
72         return WithDefaultsParam.class;
73     }
74
75     @Override
76     public String paramName() {
77         return uriName;
78     }
79
80     @Override
81     public String paramValue() {
82         return mode.getName();
83     }
84
85     public @NonNull WithDefaultsMode mode() {
86         return mode;
87     }
88
89     public static @NonNull URI capabilityUri() {
90         return CAPABILITY;
91     }
92 }