2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.clds.service;
26 import java.util.Date;
27 import java.util.List;
28 import java.util.Optional;
30 import javax.annotation.PostConstruct;
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;
42 * REST services to manage Tosca Model
45 public class CldsToscaService extends SecureServiceBase {
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;
55 private CldsDao cldsDao;
58 private final void initConstruct() {
59 permissionReadTosca = SecureServicePermission.create(cldsPermissionTypeTosca, cldsPermissionInstance, "read");
60 permissionUpdateTosca = SecureServicePermission.create(cldsPermissionTypeTosca, cldsPermissionInstance, "update");
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.
67 * @param toscaModelName
68 * Tosca model name to be used as a key
69 * @param cldsToscaModel
70 * Object containing the tosca model yaml
72 * @return clds tosca models - list of CLDS tosca models for a given policy type
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);
88 * REST service to retrieve all Tosca models from the CLDS database.
90 * @return clds tosca models - list of CLDS tosca models
92 public ResponseEntity<List<CldsToscaModel>> getAllToscaModels() {
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);
106 * REST service that retrieves a CLDS Tosca model by model name from the database.
108 * @param toscaModelName
109 * Path param with tosca model name
111 * @return clds tosca model - CLDS tosca model for a given tosca model name
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);
127 * REST service that retrieves a CLDS Tosca model lists for a policy type from the database.
130 * @return clds tosca model - CLDS tosca model for a given policy type
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);
144 public ResponseEntity<?> deleteToscaModelById(String toscaModeId) {