Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / controllerblueprints / modules / service / src / main / java / org / onap / ccsdk / cds / controllerblueprints / service / SchemaGeneratorService.java
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.controllerblueprints.service;
18
19 import com.att.eelf.configuration.EELFLogger;
20 import com.att.eelf.configuration.EELFManager;
21 import com.google.common.base.Preconditions;
22 import org.apache.commons.collections.MapUtils;
23 import org.apache.commons.lang3.StringUtils;
24 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException;
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType;
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.ServiceTemplate;
27 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils;
28 import org.onap.ccsdk.cds.controllerblueprints.service.common.SwaggerGenerator;
29
30 import java.util.HashMap;
31 import java.util.Map;
32
33 /**
34  * SchemaGeneratorService.java Purpose: Provide Service to generate service template input schema definition and Sample
35  * Json generation.
36  *
37  * @author Brinda Santh
38  * @version 1.0
39  */
40 @Deprecated
41 public class SchemaGeneratorService {
42     private static EELFLogger log = EELFManager.getInstance().getLogger(SchemaGeneratorService.class);
43
44     private Map<String, DataType> dataTypes;
45
46     /**
47      * This is a SchemaGeneratorService constructor
48      */
49     public SchemaGeneratorService() {
50         dataTypes = new HashMap<>();
51     }
52
53     /**
54      * This is a generateSchema
55      *
56      * @param serviceTemplateContent service template content
57      * @return String
58      * @throws BluePrintException Blueprint Exception
59      */
60     public String generateSchema(String serviceTemplateContent) throws BluePrintException {
61         if (StringUtils.isNotBlank(serviceTemplateContent)) {
62             ServiceTemplate serviceTemplate = JacksonUtils.Companion.readValue(serviceTemplateContent,
63                     ServiceTemplate.class);
64             return generateSchema(serviceTemplate);
65         } else {
66             throw new BluePrintException(
67                     "Service Template Content is  (" + serviceTemplateContent + ") not Defined.");
68         }
69     }
70
71     /**
72      * This is a generateSchema
73      *
74      * @param serviceTemplate service template content
75      * @return String
76      * @throws BluePrintException Blueprint Exception
77      */
78     public String generateSchema(ServiceTemplate serviceTemplate) throws BluePrintException {
79         String schemaContent = null;
80         Preconditions.checkNotNull(serviceTemplate, "Service Template is not defined.");
81         try {
82             if (serviceTemplate.getTopologyTemplate() != null
83                     && serviceTemplate.getTopologyTemplate().getInputs() != null) {
84                 SwaggerGenerator swaggerGenerator = new SwaggerGenerator(serviceTemplate);
85                 schemaContent = swaggerGenerator.generateSwagger();
86             }
87         } catch (Exception e) {
88             throw new BluePrintException(e.getMessage(), e);
89         }
90
91         return schemaContent;
92     }
93
94     private void manageServiceTemplateActions(ServiceTemplate serviceTemplate, String actionName) {
95         if (serviceTemplate != null && serviceTemplate.getTopologyTemplate() != null
96                 && StringUtils.isNotBlank(actionName)) {
97
98             if (MapUtils.isNotEmpty(serviceTemplate.getTopologyTemplate().getInputs())) {
99
100                 serviceTemplate.getTopologyTemplate().getInputs().entrySet().removeIf(entity -> {
101                     String keyName = entity.getKey();
102                     String replacedAction = actionName.replace("-action", "-request");
103                     log.debug("Key name : " + keyName + ", actionName "
104                             + actionName + ", replacedAction :" + replacedAction);
105                     if (keyName.endsWith("-request") && !keyName.equals(replacedAction)) {
106                         log.info("deleting input property {} ", keyName);
107                         return true;
108                     }
109                     return false;
110                 });
111             }
112
113         }
114     }
115
116 }