454056cd30182bf13608c7063419ac641b3d8fa7
[clamp.git] / src / main / java / org / onap / clamp / clds / service / CldsDictionaryService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights
6  *                             reserved.
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
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
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  * ===================================================================
23  *
24  */
25
26 package org.onap.clamp.clds.service;
27
28 import java.util.Date;
29 import java.util.List;
30
31 import javax.annotation.PostConstruct;
32
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;
42
43 /**
44  * REST services to manage dictionary and dictionary items for Tosca Model.
45  */
46 @Component
47 public class CldsDictionaryService extends SecureServiceBase {
48
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;
55
56     @Autowired
57     private CldsDao                 cldsDao;
58
59     private LoggingUtils util = new LoggingUtils(logger);
60
61
62     @PostConstruct
63     private void initConstruct() {
64         permissionReadTosca = SecureServicePermission.create(cldsPermissionTypeTosca, cldsPermissionInstance, "read");
65         permissionUpdateTosca = SecureServicePermission.create(cldsPermissionTypeTosca, cldsPermissionInstance,
66                 "update");
67     }
68
69     /**
70      * REST Service that creates a Dictionary.
71      *
72      * @param dictionaryName dictionary name
73      * @return CldsDictionary that was created in DB.
74      */
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;
80     }
81
82     /**
83      * REST Service that creates or Updates a Dictionary.
84      * Used in clds-services.xml
85      *
86      * @param cldsDictionary clds dictionary
87      * @return ResponseEntity with CldsDictionary that was created in DB.
88      */
89     public ResponseEntity<CldsDictionary> createOrUpdateDictionary(String dictionaryName,
90                                                                    CldsDictionary cldsDictionary) {
91
92         Date startTime = new Date();
93         LoggingUtils.setRequestContext("CldsDictionaryService: createOrUpdateDictionary", getPrincipalName());
94         // TODO revisit based on new permissions
95         isAuthorized(permissionUpdateTosca);
96
97         if (cldsDictionary == null) {
98
99             cldsDictionary = createDictionary(dictionaryName);
100         } else {
101
102             if (cldsDictionary.getDictionaryName() == null) {
103                 cldsDictionary.setDictionaryName(dictionaryName);
104             }
105
106             cldsDictionary.save(cldsDictionary.getDictionaryName(), cldsDao, getUserId());
107         }
108
109         LoggingUtils.setTimeContext(startTime, new Date());
110         LoggingUtils.setResponseContext("0", "createOrUpdateDictionary success", this.getClass().getName());
111         auditLogger.info("createOrUpdateDictionary completed");
112
113         return new ResponseEntity<>(cldsDictionary, HttpStatus.OK);
114     }
115
116     /**
117      * REST Service that creates or Updates a Dictionary Elements for dictionary
118      * in DB.
119      *
120      * @param dictionaryName dictionary name
121      * @param dictionaryItem dictionary item
122      * @return CldsDictionaryItem A dictionary items that was created or updated
123      *         in DB
124      */
125     public ResponseEntity<CldsDictionaryItem> createOrUpdateDictionaryElements(String dictionaryName,
126                                                                                CldsDictionaryItem dictionaryItem) {
127         final Date startTime = new Date();
128         LoggingUtils.setRequestContext("CldsDictionaryService: createOrUpdateDictionaryElements",
129             getPrincipalName());
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);
135     }
136
137     /**
138      * Rest Service that retrieves all CLDS dictionary in DB.
139      *
140      * @return CldsDictionary List List of CldsDictionary available in DB
141      */
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);
150     }
151
152     /**
153      * Rest Service that retrieves all CLDS dictionary items in DB for a give
154      * dictionary name.
155      *
156      * @param dictionaryName dictionary name
157      * @return CldsDictionaryItem list List of CLDS Dictionary items for a given
158      *         dictionary name
159      */
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);
168     }
169
170     public ResponseEntity<?> deleteDictionary() {
171         return null;
172     }
173
174     // Created for the integration test
175     public void setLoggingUtil(LoggingUtils utilP) {
176         util = utilP;
177     }
178
179 }