2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
26 package org.onap.clamp.clds.service;
28 import java.util.Collections;
29 import java.util.Date;
30 import java.util.List;
31 import java.util.Optional;
33 import javax.annotation.PostConstruct;
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;
47 * REST services to manage Tosca Model.
50 public class CldsToscaService extends SecureServiceBase {
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;
60 private CldsDao cldsDao;
63 private ClampProperties refProp;
66 private PolicyClient policyClient;
67 private LoggingUtils util = new LoggingUtils(logger);
70 private final void initConstruct() {
71 permissionReadTosca = SecureServicePermission.create(cldsPermissionTypeTosca, cldsPermissionInstance, "read");
72 permissionUpdateTosca = SecureServicePermission.create(cldsPermissionTypeTosca, cldsPermissionInstance,
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.
81 * @param toscaModelName
82 * Tosca model name to be used as a key
83 * @param cldsToscaModel
84 * Object containing the tosca model yaml
86 * @return clds tosca models - list of CLDS tosca models for a given policy
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);
101 * REST service to retrieve all Tosca models from the CLDS database.
103 * @return clds tosca models - list of CLDS tosca models
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);
111 Optional<List<CldsToscaModel>> cldsToscaModels = Optional.ofNullable(cldsDao.getAllToscaModels());
112 auditLogInfo("Get All tosca models", startTime);
113 return cldsToscaModels.orElse(Collections.emptyList());
117 * REST service that retrieves a CLDS Tosca model by model name from the
120 * @param toscaModelName
121 * Path param with tosca model name
123 * @return clds tosca model - CLDS tosca model for a given tosca model name
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);
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);
138 * REST service that retrieves a CLDS Tosca model lists for a policy type
141 * The type of the policy
142 * @return clds tosca model - CLDS tosca model for a given policy type
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);
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);
156 public ResponseEntity<?> deleteToscaModelById(String toscaModeId) {
161 // Created for the integration test
162 public void setLoggingUtil(LoggingUtils utilP) {