a74ec8eb7c19d6b2dcad276144d95ac2cd72b6bf
[sdc.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.core.impl.services;
22
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;
34
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Objects;
40 import org.onap.sdc.tosca.services.YamlUtil;
41 import org.openecomp.core.converter.ServiceTemplateReaderService;
42
43 public class ServiceTemplateReaderServiceImpl implements ServiceTemplateReaderService {
44
45     private final Map<String, Object> readServiceTemplate;
46
47     public ServiceTemplateReaderServiceImpl(byte[] serviceTemplateContent) {
48         this.readServiceTemplate = readServiceTemplate(serviceTemplateContent);
49     }
50
51     @Override
52     public Map<String, Object> readServiceTemplate(byte[] serviceTemplateContent) {
53         return new YamlUtil().yamlToObject(new String(serviceTemplateContent), Map.class);
54     }
55
56     @Override
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);
61     }
62
63     @Override
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);
69         }
70         return policiesAsMap;
71     }
72
73     @Override
74     public Object getMetadata() {
75         return this.readServiceTemplate.get(metadata);
76     }
77
78     @Override
79     public Object getToscaVersion() {
80         return this.readServiceTemplate.get(TOSCA_VERSION.getElementName());
81     }
82
83     @Override
84     public Map<String, Object> getNodeTypes() {
85         return Objects.isNull(this.readServiceTemplate.get(nodeTypes)) ? new HashMap<>()
86             : (Map<String, Object>) this.readServiceTemplate.get(nodeTypes);
87     }
88
89     @Override
90     public Object getTopologyTemplate() {
91         return this.readServiceTemplate.get(topologyTemplate);
92     }
93
94     @Override
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);
98     }
99
100     @Override
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);
104     }
105
106     @Override
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);
110     }
111
112     @Override
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);
117     }
118
119     @Override
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);
124     }
125 }