Merge "Integrate sdc notification"
[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.*;
18
19 import io.swagger.util.Json;
20 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
21 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
22 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
23 import org.onap.sdc.toscaparser.api.NodeTemplate;
24 import org.onap.sdc.toscaparser.api.parameters.Input;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.stereotype.Service;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
31 import io.swagger.models.Model;
32 import io.swagger.models.ModelImpl;
33 import io.swagger.models.properties.Property;
34 import io.swagger.models.properties.PropertyBuilder;
35
36 @Service
37 public class ToscaInfosProcessor {
38
39   @Autowired
40   SdcClient sdcClient;
41
42   @Autowired
43   private ServiceSpecificationDBManager serviceSpecificationDBManager;
44
45   final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); // jackson databind
46
47   private static final Logger LOGGER = LoggerFactory.getLogger(ToscaInfosProcessor.class);
48
49   public void buildResponseWithSdcToscaParser(Path path, Map serviceCatalogResponse)
50           throws SdcToscaParserException {
51
52     SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
53     ISdcCsarHelper sdcCsarHelper = factory.getSdcCsarHelper(path.toFile().getAbsolutePath(), false);
54     List<Input> inputs = sdcCsarHelper.getServiceInputs();
55     if (inputs != null && inputs.size() > 0) {
56       ArrayList serviceSpecCharacteristic = new ArrayList();
57       for (Input input : inputs) {
58         LinkedHashMap mapParameter = new LinkedHashMap();
59         mapParameter.put("name", input.getName());
60         mapParameter.put("description", input.getDescription());
61         mapParameter.put("valueType", input.getType());
62         mapParameter.put("@type", "ONAPserviceCharacteristic");
63         mapParameter.put("required", input.isRequired());
64         mapParameter.put("status", null);
65         mapParameter.put("serviceSpecCharacteristicValue", null);
66         // If this Input has a default value, then put it in serviceSpecCharacteristicValue
67         if (input.getDefault() != null) {
68           List<LinkedHashMap> serviceSpecCharacteristicValues =
69                   buildServiceSpecCharacteristicsValuesFromSdc(input);
70           mapParameter.put("serviceSpecCharacteristicValue", serviceSpecCharacteristicValues);
71         }
72         serviceSpecCharacteristic.add(mapParameter);
73       }
74       serviceCatalogResponse.put("serviceSpecCharacteristic", serviceSpecCharacteristic);
75     }
76     List<NodeTemplate> nodeTemplates = sdcCsarHelper.getServiceNodeTemplates();
77
78     List<LinkedHashMap> resourceSpecifications =
79             (List<LinkedHashMap>) serviceCatalogResponse.get("resourceSpecification");
80     for (LinkedHashMap resourceSpecification : resourceSpecifications) {
81       if (resourceSpecification.get("id") != null) {
82         String id = (String) resourceSpecification.get("id");
83         LOGGER.debug("get tosca infos for service id: {}", id);
84         NodeTemplate nodeTemplate = null;
85         for (NodeTemplate node : nodeTemplates) {
86           if (node.getMetaData().getValue("UUID").equals(id)) {
87             nodeTemplate = node;
88             break;
89           }
90         }
91         if (nodeTemplate == null)
92           continue;
93         resourceSpecification.put("modelCustomizationId",
94                 sdcCsarHelper.getNodeTemplateCustomizationUuid(nodeTemplate));
95       }
96     }
97   }
98
99   private List<LinkedHashMap> buildServiceSpecCharacteristicsValuesFromSdc(Input input) {
100
101     List<LinkedHashMap> serviceSpecCharacteristicValues = new ArrayList<>();
102     LinkedHashMap serviceSpecCharacteristicValue = new LinkedHashMap();
103
104     serviceSpecCharacteristicValue.put("isDefault", true);
105     serviceSpecCharacteristicValue.put("value", input.getDefault());
106     serviceSpecCharacteristicValue.put("valueType", input.getType());
107     serviceSpecCharacteristicValues.add(serviceSpecCharacteristicValue);
108
109     return serviceSpecCharacteristicValues;
110   }
111
112   public void buildAndSaveResponseWithSdcToscaParser(Path path, Map serviceCatalogResponse) throws SdcToscaParserException {
113
114     SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
115     ISdcCsarHelper sdcCsarHelper = factory.getSdcCsarHelper(path.toFile().getAbsolutePath(), false);
116     List<Input> inputs = sdcCsarHelper.getServiceInputs();
117
118     Map<String, Model> definitions = new HashMap<String,Model>();
119     Model model = new ModelImpl();
120
121     if (inputs != null && inputs.size() > 0) {
122       for (Input input : inputs) {
123         Property property = PropertyBuilder.build(input.getType(), null, null);
124         property.setDescription(input.getDescription());
125         property.setRequired(input.isRequired());
126
127         if (input.getDefault() != null) {
128           property.setDefault(input.getDefault().toString());
129         }
130         ((ModelImpl) model).addProperty(input.getName(), property);
131       }
132       definitions.put("ServiceCharacteristics", model);
133
134     }
135
136     String svcCharacteristicsJson = Json.pretty(definitions);
137     serviceSpecificationDBManager.saveSpecificationInputSchema(svcCharacteristicsJson,serviceCatalogResponse);
138
139     LinkedHashMap inputSchemaRef = new LinkedHashMap();
140     inputSchemaRef.put("valueType","Object");
141     inputSchemaRef.put("@schemaLocation","/serviceSpecification/"+serviceCatalogResponse.get("id")+"/specificationInputSchema");
142     inputSchemaRef.put("@type",serviceCatalogResponse.get("name") + "_ServiceCharacteristic");
143
144     LinkedHashMap serviceSpecCharacteristic = new LinkedHashMap();
145     serviceSpecCharacteristic.put("name",serviceCatalogResponse.get("name") + "_ServiceCharacteristics");
146     serviceSpecCharacteristic.put("description","This object describes all the inputs needed from the client to interact with the " + serviceCatalogResponse.get("name") + " Service Topology");
147     serviceSpecCharacteristic.put("valueType", "Object");
148     serviceSpecCharacteristic.put("@type","ONAPServiceCharacteristic");
149     serviceSpecCharacteristic.put("@schemaLocation", "null");
150     serviceSpecCharacteristic.put("serviceSpecCharacteristicValue",inputSchemaRef);
151
152     serviceCatalogResponse.put("serviceSpecCharacteristic",serviceSpecCharacteristic);
153
154     List<NodeTemplate> nodeTemplates = sdcCsarHelper.getServiceNodeTemplates();
155     List<LinkedHashMap> resourceSpecifications =
156             (List<LinkedHashMap>) serviceCatalogResponse.get("resourceSpecification");
157     for (LinkedHashMap resourceSpecification : resourceSpecifications) {
158       if (resourceSpecification.get("id") != null) {
159         String id = (String) resourceSpecification.get("id");
160         LOGGER.debug("get tosca infos for service id: {}", id);
161         NodeTemplate nodeTemplate = null;
162         for (NodeTemplate node : nodeTemplates) {
163           if (node.getMetaData().getValue("UUID").equals(id)) {
164             nodeTemplate = node;
165             break;
166           }
167         }
168         if (nodeTemplate == null)
169           continue;
170         resourceSpecification.put("modelCustomizationId", sdcCsarHelper.getNodeTemplateCustomizationUuid(nodeTemplate));
171       }
172     }
173   }
174
175 }