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