Merge "Improved user guide details"
[clamp.git] / src / main / java / org / onap / clamp / clds / service / CldsTemplateService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
6  *                             reserved.
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
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
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  * ===================================================================
23  *
24  */
25
26 package org.onap.clamp.clds.service;
27
28 import java.util.Date;
29 import java.util.List;
30
31 import javax.annotation.PostConstruct;
32 import javax.servlet.http.HttpServletRequest;
33
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;
41
42 /**
43  * Service to save and retrieve the CLDS model attributes.
44  */
45 @Component
46 public class CldsTemplateService extends SecureServiceBase {
47
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;
54     @Autowired
55     private HttpServletRequest request;
56
57     @PostConstruct
58     private final void afterConstruction() {
59         permissionReadTemplate = SecureServicePermission.create(cldsPermissionTypeTemplate,
60             cldsPermissionInstance, "read");
61         permissionUpdateTemplate = SecureServicePermission.create(cldsPermissionTypeTemplate,
62             cldsPermissionInstance, "update");
63     }
64
65     @Autowired
66     private CldsDao cldsDao;
67     private LoggingUtils util = new LoggingUtils(logger);
68
69     /**
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.
73      *
74      * @param templateName template name
75      * @return bpmn xml text - content of bpmn given name
76      */
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);
82
83         CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
84         auditLogInfo(util, "GET template bpmn", startTime);
85         return template.getBpmnText();
86     }
87
88     /**
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.
92      *
93      * @param templateName template name
94      * @return image xml text - content of image given name
95      */
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);
101
102         CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
103         auditLogInfo(util, "GET template image", startTime);
104         return template.getImageText();
105     }
106
107     /**
108      * REST service that retrieves a CLDS template by name from the database.
109      *
110      * @param templateName template name
111      * @return clds template - clds template for the given template name
112      */
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);
118
119         CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
120         template.setUserAuthorizedToUpdate(isAuthorizedNoException(permissionUpdateTemplate));
121         auditLogInfo(util, "GET template", startTime);
122         return template;
123     }
124
125     /**
126      * REST service that saves a CLDS template by name in the database.
127      *
128      * @param templateName template name
129      * @param cldsTemplate clds template
130      * @return The CldsTemplate modified and saved in DB
131      */
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);
143         return cldsTemplate;
144     }
145
146     /**
147      * REST service that retrieves a list of CLDS template names.
148      *
149      * @return template names in JSON
150      */
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");
156
157         List<ValueItem> names = cldsDao.getTemplateNames();
158         auditLogInfo(util, "GET template names", startTime);
159         return names;
160     }
161
162     // Created for the integration test
163     public void setLoggingUtil(LoggingUtils utilP) {
164         util = utilP;
165     }
166 }