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