Use SDC Tosca Jar to extract and parse SDC CSAR
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / servicecatalog / ToscaInfosProcessor.java
1 /**
2  * Copyright (c) 2018 Orange
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package org.onap.nbi.apis.servicecatalog;
15
16 import java.nio.file.Path;
17 import java.util.ArrayList;
18 import java.util.LinkedHashMap;
19 import java.util.List;
20 import java.util.Map;
21 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
22 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
23 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
24 import org.onap.sdc.toscaparser.api.NodeTemplate;
25 import org.onap.sdc.toscaparser.api.parameters.Input;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.stereotype.Service;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
32
33 @Service
34 public class ToscaInfosProcessor {
35
36   @Autowired
37   SdcClient sdcClient;
38
39   final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); // jackson databind
40
41   private static final Logger LOGGER = LoggerFactory.getLogger(ToscaInfosProcessor.class);
42
43
44
45   public void buildResponseWithSdcToscaParser(Path path, Map serviceCatalogResponse)
46       throws SdcToscaParserException {
47
48     SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
49     ISdcCsarHelper sdcCsarHelper = factory.getSdcCsarHelper(path.toFile().getAbsolutePath(), false);
50     List<Input> inputs = sdcCsarHelper.getServiceInputs();
51     if (inputs != null && inputs.size() > 0) {
52       ArrayList serviceSpecCharacteristic = new ArrayList();
53       for (Input input : inputs) {
54         LinkedHashMap mapParameter = new LinkedHashMap();
55         mapParameter.put("name", input.getName());
56         mapParameter.put("description", input.getDescription());
57         mapParameter.put("valueType", input.getType());
58         mapParameter.put("@type", "ONAPserviceCharacteristic");
59         mapParameter.put("required", input.isRequired());
60         mapParameter.put("status", null);
61         mapParameter.put("serviceSpecCharacteristicValue", null);
62         // If this Input has a default value, then put it in serviceSpecCharacteristicValue
63         if (input.getDefault() != null) {
64           List<LinkedHashMap> serviceSpecCharacteristicValues =
65               buildServiceSpecCharacteristicsValuesFromSdc(input);
66           mapParameter.put("serviceSpecCharacteristicValue", serviceSpecCharacteristicValues);
67         }
68         serviceSpecCharacteristic.add(mapParameter);
69       }
70       serviceCatalogResponse.put("serviceSpecCharacteristic", serviceSpecCharacteristic);
71     }
72     List<NodeTemplate> nodeTemplates = sdcCsarHelper.getServiceNodeTemplates();
73
74     List<LinkedHashMap> resourceSpecifications =
75         (List<LinkedHashMap>) serviceCatalogResponse.get("resourceSpecification");
76     for (LinkedHashMap resourceSpecification : resourceSpecifications) {
77       if (resourceSpecification.get("id") != null) {
78         String id = (String) resourceSpecification.get("id");
79         LOGGER.debug("get tosca infos for service id: {}", id);
80         NodeTemplate nodeTemplate = null;
81         for (NodeTemplate node : nodeTemplates) {
82           if (node.getMetaData().getValue("UUID").equals(id)) {
83             nodeTemplate = node;
84             break;
85           }
86         }
87         if (nodeTemplate == null)
88           continue;
89         resourceSpecification.put("modelCustomizationId",
90             sdcCsarHelper.getNodeTemplateCustomizationUuid(nodeTemplate));
91       }
92     }
93   }
94
95
96   private List<LinkedHashMap> buildServiceSpecCharacteristicsValuesFromSdc(Input input) {
97
98     List<LinkedHashMap> serviceSpecCharacteristicValues = new ArrayList<>();
99     LinkedHashMap serviceSpecCharacteristicValue = new LinkedHashMap();
100
101     serviceSpecCharacteristicValue.put("isDefault", true);
102     serviceSpecCharacteristicValue.put("value", input.getDefault());
103     serviceSpecCharacteristicValue.put("valueType", input.getType());
104     serviceSpecCharacteristicValues.add(serviceSpecCharacteristicValue);
105
106     return serviceSpecCharacteristicValues;
107   }
108
109 }