4a4eba98d1d2bc51d09b9cbeeb982b8076c31727
[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     private OperationBuilder() {
33
34     }
35
36     public static class Get {
37
38         protected Operation spec;
39         protected DataSchemaNode schemaNode;
40         private static final String METHOD_NAME = "GET";
41
42         public Get(final DataSchemaNode node, final boolean isConfig) {
43             this.schemaNode = node;
44             spec = new Operation();
45             spec.setMethod(METHOD_NAME);
46             spec.setNickname(METHOD_NAME + "-" + node.getQName().getLocalName());
47             spec.setType((isConfig ? CONFIG : OPERATIONAL) + node.getQName().getLocalName());
48             spec.setNotes(node.getDescription().orElse(null));
49         }
50
51         public Get pathParams(final List<Parameter> params) {
52             final List<Parameter> pathParameters = new ArrayList<>(params);
53             spec.setParameters(pathParameters);
54             return this;
55         }
56
57         public Operation build() {
58             return spec;
59         }
60     }
61
62     public static class Put {
63         protected Operation spec;
64         protected String nodeName;
65         protected String parentName;
66         private static final String METHOD_NAME = "PUT";
67
68         public Put(final String nodeName, final String description, final String parentName) {
69             this.nodeName = nodeName;
70             this.parentName = parentName;
71             spec = new Operation();
72             spec.setType(parentName + CONFIG + nodeName + TOP);
73             spec.setNotes(description);
74             spec.setConsumes(CONSUMES_PUT_POST);
75         }
76
77         public Put pathParams(final List<Parameter> params) {
78             final List<Parameter> parameters = new ArrayList<>(params);
79             final Parameter payload = new Parameter();
80             payload.setParamType("body");
81             payload.setType(parentName + CONFIG + nodeName + TOP);
82             payload.setName(CONFIG + nodeName);
83             parameters.add(payload);
84             spec.setParameters(parameters);
85             return this;
86         }
87
88         public Operation build() {
89             spec.setMethod(METHOD_NAME);
90             spec.setNickname(METHOD_NAME + "-" + nodeName);
91             return spec;
92         }
93     }
94
95     public static final class Post extends Put {
96
97         public static final String METHOD_NAME = "POST";
98         private final DataNodeContainer dataNodeContainer;
99
100         public Post(final String nodeName, final String parentName, final String description,
101                     final DataNodeContainer dataNodeContainer) {
102             super(nodeName, description, parentName.replace("_module", ""));
103             this.dataNodeContainer = dataNodeContainer;
104             spec.setType(CONFIG + nodeName + METHOD_NAME);
105             spec.setConsumes(CONSUMES_PUT_POST);
106         }
107
108         @Override
109         public Operation build() {
110             spec.setMethod(METHOD_NAME);
111             spec.setNickname(METHOD_NAME + "-" + nodeName);
112             return spec;
113         }
114
115         @Override
116         public Put pathParams(final List<Parameter> params) {
117             final List<Parameter> parameters = new ArrayList<>(params);
118             for (final DataSchemaNode node : dataNodeContainer.getChildNodes()) {
119                 if (node instanceof ListSchemaNode || node instanceof ContainerSchemaNode) {
120                     final Parameter payload = new Parameter();
121                     payload.setParamType("body");
122                     payload.setType(parentName + CONFIG + node.getQName().getLocalName() + TOP);
123                     payload.setName("**" + CONFIG + node.getQName().getLocalName());
124                     parameters.add(payload);
125                 }
126             }
127             spec.setParameters(parameters);
128             return this;
129         }
130
131         public Post summary(final String summary) {
132             spec.setSummary(summary);
133             return this;
134         }
135     }
136
137     public static final class Delete extends Get {
138         private static final String METHOD_NAME = "DELETE";
139
140         public Delete(final DataSchemaNode node) {
141             super(node, false);
142         }
143
144         @Override
145         public Operation build() {
146             spec.setMethod(METHOD_NAME);
147             spec.setNickname(METHOD_NAME + "-" + schemaNode.getQName().getLocalName());
148             spec.setType(null);
149             return spec;
150         }
151     }
152 }