Merge "Converted Inventory Manager to use Transaction Chaining"
[controller.git] / opendaylight / md-sal / sal-rest-docgen / src / main / java / org / opendaylight / controller / sal / rest / doc / model / builder / OperationBuilder.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.rest.doc.model.builder;
9
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;
18
19 /**
20  *
21  */
22 public final class OperationBuilder {
23
24     public static final String OPERATIONAL = "(operational)";
25     public static final String CONFIG = "(config)";
26
27     /**
28    *
29    */
30     public static class Get {
31
32         protected Operation spec;
33         protected DataSchemaNode schemaNode;
34         private final String METHOD_NAME = "GET";
35
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());
43         }
44
45         public Get pathParams(List<Parameter> params) {
46             List<Parameter> pathParameters = new ArrayList<>(params);
47             spec.setParameters(pathParameters);
48             return this;
49         }
50
51         public Operation build() {
52             return spec;
53         }
54     }
55
56     /**
57    *
58    */
59     public static class Put {
60         protected Operation spec;
61         protected String nodeName;
62         private final String METHOD_NAME = "PUT";
63
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);
69         }
70
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);
78             return this;
79         }
80
81         public Operation build() {
82             spec.setMethod(METHOD_NAME);
83             spec.setNickname(METHOD_NAME + "-" + nodeName);
84             return spec;
85         }
86     }
87
88     /**
89    *
90    */
91     public static final class Post extends Put {
92
93         public static final String METHOD_NAME = "POST";
94         private final DataNodeContainer dataNodeContainer;
95
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);
100         }
101
102         @Override
103         public Operation build() {
104             spec.setMethod(METHOD_NAME);
105             spec.setNickname(METHOD_NAME + "-" + nodeName);
106             return spec;
107         }
108
109         @Override
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);
119                 }
120             }
121             spec.setParameters(parameters);
122             return this;
123
124         }
125
126         public Post summary(final String summary) {
127             spec.setSummary(summary);
128             return this;
129         }
130     }
131
132     /**
133    *
134    */
135     public static final class Delete extends Get {
136         private final String METHOD_NAME = "DELETE";
137
138         public Delete(DataSchemaNode node) {
139             super(node, false);
140         }
141
142         @Override
143         public Operation build() {
144             spec.setMethod(METHOD_NAME);
145             spec.setNickname(METHOD_NAME + "-" + schemaNode.getQName().getLocalName());
146             spec.setType(null);
147             return spec;
148         }
149     }
150 }