2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Modifications Copyright (c) 2019 Samsung
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END============================================
22 * ===================================================================
26 package org.onap.clamp.clds.service;
28 import java.util.Date;
29 import java.util.List;
31 import javax.annotation.PostConstruct;
32 import javax.servlet.http.HttpServletRequest;
34 import org.onap.clamp.clds.dao.CldsDao;
35 import org.onap.clamp.clds.model.CldsTemplate;
36 import org.onap.clamp.clds.model.ValueItem;
37 import org.onap.clamp.clds.util.LoggingUtils;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.beans.factory.annotation.Value;
40 import org.springframework.stereotype.Component;
43 * Service to save and retrieve the CLDS model attributes.
46 public class CldsTemplateService extends SecureServiceBase {
48 @Value("${clamp.config.security.permission.type.template:permission-type-template}")
49 private String cldsPermissionTypeTemplate;
50 @Value("${clamp.config.security.permission.instance:dev}")
51 private String cldsPermissionInstance;
52 private SecureServicePermission permissionReadTemplate;
53 private SecureServicePermission permissionUpdateTemplate;
55 private HttpServletRequest request;
58 private final void afterConstruction() {
59 permissionReadTemplate = SecureServicePermission.create(cldsPermissionTypeTemplate,
60 cldsPermissionInstance, "read");
61 permissionUpdateTemplate = SecureServicePermission.create(cldsPermissionTypeTemplate,
62 cldsPermissionInstance, "update");
66 private CldsDao cldsDao;
67 private LoggingUtils util = new LoggingUtils(logger);
70 * REST service that retrieves BPMN for a CLDS template name from the
71 * database. This is subset of the json getModel. This is only expected to
72 * be used for testing purposes, not by the UI.
74 * @param templateName template name
75 * @return bpmn xml text - content of bpmn given name
77 public String getBpmnTemplate(String templateName) {
78 util.entering(request, "CldsTemplateService: GET template bpmn");
79 final Date startTime = new Date();
80 isAuthorized(permissionReadTemplate);
81 logger.info("GET bpmnText for templateName=" + templateName);
83 CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
84 auditLogInfo(util, "GET template bpmn", startTime);
85 return template.getBpmnText();
89 * REST service that retrieves image for a CLDS template name from the
90 * database. This is subset of the json getModel. This is only expected to
91 * be used for testing purposes, not by the UI.
93 * @param templateName template name
94 * @return image xml text - content of image given name
96 public String getImageXml(String templateName) {
97 util.entering(request, "CldsTemplateService: GET template image");
98 final Date startTime = new Date();
99 isAuthorized(permissionReadTemplate);
100 logger.info("GET imageText for templateName=" + templateName);
102 CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
103 auditLogInfo(util, "GET template image", startTime);
104 return template.getImageText();
108 * REST service that retrieves a CLDS template by name from the database.
110 * @param templateName template name
111 * @return clds template - clds template for the given template name
113 public CldsTemplate getTemplate(String templateName) {
114 util.entering(request, "CldsTemplateService: GET template");
115 final Date startTime = new Date();
116 isAuthorized(permissionReadTemplate);
117 logger.info("GET model for templateName=" + templateName);
119 CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
120 template.setUserAuthorizedToUpdate(isAuthorizedNoException(permissionUpdateTemplate));
121 auditLogInfo(util, "GET template", startTime);
126 * REST service that saves a CLDS template by name in the database.
128 * @param templateName template name
129 * @param cldsTemplate clds template
130 * @return The CldsTemplate modified and saved in DB
132 public CldsTemplate putTemplate(String templateName, CldsTemplate cldsTemplate) {
133 util.entering(request, "CldsTemplateService: PUT template");
134 final Date startTime = new Date();
135 isAuthorized(permissionUpdateTemplate);
136 logger.info("PUT Template for templateName=" + templateName);
137 logger.info("PUT bpmnText=" + cldsTemplate.getBpmnText());
138 logger.info("PUT propText=" + cldsTemplate.getPropText());
139 logger.info("PUT imageText=" + cldsTemplate.getImageText());
140 cldsTemplate.setName(templateName);
141 cldsTemplate.save(cldsDao, null);
142 auditLogInfo(util, "PUT template", startTime);
147 * REST service that retrieves a list of CLDS template names.
149 * @return template names in JSON
151 public List<ValueItem> getTemplateNames() {
152 util.entering(request, "CldsTemplateService: GET template names");
153 final Date startTime = new Date();
154 isAuthorized(permissionReadTemplate);
155 logger.info("GET list of template names");
157 List<ValueItem> names = cldsDao.getTemplateNames();
158 auditLogInfo(util, "GET template names", startTime);
162 // Created for the integration test
163 public void setLoggingUtil(LoggingUtils utilP) {