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 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.
114 * @param templateName
115 * @return image xml text - content of image given name
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);
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();
134 * REST service that retrieves a CLDS template by name from the database.
136 * @param templateName
137 * @return clds template - clds template for the given template name
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));
150 LoggingUtils.setTimeContext(startTime, new Date());
151 LoggingUtils.setResponseContext("0", "Get template success", this.getClass().getName());
152 auditLogger.info("GET template completed");
157 * REST service that saves a CLDS template by name in the database.
159 * @param templateName
160 * @throws IOException
161 * @throws JsonMappingException
162 * @throws JsonParseException
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);
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);
191 LoggingUtils.setTimeContext(startTime, new Date());
192 LoggingUtils.setResponseContext("0", "Put template success", this.getClass().getName());
193 auditLogger.info("PUT template completed");
199 * REST service that retrieves a list of CLDS template names.
201 * @return template names in JSON
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();
213 LoggingUtils.setTimeContext(startTime, new Date());
214 LoggingUtils.setResponseContext("0", "Get template names success", this.getClass().getName());
215 auditLogger.info("GET template names completed");