2  * Copyright © 2017-2018 AT&T Intellectual Property.
\r 
   4  * Licensed under the Apache License, Version 2.0 (the "License");
\r 
   5  * you may not use this file except in compliance with the License.
\r 
   6  * You may obtain a copy of the License at
\r 
   8  *     http://www.apache.org/licenses/LICENSE-2.0
\r 
  10  * Unless required by applicable law or agreed to in writing, software
\r 
  11  * distributed under the License is distributed on an "AS IS" BASIS,
\r 
  12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r 
  13  * See the License for the specific language governing permissions and
\r 
  14  * limitations under the License.
\r 
  17 package org.onap.ccsdk.apps.controllerblueprints.service;
\r 
  19 import com.att.eelf.configuration.EELFLogger;
\r 
  20 import com.att.eelf.configuration.EELFManager;
\r 
  21 import com.google.common.base.Preconditions;
\r 
  22 import org.apache.commons.collections.MapUtils;
\r 
  23 import org.apache.commons.lang3.StringUtils;
\r 
  24 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;
\r 
  25 import org.onap.ccsdk.apps.controllerblueprints.core.data.DataType;
\r 
  26 import org.onap.ccsdk.apps.controllerblueprints.core.data.ServiceTemplate;
\r 
  27 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils;
\r 
  28 import org.onap.ccsdk.apps.controllerblueprints.service.common.SwaggerGenerator;
\r 
  30 import java.util.HashMap;
\r 
  31 import java.util.Map;
\r 
  34  * SchemaGeneratorService.java Purpose: Provide Service to generate service template input schema definition and Sample
\r 
  37  * @author Brinda Santh
\r 
  41 public class SchemaGeneratorService {
\r 
  42     private static EELFLogger log = EELFManager.getInstance().getLogger(SchemaGeneratorService.class);
\r 
  44     private Map<String, DataType> dataTypes;
\r 
  47      * This is a SchemaGeneratorService constructor
\r 
  49     public SchemaGeneratorService() {
\r 
  50         dataTypes = new HashMap<>();
\r 
  54      * This is a generateSchema
\r 
  56      * @param serviceTemplateContent service template content
\r 
  58      * @throws BluePrintException Blueprint Exception
\r 
  60     public String generateSchema(String serviceTemplateContent) throws BluePrintException {
\r 
  61         if (StringUtils.isNotBlank(serviceTemplateContent)) {
\r 
  62             ServiceTemplate serviceTemplate = JacksonUtils.readValue(serviceTemplateContent,
\r 
  63                     ServiceTemplate.class);
\r 
  64             return generateSchema(serviceTemplate);
\r 
  66             throw new BluePrintException(
\r 
  67                     "Service Template Content is  (" + serviceTemplateContent + ") not Defined.");
\r 
  72      * This is a generateSchema
\r 
  74      * @param serviceTemplate service template content
\r 
  76      * @throws BluePrintException Blueprint Exception
\r 
  78     public String generateSchema(ServiceTemplate serviceTemplate) throws BluePrintException {
\r 
  79         String schemaContent = null;
\r 
  80         Preconditions.checkNotNull(serviceTemplate, "Service Template is not defined.");
\r 
  82             if (serviceTemplate.getTopologyTemplate() != null
\r 
  83                     && serviceTemplate.getTopologyTemplate().getInputs() != null) {
\r 
  84                 SwaggerGenerator swaggerGenerator = new SwaggerGenerator(serviceTemplate);
\r 
  85                 schemaContent = swaggerGenerator.generateSwagger();
\r 
  87         } catch (Exception e) {
\r 
  88             throw new BluePrintException(e.getMessage(), e);
\r 
  91         return schemaContent;
\r 
  94     private void manageServiceTemplateActions(ServiceTemplate serviceTemplate, String actionName) {
\r 
  95         if (serviceTemplate != null && serviceTemplate.getTopologyTemplate() != null
\r 
  96                 && StringUtils.isNotBlank(actionName)) {
\r 
  98             if (MapUtils.isNotEmpty(serviceTemplate.getTopologyTemplate().getInputs())) {
\r 
 100                 serviceTemplate.getTopologyTemplate().getInputs().entrySet().removeIf(entity -> {
\r 
 101                     String keyName = entity.getKey();
\r 
 102                     String replacedAction = actionName.replace("-action", "-request");
\r 
 103                     log.debug("Key name : " + keyName + ", actionName "
\r 
 104                             + actionName + ", replacedAction :" + replacedAction);
\r 
 105                     if (keyName.endsWith("-request") && !keyName.equals(replacedAction)) {
\r 
 106                         log.info("deleting input property {} ", keyName);
\r