2 * ============LICENSE_START=======================================================
3 * Modification Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.openecomp.core.impl.services;
23 import static org.openecomp.core.converter.datatypes.Constants.POLICIES;
24 import static org.openecomp.core.converter.datatypes.Constants.inputs;
25 import static org.openecomp.core.converter.datatypes.Constants.metadata;
26 import static org.openecomp.core.converter.datatypes.Constants.nodeTemplates;
27 import static org.openecomp.core.converter.datatypes.Constants.nodeTypes;
28 import static org.openecomp.core.converter.datatypes.Constants.outputs;
29 import static org.openecomp.core.converter.datatypes.Constants.substitutionMappings;
30 import static org.openecomp.core.converter.datatypes.Constants.topologyTemplate;
31 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DATA_TYPES;
32 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.IMPORTS;
33 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.List;
39 import java.util.Objects;
40 import org.onap.sdc.tosca.services.YamlUtil;
41 import org.openecomp.core.converter.ServiceTemplateReaderService;
43 public class ServiceTemplateReaderServiceImpl implements ServiceTemplateReaderService {
45 private final Map<String, Object> readServiceTemplate;
47 public ServiceTemplateReaderServiceImpl(byte[] serviceTemplateContent) {
48 this.readServiceTemplate = readServiceTemplate(serviceTemplateContent);
52 public Map<String, Object> readServiceTemplate(byte[] serviceTemplateContent) {
53 return new YamlUtil().yamlToObject(new String(serviceTemplateContent), Map.class);
57 public List<Object> getImports() {
58 final String importsElementName = IMPORTS.getElementName();
59 return Objects.isNull(this.readServiceTemplate.get(importsElementName)) ? new ArrayList<>()
60 : (List<Object>) this.readServiceTemplate.get(importsElementName);
64 public Map<String, Object> getPolicies() {
65 Map<String, Object> policiesAsMap = new HashMap<>();
66 if (!Objects.isNull(this.getTopologyTemplate()) && !Objects.isNull(
67 ((Map<String, Object>) this.getTopologyTemplate()).get(POLICIES))) {
68 policiesAsMap = (Map<String, Object>) ((Map<String, Object>) this.getTopologyTemplate()).get(POLICIES);
74 public Object getMetadata() {
75 return this.readServiceTemplate.get(metadata);
79 public Object getToscaVersion() {
80 return this.readServiceTemplate.get(TOSCA_VERSION.getElementName());
84 public Map<String, Object> getNodeTypes() {
85 return Objects.isNull(this.readServiceTemplate.get(nodeTypes)) ? new HashMap<>()
86 : (Map<String, Object>) this.readServiceTemplate.get(nodeTypes);
90 public Object getTopologyTemplate() {
91 return this.readServiceTemplate.get(topologyTemplate);
95 public Map<String, Object> getNodeTemplates() {
96 return Objects.isNull(this.getTopologyTemplate()) ? new HashMap<>()
97 : (Map<String, Object>) ((Map<String, Object>) this.getTopologyTemplate()).get(nodeTemplates);
101 public Map<String, Object> getInputs() {
102 return Objects.isNull(this.getTopologyTemplate()) ? new HashMap<>()
103 : (Map<String, Object>) ((Map<String, Object>) this.getTopologyTemplate()).get(inputs);
107 public Map<String, Object> getOutputs() {
108 return Objects.isNull(this.getTopologyTemplate()) ? new HashMap<>()
109 : (Map<String, Object>) ((Map<String, Object>) this.getTopologyTemplate()).get(outputs);
113 public Map<String, Object> getSubstitutionMappings() {
114 return Objects.isNull(this.getTopologyTemplate()) ? new HashMap<>()
115 : (Map<String, Object>) ((Map<String, Object>) this.getTopologyTemplate())
116 .get(substitutionMappings);
120 public Map<String, Object> getDataTypes() {
121 final String dataTypesElementName = DATA_TYPES.getElementName();
122 return Objects.isNull(this.readServiceTemplate.get(dataTypesElementName)) ? new HashMap<>()
123 : (Map<String, Object>) this.readServiceTemplate.get(dataTypesElementName);