Remove useless methods
[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 retrieves image for a CLDS template name from the
111      * database. This is subset of the json getModel. This is only expected to
112      * be used for testing purposes, not by the UI.
113      *
114      * @param templateName
115      * @return image xml text - content of image given name
116      */
117     @GET
118     @Path("/template/image/{templateName}")
119     @Produces(MediaType.TEXT_XML)
120     public String getImageXml(@PathParam("templateName") String templateName) {
121         Date startTime = new Date();
122         LoggingUtils.setRequestContext("CldsTemplateService: GET template image", getPrincipalName());
123         isAuthorized(permissionReadTemplate);
124         logger.info("GET imageText for templateName=" + templateName);
125         CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
126         // audit log
127         LoggingUtils.setTimeContext(startTime, new Date());
128         LoggingUtils.setResponseContext("0", "Get template image success", this.getClass().getName());
129         auditLogger.info("GET template image completed");
130         return template.getImageText();
131     }
132
133     /**
134      * REST service that retrieves a CLDS template by name from the database.
135      *
136      * @param templateName
137      * @return clds template - clds template for the given template name
138      */
139     @GET
140     @Path("/template/{templateName}")
141     @Produces(MediaType.APPLICATION_JSON)
142     public CldsTemplate getTemplate(@PathParam("templateName") String templateName) {
143         Date startTime = new Date();
144         LoggingUtils.setRequestContext("CldsTemplateService: GET template", getPrincipalName());
145         isAuthorized(permissionReadTemplate);
146         logger.info("GET model for  templateName=" + templateName);
147         CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
148         template.setUserAuthorizedToUpdate(isAuthorizedNoException(permissionUpdateTemplate));
149         // audit log
150         LoggingUtils.setTimeContext(startTime, new Date());
151         LoggingUtils.setResponseContext("0", "Get template success", this.getClass().getName());
152         auditLogger.info("GET template completed");
153         return template;
154     }
155
156     /**
157      * REST service that saves a CLDS template by name in the database.
158      *
159      * @param templateName
160      * @throws IOException
161      * @throws JsonMappingException
162      * @throws JsonParseException
163      */
164     @PUT
165     @Path("/template/{templateName}")
166     @Consumes(MediaType.APPLICATION_JSON)
167     @Produces(MediaType.APPLICATION_JSON)
168     public CldsTemplate putTemplate(@PathParam("templateName") String templateName, CldsTemplate cldsTemplate)
169             throws TransformerException, IOException {
170         Date startTime = new Date();
171         LoggingUtils.setRequestContext("CldsTemplateService: PUT template", getPrincipalName());
172         isAuthorized(permissionUpdateTemplate);
173
174         logger.info("PUT Template for  templateName=" + templateName);
175         logger.info("PUT bpmnText=" + cldsTemplate.getBpmnText());
176         logger.info("PUT propText=" + cldsTemplate.getPropText());
177         logger.info("PUT imageText=" + cldsTemplate.getImageText());
178         cldsTemplate.setName(templateName);
179         String bpmnText = cldsTemplate.getBpmnText();
180         String imageText = cldsTemplate.getImageText();
181         String propText = cldsTemplate.getPropText();
182         cldsTemplate.setBpmnText(bpmnText);
183         cldsTemplate.setImageText(imageText);
184         cldsTemplate.setPropText(propText);
185         logger.info(" bpmnText : " + cldsTemplate.getBpmnText());
186         logger.info(" Image Text : " + cldsTemplate.getImageText());
187         logger.info(" Prop Text : " + cldsTemplate.getPropText());
188         cldsTemplate.save(cldsDao, userid);
189
190         // audit log
191         LoggingUtils.setTimeContext(startTime, new Date());
192         LoggingUtils.setResponseContext("0", "Put template success", this.getClass().getName());
193         auditLogger.info("PUT template completed");
194
195         return cldsTemplate;
196     }
197
198     /**
199      * REST service that retrieves a list of CLDS template names.
200      *
201      * @return template names in JSON
202      */
203     @GET
204     @Path("/template-names")
205     @Produces(MediaType.APPLICATION_JSON)
206     public List<ValueItem> getTemplateNames() {
207         Date startTime = new Date();
208         LoggingUtils.setRequestContext("CldsTemplateService: GET template names", getPrincipalName());
209         isAuthorized(permissionReadTemplate);
210         logger.info("GET list of template names");
211         List<ValueItem> names = cldsDao.getTemplateNames();
212         // audit log
213         LoggingUtils.setTimeContext(startTime, new Date());
214         LoggingUtils.setResponseContext("0", "Get template names success", this.getClass().getName());
215         auditLogger.info("GET template names completed");
216         return names;
217     }
218 }