Bug 5997 - Fix put and post body examples
[netconf.git] / restconf / sal-rest-docgen / src / main / java / org / opendaylight / netconf / sal / rest / doc / model / builder / OperationBuilder.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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.sal.rest.doc.model.builder;
9
10 import java.util.ArrayList;
11 import java.util.List;
12 import org.opendaylight.netconf.sal.rest.doc.swagger.Operation;
13 import org.opendaylight.netconf.sal.rest.doc.swagger.Parameter;
14 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
16 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
18
19 public final class OperationBuilder {
20
21     public static final String OPERATIONAL = "(operational)";
22     public static final String CONFIG = "(config)";
23     public static final String TOP = "-TOP";
24
25     public static final List<String> CONSUMES_PUT_POST = new ArrayList<>();
26
27     static {
28         CONSUMES_PUT_POST.add("application/json");
29         CONSUMES_PUT_POST.add("application/xml");
30     }
31
32     public static class Get {
33
34         protected Operation spec;
35         protected DataSchemaNode schemaNode;
36         private static final String METHOD_NAME = "GET";
37
38         public Get(final DataSchemaNode node, final boolean isConfig) {
39             this.schemaNode = node;
40             spec = new Operation();
41             spec.setMethod(METHOD_NAME);
42             spec.setNickname(METHOD_NAME + "-" + node.getQName().getLocalName());
43             spec.setType((isConfig ? CONFIG : OPERATIONAL) + node.getQName().getLocalName());
44             spec.setNotes(node.getDescription());
45         }
46
47         public Get pathParams(final List<Parameter> params) {
48             final List<Parameter> pathParameters = new ArrayList<>(params);
49             spec.setParameters(pathParameters);
50             return this;
51         }
52
53         public Operation build() {
54             return spec;
55         }
56     }
57
58     public static class Put {
59         protected Operation spec;
60         protected String nodeName;
61         private static final String METHOD_NAME = "PUT";
62
63         public Put(final String nodeName, final String description) {
64             this.nodeName = nodeName;
65             spec = new Operation();
66             spec.setType(CONFIG + nodeName);
67             spec.setNotes(description);
68             spec.setConsumes(CONSUMES_PUT_POST);
69         }
70
71         public Put pathParams(final List<Parameter> params) {
72             final List<Parameter> parameters = new ArrayList<>(params);
73             final Parameter payload = new Parameter();
74             payload.setParamType("body");
75             payload.setType(CONFIG + nodeName + TOP);
76             payload.setName(CONFIG + nodeName);
77             parameters.add(payload);
78             spec.setParameters(parameters);
79             return this;
80         }
81
82         public Operation build() {
83             spec.setMethod(METHOD_NAME);
84             spec.setNickname(METHOD_NAME + "-" + nodeName);
85             return spec;
86         }
87     }
88
89     public static final class Post extends Put {
90
91         public static final String METHOD_NAME = "POST";
92         private final DataNodeContainer dataNodeContainer;
93
94         public Post(final String nodeName, final String description, final DataNodeContainer dataNodeContainer) {
95             super(nodeName, description);
96             this.dataNodeContainer = dataNodeContainer;
97             spec.setType(CONFIG + nodeName + METHOD_NAME);
98             spec.setConsumes(CONSUMES_PUT_POST);
99         }
100
101         @Override
102         public Operation build() {
103             spec.setMethod(METHOD_NAME);
104             spec.setNickname(METHOD_NAME + "-" + nodeName);
105             return spec;
106         }
107
108         @Override
109         public Put pathParams(final List<Parameter> params) {
110             final List<Parameter> parameters = new ArrayList<>(params);
111             for (final DataSchemaNode node : dataNodeContainer.getChildNodes()) {
112                 if (node instanceof ListSchemaNode || node instanceof ContainerSchemaNode) {
113                     final Parameter payload = new Parameter();
114                     payload.setParamType("body");
115                     payload.setType(CONFIG + node.getQName().getLocalName() + TOP);
116                     payload.setName("**" + CONFIG + node.getQName().getLocalName());
117                     parameters.add(payload);
118                 }
119             }
120             spec.setParameters(parameters);
121             return this;
122
123         }
124
125         public Post summary(final String summary) {
126             spec.setSummary(summary);
127             return this;
128         }
129     }
130
131     public static final class Delete extends Get {
132         private static final String METHOD_NAME = "DELETE";
133
134         public Delete(final DataSchemaNode node) {
135             super(node, false);
136         }
137
138         @Override
139         public Operation build() {
140             spec.setMethod(METHOD_NAME);
141             spec.setNickname(METHOD_NAME + "-" + schemaNode.getQName().getLocalName());
142             spec.setType(null);
143             return spec;
144         }
145     }
146 }