859ea522f97cb10778ae8462eb5c7de8bd4a44ba
[vfc/nfvo/catalog.git] /
1 /**
2  * Copyright 2016 [ZTE] and others.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.openo.commontosca.catalog.model.common;
17
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;
23 import java.util.Map;
24 import java.util.Map.Entry;
25
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;
37
38 import com.google.gson.Gson;
39 import com.google.gson.JsonElement;
40 import com.google.gson.JsonObject;
41 import com.google.gson.JsonPrimitive;
42
43 /**
44  * @author 10090474
45  *
46  */
47 public class TemplateDataHelper {
48
49     /**
50      * @param st
51      * @param ntList
52      * @return
53      */
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()));
60         return td;
61     }
62
63     /**
64      * @param st
65      * @return
66      */
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);
80         return std;
81     }
82
83     /**
84      * @param ntList
85      * @param serviceTemplateId
86      * @return
87      */
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));
93
94         }
95         return ntdList;
96     }
97
98
99     /**
100      * @param nt
101      * @param serviceTemplateId
102      * @return
103      */
104     private static NodeTemplateData convert2NodeTemplateData(NodeTemplate nt,
105             String serviceTemplateId) {
106         NodeTemplateData ntd = new NodeTemplateData();
107
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()));
114
115         return ntd;
116     }
117
118
119     /**
120      * @param stdList
121      * @return
122      */
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));
128         }
129
130         return stList.toArray(new ServiceTemplate[0]);
131     }
132
133     /**
134      * @param std
135      * @return
136      */
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);
143
144         return new ServiceTemplate(std.getServiceTemplateId(),
145                 std.getTemplateName(), std.getVendor(), std.getVersion(),
146                 std.getCsarId(), std.getType(), std.getDownloadUri(), inputs,
147                 operations);
148     }
149
150     /**
151      * 
152      * @param ntdList
153      * @return
154      */
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));
160         }
161         return ntList.toArray(new NodeTemplate[0]);
162     }
163
164     /**
165      * @param ntd
166      * @return
167      */
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()),
173                 relationShips);
174     }
175
176     /**
177      * @param sRelationShips
178      * @return
179      */
180     private static List<RelationShip> convert2RelationShipList(
181             String sRelationShips) {
182         RelationShip[] relationShips = ToolUtil.fromJson(sRelationShips,
183                 RelationShip[].class);
184         return Arrays.asList(relationShips);
185     }
186
187     /**
188      * @param properties
189      * @return
190      */
191     private static Map<String, Object> convert2Property(String properties) {
192         JsonObject jsonObject = new Gson().fromJson(properties,
193                 JsonObject.class);
194         return parseMapValue(jsonObject);
195     }
196
197     private static Map<String, Object> parseMapValue(JsonObject jsonObject) {
198         Map<String, Object> map = new HashMap<>();
199
200         Iterator<Entry<String, JsonElement>> iterator = jsonObject.entrySet()
201                 .iterator();
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());
206                 continue;
207             }
208
209             if (next.getValue() instanceof JsonObject) {
210                 map.put(next.getKey(),
211                         parseMapValue((JsonObject) next.getValue()));
212                 continue;
213             }
214         }
215         return map;
216     }
217
218
219     /**
220      * @param stm
221      * @return
222      */
223     public static ServiceTemplateMappingData convert2TemplateMappingData(
224             SubstitutionMapping stm) {
225         ServiceTemplateMappingData stmd = new ServiceTemplateMappingData();
226
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()));
232
233         return stmd;
234     }
235
236     /**
237      * @param stmData
238      * @return
239      */
240     public static SubstitutionMapping convert2SubstitutionMapping(
241             ServiceTemplateMappingData stmData) {
242         return new SubstitutionMapping(stmData.getServiceTemplateId(),
243                 stmData.getNodeType());
244     }
245
246 }