Additional code for Tosca
[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  * 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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.service;
25
26 import java.util.Date;
27 import java.util.List;
28
29 import javax.annotation.PostConstruct;
30
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;
40
41 /**
42  * REST services to manage dictionary and dictionary items for Tosca Model
43  */
44 @Component
45 public class CldsDictionaryService extends SecureServiceBase {
46
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;
53
54     @Autowired
55     private CldsDao                 cldsDao;
56     
57     private LoggingUtils util = new LoggingUtils(logger);
58     
59
60     @PostConstruct
61     private final void initConstruct() {
62         permissionReadTosca = SecureServicePermission.create(cldsPermissionTypeTosca, cldsPermissionInstance, "read");
63         permissionUpdateTosca = SecureServicePermission.create(cldsPermissionTypeTosca, cldsPermissionInstance,
64                 "update");
65     }
66
67     /**
68      * REST Service that creates or Updates a Dictionary
69      * 
70      * @param dictionaryName
71      * @param cldsDictionary
72      * @return CldsDictionary that was created in DB.
73      */
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);
83         }
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);
89     }
90
91     /**
92      * REST Service that creates or Updates a Dictionary Elements for dictionary
93      * in DB
94      * 
95      * @param dictionaryName
96      * @param dictionaryItem
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         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);
111     }
112
113     /**
114      * Rest Service that retrieves all CLDS dictionary in DB
115      * 
116      * @return CldsDictionary List List of CldsDictionary available in DB
117      */
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);
128     }
129
130     /**
131      * Rest Service that retrieves all CLDS dictionary items in DB for a give
132      * dictionary name
133      * 
134      * @param dictionaryName
135      * @return CldsDictionaryItem list List of CLDS Dictionary items for a given
136      *         dictionary name
137      */
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);
148     }
149
150     public ResponseEntity<?> deleteDictionary() {
151         return null;
152     }
153
154     // Created for the integration test
155     public void setLoggingUtil(LoggingUtils utilP) {
156         util = utilP;
157     }
158
159 }