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