Resubmitting KeyProperty code change since tests failed
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / service / MicroserviceServiceImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ===================================================================
9  *
10  * Unless otherwise specified, all software contained herein is licensed
11  * under the Apache License, Version 2.0 (the "License");
12  * you may not use this software except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *             http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * Unless otherwise specified, all documentation contained herein is licensed
24  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25  * you may not use this documentation except in compliance with the License.
26  * You may obtain a copy of the License at
27  *
28  *             https://creativecommons.org/licenses/by/4.0/
29  *
30  * Unless required by applicable law or agreed to in writing, documentation
31  * distributed under the License is distributed on an "AS IS" BASIS,
32  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33  * See the License for the specific language governing permissions and
34  * limitations under the License.
35  *
36  * ============LICENSE_END============================================
37  *
38  * 
39  */
40 package org.onap.portalapp.portal.service;
41
42 import java.util.ArrayList;
43 import java.util.HashMap;
44 import java.util.List;
45 import java.util.Map;
46
47 import org.hibernate.criterion.Criterion;
48 import org.hibernate.criterion.Restrictions;
49 import org.onap.portalapp.portal.domain.MicroserviceData;
50 import org.onap.portalapp.portal.domain.MicroserviceParameter;
51 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
52 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
53 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
54 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
55 import org.onap.portalsdk.core.onboarding.util.KeyConstants;
56 import org.onap.portalsdk.core.onboarding.util.KeyProperties;
57 import org.onap.portalsdk.core.service.DataAccessService;
58 import org.onap.portalsdk.core.util.SystemProperties;
59 import org.springframework.beans.factory.annotation.Autowired;
60 import org.springframework.context.annotation.EnableAspectJAutoProxy;
61 import org.springframework.stereotype.Service;
62
63 @Service("microserviceService")
64 @EnableAspectJAutoProxy
65 @EPMetricsLog
66 public class MicroserviceServiceImpl implements MicroserviceService {
67
68         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MicroserviceServiceImpl.class);
69
70         @Autowired
71         private DataAccessService dataAccessService;
72
73         public Long saveMicroservice(MicroserviceData newService) throws Exception {
74                 if (newService.getPassword() != null)
75                         newService.setPassword(encryptedPassword(newService.getPassword()));
76                 getDataAccessService().saveDomainObject(newService, null);
77                 return newService.getId();
78         }
79
80         public void saveServiceParameters(long serviceId, List<MicroserviceParameter> list) {
81                 for (MicroserviceParameter para : list) {
82                         para.setServiceId(serviceId);
83                         getDataAccessService().saveDomainObject(para, null);
84                 }
85         }
86
87         @Override
88         public MicroserviceData getMicroserviceDataById(long id) {
89                 MicroserviceData data;
90                 try {
91                         List<Criterion> restrictionsList = new ArrayList<>();
92                         Criterion idCriterion = Restrictions.eq("id", id);
93                         restrictionsList.add(idCriterion);
94                         data = (MicroserviceData) dataAccessService.getList(MicroserviceData.class, null, restrictionsList, null).get(0);
95                         
96                         data.setParameterList(getServiceParameters(id));
97                 } catch (Exception e) {
98                         logger.error(EELFLoggerDelegate.errorLogger, "getMicroserviceDataById failed", e);
99                         throw e;
100                 }
101                 return data;
102         }
103
104         @SuppressWarnings("unchecked")
105         @Override
106         public List<MicroserviceData> getMicroserviceData() {
107                 List<MicroserviceData> list = (List<MicroserviceData>) dataAccessService.getList(MicroserviceData.class, null);
108                 for (MicroserviceData microserviceData : list) {
109                         if (microserviceData.getPassword() != null) {
110                                 microserviceData
111                                         .setPassword(EPCommonSystemProperties.APP_DISPLAY_PASSWORD);  //to hide password from get request
112                         }
113                         microserviceData.setParameterList(getServiceParameters(microserviceData.getId()));
114                 }
115                 return list;
116         }
117
118         private List<MicroserviceParameter> getServiceParameters(long serviceId) {
119                 return getMicroServiceParametersList(serviceId);
120         }
121
122         @SuppressWarnings("unchecked")
123         private List<MicroserviceParameter> getMicroServiceParametersList(long serviceId) {
124                 List<Criterion> restrictionsList = new ArrayList<>();
125                 Criterion serviceIdCriterion = Restrictions.eq("serviceId", serviceId);
126                 restrictionsList.add(serviceIdCriterion);
127                 return (List<MicroserviceParameter>) dataAccessService.getList(MicroserviceParameter.class, null, restrictionsList, null);
128         }
129
130         @Override
131         public void deleteMicroservice(long serviceId) {
132
133                 try {
134                         Map<String, String> params = new HashMap<>();
135                         params.put("serviceId", Long.toString(serviceId));
136
137                         dataAccessService.executeNamedQuery("deleteMicroserviceParameter", params, null);
138                         dataAccessService.executeNamedQuery("deleteMicroservice", params, null);
139
140                 } catch (Exception e) {
141                         logger.error(EELFLoggerDelegate.errorLogger, "deleteMicroservice failed", e);
142                         throw e;
143                 }
144         }
145
146         @Override
147         public void updateMicroservice(long serviceId, MicroserviceData newService) throws Exception {
148                 try {
149                         newService.setId(serviceId);
150                         if (newService.getPassword() != null){
151                                 if(newService.getPassword().equals(EPCommonSystemProperties.APP_DISPLAY_PASSWORD)){
152                                         MicroserviceData oldMS = getMicroserviceDataById(serviceId);
153                                         newService.setPassword(oldMS.getPassword()); // keep the old password
154                                 }else
155                                         newService.setPassword(encryptedPassword(newService.getPassword())); //new password
156                         }
157                         getDataAccessService().saveDomainObject(newService, null);
158                         List<MicroserviceParameter> oldService = getServiceParameters(serviceId);
159                         boolean foundParam;
160                         for (MicroserviceParameter microserviceParameter : oldService) {
161                                 foundParam = false;
162                                 for (int n = 0; n < newService.getParameterList().size(); n++) {
163                                         if (newService.getParameterList().get(n).getId().equals(microserviceParameter.getId())) {
164                                                 foundParam = true;
165                                                 break;
166                                         }
167                                 }
168                                 if (!foundParam) {
169                                         getDataAccessService().deleteDomainObject(microserviceParameter, null);
170                                 }
171                         }
172                         for (int i = 0; i < newService.getParameterList().size(); i++) {
173                                 MicroserviceParameter param = newService.getParameterList().get(i);
174                                 param.setServiceId(serviceId);
175                                 getDataAccessService().saveDomainObject(param, null);
176                         }
177                 } catch (Exception e) {
178                         logger.error(EELFLoggerDelegate.errorLogger, "updateMicroservice failed", e);
179                         throw e;
180                 }
181                 saveServiceParameters(serviceId, newService.getParameterList());
182         }
183
184         @Override
185         @SuppressWarnings("unchecked")
186         public List<MicroserviceParameter> getParametersById(long serviceId) {
187                 List<Criterion> restrictionsList = new ArrayList<>();
188                 Criterion contextIdCrit = Restrictions.eq("serviceId", serviceId);
189                 restrictionsList.add(contextIdCrit);
190                 List<MicroserviceParameter> list = (List<MicroserviceParameter>) dataAccessService
191                                 .getList(MicroserviceParameter.class, null, restrictionsList, null);
192                 logger.debug(EELFLoggerDelegate.debugLogger,
193                                 "getParametersById: microservice parameters list size: " + list.size());
194                 return list;
195         }
196
197         private String decryptedPassword(String encryptedPwd) throws Exception {
198                 String result = "";
199                 if (encryptedPwd != null && !encryptedPwd.isEmpty()) {
200                         try {
201                                 result = CipherUtil.decryptPKC(encryptedPwd,
202                                                 KeyProperties.getProperty(KeyConstants.CIPHER_ENCRYPTION_KEY));
203                         } catch (Exception e) {
204                                 logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword failed", e);
205                                 throw e;
206                         }
207                 }
208                 return result;
209         }
210
211         private String encryptedPassword(String decryptedPwd) throws Exception {
212                 String result = "";
213                 if (decryptedPwd != null && !decryptedPwd.isEmpty()) {
214                         try {
215                                 result = CipherUtil.encryptPKC(decryptedPwd,
216                                                 KeyProperties.getProperty(KeyConstants.CIPHER_ENCRYPTION_KEY));
217                         } catch (Exception e) {
218                                 logger.error(EELFLoggerDelegate.errorLogger, "encryptedPassword failed", e);
219                                 throw e;
220                         }
221                 }
222                 return result;
223         }
224
225         public DataAccessService getDataAccessService() {
226                 return dataAccessService;
227         }
228
229 }