Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / controllerblueprints / modules / service / src / main / java / org / onap / ccsdk / cds / controllerblueprints / service / common / SwaggerGenerator.java
1 /*
2  *  Copyright © 2017-2018 AT&T Intellectual Property.
3  *  Modifications Copyright © 2018 IBM.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17
18 package org.onap.ccsdk.cds.controllerblueprints.service.common;
19
20 import io.swagger.models.*;
21 import io.swagger.models.parameters.BodyParameter;
22 import io.swagger.models.parameters.Parameter;
23 import io.swagger.models.properties.*;
24 import org.apache.commons.collections.MapUtils;
25 import org.apache.commons.lang3.BooleanUtils;
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants;
27 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes;
28 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition;
29 import org.onap.ccsdk.cds.controllerblueprints.core.data.ServiceTemplate;
30
31 import java.util.*;
32
33 /**
34  * SwaggerGenerator.java Purpose: Provide Service to generate service template input schema definition and Sample Json
35  * generation.
36  *
37  * @author Brinda Santh
38  * @version 1.0
39  */
40 @Deprecated
41 public class SwaggerGenerator {
42
43     private ServiceTemplate serviceTemplate;
44     public static final String INPUTS="inputs";
45
46     /**
47      * This is a SwaggerGenerator constructor
48      */
49     public SwaggerGenerator(ServiceTemplate serviceTemplate) {
50         this.serviceTemplate = serviceTemplate;
51     }
52
53     /**
54      * This is a generateSwagger
55      *
56      * @return String
57      */
58     public String generateSwagger() {
59         
60         Swagger swagger = new Swagger().info(getInfo());
61
62         swagger.setPaths(getPaths());
63         swagger.setDefinitions(getDefinition());
64
65
66         return swagger.toString();
67     }
68
69     private Info getInfo() {
70         Info info = new Info();
71         Contact contact = new Contact();
72         contact.setName(serviceTemplate.getMetadata().get(BluePrintConstants.METADATA_TEMPLATE_AUTHOR));
73         info.setContact(contact);
74         info.setTitle(serviceTemplate.getMetadata().get(BluePrintConstants.METADATA_TEMPLATE_NAME));
75         info.setDescription(serviceTemplate.getDescription());
76         info.setVersion(serviceTemplate.getMetadata().get(BluePrintConstants.METADATA_TEMPLATE_VERSION));
77         return info;
78     }
79
80     private Map<String, Path> getPaths() {
81         Map<String, Path> paths = new HashMap<>();
82         Path path = new Path();
83         Operation post = new Operation();
84         post.setOperationId("configure");
85         post.setConsumes(Arrays.asList("application/json", "application/xml"));
86         post.setProduces(Arrays.asList("application/json", "application/xml"));
87         List<Parameter> parameters = new ArrayList<>();
88         Parameter in = new BodyParameter().schema(new RefModel("#/definitions/inputs"));
89         in.setRequired(true);
90         in.setName(INPUTS);
91         parameters.add(in);
92         post.setParameters(parameters);
93
94         Map<String, Response> responses = new HashMap<>();
95         Response response = new Response().description("Success");
96         responses.put("200", response);
97
98         Response failureResponse = new Response().description("Failure");
99         responses.put("400", failureResponse);
100         post.setResponses(responses);
101
102         path.setPost(post);
103         paths.put("/operations/config-selfservice-api:configure", path);
104         return paths;
105     }
106
107     private Map<String, Model> getDefinition() {
108         Map<String, Model> models = new HashMap<>();
109
110         ModelImpl inputmodel = new ModelImpl();
111         inputmodel.setTitle(INPUTS);
112         serviceTemplate.getTopologyTemplate().getInputs().forEach((propertyName, property) -> {
113             Property defProperty = getPropery(propertyName, property);
114             inputmodel.property(propertyName, defProperty);
115         });
116         models.put(INPUTS, inputmodel);
117
118         if (MapUtils.isNotEmpty(serviceTemplate.getDataTypes())) {
119             serviceTemplate.getDataTypes().forEach((name, dataType) -> {
120                 ModelImpl model = new ModelImpl();
121                 model.setDescription(dataType.getDescription());
122                 if (dataType != null && MapUtils.isNotEmpty(dataType.getProperties())) {
123
124                     dataType.getProperties().forEach((propertyName, property) -> {
125                         Property defProperty = getPropery(propertyName, property);
126                         model.addProperty(propertyName, defProperty);
127                     });
128                 }
129                 models.put(name, model);
130             });
131         }
132         return models;
133
134     }
135
136     private Property getPropery(String name, PropertyDefinition propertyDefinition) {
137         Property defProperty = null;
138
139         if (BluePrintTypes.validPrimitiveTypes().contains(propertyDefinition.getType())) {
140             if (BluePrintConstants.DATA_TYPE_BOOLEAN.equals(propertyDefinition.getType())) {
141                 defProperty = new BooleanProperty();
142             } else if (BluePrintConstants.DATA_TYPE_INTEGER.equals(propertyDefinition.getType())) {
143                 StringProperty stringProperty = new StringProperty();
144                 stringProperty.setType("integer");
145                 defProperty = stringProperty;
146             } else if (BluePrintConstants.DATA_TYPE_FLOAT.equals(propertyDefinition.getType())) {
147                 StringProperty stringProperty = new StringProperty();
148                 stringProperty.setFormat("float");
149                 defProperty = stringProperty;
150             } else if (BluePrintConstants.DATA_TYPE_TIMESTAMP.equals(propertyDefinition.getType())) {
151                 DateTimeProperty dateTimeProperty = new DateTimeProperty();
152                 dateTimeProperty.setFormat("date-time");
153                 defProperty = dateTimeProperty;
154             } else {
155                 defProperty = new StringProperty();
156             }
157         } else if (BluePrintTypes.validCollectionTypes().contains(propertyDefinition.getType())) {
158             ArrayProperty arrayProperty = new ArrayProperty();
159             if (propertyDefinition.getEntrySchema() != null) {
160                 String entrySchema = propertyDefinition.getEntrySchema().getType();
161                 if (!BluePrintTypes.validPrimitiveTypes().contains(entrySchema)) {
162                     Property innerType = new RefProperty("#/definitions/" + entrySchema);
163                     arrayProperty.setItems(innerType);
164                 } else {
165                     Property innerType = new StringProperty();
166                     arrayProperty.setItems(innerType);
167                 }
168                 defProperty = arrayProperty;
169             }
170
171         } else {
172             defProperty = new RefProperty("#/definitions/" + propertyDefinition.getType());
173         }
174         defProperty.setName(name);
175         if (propertyDefinition.getDefaultValue() != null) {
176             defProperty.setDefault(String.valueOf(propertyDefinition.getDefaultValue()));
177         }
178
179         defProperty.setRequired(BooleanUtils.isTrue(propertyDefinition.getRequired()));
180         defProperty.setDescription(propertyDefinition.getDescription());
181         return defProperty;
182     }
183
184
185 }