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;
29 import javax.annotation.PostConstruct;
31 import org.onap.clamp.clds.dao.CldsDao;
32 import org.onap.clamp.clds.model.CldsDictionary;
33 import org.onap.clamp.clds.model.CldsDictionaryItem;
34 import org.onap.clamp.clds.util.LoggingUtils;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.beans.factory.annotation.Value;
37 import org.springframework.http.HttpStatus;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.stereotype.Component;
42 * REST services to manage dictionary and dictionary items for Tosca Model.
45 public class CldsDictionaryService extends SecureServiceBase {
47 @Value("${clamp.config.security.permission.type.tosca:permission-type-tosca}")
48 private String cldsPermissionTypeTosca;
49 @Value("${clamp.config.security.permission.instance:dev}")
50 private String cldsPermissionInstance;
51 private SecureServicePermission permissionReadTosca;
52 private SecureServicePermission permissionUpdateTosca;
55 private CldsDao cldsDao;
57 private LoggingUtils util = new LoggingUtils(logger);
61 private final void initConstruct() {
62 permissionReadTosca = SecureServicePermission.create(cldsPermissionTypeTosca, cldsPermissionInstance, "read");
63 permissionUpdateTosca = SecureServicePermission.create(cldsPermissionTypeTosca, cldsPermissionInstance,
68 * REST Service that creates or Updates a Dictionary.
70 * @param dictionaryName dictionary name
71 * @param cldsDictionary clds dictionary
72 * @return CldsDictionary that was created in DB.
74 public ResponseEntity<CldsDictionary> createOrUpdateDictionary(String dictionaryName,
75 CldsDictionary cldsDictionary) {
76 Date startTime = new Date();
77 LoggingUtils.setRequestContext("CldsDictionaryService: createOrUpdateDictionary", getPrincipalName());
78 // TODO revisit based on new permissions
79 isAuthorized(permissionUpdateTosca);
80 if (cldsDictionary == null) {
81 cldsDictionary = new CldsDictionary();
82 cldsDictionary.setDictionaryName(dictionaryName);
84 cldsDictionary.save(dictionaryName, cldsDao, getUserId());
85 LoggingUtils.setTimeContext(startTime, new Date());
86 LoggingUtils.setResponseContext("0", "createOrUpdateDictionary success", this.getClass().getName());
87 auditLogger.info("createOrUpdateDictionary completed");
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 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 LoggingUtils.setTimeContext(startTime, new Date());
108 LoggingUtils.setResponseContext("0", "createOrUpdateDictionaryElements success", this.getClass().getName());
109 auditLogger.info("createOrUpdateDictionaryElements completed");
110 return new ResponseEntity<>(dictionaryItem, HttpStatus.OK);
114 * Rest Service that retrieves all CLDS dictionary in DB.
116 * @return CldsDictionary List List of CldsDictionary available in DB
118 public ResponseEntity<List<CldsDictionary>> getAllDictionaryNames() {
119 Date startTime = new Date();
120 LoggingUtils.setRequestContext("CldsDictionaryService: getAllDictionaryNames", getPrincipalName());
121 // TODO revisit based on new permissions
122 isAuthorized(permissionReadTosca);
123 List<CldsDictionary> dictionaries = cldsDao.getDictionary(null, null);
124 LoggingUtils.setTimeContext(startTime, new Date());
125 LoggingUtils.setResponseContext("0", "getAllDictionaryNames success", this.getClass().getName());
126 auditLogger.info("getAllDictionaryNames completed");
127 return new ResponseEntity<>(dictionaries, HttpStatus.OK);
131 * Rest Service that retrieves all CLDS dictionary items in DB for a give
134 * @param dictionaryName dictionary name
135 * @return CldsDictionaryItem list List of CLDS Dictionary items for a given
138 public ResponseEntity<List<CldsDictionaryItem>> getDictionaryElementsByName(String dictionaryName) {
139 Date startTime = new Date();
140 LoggingUtils.setRequestContext("CldsDictionaryService: getDictionaryElementsByName", getPrincipalName());
141 // TODO revisit based on new permissions
142 isAuthorized(permissionReadTosca);
143 List<CldsDictionaryItem> dictionaryItems = cldsDao.getDictionaryElements(dictionaryName, null, null);
144 LoggingUtils.setTimeContext(startTime, new Date());
145 LoggingUtils.setResponseContext("0", "getAllDictionaryNames success", this.getClass().getName());
146 auditLogger.info("getAllDictionaryNames completed");
147 return new ResponseEntity<>(dictionaryItems, HttpStatus.OK);
150 public ResponseEntity<?> deleteDictionary() {
154 // Created for the integration test
155 public void setLoggingUtil(LoggingUtils utilP) {