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 void initConstruct() {
64 permissionReadTosca = SecureServicePermission.create(cldsPermissionTypeTosca, cldsPermissionInstance, "read");
65 permissionUpdateTosca = SecureServicePermission.create(cldsPermissionTypeTosca, cldsPermissionInstance,
70 * REST Service that creates a Dictionary.
72 * @param dictionaryName dictionary name
73 * @return CldsDictionary that was created in DB.
75 public CldsDictionary createDictionary(String dictionaryName) {
76 CldsDictionary cldsDictionary = new CldsDictionary();
77 cldsDictionary.setDictionaryName(dictionaryName);
78 cldsDictionary.save(cldsDictionary.getDictionaryName(), cldsDao, getUserId());
79 return cldsDictionary;
83 * REST Service that creates or Updates a Dictionary.
84 * Used in clds-services.xml
86 * @param cldsDictionary clds dictionary
87 * @return ResponseEntity with CldsDictionary that was created in DB.
89 public ResponseEntity<CldsDictionary> createOrUpdateDictionary(String dictionaryName,
90 CldsDictionary cldsDictionary) {
92 Date startTime = new Date();
93 LoggingUtils.setRequestContext("CldsDictionaryService: createOrUpdateDictionary", getPrincipalName());
94 // TODO revisit based on new permissions
95 isAuthorized(permissionUpdateTosca);
97 if (cldsDictionary == null) {
99 cldsDictionary = createDictionary(dictionaryName);
102 if (cldsDictionary.getDictionaryName() == null) {
103 cldsDictionary.setDictionaryName(dictionaryName);
106 cldsDictionary.save(cldsDictionary.getDictionaryName(), cldsDao, getUserId());
109 LoggingUtils.setTimeContext(startTime, new Date());
110 LoggingUtils.setResponseContext("0", "createOrUpdateDictionary success", this.getClass().getName());
111 auditLogger.info("createOrUpdateDictionary completed");
113 return new ResponseEntity<>(cldsDictionary, HttpStatus.OK);
117 * REST Service that creates or Updates a Dictionary Elements for dictionary
120 * @param dictionaryName dictionary name
121 * @param dictionaryItem dictionary item
122 * @return CldsDictionaryItem A dictionary items that was created or updated
125 public ResponseEntity<CldsDictionaryItem> createOrUpdateDictionaryElements(String dictionaryName,
126 CldsDictionaryItem dictionaryItem) {
127 final Date startTime = new Date();
128 LoggingUtils.setRequestContext("CldsDictionaryService: createOrUpdateDictionaryElements",
130 // TODO revisit based on new permissions
131 isAuthorized(permissionUpdateTosca);
132 dictionaryItem.save(dictionaryName, cldsDao, getUserId());
133 auditLogInfo("createOrUpdateDictionaryElements", startTime);
134 return new ResponseEntity<>(dictionaryItem, HttpStatus.OK);
138 * Rest Service that retrieves all CLDS dictionary in DB.
140 * @return CldsDictionary List List of CldsDictionary available in DB
142 public ResponseEntity<List<CldsDictionary>> getAllDictionaryNames() {
143 final Date startTime = new Date();
144 LoggingUtils.setRequestContext("CldsDictionaryService: getAllDictionaryNames", getPrincipalName());
145 // TODO revisit based on new permissions
146 isAuthorized(permissionReadTosca);
147 List<CldsDictionary> dictionaries = cldsDao.getDictionary(null, null);
148 auditLogInfo("getAllDictionaryNames", startTime);
149 return new ResponseEntity<>(dictionaries, HttpStatus.OK);
153 * Rest Service that retrieves all CLDS dictionary items in DB for a give
156 * @param dictionaryName dictionary name
157 * @return CldsDictionaryItem list List of CLDS Dictionary items for a given
160 public ResponseEntity<List<CldsDictionaryItem>> getDictionaryElementsByName(String dictionaryName) {
161 final Date startTime = new Date();
162 LoggingUtils.setRequestContext("CldsDictionaryService: getDictionaryElementsByName", getPrincipalName());
163 // TODO revisit based on new permissions
164 isAuthorized(permissionReadTosca);
165 List<CldsDictionaryItem> dictionaryItems = cldsDao.getDictionaryElements(dictionaryName, null, null);
166 auditLogInfo("getDictionaryElementsByName", startTime);
167 return new ResponseEntity<>(dictionaryItems, HttpStatus.OK);
170 public ResponseEntity<?> deleteDictionary() {
174 // Created for the integration test
175 public void setLoggingUtil(LoggingUtils utilP) {