2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2018 IBM.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.onap.ccsdk.cds.controllerblueprints.service.common;
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;
34 * SwaggerGenerator.java Purpose: Provide Service to generate service template input schema definition and Sample Json
37 * @author Brinda Santh
41 public class SwaggerGenerator {
43 private ServiceTemplate serviceTemplate;
44 public static final String INPUTS="inputs";
47 * This is a SwaggerGenerator constructor
49 public SwaggerGenerator(ServiceTemplate serviceTemplate) {
50 this.serviceTemplate = serviceTemplate;
54 * This is a generateSwagger
58 public String generateSwagger() {
60 Swagger swagger = new Swagger().info(getInfo());
62 swagger.setPaths(getPaths());
63 swagger.setDefinitions(getDefinition());
66 return swagger.toString();
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));
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"));
92 post.setParameters(parameters);
94 Map<String, Response> responses = new HashMap<>();
95 Response response = new Response().description("Success");
96 responses.put("200", response);
98 Response failureResponse = new Response().description("Failure");
99 responses.put("400", failureResponse);
100 post.setResponses(responses);
103 paths.put("/operations/config-selfservice-api:configure", path);
107 private Map<String, Model> getDefinition() {
108 Map<String, Model> models = new HashMap<>();
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);
116 models.put(INPUTS, inputmodel);
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())) {
124 dataType.getProperties().forEach((propertyName, property) -> {
125 Property defProperty = getPropery(propertyName, property);
126 model.addProperty(propertyName, defProperty);
129 models.put(name, model);
136 private Property getPropery(String name, PropertyDefinition propertyDefinition) {
137 Property defProperty = null;
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;
155 defProperty = new StringProperty();
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);
165 Property innerType = new StringProperty();
166 arrayProperty.setItems(innerType);
168 defProperty = arrayProperty;
172 defProperty = new RefProperty("#/definitions/" + propertyDefinition.getType());
174 defProperty.setName(name);
175 if (propertyDefinition.getDefaultValue() != null) {
176 defProperty.setDefault(String.valueOf(propertyDefinition.getDefaultValue()));
179 defProperty.setRequired(BooleanUtils.isTrue(propertyDefinition.getRequired()));
180 defProperty.setDescription(propertyDefinition.getDescription());