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.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;
44 * REST services to manage Tosca Model
47 public class CldsToscaService extends SecureServiceBase {
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;
57 private CldsDao cldsDao;
60 private ClampProperties refProp;
63 private PolicyClient policyClient;
64 private LoggingUtils util = new LoggingUtils(logger);
67 private final void initConstruct() {
68 permissionReadTosca = SecureServicePermission.create(cldsPermissionTypeTosca, cldsPermissionInstance, "read");
69 permissionUpdateTosca = SecureServicePermission.create(cldsPermissionTypeTosca, cldsPermissionInstance,
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.
78 * @param toscaModelName
79 * Tosca model name to be used as a key
80 * @param cldsToscaModel
81 * Object containing the tosca model yaml
83 * @return clds tosca models - list of CLDS tosca models for a given policy
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);
100 * REST service to retrieve all Tosca models from the CLDS database.
102 * @return clds tosca models - list of CLDS tosca models
104 public List<CldsToscaModel> getAllToscaModels() {
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 cldsToscaModels;
118 * REST service that retrieves a CLDS Tosca model by model name from the
121 * @param toscaModelName
122 * Path param with tosca model name
124 * @return clds tosca model - CLDS tosca model for a given tosca model name
126 public 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 cldsToscaModels.get(0);
139 * REST service that retrieves a CLDS Tosca model lists for a policy type
143 * @return clds tosca model - CLDS tosca model for a given policy type
145 public CldsToscaModel getToscaModelsByPolicyType(String policyType) {
146 Date startTime = new Date();
147 LoggingUtils.setRequestContext("CldsToscaService: Get tosca models by policyType", getPrincipalName());
148 // TODO revisit based on new permissions
149 isAuthorized(permissionReadTosca);
150 List<CldsToscaModel> cldsToscaModels = Optional.ofNullable(cldsDao.getToscaModelByPolicyType(policyType)).get();
151 LoggingUtils.setTimeContext(startTime, new Date());
152 LoggingUtils.setResponseContext("0", "Get tosca models by policyType success", this.getClass().getName());
153 auditLogger.info("GET tosca models by policyType completed");
154 return cldsToscaModels.get(0);
157 public ResponseEntity<?> deleteToscaModelById(String toscaModeId) {
162 // Created for the integration test
163 public void setLoggingUtil(LoggingUtils utilP) {