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.onap.clamp.clds.util.ONAPLogConstants;
39 import org.slf4j.event.Level;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.beans.factory.annotation.Value;
42 import org.springframework.stereotype.Component;
45 * Service to save and retrieve the CLDS model attributes.
48 public class CldsTemplateService extends SecureServiceBase {
50 @Value("${clamp.config.security.permission.type.template:permission-type-template}")
51 private String cldsPermissionTypeTemplate;
52 @Value("${clamp.config.security.permission.instance:dev}")
53 private String cldsPermissionInstance;
54 private SecureServicePermission permissionReadTemplate;
55 private SecureServicePermission permissionUpdateTemplate;
57 private HttpServletRequest request;
60 private final void afterConstruction() {
61 permissionReadTemplate = SecureServicePermission.create(cldsPermissionTypeTemplate, cldsPermissionInstance,
63 permissionUpdateTemplate = SecureServicePermission.create(cldsPermissionTypeTemplate, cldsPermissionInstance,
68 private CldsDao cldsDao;
69 private LoggingUtils util = new LoggingUtils(logger);
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.
76 * @param templateName template name
77 * @return bpmn xml text - content of bpmn given name
79 public String getBpmnTemplate(String templateName) {
80 util.entering(request, "CldsTemplateService: GET template bpmn");
81 Date startTime = new Date();
82 isAuthorized(permissionReadTemplate);
83 logger.info("GET bpmnText for templateName=" + templateName);
84 final CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
86 LoggingUtils.setTimeContext(startTime, new Date());
87 auditLogger.info("GET template bpmn completed");
88 util.exiting("200", "Get template bpmn success", Level.INFO, ONAPLogConstants.ResponseStatus.COMPLETED);
89 return template.getBpmnText();
93 * REST service that retrieves image for a CLDS template name from the
94 * database. This is subset of the json getModel. This is only expected to
95 * be used for testing purposes, not by the UI.
97 * @param templateName template name
98 * @return image xml text - content of image given name
100 public String getImageXml(String templateName) {
101 util.entering(request, "CldsTemplateService: GET template image");
102 Date startTime = new Date();
103 isAuthorized(permissionReadTemplate);
104 logger.info("GET imageText for templateName=" + templateName);
105 final CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
107 LoggingUtils.setTimeContext(startTime, new Date());
108 auditLogger.info("GET template image completed");
109 util.exiting("200", "Get template image success", Level.INFO, ONAPLogConstants.ResponseStatus.COMPLETED);
110 return template.getImageText();
114 * REST service that retrieves a CLDS template by name from the database.
116 * @param templateName template name
117 * @return clds template - clds template for the given template name
119 public CldsTemplate getTemplate(String templateName) {
120 util.entering(request, "CldsTemplateService: GET template");
121 final Date startTime = new Date();
122 isAuthorized(permissionReadTemplate);
123 logger.info("GET model for templateName=" + templateName);
124 CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
125 template.setUserAuthorizedToUpdate(isAuthorizedNoException(permissionUpdateTemplate));
127 LoggingUtils.setTimeContext(startTime, new Date());
128 auditLogger.info("GET template completed");
129 util.exiting("200", "Get template success", Level.INFO, ONAPLogConstants.ResponseStatus.COMPLETED);
134 * REST service that saves a CLDS template by name in the database.
136 * @param templateName template name
137 * @param cldsTemplate clds template
138 * @return The CldsTemplate modified and saved in DB
140 public CldsTemplate putTemplate(String templateName, CldsTemplate cldsTemplate) {
141 util.entering(request, "CldsTemplateService: PUT template");
142 final Date startTime = new Date();
143 isAuthorized(permissionUpdateTemplate);
144 logger.info("PUT Template for templateName=" + templateName);
145 logger.info("PUT bpmnText=" + cldsTemplate.getBpmnText());
146 logger.info("PUT propText=" + cldsTemplate.getPropText());
147 logger.info("PUT imageText=" + cldsTemplate.getImageText());
148 cldsTemplate.setName(templateName);
149 cldsTemplate.save(cldsDao, null);
151 LoggingUtils.setTimeContext(startTime, new Date());
152 auditLogger.info("PUT template completed");
153 util.exiting("200", "Put template success", Level.INFO, ONAPLogConstants.ResponseStatus.COMPLETED);
158 * REST service that retrieves a list of CLDS template names.
160 * @return template names in JSON
162 public List<ValueItem> getTemplateNames() {
163 util.entering(request, "CldsTemplateService: GET template names");
164 Date startTime = new Date();
165 isAuthorized(permissionReadTemplate);
166 logger.info("GET list of template names");
167 final List<ValueItem> names = cldsDao.getTemplateNames();
169 LoggingUtils.setTimeContext(startTime, new Date());
170 auditLogger.info("GET template names completed");
171 util.exiting("200", "Get template names success", Level.INFO, ONAPLogConstants.ResponseStatus.COMPLETED);
175 // Created for the integration test
176 public void setLoggingUtil(LoggingUtils utilP) {