Merge "Allow additional parameters to deployment"
[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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.clamp.clds.service;
25
26 import com.fasterxml.jackson.core.JsonParseException;
27 import com.fasterxml.jackson.databind.JsonMappingException;
28
29 import java.io.IOException;
30 import java.util.Date;
31 import java.util.List;
32
33 import javax.annotation.PostConstruct;
34 import javax.ws.rs.Consumes;
35 import javax.ws.rs.GET;
36 import javax.ws.rs.PUT;
37 import javax.ws.rs.Path;
38 import javax.ws.rs.PathParam;
39 import javax.ws.rs.Produces;
40 import javax.ws.rs.core.MediaType;
41
42 import org.onap.clamp.clds.dao.CldsDao;
43 import org.onap.clamp.clds.model.CldsTemplate;
44 import org.onap.clamp.clds.model.ValueItem;
45 import org.onap.clamp.clds.util.LoggingUtils;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.beans.factory.annotation.Value;
48 import org.springframework.stereotype.Component;
49
50 /**
51  * Service to save and retrieve the CLDS model attributes.
52  */
53 @Component
54 @Path("/cldsTempate")
55 public class CldsTemplateService extends SecureServiceBase {
56
57     @Value("${clamp.config.security.permission.type.template:permission-type-template}")
58     private String cldsPermissionTypeTemplate;
59     @Value("${clamp.config.security.permission.instance:dev}")
60     private String cldsPermissionInstance;
61     private SecureServicePermission permissionReadTemplate;
62     private SecureServicePermission permissionUpdateTemplate;
63
64     @PostConstruct
65     private final void afterConstruction() {
66         permissionReadTemplate = SecureServicePermission.create(cldsPermissionTypeTemplate, cldsPermissionInstance,
67                 "read");
68         permissionUpdateTemplate = SecureServicePermission.create(cldsPermissionTypeTemplate, cldsPermissionInstance,
69                 "update");
70     }
71
72     @Autowired
73     private CldsDao cldsDao;
74
75     /**
76      * REST service that retrieves BPMN for a CLDS template name from the
77      * database. This is subset of the json getModel. This is only expected to
78      * be used for testing purposes, not by the UI.
79      *
80      * @param templateName
81      * @return bpmn xml text - content of bpmn given name
82      */
83     @GET
84     @Path("/template/bpmn/{templateName}")
85     @Produces(MediaType.TEXT_XML)
86     public String getBpmnTemplate(@PathParam("templateName") String templateName) {
87         Date startTime = new Date();
88         LoggingUtils.setRequestContext("CldsTemplateService: GET template bpmn", getPrincipalName());
89         isAuthorized(permissionReadTemplate);
90         logger.info("GET bpmnText for templateName=" + templateName);
91         CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
92         // audit log
93         LoggingUtils.setTimeContext(startTime, new Date());
94         LoggingUtils.setResponseContext("0", "Get template bpmn success", this.getClass().getName());
95         auditLogger.info("GET template bpmn completed");
96         return template.getBpmnText();
97     }
98
99     /**
100      * REST service that retrieves image for a CLDS template name from the
101      * database. This is subset of the json getModel. This is only expected to
102      * be used for testing purposes, not by the UI.
103      *
104      * @param templateName
105      * @return image xml text - content of image given name
106      */
107     @GET
108     @Path("/template/image/{templateName}")
109     @Produces(MediaType.TEXT_XML)
110     public String getImageXml(@PathParam("templateName") String templateName) {
111         Date startTime = new Date();
112         LoggingUtils.setRequestContext("CldsTemplateService: GET template image", getPrincipalName());
113         isAuthorized(permissionReadTemplate);
114         logger.info("GET imageText for templateName=" + templateName);
115         CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
116         // audit log
117         LoggingUtils.setTimeContext(startTime, new Date());
118         LoggingUtils.setResponseContext("0", "Get template image success", this.getClass().getName());
119         auditLogger.info("GET template image completed");
120         return template.getImageText();
121     }
122
123     /**
124      * REST service that retrieves a CLDS template by name from the database.
125      *
126      * @param templateName
127      * @return clds template - clds template for the given template name
128      */
129     @GET
130     @Path("/template/{templateName}")
131     @Produces(MediaType.APPLICATION_JSON)
132     public CldsTemplate getTemplate(@PathParam("templateName") String templateName) {
133         Date startTime = new Date();
134         LoggingUtils.setRequestContext("CldsTemplateService: GET template", getPrincipalName());
135         isAuthorized(permissionReadTemplate);
136         logger.info("GET model for  templateName=" + templateName);
137         CldsTemplate template = CldsTemplate.retrieve(cldsDao, templateName, false);
138         template.setUserAuthorizedToUpdate(isAuthorizedNoException(permissionUpdateTemplate));
139         // audit log
140         LoggingUtils.setTimeContext(startTime, new Date());
141         LoggingUtils.setResponseContext("0", "Get template success", this.getClass().getName());
142         auditLogger.info("GET template completed");
143         return template;
144     }
145
146     /**
147      * REST service that saves a CLDS template by name in the database.
148      *
149      * @param templateName
150      * @throws IOException
151      * @throws JsonMappingException
152      * @throws JsonParseException
153      */
154     @PUT
155     @Path("/template/{templateName}")
156     @Consumes(MediaType.APPLICATION_JSON)
157     @Produces(MediaType.APPLICATION_JSON)
158     public CldsTemplate putTemplate(@PathParam("templateName") String templateName, CldsTemplate cldsTemplate) {
159         Date startTime = new Date();
160         LoggingUtils.setRequestContext("CldsTemplateService: PUT template", getPrincipalName());
161         isAuthorized(permissionUpdateTemplate);
162         logger.info("PUT Template for  templateName=" + templateName);
163         logger.info("PUT bpmnText=" + cldsTemplate.getBpmnText());
164         logger.info("PUT propText=" + cldsTemplate.getPropText());
165         logger.info("PUT imageText=" + cldsTemplate.getImageText());
166         cldsTemplate.setName(templateName);
167         cldsTemplate.save(cldsDao, null);
168         // audit log
169         LoggingUtils.setTimeContext(startTime, new Date());
170         LoggingUtils.setResponseContext("0", "Put template success", this.getClass().getName());
171         auditLogger.info("PUT template completed");
172         return cldsTemplate;
173     }
174
175     /**
176      * REST service that retrieves a list of CLDS template names.
177      *
178      * @return template names in JSON
179      */
180     @GET
181     @Path("/template-names")
182     @Produces(MediaType.APPLICATION_JSON)
183     public List<ValueItem> getTemplateNames() {
184         Date startTime = new Date();
185         LoggingUtils.setRequestContext("CldsTemplateService: GET template names", getPrincipalName());
186         isAuthorized(permissionReadTemplate);
187         logger.info("GET list of template names");
188         List<ValueItem> names = cldsDao.getTemplateNames();
189         // audit log
190         LoggingUtils.setTimeContext(startTime, new Date());
191         LoggingUtils.setResponseContext("0", "Get template names success", this.getClass().getName());
192         auditLogger.info("GET template names completed");
193         return names;
194     }
195 }