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