d24636bfee060451de3cf7cda594036fbfc027e1
[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, final DataNodeContainer dataNodeContainer) {
97             super(nodeName, description, parentName.replace("_module", ""));
98             this.dataNodeContainer = dataNodeContainer;
99             spec.setType(CONFIG + nodeName + METHOD_NAME);
100             spec.setConsumes(CONSUMES_PUT_POST);
101         }
102
103         @Override
104         public Operation build() {
105             spec.setMethod(METHOD_NAME);
106             spec.setNickname(METHOD_NAME + "-" + nodeName);
107             return spec;
108         }
109
110         @Override
111         public Put pathParams(final List<Parameter> params) {
112             final List<Parameter> parameters = new ArrayList<>(params);
113             for (final DataSchemaNode node : dataNodeContainer.getChildNodes()) {
114                 if (node instanceof ListSchemaNode || node instanceof ContainerSchemaNode) {
115                     final Parameter payload = new Parameter();
116                     payload.setParamType("body");
117                     payload.setType(parentName + CONFIG + node.getQName().getLocalName() + TOP);
118                     payload.setName("**" + CONFIG + node.getQName().getLocalName());
119                     parameters.add(payload);
120                 }
121             }
122             spec.setParameters(parameters);
123             return this;
124         }
125
126         public Post summary(final String summary) {
127             spec.setSummary(summary);
128             return this;
129         }
130     }
131
132     public static final class Delete extends Get {
133         private static final String METHOD_NAME = "DELETE";
134
135         public Delete(final DataSchemaNode node) {
136             super(node, false);
137         }
138
139         @Override
140         public Operation build() {
141             spec.setMethod(METHOD_NAME);
142             spec.setNickname(METHOD_NAME + "-" + schemaNode.getQName().getLocalName());
143             spec.setType(null);
144             return spec;
145         }
146     }
147 }