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