Merge changes Ib3c339b2,I47888da8,Ifdc5a4d9,I2f7b7417,If10e2dff, ...
[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  * 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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * 
22  */
23
24 package org.onap.clamp.clds.service;
25
26 import java.util.Date;
27 import java.util.List;
28
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;
37
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;
45
46 /**
47  * Service to save and retrieve the CLDS model attributes.
48  */
49 @Component
50 @Path("/cldsTempate")
51 public class CldsTemplateService extends SecureServiceBase {
52
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;
59
60     @PostConstruct
61     private final void afterConstruction() {
62         permissionReadTemplate = SecureServicePermission.create(cldsPermissionTypeTemplate, cldsPermissionInstance,
63                 "read");
64         permissionUpdateTemplate = SecureServicePermission.create(cldsPermissionTypeTemplate, cldsPermissionInstance,
65                 "update");
66     }
67
68     @Autowired
69     private CldsDao cldsDao;
70
71     /**
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.
75      *
76      * @param templateName
77      * @return bpmn xml text - content of bpmn given name
78      */
79     @GET
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);
88         // audit log
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();
93     }
94
95     /**
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.
99      *
100      * @param templateName
101      * @return image xml text - content of image given name
102      */
103     @GET
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);
112         // audit log
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();
117     }
118
119     /**
120      * REST service that retrieves a CLDS template by name from the database.
121      *
122      * @param templateName
123      * @return clds template - clds template for the given template name
124      */
125     @GET
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));
135         // audit log
136         LoggingUtils.setTimeContext(startTime, new Date());
137         LoggingUtils.setResponseContext("0", "Get template success", this.getClass().getName());
138         auditLogger.info("GET template completed");
139         return template;
140     }
141
142     /**
143      * REST service that saves a CLDS template by name in the database.
144      *
145      * @param templateName
146      * @param cldsTemplate
147      * @return The CldsTemplate modified and saved in DB
148      */
149     @PUT
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);
163         // audit log
164         LoggingUtils.setTimeContext(startTime, new Date());
165         LoggingUtils.setResponseContext("0", "Put template success", this.getClass().getName());
166         auditLogger.info("PUT template completed");
167         return cldsTemplate;
168     }
169
170     /**
171      * REST service that retrieves a list of CLDS template names.
172      *
173      * @return template names in JSON
174      */
175     @GET
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();
184         // audit log
185         LoggingUtils.setTimeContext(startTime, new Date());
186         LoggingUtils.setResponseContext("0", "Get template names success", this.getClass().getName());
187         auditLogger.info("GET template names completed");
188         return names;
189     }
190 }