Initial OpenECOMP MSO commit
[so.git] / mso-catalog-db / src / main / java / org / openecomp / mso / db / catalog / beans / HeatTemplate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.db.catalog.beans;
22
23
24 import java.io.BufferedReader;
25 import java.io.FileReader;
26 import java.sql.Timestamp;
27 import java.text.DateFormat;
28 import java.util.Date;
29 import java.util.Set;
30
31 import org.openecomp.mso.db.catalog.utils.MavenLikeVersioning;
32 import org.openecomp.mso.logger.MsoLogger;
33
34 public class HeatTemplate extends MavenLikeVersioning {
35
36     private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.GENERAL);
37
38     private int id;
39     private String templateName;
40     private String templatePath = null;
41     private String templateBody = null;
42     private int timeoutMinutes;
43     private Set <HeatTemplateParam> parameters;
44     private Set <HeatNestedTemplate> files;
45     private String description;
46     private String asdcUuid;
47     private String asdcResourceName;
48     private String asdcLabel;
49     
50     private Timestamp created;
51     
52     public enum TemplateStatus {
53                                 PARENT, CHILD, PARENT_COMPLETE
54     }
55
56     public HeatTemplate () {
57     }
58
59     public int getId () {
60         return id;
61     }
62
63     public void setId (int id) {
64         this.id = id;
65     }
66
67     public String getTemplateName () {
68         return templateName;
69     }
70
71     public void setTemplateName (String templateName) {
72         this.templateName = templateName;
73     }
74
75     public String getTemplatePath () {
76         return templatePath;
77     }
78
79     public void setTemplatePath (String templatePath) {
80         this.templatePath = templatePath;
81     }
82
83     public String getTemplateBody () {
84         return templateBody;
85     }
86
87     public void setTemplateBody (String templateBody) {
88         this.templateBody = templateBody;
89     }
90
91     public int getTimeoutMinutes () {
92         return timeoutMinutes;
93     }
94
95     public void setTimeoutMinutes (int timeout) {
96         this.timeoutMinutes = timeout;
97     }
98
99     public Set <HeatTemplateParam> getParameters () {
100         return parameters;
101     }
102
103     public void setParameters (Set <HeatTemplateParam> parameters) {
104         this.parameters = parameters;
105     }
106     
107     public String getDescription() {
108                 return description;
109         }
110
111         public void setDescription(String description) {
112                 this.description = description;
113         }
114
115         public String getHeatTemplate () {
116         if (templateBody != null && !templateBody.isEmpty ()) {
117             // The template body is in the DB table. Just return it.
118             return templateBody;
119         }
120
121         String body = null;
122
123         try (BufferedReader reader = new BufferedReader (new FileReader (templatePath)))
124         {
125             String line = null;
126             StringBuilder stringBuilder = new StringBuilder ();
127             while ((line = reader.readLine ()) != null) {
128                 stringBuilder.append (line);
129             }
130             body = stringBuilder.toString ();
131         } catch (Exception e) {
132             LOGGER.debug ("Error reading template file " + templatePath, e);
133         }
134         
135         return body;
136     }
137
138     public void setFiles (Set <HeatNestedTemplate> files) {
139         this.files = files;
140     }
141
142     public Set <HeatNestedTemplate> getFiles () {
143         return this.files;
144     }
145
146         public String getAsdcUuid() {
147                 return asdcUuid;
148         }
149
150         public void setAsdcUuid(String asdcUuidp) {
151                 this.asdcUuid = asdcUuidp;
152         }
153             
154     public String getAsdcResourceName() {
155                 return asdcResourceName;
156         }
157
158         public void setAsdcResourceName(String asdcResourceName) {
159                 this.asdcResourceName = asdcResourceName;
160         }
161         public String getAsdcLabel() {
162                 return this.asdcLabel;
163         }
164         public void setAsdcLabel(String asdcLabel) {
165                 this.asdcLabel = asdcLabel;
166         }
167         
168         public Timestamp getCreated() {
169                 return created;
170         }
171
172         public void setCreated(Timestamp created) {
173                 this.created = created;
174         }
175
176         @Override
177     public String toString () {
178         String body = (templateBody != null) ? "(" + templateBody.length () + " chars)" : "(Not defined)";
179         StringBuilder sb = new StringBuilder ();
180         sb.append ("Template=")
181           .append (templateName)
182           .append (",version=")
183           .append (version)
184           .append (",path=")
185           .append (templatePath)
186           .append (",body=")
187           .append (body)
188           .append (",timeout=")
189           .append (timeoutMinutes)
190           .append (",asdcUuid=")
191           .append (asdcUuid)
192           .append (",asdcResourceName=")
193           .append (asdcResourceName)
194           .append (",description=")
195           .append (description);
196         if (created != null) {
197                 sb.append (",created=");
198                 sb.append (DateFormat.getInstance().format(created));
199         }
200         
201
202         if (parameters != null && !parameters.isEmpty ()) {
203             sb.append (",params=[");
204             for (HeatTemplateParam param : parameters) {
205                 sb.append (param.getParamName ());
206                 if (param.isRequired ()) {
207                     sb.append ("(reqd)");
208                 }
209                 sb.append (",");
210             }
211             sb.replace (sb.length () - 1, sb.length (), "]");
212         }
213         return sb.toString ();
214     }
215
216
217 }