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