aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/nbi/apis/servicecatalog/ToscaInfosProcessor.java
blob: 2153b84f3ddf78bd63c9dafca60a31ceeddf2462 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
 * Copyright (c) 2018 Orange
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package org.onap.nbi.apis.servicecatalog;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
import org.onap.sdc.toscaparser.api.NodeTemplate;
import org.onap.sdc.toscaparser.api.elements.Metadata;
import org.onap.sdc.toscaparser.api.parameters.Input;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.PropertyBuilder;
import io.swagger.util.Json;

@Service
public class ToscaInfosProcessor {

    @Autowired
    SdcClient sdcClient;

    @Autowired
    private ServiceSpecificationDBManager serviceSpecificationDBManager;

    final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); // jackson databind

    private static final Logger LOGGER = LoggerFactory.getLogger(ToscaInfosProcessor.class);

    public void buildResponseWithSdcToscaParser(Path path, Map serviceCatalogResponse) throws SdcToscaParserException {

        SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
        ISdcCsarHelper sdcCsarHelper = factory.getSdcCsarHelper(path.toFile().getAbsolutePath(), false);
        List<Input> inputs = sdcCsarHelper.getServiceInputs();
        if (inputs != null && inputs.size() > 0) {
            ArrayList serviceSpecCharacteristic = new ArrayList();
            for (Input input : inputs) {
                LinkedHashMap mapParameter = new LinkedHashMap();
                mapParameter.put("name", input.getName());
                mapParameter.put("description", input.getDescription());
                mapParameter.put("valueType", input.getType());
                mapParameter.put("@type", "ONAPserviceCharacteristic");
                mapParameter.put("required", input.isRequired());
                mapParameter.put("status", null);
                mapParameter.put("serviceSpecCharacteristicValue", null);
                // If this Input has a default value, then put it in serviceSpecCharacteristicValue
                if (input.getDefault() != null) {
                    List<LinkedHashMap> serviceSpecCharacteristicValues =
                            buildServiceSpecCharacteristicsValuesFromSdc(input);
                    mapParameter.put("serviceSpecCharacteristicValue", serviceSpecCharacteristicValues);
                }
                serviceSpecCharacteristic.add(mapParameter);
            }
            serviceCatalogResponse.put("serviceSpecCharacteristic", serviceSpecCharacteristic);
        }
        List<NodeTemplate> nodeTemplates = sdcCsarHelper.getServiceNodeTemplates();

        List<LinkedHashMap> resourceSpecifications =
                (List<LinkedHashMap>) serviceCatalogResponse.get("resourceSpecification");
        for (LinkedHashMap resourceSpecification : resourceSpecifications) {
            if (resourceSpecification.get("id") != null) {
                String id = (String) resourceSpecification.get("id");
                LOGGER.debug("get tosca infos for service id: {}", id);
                NodeTemplate nodeTemplate = null;
                for (NodeTemplate node : nodeTemplates) {
                    if (node.getMetaData().getValue("UUID").equals(id)) {
                        nodeTemplate = node;
                        break;
                    }
                }
                if (nodeTemplate == null)
                    continue;
                resourceSpecification.put("modelCustomizationId",
                        sdcCsarHelper.getNodeTemplateCustomizationUuid(nodeTemplate));
            }
        }
    }

    private List<LinkedHashMap> buildServiceSpecCharacteristicsValuesFromSdc(Input input) {

        List<LinkedHashMap> serviceSpecCharacteristicValues = new ArrayList<>();
        LinkedHashMap serviceSpecCharacteristicValue = new LinkedHashMap();

        serviceSpecCharacteristicValue.put("isDefault", true);
        serviceSpecCharacteristicValue.put("value", input.getDefault());
        serviceSpecCharacteristicValue.put("valueType", input.getType());
        serviceSpecCharacteristicValues.add(serviceSpecCharacteristicValue);

        return serviceSpecCharacteristicValues;
    }

    public void buildAndSaveResponseWithSdcToscaParser(Path path, Map serviceCatalogResponse)
            throws SdcToscaParserException {

        SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
        ISdcCsarHelper sdcCsarHelper = factory.getSdcCsarHelper(path.toFile().getAbsolutePath(), false);
        List<Input> inputs = sdcCsarHelper.getServiceInputs();

        Map<String, Model> definitions = new HashMap<String, Model>();
        Model model = new ModelImpl();

        if (inputs != null && inputs.size() > 0) {
            for (Input input : inputs) {
                Property property = null;
                if (input.getType().equals("list") || input.getType().equals("map"))
                    property = PropertyBuilder.build("array", null, null);
                else
                    property = PropertyBuilder.build(input.getType(), null, null);

                property.setDescription(input.getDescription());
                property.setRequired(input.isRequired());

                if (input.getDefault() != null) {
                    property.setDefault(input.getDefault().toString());
                }
                ((ModelImpl) model).addProperty(input.getName(), property);
            }
            definitions.put("ServiceCharacteristics", model);

        }

        String svcCharacteristicsJson = Json.pretty(definitions);
        serviceSpecificationDBManager.saveSpecificationInputSchema(svcCharacteristicsJson, serviceCatalogResponse);

        Metadata serviceMetadata = sdcCsarHelper.getServiceMetadata();
        String instantationType = serviceMetadata.getValue("instantiationType");
        serviceCatalogResponse.put("instantiationType", instantationType);

        LinkedHashMap inputSchemaRef = new LinkedHashMap();
        // use object to match examples in Specifications
        inputSchemaRef.put("valueType", "object");
        inputSchemaRef.put("@schemaLocation",
                "/serviceSpecification/" + serviceCatalogResponse.get("id") + "/specificationInputSchema");
        inputSchemaRef.put("@type", serviceCatalogResponse.get("name") + "_ServiceCharacteristic");

        LinkedHashMap serviceSpecCharacteristic = new LinkedHashMap();
        serviceSpecCharacteristic.put("name", serviceCatalogResponse.get("name") + "_ServiceCharacteristics");
        serviceSpecCharacteristic.put("description",
                "This object describes all the inputs needed from the client to interact with the "
                        + serviceCatalogResponse.get("name") + " Service Topology");
        serviceSpecCharacteristic.put("valueType", "object");
        serviceSpecCharacteristic.put("@type", "ONAPServiceCharacteristic");
        serviceSpecCharacteristic.put("@schemaLocation", "null");
        serviceSpecCharacteristic.put("serviceSpecCharacteristicValue", inputSchemaRef);

        serviceCatalogResponse.put("serviceSpecCharacteristic", serviceSpecCharacteristic);

        List<NodeTemplate> nodeTemplates = sdcCsarHelper.getServiceNodeTemplates();
        List<LinkedHashMap> resourceSpecifications =
                (List<LinkedHashMap>) serviceCatalogResponse.get("resourceSpecification");
        for (LinkedHashMap resourceSpecification : resourceSpecifications) {
            if (resourceSpecification.get("id") != null) {
                String id = (String) resourceSpecification.get("id");
                LOGGER.debug("get tosca infos for service id: {}", id);
                NodeTemplate nodeTemplate = null;
                for (NodeTemplate node : nodeTemplates) {
                    if (node.getMetaData().getValue("UUID").equals(id)) {
                        nodeTemplate = node;
                        break;
                    }
                }
                if (nodeTemplate == null)
                    continue;
                resourceSpecification.put("modelCustomizationId",
                        sdcCsarHelper.getNodeTemplateCustomizationUuid(nodeTemplate));
            }
        }
    }

}