bb14d5a593e75eaac73569cb9cf652f3547b5210
[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         protected String parentName;
62         private static final String METHOD_NAME = "PUT";
63
64         public Put(final String nodeName, final String description, final String parentName) {
65             this.nodeName = nodeName;
66             this.parentName = parentName;
67             spec = new Operation();
68             spec.setType(parentName + CONFIG + nodeName + TOP);
69             spec.setNotes(description);
70             spec.setConsumes(CONSUMES_PUT_POST);
71         }
72
73         public Put pathParams(final List<Parameter> params) {
74             final List<Parameter> parameters = new ArrayList<>(params);
75             final Parameter payload = new Parameter();
76             payload.setParamType("body");
77             payload.setType(parentName + CONFIG + nodeName + TOP);
78             payload.setName(CONFIG + nodeName);
79             parameters.add(payload);
80             spec.setParameters(parameters);
81             return this;
82         }
83
84         public Operation build() {
85             spec.setMethod(METHOD_NAME);
86             spec.setNickname(METHOD_NAME + "-" + nodeName);
87             return spec;
88         }
89     }
90
91     public static final class Post extends Put {
92
93         public static final String METHOD_NAME = "POST";
94         private final DataNodeContainer dataNodeContainer;
95
96         public Post(final String nodeName, final String parentName, final String description,
97                     final DataNodeContainer dataNodeContainer) {
98             super(nodeName, description, parentName.replace("_module", ""));
99             this.dataNodeContainer = dataNodeContainer;
100             spec.setType(CONFIG + nodeName + METHOD_NAME);
101             spec.setConsumes(CONSUMES_PUT_POST);
102         }
103
104         @Override
105         public Operation build() {
106             spec.setMethod(METHOD_NAME);
107             spec.setNickname(METHOD_NAME + "-" + nodeName);
108             return spec;
109         }
110
111         @Override
112         public Put pathParams(final List<Parameter> params) {
113             final List<Parameter> parameters = new ArrayList<>(params);
114             for (final DataSchemaNode node : dataNodeContainer.getChildNodes()) {
115                 if (node instanceof ListSchemaNode || node instanceof ContainerSchemaNode) {
116                     final Parameter payload = new Parameter();
117                     payload.setParamType("body");
118                     payload.setType(parentName + CONFIG + node.getQName().getLocalName() + TOP);
119                     payload.setName("**" + CONFIG + node.getQName().getLocalName());
120                     parameters.add(payload);
121                 }
122             }
123             spec.setParameters(parameters);
124             return this;
125         }
126
127         public Post summary(final String summary) {
128             spec.setSummary(summary);
129             return this;
130         }
131     }
132
133     public static final class Delete extends Get {
134         private static final String METHOD_NAME = "DELETE";
135
136         public Delete(final DataSchemaNode node) {
137             super(node, false);
138         }
139
140         @Override
141         public Operation build() {
142             spec.setMethod(METHOD_NAME);
143             spec.setNickname(METHOD_NAME + "-" + schemaNode.getQName().getLocalName());
144             spec.setType(null);
145             return spec;
146         }
147     }
148 }