2 * Copyright 2016 [ZTE] and others.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.openo.commontosca.catalog.model.common;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.HashMap;
21 import java.util.Iterator;
22 import java.util.List;
24 import java.util.Map.Entry;
26 import org.openo.commontosca.catalog.db.entity.ServiceTemplateMappingData;
27 import org.openo.commontosca.catalog.db.entity.TemplateData;
28 import org.openo.commontosca.catalog.model.entity.ServiceTemplate;
29 import org.openo.commontosca.catalog.model.entity.ServiceTemplateOperation;
30 import org.openo.commontosca.catalog.model.entity.SubstitutionMapping;
31 import org.openo.commontosca.catalog.common.ToolUtil;
32 import org.openo.commontosca.catalog.db.entity.NodeTemplateData;
33 import org.openo.commontosca.catalog.db.entity.ServiceTemplateData;
34 import org.openo.commontosca.catalog.model.entity.InputParameter;
35 import org.openo.commontosca.catalog.model.entity.NodeTemplate;
36 import org.openo.commontosca.catalog.model.entity.RelationShip;
38 import com.google.gson.Gson;
39 import com.google.gson.JsonElement;
40 import com.google.gson.JsonObject;
41 import com.google.gson.JsonPrimitive;
47 public class TemplateDataHelper {
54 public static TemplateData convert2TemplateData(ServiceTemplate st,
55 String rawData, List<NodeTemplate> ntList) {
56 TemplateData td = new TemplateData();
57 td.setServiceTemplate(convert2ServiceTemplateData(st, rawData));
58 td.setNodeTemplates(convert2NodeTemplateDataList(ntList,
59 st.getServiceTemplateId()));
67 private static ServiceTemplateData convert2ServiceTemplateData(
68 ServiceTemplate st, String rawData) {
69 ServiceTemplateData std = new ServiceTemplateData();
70 std.setServiceTemplateId(st.getServiceTemplateId());
71 std.setTemplateName(st.getTemplateName());
72 std.setVendor(st.getVendor());
73 std.setVersion(st.getVersion());
74 std.setCsarId(st.getCsarid());
75 std.setType(st.getType());
76 std.setDownloadUri(st.getDownloadUri());
77 std.setInputs(ToolUtil.toJson(st.getInputs()));
78 std.setOperations(ToolUtil.toJson(st.getOperations()));
79 std.setRowData(rawData);
85 * @param serviceTemplateId
88 private static ArrayList<NodeTemplateData> convert2NodeTemplateDataList(
89 List<NodeTemplate> ntList, String serviceTemplateId) {
90 ArrayList<NodeTemplateData> ntdList = new ArrayList<>();
91 for (NodeTemplate nt : ntList) {
92 ntdList.add(convert2NodeTemplateData(nt, serviceTemplateId));
101 * @param serviceTemplateId
104 private static NodeTemplateData convert2NodeTemplateData(NodeTemplate nt,
105 String serviceTemplateId) {
106 NodeTemplateData ntd = new NodeTemplateData();
108 ntd.setNodeTemplateId(nt.getId());
109 ntd.setName(nt.getName());
110 ntd.setType(nt.getType());
111 ntd.setServiceTemplateId(serviceTemplateId);
112 ntd.setProperties(ToolUtil.toJson(nt.getProperties()));
113 ntd.setRelationShips(ToolUtil.toJson(nt.getRelationShips()));
123 public static ServiceTemplate[] convert2ServiceTemplates(
124 List<ServiceTemplateData> stdList) {
125 List<ServiceTemplate> stList = new ArrayList<>();
126 for (ServiceTemplateData std : stdList) {
127 stList.add(convert2ServiceTemplate(std));
130 return stList.toArray(new ServiceTemplate[0]);
137 public static ServiceTemplate convert2ServiceTemplate(
138 ServiceTemplateData std) {
139 InputParameter[] inputs = ToolUtil.fromJson(std.getInputs(),
140 InputParameter[].class);
141 ServiceTemplateOperation[] operations = ToolUtil.fromJson(
142 std.getOperations(), ServiceTemplateOperation[].class);
144 return new ServiceTemplate(std.getServiceTemplateId(),
145 std.getTemplateName(), std.getVendor(), std.getVersion(),
146 std.getCsarId(), std.getType(), std.getDownloadUri(), inputs,
155 public static NodeTemplate[] convert2NodeTemplates(
156 List<NodeTemplateData> ntdList) {
157 List<NodeTemplate> ntList = new ArrayList<>();
158 for (NodeTemplateData ntd : ntdList) {
159 ntList.add(convert2NodeTemplate(ntd));
161 return ntList.toArray(new NodeTemplate[0]);
168 public static NodeTemplate convert2NodeTemplate(NodeTemplateData ntd) {
169 List<RelationShip> relationShips = convert2RelationShipList(ntd
170 .getRelationShips());
171 return new NodeTemplate(ntd.getNodeTemplateId(), ntd.getName(),
172 ntd.getType(), convert2Property(ntd.getProperties()),
177 * @param sRelationShips
180 private static List<RelationShip> convert2RelationShipList(
181 String sRelationShips) {
182 RelationShip[] relationShips = ToolUtil.fromJson(sRelationShips,
183 RelationShip[].class);
184 return Arrays.asList(relationShips);
191 private static Map<String, Object> convert2Property(String properties) {
192 JsonObject jsonObject = new Gson().fromJson(properties,
194 return parseMapValue(jsonObject);
197 private static Map<String, Object> parseMapValue(JsonObject jsonObject) {
198 Map<String, Object> map = new HashMap<>();
200 Iterator<Entry<String, JsonElement>> iterator = jsonObject.entrySet()
202 while (iterator.hasNext()) {
203 Entry<String, JsonElement> next = iterator.next();
204 if (next.getValue() instanceof JsonPrimitive) {
205 map.put(next.getKey(), next.getValue().getAsString());
209 if (next.getValue() instanceof JsonObject) {
210 map.put(next.getKey(),
211 parseMapValue((JsonObject) next.getValue()));
223 public static ServiceTemplateMappingData convert2TemplateMappingData(
224 SubstitutionMapping stm) {
225 ServiceTemplateMappingData stmd = new ServiceTemplateMappingData();
227 stmd.setMappingId(ToolUtil.generateId());
228 stmd.setServiceTemplateId(stm.getServiceTemplateId());
229 stmd.setNodeType(stm.getNode_type());
230 stmd.setRequirements(ToolUtil.toJson(stm.getRequirements()));
231 stmd.setCapabilities(ToolUtil.toJson(stm.getCapabilities()));
240 public static SubstitutionMapping convert2SubstitutionMapping(
241 ServiceTemplateMappingData stmData) {
242 return new SubstitutionMapping(stmData.getServiceTemplateId(),
243 stmData.getNodeType());