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<>();
26 CONSUMES_PUT_POST.add("application/json");
27 CONSUMES_PUT_POST.add("application/xml");
30 public static class Get {
32 protected Operation spec;
33 protected DataSchemaNode schemaNode;
34 private static final String METHOD_NAME = "GET";
36 public Get(DataSchemaNode node, boolean isConfig) {
37 this.schemaNode = node;
38 spec = new Operation();
39 spec.setMethod(METHOD_NAME);
40 spec.setNickname(METHOD_NAME + "-" + node.getQName().getLocalName());
41 spec.setType((isConfig ? CONFIG : OPERATIONAL) + node.getQName().getLocalName());
42 spec.setNotes(node.getDescription());
45 public Get pathParams(List<Parameter> params) {
46 List<Parameter> pathParameters = new ArrayList<>(params);
47 spec.setParameters(pathParameters);
51 public Operation build() {
56 public static class Put {
57 protected Operation spec;
58 protected String nodeName;
59 private static final String METHOD_NAME = "PUT";
61 public Put(String nodeName, final String description) {
62 this.nodeName = nodeName;
63 spec = new Operation();
64 spec.setType(CONFIG + nodeName);
65 spec.setNotes(description);
66 spec.setConsumes(CONSUMES_PUT_POST);
69 public Put pathParams(List<Parameter> params) {
70 List<Parameter> parameters = new ArrayList<>(params);
71 Parameter payload = new Parameter();
72 payload.setParamType("body");
73 payload.setType(CONFIG + nodeName);
74 parameters.add(payload);
75 spec.setParameters(parameters);
79 public Operation build() {
80 spec.setMethod(METHOD_NAME);
81 spec.setNickname(METHOD_NAME + "-" + nodeName);
86 public static final class Post extends Put {
88 public static final String METHOD_NAME = "POST";
89 private final DataNodeContainer dataNodeContainer;
91 public Post(final String nodeName, final String description, final DataNodeContainer dataNodeContainer) {
92 super(nodeName, description);
93 this.dataNodeContainer = dataNodeContainer;
94 spec.setType(CONFIG + nodeName + METHOD_NAME);
95 spec.setConsumes(CONSUMES_PUT_POST);
99 public Operation build() {
100 spec.setMethod(METHOD_NAME);
101 spec.setNickname(METHOD_NAME + "-" + nodeName);
106 public Put pathParams(List<Parameter> params) {
107 List<Parameter> parameters = new ArrayList<>(params);
108 for (DataSchemaNode node : dataNodeContainer.getChildNodes()) {
109 if (node instanceof ListSchemaNode || node instanceof ContainerSchemaNode) {
110 Parameter payload = new Parameter();
111 payload.setParamType("body");
112 payload.setType(CONFIG + node.getQName().getLocalName());
113 payload.setName("**"+CONFIG + node.getQName().getLocalName());
114 parameters.add(payload);
117 spec.setParameters(parameters);
122 public Post summary(final String summary) {
123 spec.setSummary(summary);
128 public static final class Delete extends Get {
129 private static final String METHOD_NAME = "DELETE";
131 public Delete(DataSchemaNode node) {
136 public Operation build() {
137 spec.setMethod(METHOD_NAME);
138 spec.setNickname(METHOD_NAME + "-" + schemaNode.getQName().getLocalName());