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 private OperationBuilder() {
36 public static class Get {
38 protected Operation spec;
39 protected DataSchemaNode schemaNode;
40 private static final String METHOD_NAME = "GET";
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));
51 public Get pathParams(final List<Parameter> params) {
52 final List<Parameter> pathParameters = new ArrayList<>(params);
53 spec.setParameters(pathParameters);
57 public Operation build() {
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";
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);
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);
88 public Operation build() {
89 spec.setMethod(METHOD_NAME);
90 spec.setNickname(METHOD_NAME + "-" + nodeName);
95 public static final class Post extends Put {
97 public static final String METHOD_NAME = "POST";
98 private final DataNodeContainer dataNodeContainer;
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);
109 public Operation build() {
110 spec.setMethod(METHOD_NAME);
111 spec.setNickname(METHOD_NAME + "-" + nodeName);
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);
127 spec.setParameters(parameters);
131 public Post summary(final String summary) {
132 spec.setSummary(summary);
137 public static final class Delete extends Get {
138 private static final String METHOD_NAME = "DELETE";
140 public Delete(final DataSchemaNode node) {
145 public Operation build() {
146 spec.setMethod(METHOD_NAME);
147 spec.setNickname(METHOD_NAME + "-" + schemaNode.getQName().getLocalName());