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;
22 public final class OperationBuilder {
24 public static final String OPERATIONAL = "(operational)";
25 public static final String CONFIG = "(config)";
30 public static class Get {
32 protected Operation spec;
33 protected DataSchemaNode schemaNode;
34 private 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() {
59 public static class Put {
60 protected Operation spec;
61 protected String nodeName;
62 private final String METHOD_NAME = "PUT";
64 public Put(String nodeName, final String description) {
65 this.nodeName = nodeName;
66 spec = new Operation();
67 spec.setType(CONFIG + nodeName);
68 spec.setNotes(description);
71 public Put pathParams(List<Parameter> params) {
72 List<Parameter> parameters = new ArrayList<>(params);
73 Parameter payload = new Parameter();
74 payload.setParamType("body");
75 payload.setType(CONFIG + nodeName);
76 parameters.add(payload);
77 spec.setParameters(parameters);
81 public Operation build() {
82 spec.setMethod(METHOD_NAME);
83 spec.setNickname(METHOD_NAME + "-" + nodeName);
91 public static final class Post extends Put {
93 public static final String METHOD_NAME = "POST";
94 private final DataNodeContainer dataNodeContainer;
96 public Post(final String nodeName, final String description, final DataNodeContainer dataNodeContainer) {
97 super(nodeName, description);
98 this.dataNodeContainer = dataNodeContainer;
99 spec.setType(CONFIG + nodeName + METHOD_NAME);
103 public Operation build() {
104 spec.setMethod(METHOD_NAME);
105 spec.setNickname(METHOD_NAME + "-" + nodeName);
110 public Put pathParams(List<Parameter> params) {
111 List<Parameter> parameters = new ArrayList<>(params);
112 for (DataSchemaNode node : dataNodeContainer.getChildNodes()) {
113 if (node instanceof ListSchemaNode || node instanceof ContainerSchemaNode) {
114 Parameter payload = new Parameter();
115 payload.setParamType("body");
116 payload.setType(CONFIG + node.getQName().getLocalName());
117 payload.setName("**"+CONFIG + node.getQName().getLocalName());
118 parameters.add(payload);
121 spec.setParameters(parameters);
126 public Post summary(final String summary) {
127 spec.setSummary(summary);
135 public static final class Delete extends Get {
136 private final String METHOD_NAME = "DELETE";
138 public Delete(DataSchemaNode node) {
143 public Operation build() {
144 spec.setMethod(METHOD_NAME);
145 spec.setNickname(METHOD_NAME + "-" + schemaNode.getQName().getLocalName());