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;
31 import javax.annotation.PostConstruct;
33 import org.onap.clamp.clds.dao.CldsDao;
34 import org.onap.clamp.clds.model.CldsDictionary;
35 import org.onap.clamp.clds.model.CldsDictionaryItem;
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 dictionary and dictionary items for Tosca Model.
47 public class CldsDictionaryService 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;
59 private LoggingUtils util = new LoggingUtils(logger);
63 private final void initConstruct() {
64 permissionReadTosca = SecureServicePermission.create(cldsPermissionTypeTosca, cldsPermissionInstance, "read");
65 permissionUpdateTosca = SecureServicePermission.create(cldsPermissionTypeTosca, cldsPermissionInstance,
70 * REST Service that creates or Updates a Dictionary.
72 * @param dictionaryName dictionary name
73 * @param cldsDictionary clds dictionary
74 * @return CldsDictionary that was created in DB.
76 public ResponseEntity<CldsDictionary> createOrUpdateDictionary(String dictionaryName,
77 CldsDictionary cldsDictionary) {
78 final Date startTime = new Date();
79 LoggingUtils.setRequestContext("CldsDictionaryService: createOrUpdateDictionary", getPrincipalName());
80 // TODO revisit based on new permissions
81 isAuthorized(permissionUpdateTosca);
82 if (cldsDictionary == null) {
83 cldsDictionary = new CldsDictionary();
84 cldsDictionary.setDictionaryName(dictionaryName);
86 cldsDictionary.save(dictionaryName, cldsDao, getUserId());
87 auditLogInfo("createOrUpdateDictionary", startTime);
88 return new ResponseEntity<>(cldsDictionary, HttpStatus.OK);
92 * REST Service that creates or Updates a Dictionary Elements for dictionary
95 * @param dictionaryName dictionary name
96 * @param dictionaryItem dictionary item
97 * @return CldsDictionaryItem A dictionary items that was created or updated
100 public ResponseEntity<CldsDictionaryItem> createOrUpdateDictionaryElements(String dictionaryName,
101 CldsDictionaryItem dictionaryItem) {
102 final Date startTime = new Date();
103 LoggingUtils.setRequestContext("CldsDictionaryService: createOrUpdateDictionaryElements", getPrincipalName());
104 // TODO revisit based on new permissions
105 isAuthorized(permissionUpdateTosca);
106 dictionaryItem.save(dictionaryName, cldsDao, getUserId());
107 auditLogInfo("createOrUpdateDictionaryElements", startTime);
108 return new ResponseEntity<>(dictionaryItem, HttpStatus.OK);
112 * Rest Service that retrieves all CLDS dictionary in DB.
114 * @return CldsDictionary List List of CldsDictionary available in DB
116 public ResponseEntity<List<CldsDictionary>> getAllDictionaryNames() {
117 final Date startTime = new Date();
118 LoggingUtils.setRequestContext("CldsDictionaryService: getAllDictionaryNames", getPrincipalName());
119 // TODO revisit based on new permissions
120 isAuthorized(permissionReadTosca);
121 List<CldsDictionary> dictionaries = cldsDao.getDictionary(null, null);
122 auditLogInfo("getAllDictionaryNames", startTime);
123 return new ResponseEntity<>(dictionaries, HttpStatus.OK);
127 * Rest Service that retrieves all CLDS dictionary items in DB for a give
130 * @param dictionaryName dictionary name
131 * @return CldsDictionaryItem list List of CLDS Dictionary items for a given
134 public ResponseEntity<List<CldsDictionaryItem>> getDictionaryElementsByName(String dictionaryName) {
135 final Date startTime = new Date();
136 LoggingUtils.setRequestContext("CldsDictionaryService: getDictionaryElementsByName", getPrincipalName());
137 // TODO revisit based on new permissions
138 isAuthorized(permissionReadTosca);
139 List<CldsDictionaryItem> dictionaryItems = cldsDao.getDictionaryElements(dictionaryName, null, null);
140 auditLogInfo("getDictionaryElementsByName", startTime);
141 return new ResponseEntity<>(dictionaryItems, HttpStatus.OK);
144 public ResponseEntity<?> deleteDictionary() {
148 // Created for the integration test
149 public void setLoggingUtil(LoggingUtils utilP) {