Merge "logstash input"
[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         @PostConstruct
58         private final void initConstruct() {
59                 permissionReadTosca = SecureServicePermission.create(cldsPermissionTypeTosca, cldsPermissionInstance, "read");
60         permissionUpdateTosca = SecureServicePermission.create(cldsPermissionTypeTosca, cldsPermissionInstance, "update");
61         }
62
63         /**
64          *  REST Service that creates or Updates a Dictionary
65          *  
66          * @param dictionaryName
67          * @param cldsDictionary
68          * @return CldsDictionary that was created in DB.
69          */
70         public ResponseEntity<CldsDictionary> createOrUpdateDictionary(String dictionaryName,
71                         CldsDictionary cldsDictionary) {
72                 Date startTime = new Date();
73                 LoggingUtils.setRequestContext("CldsDictionaryService: createOrUpdateDictionary", getPrincipalName());
74                 // TODO revisit based on new permissions
75                 isAuthorized(permissionUpdateTosca);
76                 if (cldsDictionary == null) {
77                         cldsDictionary = new CldsDictionary();
78                         cldsDictionary.setDictionaryName(dictionaryName);
79                 }
80                 cldsDictionary.save(dictionaryName, cldsDao, getUserId());
81                 LoggingUtils.setTimeContext(startTime, new Date());
82                 LoggingUtils.setResponseContext("0", "createOrUpdateDictionary success", this.getClass().getName());
83                 auditLogger.info("createOrUpdateDictionary completed");
84                 return new ResponseEntity<>(cldsDictionary, HttpStatus.OK);
85         }
86
87         /**
88          * REST Service that creates or Updates a Dictionary Elements for dictionary in DB
89          * 
90          * @param dictionaryName
91          * @param dictionaryItem
92          * @return CldsDictionaryItem
93          *                      A dictionary items that was created or updated in DB
94          */
95         public ResponseEntity<CldsDictionaryItem> createOrUpdateDictionaryElements(String dictionaryName,
96                         CldsDictionaryItem dictionaryItem) {
97                 Date startTime = new Date();
98                 LoggingUtils.setRequestContext("CldsDictionaryService: createOrUpdateDictionaryElements", getPrincipalName());
99                 // TODO revisit based on new permissions
100                 isAuthorized(permissionUpdateTosca);
101                 dictionaryItem.save(dictionaryName, cldsDao, getUserId());
102                 LoggingUtils.setTimeContext(startTime, new Date());
103                 LoggingUtils.setResponseContext("0", "createOrUpdateDictionaryElements success", this.getClass().getName());
104                 auditLogger.info("createOrUpdateDictionaryElements completed");
105                 return new ResponseEntity<>(dictionaryItem, HttpStatus.OK);
106         }
107
108         /**
109          * Rest Service that retrieves all CLDS dictionary in DB
110          * 
111          * @return CldsDictionary List
112          *                      List of CldsDictionary available in DB
113          */
114         public ResponseEntity<List<CldsDictionary>> getAllDictionaryNames() {
115                 Date startTime = new Date();
116                 LoggingUtils.setRequestContext("CldsDictionaryService: getAllDictionaryNames", getPrincipalName());
117                 // TODO revisit based on new permissions
118                 isAuthorized(permissionReadTosca);
119                 List<CldsDictionary> dictionaries = cldsDao.getDictionary(null, null);
120                 LoggingUtils.setTimeContext(startTime, new Date());
121                 LoggingUtils.setResponseContext("0", "getAllDictionaryNames success", this.getClass().getName());
122                 auditLogger.info("getAllDictionaryNames completed");
123                 return new ResponseEntity<>(dictionaries, HttpStatus.OK);
124         }
125
126         /**
127          * Rest Service that retrieves all CLDS dictionary items in DB for a give dictionary name
128          * 
129          * @param dictionaryName
130          * @return CldsDictionaryItem list
131          *                      List of CLDS Dictionary items for a given dictionary name
132          */
133         public ResponseEntity<List<CldsDictionaryItem>> getDictionaryElementsByName(String dictionaryName) {
134                 Date startTime = new Date();
135                 LoggingUtils.setRequestContext("CldsDictionaryService: getDictionaryElementsByName", getPrincipalName());
136                 // TODO revisit based on new permissions
137                 isAuthorized(permissionReadTosca);
138                 List<CldsDictionaryItem> dictionaryItems = cldsDao.getDictionaryElements(dictionaryName, null, null);
139                 LoggingUtils.setTimeContext(startTime, new Date());
140                 LoggingUtils.setResponseContext("0", "getAllDictionaryNames success", this.getClass().getName());
141                 auditLogger.info("getAllDictionaryNames completed");
142                 return new ResponseEntity<>(dictionaryItems, HttpStatus.OK);
143         }
144
145         public ResponseEntity<?> deleteDictionary() {
146                 return null;
147         }
148
149 }