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)";
24 public static final List<String> CONSUMES_PUT_POST = new ArrayList<>();
27 CONSUMES_PUT_POST.add("application/json");
28 CONSUMES_PUT_POST.add("application/xml");
31 public static class Get {
33 protected Operation spec;
34 protected DataSchemaNode schemaNode;
35 private static final String METHOD_NAME = "GET";
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());
46 public Get pathParams(final List<Parameter> params) {
47 final List<Parameter> pathParameters = new ArrayList<>(params);
48 spec.setParameters(pathParameters);
52 public Operation build() {
57 public static class Put {
58 protected Operation spec;
59 protected String nodeName;
60 private static final String METHOD_NAME = "PUT";
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);
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);
80 public Operation build() {
81 spec.setMethod(METHOD_NAME);
82 spec.setNickname(METHOD_NAME + "-" + nodeName);
87 public static final class Post extends Put {
89 public static final String METHOD_NAME = "POST";
90 private final DataNodeContainer dataNodeContainer;
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);
100 public Operation build() {
101 spec.setMethod(METHOD_NAME);
102 spec.setNickname(METHOD_NAME + "-" + nodeName);
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);
118 spec.setParameters(parameters);
123 public Post summary(final String summary) {
124 spec.setSummary(summary);
129 public static final class Delete extends Get {
130 private static final String METHOD_NAME = "DELETE";
132 public Delete(final DataSchemaNode node) {
137 public Operation build() {
138 spec.setMethod(METHOD_NAME);
139 spec.setNickname(METHOD_NAME + "-" + schemaNode.getQName().getLocalName());