2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 * ===================================================================
24 package org.onap.clamp.clds.service;
26 import java.util.Date;
27 import java.util.List;
29 import javax.annotation.PostConstruct;
30 import javax.ws.rs.Consumes;
31 import javax.ws.rs.GET;
32 import javax.ws.rs.PUT;
33 import javax.ws.rs.Path;
34 import javax.ws.rs.PathParam;
35 import javax.ws.rs.Produces;
36 import javax.ws.rs.core.MediaType;
38 import org.onap.clamp.clds.dao.CldsDao;
39 import org.onap.clamp.clds.model.CldsTemplate;
40 import org.onap.clamp.clds.model.ValueItem;
41 import org.onap.clamp.clds.util.LoggingUtils;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.beans.factory.annotation.Value;
44 import org.springframework.stereotype.Component;
47 * Service to save and retrieve the CLDS model attributes.
51 public class CldsTemplateService extends SecureServiceBase {
53 @Value("${clamp.config.security.permission.type.template:permission-type-template}")
54 private String cldsPermissionTypeTemplate;
55 @Value("${clamp.config.security.permission.instance:dev}")
56 private String cldsPermissionInstance;
57 private SecureServicePermission permissionReadTemplate;
58 private SecureServicePermission permissionUpdateTemplate;
61 private final void afterConstruction() {
62 permissionReadTemplate = SecureServicePermission.create(cldsPermissionTypeTemplate, cldsPermissionInstance,
64 permissionUpdateTemplate = SecureServicePermission.create(cldsPermissionTypeTemplate, cldsPermissionInstance,
69 private CldsDao cldsDao;
72 * REST service that retrieves BPMN for a CLDS template name from the
73 * database. This is subset of the json getModel. This is only expected to
74 * be used for testing purposes, not by the UI.
77 * @return bpmn xml text - content of bpmn given name
80 @Path("/template/bpmn/{templateName}")
81 @Produces(MediaType.TEXT_XML)
82 public String getBpmnTemplate(@PathParam("templateName") String templateName) {
83 Date startTime = new Date();
84 LoggingUtils.setRequestContext("CldsTemplateService: GET template bpmn", getPrincipalName());
85 isAuthorized(permissionReadTemplate);
86 logger.info("GET bpmnText for templateName=" + templateName);
87 CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
89 LoggingUtils.setTimeContext(startTime, new Date());
90 LoggingUtils.setResponseContext("0", "Get template bpmn success", this.getClass().getName());
91 auditLogger.info("GET template bpmn completed");
92 return template.getBpmnText();
96 * REST service that retrieves image for a CLDS template name from the
97 * database. This is subset of the json getModel. This is only expected to
98 * be used for testing purposes, not by the UI.
100 * @param templateName
101 * @return image xml text - content of image given name
104 @Path("/template/image/{templateName}")
105 @Produces(MediaType.TEXT_XML)
106 public String getImageXml(@PathParam("templateName") String templateName) {
107 Date startTime = new Date();
108 LoggingUtils.setRequestContext("CldsTemplateService: GET template image", getPrincipalName());
109 isAuthorized(permissionReadTemplate);
110 logger.info("GET imageText for templateName=" + templateName);
111 CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
113 LoggingUtils.setTimeContext(startTime, new Date());
114 LoggingUtils.setResponseContext("0", "Get template image success", this.getClass().getName());
115 auditLogger.info("GET template image completed");
116 return template.getImageText();
120 * REST service that retrieves a CLDS template by name from the database.
122 * @param templateName
123 * @return clds template - clds template for the given template name
126 @Path("/template/{templateName}")
127 @Produces(MediaType.APPLICATION_JSON)
128 public CldsTemplate getTemplate(@PathParam("templateName") String templateName) {
129 Date startTime = new Date();
130 LoggingUtils.setRequestContext("CldsTemplateService: GET template", getPrincipalName());
131 isAuthorized(permissionReadTemplate);
132 logger.info("GET model for templateName=" + templateName);
133 CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
134 template.setUserAuthorizedToUpdate(isAuthorizedNoException(permissionUpdateTemplate));
136 LoggingUtils.setTimeContext(startTime, new Date());
137 LoggingUtils.setResponseContext("0", "Get template success", this.getClass().getName());
138 auditLogger.info("GET template completed");
143 * REST service that saves a CLDS template by name in the database.
145 * @param templateName
146 * @param cldsTemplate
147 * @return The CldsTemplate modified and saved in DB
150 @Path("/template/{templateName}")
151 @Consumes(MediaType.APPLICATION_JSON)
152 @Produces(MediaType.APPLICATION_JSON)
153 public CldsTemplate putTemplate(@PathParam("templateName") String templateName, CldsTemplate cldsTemplate) {
154 Date startTime = new Date();
155 LoggingUtils.setRequestContext("CldsTemplateService: PUT template", getPrincipalName());
156 isAuthorized(permissionUpdateTemplate);
157 logger.info("PUT Template for templateName=" + templateName);
158 logger.info("PUT bpmnText=" + cldsTemplate.getBpmnText());
159 logger.info("PUT propText=" + cldsTemplate.getPropText());
160 logger.info("PUT imageText=" + cldsTemplate.getImageText());
161 cldsTemplate.setName(templateName);
162 cldsTemplate.save(cldsDao, null);
164 LoggingUtils.setTimeContext(startTime, new Date());
165 LoggingUtils.setResponseContext("0", "Put template success", this.getClass().getName());
166 auditLogger.info("PUT template completed");
171 * REST service that retrieves a list of CLDS template names.
173 * @return template names in JSON
176 @Path("/template-names")
177 @Produces(MediaType.APPLICATION_JSON)
178 public List<ValueItem> getTemplateNames() {
179 Date startTime = new Date();
180 LoggingUtils.setRequestContext("CldsTemplateService: GET template names", getPrincipalName());
181 isAuthorized(permissionReadTemplate);
182 logger.info("GET list of template names");
183 List<ValueItem> names = cldsDao.getTemplateNames();
185 LoggingUtils.setTimeContext(startTime, new Date());
186 LoggingUtils.setResponseContext("0", "Get template names success", this.getClass().getName());
187 auditLogger.info("GET template names completed");