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 private static final String METHOD_NAME = "PUT";
63 public Put(final String nodeName, final String description) {
64 this.nodeName = nodeName;
65 spec = new Operation();
66 spec.setType(CONFIG + nodeName);
67 spec.setNotes(description);
68 spec.setConsumes(CONSUMES_PUT_POST);
71 public Put pathParams(final List<Parameter> params) {
72 final List<Parameter> parameters = new ArrayList<>(params);
73 final Parameter payload = new Parameter();
74 payload.setParamType("body");
75 payload.setType(CONFIG + nodeName + TOP);
76 payload.setName(CONFIG + nodeName);
77 parameters.add(payload);
78 spec.setParameters(parameters);
82 public Operation build() {
83 spec.setMethod(METHOD_NAME);
84 spec.setNickname(METHOD_NAME + "-" + nodeName);
89 public static final class Post extends Put {
91 public static final String METHOD_NAME = "POST";
92 private final DataNodeContainer dataNodeContainer;
94 public Post(final String nodeName, final String description, final DataNodeContainer dataNodeContainer) {
95 super(nodeName, description);
96 this.dataNodeContainer = dataNodeContainer;
97 spec.setType(CONFIG + nodeName + METHOD_NAME);
98 spec.setConsumes(CONSUMES_PUT_POST);
102 public Operation build() {
103 spec.setMethod(METHOD_NAME);
104 spec.setNickname(METHOD_NAME + "-" + nodeName);
109 public Put pathParams(final List<Parameter> params) {
110 final List<Parameter> parameters = new ArrayList<>(params);
111 for (final DataSchemaNode node : dataNodeContainer.getChildNodes()) {
112 if (node instanceof ListSchemaNode || node instanceof ContainerSchemaNode) {
113 final Parameter payload = new Parameter();
114 payload.setParamType("body");
115 payload.setType(CONFIG + node.getQName().getLocalName() + TOP);
116 payload.setName("**" + CONFIG + node.getQName().getLocalName());
117 parameters.add(payload);
120 spec.setParameters(parameters);
125 public Post summary(final String summary) {
126 spec.setSummary(summary);
131 public static final class Delete extends Get {
132 private static final String METHOD_NAME = "DELETE";
134 public Delete(final DataSchemaNode node) {
139 public Operation build() {
140 spec.setMethod(METHOD_NAME);
141 spec.setNickname(METHOD_NAME + "-" + schemaNode.getQName().getLocalName());