Merge "Rework action itmes"
[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 final 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 or Updates a Dictionary.
71      * 
72      * @param dictionaryName dictionary name
73      * @param cldsDictionary clds dictionary
74      * @return CldsDictionary that was created in DB.
75      */
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);
85         }
86         cldsDictionary.save(dictionaryName, cldsDao, getUserId());
87         auditLogInfo("createOrUpdateDictionary", startTime);
88         return new ResponseEntity<>(cldsDictionary, HttpStatus.OK);
89     }
90
91     /**
92      * REST Service that creates or Updates a Dictionary Elements for dictionary
93      * in DB.
94      * 
95      * @param dictionaryName dictionary name
96      * @param dictionaryItem dictionary item
97      * @return CldsDictionaryItem A dictionary items that was created or updated
98      *         in DB
99      */
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);
109     }
110
111     /**
112      * Rest Service that retrieves all CLDS dictionary in DB.
113      * 
114      * @return CldsDictionary List List of CldsDictionary available in DB
115      */
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);
124     }
125
126     /**
127      * Rest Service that retrieves all CLDS dictionary items in DB for a give
128      * dictionary name.
129      * 
130      * @param dictionaryName dictionary name
131      * @return CldsDictionaryItem list List of CLDS Dictionary items for a given
132      *         dictionary name
133      */
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);
142     }
143
144     public ResponseEntity<?> deleteDictionary() {
145         return null;
146     }
147
148     // Created for the integration test
149     public void setLoggingUtil(LoggingUtils utilP) {
150         util = utilP;
151     }
152
153 }