Merge "Validate ids"
[clamp.git] / src / main / java / org / onap / clamp / clds / service / CldsToscaService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 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 import java.util.Optional;
31
32 import javax.annotation.PostConstruct;
33
34 import org.onap.clamp.clds.client.req.policy.PolicyClient;
35 import org.onap.clamp.clds.config.ClampProperties;
36 import org.onap.clamp.clds.dao.CldsDao;
37 import org.onap.clamp.clds.model.CldsToscaModel;
38 import org.onap.clamp.clds.util.LoggingUtils;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.beans.factory.annotation.Value;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.stereotype.Component;
44
45 /**
46  * REST services to manage Tosca Model.
47  */
48 @Component
49 public class CldsToscaService extends SecureServiceBase {
50
51     @Value("${clamp.config.security.permission.type.tosca:permission-type-tosca}")
52     private String                  cldsPermissionTypeTosca;
53     @Value("${clamp.config.security.permission.instance:dev}")
54     private String                  cldsPermissionInstance;
55     private SecureServicePermission permissionReadTosca;
56     private SecureServicePermission permissionUpdateTosca;
57
58     @Autowired
59     private CldsDao                 cldsDao;
60
61     @Autowired
62     private ClampProperties         refProp;
63
64     @Autowired
65     private PolicyClient            policyClient;
66     private LoggingUtils util = new LoggingUtils(logger);
67
68     @PostConstruct
69     private final void initConstruct() {
70         permissionReadTosca = SecureServicePermission.create(cldsPermissionTypeTosca, cldsPermissionInstance, "read");
71         permissionUpdateTosca = SecureServicePermission.create(cldsPermissionTypeTosca, cldsPermissionInstance,
72                 "update");
73     }
74
75     /**
76      * REST service to upload a new Tosca Model or update an existing Tosca
77      * model with new version. This API will parse the Tosca model yaml and
78      * generates a JSON schema out of it.
79      * 
80      * @param toscaModelName
81      *            Tosca model name to be used as a key
82      * @param cldsToscaModel
83      *            Object containing the tosca model yaml
84      * 
85      * @return clds tosca models - list of CLDS tosca models for a given policy
86      *         type
87      */
88     public ResponseEntity<?> parseToscaModelAndSave(String toscaModelName, CldsToscaModel cldsToscaModel) {
89         final Date startTime = new Date();
90         LoggingUtils.setRequestContext("CldsToscaService: Parse Tosca model and save", getPrincipalName());
91         // TODO revisit based on new permissions
92         isAuthorized(permissionUpdateTosca);
93         cldsToscaModel.setToscaModelName(toscaModelName);
94         cldsToscaModel = cldsToscaModel.save(cldsDao, refProp, policyClient, getUserId());
95         LoggingUtils.setTimeContext(startTime, new Date());
96         LoggingUtils.setResponseContext("0", "Parse Tosca model and save success", this.getClass().getName());
97         auditLogger.info("Parse Tosca model and save completed");
98         return new ResponseEntity<>(cldsToscaModel, HttpStatus.CREATED);
99     }
100
101     /**
102      * REST service to retrieve all Tosca models from the CLDS database.
103      * 
104      * @return clds tosca models - list of CLDS tosca models
105      */
106     public List<CldsToscaModel> getAllToscaModels() {
107
108         Date startTime = new Date();
109         LoggingUtils.setRequestContext("CldsToscaService: Get All tosca models", getPrincipalName());
110         // TODO revisit based on new permissions
111         isAuthorized(permissionReadTosca);
112         final List<CldsToscaModel> cldsToscaModels = Optional.ofNullable(cldsDao.getAllToscaModels()).get();
113         LoggingUtils.setTimeContext(startTime, new Date());
114         LoggingUtils.setResponseContext("0", "Get All tosca models success", this.getClass().getName());
115         auditLogger.info("Get All tosca models");
116         return cldsToscaModels;
117     }
118
119     /**
120      * REST service that retrieves a CLDS Tosca model by model name from the
121      * database.
122      * 
123      * @param toscaModelName
124      *            Path param with tosca model name
125      * 
126      * @return clds tosca model - CLDS tosca model for a given tosca model name
127      */
128     public CldsToscaModel getToscaModel(String toscaModelName) {
129         Date startTime = new Date();
130         LoggingUtils.setRequestContext("CldsToscaService: Get tosca models by model name", getPrincipalName());
131         // TODO revisit based on new permissions
132         isAuthorized(permissionReadTosca);
133         final List<CldsToscaModel> cldsToscaModels = Optional.ofNullable(cldsDao.getToscaModelByName(toscaModelName))
134                 .get();
135         LoggingUtils.setTimeContext(startTime, new Date());
136         LoggingUtils.setResponseContext("0", "Get tosca models by model name success", this.getClass().getName());
137         auditLogger.info("GET tosca models by model name completed");
138         return cldsToscaModels.get(0);
139     }
140
141     /**
142      * REST service that retrieves a CLDS Tosca model lists for a policy type
143      * from the database.
144      * 
145      * @param policyType
146      *            The type of the policy
147      * @return clds tosca model - CLDS tosca model for a given policy type
148      */
149     public CldsToscaModel getToscaModelsByPolicyType(String policyType) {
150         Date startTime = new Date();
151         LoggingUtils.setRequestContext("CldsToscaService: Get tosca models by policyType", getPrincipalName());
152         // TODO revisit based on new permissions
153         isAuthorized(permissionReadTosca);
154         final List<CldsToscaModel> cldsToscaModels = Optional.ofNullable(cldsDao.getToscaModelByPolicyType(policyType))
155                 .get();
156         LoggingUtils.setTimeContext(startTime, new Date());
157         LoggingUtils.setResponseContext("0", "Get tosca models by policyType success", this.getClass().getName());
158         auditLogger.info("GET tosca models by policyType completed");
159         return cldsToscaModels.get(0);
160     }
161
162     public ResponseEntity<?> deleteToscaModelById(String toscaModeId) {
163         // TODO
164         return null;
165     }
166     
167     // Created for the integration test
168     public void setLoggingUtil(LoggingUtils utilP) {
169         util = utilP;
170     }
171
172 }