2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 package org.onap.clamp.clds.service;
26 import com.att.ajsc.common.AjscService;
27 import com.fasterxml.jackson.core.JsonParseException;
28 import com.fasterxml.jackson.databind.JsonMappingException;
30 import java.io.IOException;
31 import java.util.Date;
32 import java.util.List;
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;
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;
53 * Service to save and retrieve the CLDS model attributes.
57 public class CldsTemplateService extends SecureServiceBase {
59 @Value("${CLDS_PERMISSION_TYPE_TEMPLATE:permission-type-template}")
60 private String cldsPermissionTypeTemplate;
62 @Value("${CLDS_PERMISSION_INSTANCE:dev}")
63 private String cldsPermissionInstance;
65 private SecureServicePermission permissionReadTemplate;
67 private SecureServicePermission permissionUpdateTemplate;
70 private final void afterConstruction() {
71 permissionReadTemplate = SecureServicePermission.create(cldsPermissionTypeTemplate, cldsPermissionInstance,
73 permissionUpdateTemplate = SecureServicePermission.create(cldsPermissionTypeTemplate, cldsPermissionInstance,
78 private CldsDao cldsDao;
81 private XslTransformer cldsBpmnTransformer;
83 private static String userid;
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.
91 * @return bpmn xml text - content of bpmn given name
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);
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();
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.
114 * @param templateName
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);
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;
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.
141 * @param templateName
142 * @return image xml text - content of image given name
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);
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();
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.
165 * @param templateName
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);
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;
188 * REST service that retrieves a CLDS template by name from the database.
190 * @param templateName
191 * @return clds template - clds template for the given template name
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));
204 LoggingUtils.setTimeContext(startTime, new Date());
205 LoggingUtils.setResponseContext("0", "Get template success", this.getClass().getName());
206 auditLogger.info("GET template completed");
211 * REST service that saves a CLDS template by name in the database.
213 * @param templateName
214 * @throws IOException
215 * @throws JsonMappingException
216 * @throws JsonParseException
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);
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);
245 LoggingUtils.setTimeContext(startTime, new Date());
246 LoggingUtils.setResponseContext("0", "Put template success", this.getClass().getName());
247 auditLogger.info("PUT template completed");
253 * REST service that retrieves a list of CLDS template names.
255 * @return template names in JSON
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();
267 LoggingUtils.setTimeContext(startTime, new Date());
268 LoggingUtils.setResponseContext("0", "Get template names success", this.getClass().getName());
269 auditLogger.info("GET template names completed");