2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.sal.rest.doc.model.builder;
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;
19 public final class OperationBuilder {
21 public static final String OPERATIONAL = "(operational)";
22 public static final String CONFIG = "(config)";
23 public static final String TOP = "-TOP";
25 public static final List<String> CONSUMES_PUT_POST = new ArrayList<>();
28 CONSUMES_PUT_POST.add("application/json");
29 CONSUMES_PUT_POST.add("application/xml");
32 public static class Get {
34 protected Operation spec;
35 protected DataSchemaNode schemaNode;
36 private static final String METHOD_NAME = "GET";
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());
47 public Get pathParams(final List<Parameter> params) {
48 final List<Parameter> pathParameters = new ArrayList<>(params);
49 spec.setParameters(pathParameters);
53 public Operation build() {
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";
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);
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);
84 public Operation build() {
85 spec.setMethod(METHOD_NAME);
86 spec.setNickname(METHOD_NAME + "-" + nodeName);
91 public static final class Post extends Put {
93 public static final String METHOD_NAME = "POST";
94 private final DataNodeContainer dataNodeContainer;
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);
104 public Operation build() {
105 spec.setMethod(METHOD_NAME);
106 spec.setNickname(METHOD_NAME + "-" + nodeName);
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);
122 spec.setParameters(parameters);
126 public Post summary(final String summary) {
127 spec.setSummary(summary);
132 public static final class Delete extends Get {
133 private static final String METHOD_NAME = "DELETE";
135 public Delete(final DataSchemaNode node) {
140 public Operation build() {
141 spec.setMethod(METHOD_NAME);
142 spec.setNickname(METHOD_NAME + "-" + schemaNode.getQName().getLocalName());