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.controller.sal.rest.doc.model.builder;
10 import java.util.ArrayList;
11 import java.util.List;
12 import org.opendaylight.controller.sal.rest.doc.swagger.Operation;
13 import org.opendaylight.controller.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 class Get {
26 protected Operation spec;
27 protected DataSchemaNode schemaNode;
28 private static final String METHOD_NAME = "GET";
30 public Get(DataSchemaNode node, boolean isConfig) {
31 this.schemaNode = node;
32 spec = new Operation();
33 spec.setMethod(METHOD_NAME);
34 spec.setNickname(METHOD_NAME + "-" + node.getQName().getLocalName());
35 spec.setType((isConfig ? CONFIG : OPERATIONAL) + node.getQName().getLocalName());
36 spec.setNotes(node.getDescription());
39 public Get pathParams(List<Parameter> params) {
40 List<Parameter> pathParameters = new ArrayList<>(params);
41 spec.setParameters(pathParameters);
45 public Operation build() {
50 public static class Put {
51 protected Operation spec;
52 protected String nodeName;
53 private static final String METHOD_NAME = "PUT";
55 public Put(String nodeName, final String description) {
56 this.nodeName = nodeName;
57 spec = new Operation();
58 spec.setType(CONFIG + nodeName);
59 spec.setNotes(description);
62 public Put pathParams(List<Parameter> params) {
63 List<Parameter> parameters = new ArrayList<>(params);
64 Parameter payload = new Parameter();
65 payload.setParamType("body");
66 payload.setType(CONFIG + nodeName);
67 parameters.add(payload);
68 spec.setParameters(parameters);
72 public Operation build() {
73 spec.setMethod(METHOD_NAME);
74 spec.setNickname(METHOD_NAME + "-" + nodeName);
79 public static final class Post extends Put {
81 public static final String METHOD_NAME = "POST";
82 private final DataNodeContainer dataNodeContainer;
84 public Post(final String nodeName, final String description, final DataNodeContainer dataNodeContainer) {
85 super(nodeName, description);
86 this.dataNodeContainer = dataNodeContainer;
87 spec.setType(CONFIG + nodeName + METHOD_NAME);
91 public Operation build() {
92 spec.setMethod(METHOD_NAME);
93 spec.setNickname(METHOD_NAME + "-" + nodeName);
98 public Put pathParams(List<Parameter> params) {
99 List<Parameter> parameters = new ArrayList<>(params);
100 for (DataSchemaNode node : dataNodeContainer.getChildNodes()) {
101 if (node instanceof ListSchemaNode || node instanceof ContainerSchemaNode) {
102 Parameter payload = new Parameter();
103 payload.setParamType("body");
104 payload.setType(CONFIG + node.getQName().getLocalName());
105 payload.setName("**"+CONFIG + node.getQName().getLocalName());
106 parameters.add(payload);
109 spec.setParameters(parameters);
114 public Post summary(final String summary) {
115 spec.setSummary(summary);
120 public static final class Delete extends Get {
121 private static final String METHOD_NAME = "DELETE";
123 public Delete(DataSchemaNode node) {
128 public Operation build() {
129 spec.setMethod(METHOD_NAME);
130 spec.setNickname(METHOD_NAME + "-" + schemaNode.getQName().getLocalName());