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