Remove the clds-reference.properties
[clamp.git] / src / main / java / org / onap / clamp / clds / service / CldsTemplateService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 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.fasterxml.jackson.core.JsonParseException;
27 import com.fasterxml.jackson.databind.JsonMappingException;
28
29 import java.io.IOException;
30 import java.util.Date;
31 import java.util.List;
32
33 import javax.annotation.PostConstruct;
34 import javax.ws.rs.Consumes;
35 import javax.ws.rs.GET;
36 import javax.ws.rs.PUT;
37 import javax.ws.rs.Path;
38 import javax.ws.rs.PathParam;
39 import javax.ws.rs.Produces;
40 import javax.ws.rs.core.MediaType;
41 import javax.xml.transform.TransformerException;
42
43 import org.onap.clamp.clds.dao.CldsDao;
44 import org.onap.clamp.clds.model.CldsTemplate;
45 import org.onap.clamp.clds.model.ValueItem;
46 import org.onap.clamp.clds.util.LoggingUtils;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.beans.factory.annotation.Value;
49 import org.springframework.stereotype.Component;
50
51 /**
52  * Service to save and retrieve the CLDS model attributes.
53  */
54 @Component
55 @Path("/cldsTempate")
56 public class CldsTemplateService extends SecureServiceBase {
57
58     @Value("${clamp.config.security.permission.type.template:permission-type-template}")
59     private String cldsPermissionTypeTemplate;
60     @Value("${clamp.config.security.permission.instance:dev}")
61     private String cldsPermissionInstance;
62     private SecureServicePermission permissionReadTemplate;
63     private SecureServicePermission permissionUpdateTemplate;
64
65     @PostConstruct
66     private final void afterConstruction() {
67         permissionReadTemplate = SecureServicePermission.create(cldsPermissionTypeTemplate, cldsPermissionInstance,
68                 "read");
69         permissionUpdateTemplate = SecureServicePermission.create(cldsPermissionTypeTemplate, cldsPermissionInstance,
70                 "update");
71     }
72
73     @Autowired
74     private CldsDao cldsDao;
75
76     /**
77      * REST service that retrieves BPMN for a CLDS template name from the
78      * database. This is subset of the json getModel. This is only expected to
79      * be used for testing purposes, not by the UI.
80      *
81      * @param templateName
82      * @return bpmn xml text - content of bpmn given name
83      */
84     @GET
85     @Path("/template/bpmn/{templateName}")
86     @Produces(MediaType.TEXT_XML)
87     public String getBpmnTemplate(@PathParam("templateName") String templateName) {
88         Date startTime = new Date();
89         LoggingUtils.setRequestContext("CldsTemplateService: GET template bpmn", getPrincipalName());
90         isAuthorized(permissionReadTemplate);
91         logger.info("GET bpmnText for templateName=" + templateName);
92         CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
93         // audit log
94         LoggingUtils.setTimeContext(startTime, new Date());
95         LoggingUtils.setResponseContext("0", "Get template bpmn success", this.getClass().getName());
96         auditLogger.info("GET template bpmn completed");
97         return template.getBpmnText();
98     }
99
100     /**
101      * REST service that retrieves image for a CLDS template name from the
102      * database. This is subset of the json getModel. This is only expected to
103      * be used for testing purposes, not by the UI.
104      *
105      * @param templateName
106      * @return image xml text - content of image given name
107      */
108     @GET
109     @Path("/template/image/{templateName}")
110     @Produces(MediaType.TEXT_XML)
111     public String getImageXml(@PathParam("templateName") String templateName) {
112         Date startTime = new Date();
113         LoggingUtils.setRequestContext("CldsTemplateService: GET template image", getPrincipalName());
114         isAuthorized(permissionReadTemplate);
115         logger.info("GET imageText for templateName=" + templateName);
116         CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
117         // audit log
118         LoggingUtils.setTimeContext(startTime, new Date());
119         LoggingUtils.setResponseContext("0", "Get template image success", this.getClass().getName());
120         auditLogger.info("GET template image completed");
121         return template.getImageText();
122     }
123
124     /**
125      * REST service that retrieves a CLDS template by name from the database.
126      *
127      * @param templateName
128      * @return clds template - clds template for the given template name
129      */
130     @GET
131     @Path("/template/{templateName}")
132     @Produces(MediaType.APPLICATION_JSON)
133     public CldsTemplate getTemplate(@PathParam("templateName") String templateName) {
134         Date startTime = new Date();
135         LoggingUtils.setRequestContext("CldsTemplateService: GET template", getPrincipalName());
136         isAuthorized(permissionReadTemplate);
137         logger.info("GET model for  templateName=" + templateName);
138         CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
139         template.setUserAuthorizedToUpdate(isAuthorizedNoException(permissionUpdateTemplate));
140         // audit log
141         LoggingUtils.setTimeContext(startTime, new Date());
142         LoggingUtils.setResponseContext("0", "Get template success", this.getClass().getName());
143         auditLogger.info("GET template completed");
144         return template;
145     }
146
147     /**
148      * REST service that saves a CLDS template by name in the database.
149      *
150      * @param templateName
151      * @throws IOException
152      * @throws JsonMappingException
153      * @throws JsonParseException
154      */
155     @PUT
156     @Path("/template/{templateName}")
157     @Consumes(MediaType.APPLICATION_JSON)
158     @Produces(MediaType.APPLICATION_JSON)
159     public CldsTemplate putTemplate(@PathParam("templateName") String templateName, CldsTemplate cldsTemplate)
160             throws TransformerException, IOException {
161         Date startTime = new Date();
162         LoggingUtils.setRequestContext("CldsTemplateService: PUT template", getPrincipalName());
163         isAuthorized(permissionUpdateTemplate);
164         logger.info("PUT Template for  templateName=" + templateName);
165         logger.info("PUT bpmnText=" + cldsTemplate.getBpmnText());
166         logger.info("PUT propText=" + cldsTemplate.getPropText());
167         logger.info("PUT imageText=" + cldsTemplate.getImageText());
168         cldsTemplate.setName(templateName);
169         String bpmnText = cldsTemplate.getBpmnText();
170         String imageText = cldsTemplate.getImageText();
171         String propText = cldsTemplate.getPropText();
172         cldsTemplate.setBpmnText(bpmnText);
173         cldsTemplate.setImageText(imageText);
174         cldsTemplate.setPropText(propText);
175         logger.info(" bpmnText : " + cldsTemplate.getBpmnText());
176         logger.info(" Image Text : " + cldsTemplate.getImageText());
177         logger.info(" Prop Text : " + cldsTemplate.getPropText());
178         cldsTemplate.save(cldsDao, null);
179         // audit log
180         LoggingUtils.setTimeContext(startTime, new Date());
181         LoggingUtils.setResponseContext("0", "Put template success", this.getClass().getName());
182         auditLogger.info("PUT template completed");
183         return cldsTemplate;
184     }
185
186     /**
187      * REST service that retrieves a list of CLDS template names.
188      *
189      * @return template names in JSON
190      */
191     @GET
192     @Path("/template-names")
193     @Produces(MediaType.APPLICATION_JSON)
194     public List<ValueItem> getTemplateNames() {
195         Date startTime = new Date();
196         LoggingUtils.setRequestContext("CldsTemplateService: GET template names", getPrincipalName());
197         isAuthorized(permissionReadTemplate);
198         logger.info("GET list of template names");
199         List<ValueItem> names = cldsDao.getTemplateNames();
200         // audit log
201         LoggingUtils.setTimeContext(startTime, new Date());
202         LoggingUtils.setResponseContext("0", "Get template names success", this.getClass().getName());
203         auditLogger.info("GET template names completed");
204         return names;
205     }
206 }