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