73b593c377bb8cac33b377ec6bd16c92a8c4e4d6
[clamp.git] / src / main / java / org / onap / clamp / clds / service / CldsTemplateService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.clamp.clds.service;
25
26 import com.att.ajsc.common.AjscService;
27 import com.fasterxml.jackson.core.JsonParseException;
28 import com.fasterxml.jackson.databind.JsonMappingException;
29
30 import java.io.IOException;
31 import java.util.Date;
32 import java.util.List;
33
34 import javax.annotation.PostConstruct;
35 import javax.ws.rs.Consumes;
36 import javax.ws.rs.GET;
37 import javax.ws.rs.PUT;
38 import javax.ws.rs.Path;
39 import javax.ws.rs.PathParam;
40 import javax.ws.rs.Produces;
41 import javax.ws.rs.core.MediaType;
42 import javax.xml.transform.TransformerException;
43
44 import org.onap.clamp.clds.dao.CldsDao;
45 import org.onap.clamp.clds.model.CldsTemplate;
46 import org.onap.clamp.clds.model.ValueItem;
47 import org.onap.clamp.clds.transform.XslTransformer;
48 import org.onap.clamp.clds.util.LoggingUtils;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.beans.factory.annotation.Value;
51
52 /**
53  * Service to save and retrieve the CLDS model attributes.
54  */
55 @AjscService
56 @Path("/cldsTempate")
57 public class CldsTemplateService extends SecureServiceBase {
58
59     @Value("${CLDS_PERMISSION_TYPE_TEMPLATE:permission-type-template}")
60     private String                  cldsPermissionTypeTemplate;
61
62     @Value("${CLDS_PERMISSION_INSTANCE:dev}")
63     private String                  cldsPermissionInstance;
64
65     private SecureServicePermission permissionReadTemplate;
66
67     private SecureServicePermission permissionUpdateTemplate;
68
69     @PostConstruct
70     private final void afterConstruction() {
71         permissionReadTemplate = SecureServicePermission.create(cldsPermissionTypeTemplate, cldsPermissionInstance,
72                 "read");
73         permissionUpdateTemplate = SecureServicePermission.create(cldsPermissionTypeTemplate, cldsPermissionInstance,
74                 "update");
75     }
76
77     @Autowired
78     private CldsDao        cldsDao;
79
80     @Autowired
81     private XslTransformer cldsBpmnTransformer;
82
83     private static String  userid;
84
85     /**
86      * REST service that retrieves BPMN for a CLDS template name from the
87      * database. This is subset of the json getModel. This is only expected to
88      * be used for testing purposes, not by the UI.
89      *
90      * @param templateName
91      * @return bpmn xml text - content of bpmn given name
92      */
93     @GET
94     @Path("/template/bpmn/{templateName}")
95     @Produces(MediaType.TEXT_XML)
96     public String getBpmnTemplate(@PathParam("templateName") String templateName) {
97         Date startTime = new Date();
98         LoggingUtils.setRequestContext("CldsTemplateService: GET template bpmn", getPrincipalName());
99         isAuthorized(permissionReadTemplate);
100         logger.info("GET bpmnText for templateName=" + templateName);
101         CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
102         // audit log
103         LoggingUtils.setTimeContext(startTime, new Date());
104         LoggingUtils.setResponseContext("0", "Get template bpmn success", this.getClass().getName());
105         auditLogger.info("GET template bpmn completed");
106         return template.getBpmnText();
107     }
108
109     /**
110      * REST service that saves BPMN for a CLDS template by name in the database.
111      * This is subset of the json putModel. This is only expected to be used for
112      * testing purposes, not by the UI.
113      *
114      * @param templateName
115      * @param bpmnText
116      */
117     @PUT
118     @Path("/template/bpmn/{templateName}")
119     @Consumes(MediaType.TEXT_XML)
120     public String putBpmnTemplateXml(@PathParam("templateName") String templateName, String bpmnText) {
121         Date startTime = new Date();
122         LoggingUtils.setRequestContext("CldsTemplateService: PUT template bpmn", getPrincipalName());
123         isAuthorized(permissionUpdateTemplate);
124         logger.info("PUT bpmnText for templateName=" + templateName);
125         logger.info("PUT bpmnText=" + bpmnText);
126         CldsTemplate cldsTemplate = CldsTemplate.retrieve(cldsDao, templateName, true);
127         cldsTemplate.setBpmnText(bpmnText);
128         cldsTemplate.save(cldsDao, userid);
129         // audit log
130         LoggingUtils.setTimeContext(startTime, new Date());
131         LoggingUtils.setResponseContext("0", "Put template bpmn success", this.getClass().getName());
132         auditLogger.info("PUT template bpm completed");
133         return "wrote bpmnText for templateName=" + templateName;
134     }
135
136     /**
137      * REST service that retrieves image for a CLDS template name from the
138      * database. This is subset of the json getModel. This is only expected to
139      * be used for testing purposes, not by the UI.
140      *
141      * @param templateName
142      * @return image xml text - content of image given name
143      */
144     @GET
145     @Path("/template/image/{templateName}")
146     @Produces(MediaType.TEXT_XML)
147     public String getImageXml(@PathParam("templateName") String templateName) {
148         Date startTime = new Date();
149         LoggingUtils.setRequestContext("CldsTemplateService: GET template image", getPrincipalName());
150         isAuthorized(permissionReadTemplate);
151         logger.info("GET imageText for templateName=" + templateName);
152         CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
153         // audit log
154         LoggingUtils.setTimeContext(startTime, new Date());
155         LoggingUtils.setResponseContext("0", "Get template image success", this.getClass().getName());
156         auditLogger.info("GET template image completed");
157         return template.getImageText();
158     }
159
160     /**
161      * REST service that saves image for a CLDS template by name in the
162      * database. This is subset of the json putModel. This is only expected to
163      * be used for testing purposes, not by the UI.
164      *
165      * @param templateName
166      * @param imageText
167      */
168     @PUT
169     @Path("/template/image/{templateName}")
170     @Consumes(MediaType.TEXT_XML)
171     public String putImageXml(@PathParam("templateName") String templateName, String imageText) {
172         Date startTime = new Date();
173         LoggingUtils.setRequestContext("CldsTemplateService: PUT template image", getPrincipalName());
174         isAuthorized(permissionUpdateTemplate);
175         logger.info("PUT iamgeText for modelName=" + templateName);
176         logger.info("PUT imageText=" + imageText);
177         CldsTemplate cldsTemplate = CldsTemplate.retrieve(cldsDao, templateName, true);
178         cldsTemplate.setImageText(imageText);
179         cldsTemplate.save(cldsDao, userid);
180         // audit log
181         LoggingUtils.setTimeContext(startTime, new Date());
182         LoggingUtils.setResponseContext("0", "Put template image success", this.getClass().getName());
183         auditLogger.info("PUT template image completed");
184         return "wrote imageText for modelName=" + templateName;
185     }
186
187     /**
188      * REST service that retrieves a CLDS template by name from the database.
189      *
190      * @param templateName
191      * @return clds template - clds template for the given template name
192      */
193     @GET
194     @Path("/template/{templateName}")
195     @Produces(MediaType.APPLICATION_JSON)
196     public CldsTemplate getTemplate(@PathParam("templateName") String templateName) {
197         Date startTime = new Date();
198         LoggingUtils.setRequestContext("CldsTemplateService: GET template", getPrincipalName());
199         isAuthorized(permissionReadTemplate);
200         logger.info("GET model for  templateName=" + templateName);
201         CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
202         template.setUserAuthorizedToUpdate(isAuthorizedNoException(permissionUpdateTemplate));
203         // audit log
204         LoggingUtils.setTimeContext(startTime, new Date());
205         LoggingUtils.setResponseContext("0", "Get template success", this.getClass().getName());
206         auditLogger.info("GET template completed");
207         return template;
208     }
209
210     /**
211      * REST service that saves a CLDS template by name in the database.
212      *
213      * @param templateName
214      * @throws IOException
215      * @throws JsonMappingException
216      * @throws JsonParseException
217      */
218     @PUT
219     @Path("/template/{templateName}")
220     @Consumes(MediaType.APPLICATION_JSON)
221     @Produces(MediaType.APPLICATION_JSON)
222     public CldsTemplate putTemplate(@PathParam("templateName") String templateName, CldsTemplate cldsTemplate)
223             throws TransformerException, IOException {
224         Date startTime = new Date();
225         LoggingUtils.setRequestContext("CldsTemplateService: PUT template", getPrincipalName());
226         isAuthorized(permissionUpdateTemplate);
227
228         logger.info("PUT Template for  templateName=" + templateName);
229         logger.info("PUT bpmnText=" + cldsTemplate.getBpmnText());
230         logger.info("PUT propText=" + cldsTemplate.getPropText());
231         logger.info("PUT imageText=" + cldsTemplate.getImageText());
232         cldsTemplate.setName(templateName);
233         String bpmnText = cldsTemplate.getBpmnText();
234         String imageText = cldsTemplate.getImageText();
235         String propText = cldsTemplate.getPropText();
236         cldsTemplate.setBpmnText(bpmnText);
237         cldsTemplate.setImageText(imageText);
238         cldsTemplate.setPropText(propText);
239         logger.info(" bpmnText : " + cldsTemplate.getBpmnText());
240         logger.info(" Image Text : " + cldsTemplate.getImageText());
241         logger.info(" Prop Text : " + cldsTemplate.getPropText());
242         cldsTemplate.save(cldsDao, userid);
243
244         // audit log
245         LoggingUtils.setTimeContext(startTime, new Date());
246         LoggingUtils.setResponseContext("0", "Put template success", this.getClass().getName());
247         auditLogger.info("PUT template completed");
248
249         return cldsTemplate;
250     }
251
252     /**
253      * REST service that retrieves a list of CLDS template names.
254      *
255      * @return template names in JSON
256      */
257     @GET
258     @Path("/template-names")
259     @Produces(MediaType.APPLICATION_JSON)
260     public List<ValueItem> getTemplateNames() {
261         Date startTime = new Date();
262         LoggingUtils.setRequestContext("CldsTemplateService: GET template names", getPrincipalName());
263         isAuthorized(permissionReadTemplate);
264         logger.info("GET list of template names");
265         List<ValueItem> names = cldsDao.getTemplateNames();
266         // audit log
267         LoggingUtils.setTimeContext(startTime, new Date());
268         LoggingUtils.setResponseContext("0", "Get template names success", this.getClass().getName());
269         auditLogger.info("GET template names completed");
270         return names;
271     }
272 }