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.util.LoggingUtils;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.beans.factory.annotation.Value;
52 * Service to save and retrieve the CLDS model attributes.
56 public class CldsTemplateService extends SecureServiceBase {
57 @Value("${CLDS_PERMISSION_TYPE_TEMPLATE:permission-type-template}")
58 private String cldsPermissionTypeTemplate;
59 @Value("${CLDS_PERMISSION_INSTANCE:dev}")
60 private String cldsPermissionInstance;
61 private SecureServicePermission permissionReadTemplate;
62 private SecureServicePermission permissionUpdateTemplate;
65 private final void afterConstruction() {
66 permissionReadTemplate = SecureServicePermission.create(cldsPermissionTypeTemplate, cldsPermissionInstance,
68 permissionUpdateTemplate = SecureServicePermission.create(cldsPermissionTypeTemplate, cldsPermissionInstance,
73 private CldsDao cldsDao;
76 * REST service that retrieves BPMN for a CLDS template name from the
77 * database. This is subset of the json getModel. This is only expected to
78 * be used for testing purposes, not by the UI.
81 * @return bpmn xml text - content of bpmn given name
84 @Path("/template/bpmn/{templateName}")
85 @Produces(MediaType.TEXT_XML)
86 public String getBpmnTemplate(@PathParam("templateName") String templateName) {
87 Date startTime = new Date();
88 LoggingUtils.setRequestContext("CldsTemplateService: GET template bpmn", getPrincipalName());
89 isAuthorized(permissionReadTemplate);
90 logger.info("GET bpmnText for templateName=" + templateName);
91 CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
93 LoggingUtils.setTimeContext(startTime, new Date());
94 LoggingUtils.setResponseContext("0", "Get template bpmn success", this.getClass().getName());
95 auditLogger.info("GET template bpmn completed");
96 return template.getBpmnText();
100 * REST service that retrieves image for a CLDS template name from the
101 * database. This is subset of the json getModel. This is only expected to
102 * be used for testing purposes, not by the UI.
104 * @param templateName
105 * @return image xml text - content of image given name
108 @Path("/template/image/{templateName}")
109 @Produces(MediaType.TEXT_XML)
110 public String getImageXml(@PathParam("templateName") String templateName) {
111 Date startTime = new Date();
112 LoggingUtils.setRequestContext("CldsTemplateService: GET template image", getPrincipalName());
113 isAuthorized(permissionReadTemplate);
114 logger.info("GET imageText for templateName=" + templateName);
115 CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
117 LoggingUtils.setTimeContext(startTime, new Date());
118 LoggingUtils.setResponseContext("0", "Get template image success", this.getClass().getName());
119 auditLogger.info("GET template image completed");
120 return template.getImageText();
124 * REST service that retrieves a CLDS template by name from the database.
126 * @param templateName
127 * @return clds template - clds template for the given template name
130 @Path("/template/{templateName}")
131 @Produces(MediaType.APPLICATION_JSON)
132 public CldsTemplate getTemplate(@PathParam("templateName") String templateName) {
133 Date startTime = new Date();
134 LoggingUtils.setRequestContext("CldsTemplateService: GET template", getPrincipalName());
135 isAuthorized(permissionReadTemplate);
136 logger.info("GET model for templateName=" + templateName);
137 CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
138 template.setUserAuthorizedToUpdate(isAuthorizedNoException(permissionUpdateTemplate));
140 LoggingUtils.setTimeContext(startTime, new Date());
141 LoggingUtils.setResponseContext("0", "Get template success", this.getClass().getName());
142 auditLogger.info("GET template completed");
147 * REST service that saves a CLDS template by name in the database.
149 * @param templateName
150 * @throws IOException
151 * @throws JsonMappingException
152 * @throws JsonParseException
155 @Path("/template/{templateName}")
156 @Consumes(MediaType.APPLICATION_JSON)
157 @Produces(MediaType.APPLICATION_JSON)
158 public CldsTemplate putTemplate(@PathParam("templateName") String templateName, CldsTemplate cldsTemplate)
159 throws TransformerException, IOException {
160 Date startTime = new Date();
161 LoggingUtils.setRequestContext("CldsTemplateService: PUT template", getPrincipalName());
162 isAuthorized(permissionUpdateTemplate);
163 logger.info("PUT Template for templateName=" + templateName);
164 logger.info("PUT bpmnText=" + cldsTemplate.getBpmnText());
165 logger.info("PUT propText=" + cldsTemplate.getPropText());
166 logger.info("PUT imageText=" + cldsTemplate.getImageText());
167 cldsTemplate.setName(templateName);
168 String bpmnText = cldsTemplate.getBpmnText();
169 String imageText = cldsTemplate.getImageText();
170 String propText = cldsTemplate.getPropText();
171 cldsTemplate.setBpmnText(bpmnText);
172 cldsTemplate.setImageText(imageText);
173 cldsTemplate.setPropText(propText);
174 logger.info(" bpmnText : " + cldsTemplate.getBpmnText());
175 logger.info(" Image Text : " + cldsTemplate.getImageText());
176 logger.info(" Prop Text : " + cldsTemplate.getPropText());
177 cldsTemplate.save(cldsDao, null);
179 LoggingUtils.setTimeContext(startTime, new Date());
180 LoggingUtils.setResponseContext("0", "Put template success", this.getClass().getName());
181 auditLogger.info("PUT template completed");
186 * REST service that retrieves a list of CLDS template names.
188 * @return template names in JSON
191 @Path("/template-names")
192 @Produces(MediaType.APPLICATION_JSON)
193 public List<ValueItem> getTemplateNames() {
194 Date startTime = new Date();
195 LoggingUtils.setRequestContext("CldsTemplateService: GET template names", getPrincipalName());
196 isAuthorized(permissionReadTemplate);
197 logger.info("GET list of template names");
198 List<ValueItem> names = cldsDao.getTemplateNames();
200 LoggingUtils.setTimeContext(startTime, new Date());
201 LoggingUtils.setResponseContext("0", "Get template names success", this.getClass().getName());
202 auditLogger.info("GET template names completed");