Merge "Sonar critical fixes in MicroserviceServiceImpl"
[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.service.DataAccessService;
56 import org.onap.portalsdk.core.util.SystemProperties;
57 import org.springframework.beans.factory.annotation.Autowired;
58 import org.springframework.context.annotation.EnableAspectJAutoProxy;
59 import org.springframework.stereotype.Service;
60
61 @Service("microserviceService")
62 @EnableAspectJAutoProxy
63 @EPMetricsLog
64 public class MicroserviceServiceImpl implements MicroserviceService {
65
66         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MicroserviceServiceImpl.class);
67
68         @Autowired
69         private DataAccessService dataAccessService;
70
71         public Long saveMicroservice(MicroserviceData newService) throws Exception {
72                 if (newService.getPassword() != null)
73                         newService.setPassword(encryptedPassword(newService.getPassword()));
74                 getDataAccessService().saveDomainObject(newService, null);
75                 return newService.getId();
76         }
77
78         public void saveServiceParameters(long serviceId, List<MicroserviceParameter> list) {
79                 for (MicroserviceParameter para : list) {
80                         para.setServiceId(serviceId);
81                         getDataAccessService().saveDomainObject(para, null);
82                 }
83         }
84
85         @Override
86         public MicroserviceData getMicroserviceDataById(long id) {
87                 MicroserviceData data;
88                 try {
89                         List<Criterion> restrictionsList = new ArrayList<>();
90                         Criterion idCriterion = Restrictions.eq("id", id);
91                         restrictionsList.add(idCriterion);
92                         data = (MicroserviceData) dataAccessService.getList(MicroserviceData.class, null, restrictionsList, null).get(0);
93                         
94                         data.setParameterList(getServiceParameters(id));
95                 } catch (Exception e) {
96                         logger.error(EELFLoggerDelegate.errorLogger, "getMicroserviceDataById failed", e);
97                         throw e;
98                 }
99                 return data;
100         }
101
102         @SuppressWarnings("unchecked")
103         @Override
104         public List<MicroserviceData> getMicroserviceData() {
105                 List<MicroserviceData> list = (List<MicroserviceData>) dataAccessService.getList(MicroserviceData.class, null);
106                 for (MicroserviceData microserviceData : list) {
107                         if (microserviceData.getPassword() != null) {
108                                 microserviceData
109                                         .setPassword(EPCommonSystemProperties.APP_DISPLAY_PASSWORD);  //to hide password from get request
110                         }
111                         microserviceData.setParameterList(getServiceParameters(microserviceData.getId()));
112                 }
113                 return list;
114         }
115
116         private List<MicroserviceParameter> getServiceParameters(long serviceId) {
117                 return getMicroServiceParametersList(serviceId);
118         }
119
120         @SuppressWarnings("unchecked")
121         private List<MicroserviceParameter> getMicroServiceParametersList(long serviceId) {
122                 List<Criterion> restrictionsList = new ArrayList<>();
123                 Criterion serviceIdCriterion = Restrictions.eq("serviceId", serviceId);
124                 restrictionsList.add(serviceIdCriterion);
125                 return (List<MicroserviceParameter>) dataAccessService.getList(MicroserviceParameter.class, null, restrictionsList, null);
126         }
127
128         @Override
129         public void deleteMicroservice(long serviceId) {
130
131                 try {
132                         Map<String, String> params = new HashMap<>();
133                         params.put("serviceId", Long.toString(serviceId));
134
135                         dataAccessService.executeNamedQuery("deleteMicroserviceParameter", params, null);
136                         dataAccessService.executeNamedQuery("deleteMicroservice", params, null);
137
138                 } catch (Exception e) {
139                         e.printStackTrace();
140                         logger.error(EELFLoggerDelegate.errorLogger, "deleteMicroservice failed", e);
141                         throw e;
142                 }
143         }
144
145         @Override
146         public void updateMicroservice(long serviceId, MicroserviceData newService) throws Exception {
147                 try {
148                         newService.setId(serviceId);
149                         if (newService.getPassword() != null){
150                                 if(newService.getPassword().equals(EPCommonSystemProperties.APP_DISPLAY_PASSWORD)){
151                                         MicroserviceData oldMS = getMicroserviceDataById(serviceId);
152                                         newService.setPassword(oldMS.getPassword()); // keep the old password
153                                 }else
154                                         newService.setPassword(encryptedPassword(newService.getPassword())); //new password
155                         }
156                         getDataAccessService().saveDomainObject(newService, null);
157                         List<MicroserviceParameter> oldService = getServiceParameters(serviceId);
158                         boolean foundParam;
159                         for (MicroserviceParameter microserviceParameter : oldService) {
160                                 foundParam = false;
161                                 for (int n = 0; n < newService.getParameterList().size(); n++) {
162                                         if (newService.getParameterList().get(n).getId().equals(microserviceParameter.getId())) {
163                                                 foundParam = true;
164                                                 break;
165                                         }
166                                 }
167                                 if (!foundParam) {
168                                         getDataAccessService().deleteDomainObject(microserviceParameter, null);
169                                 }
170                         }
171                         for (int i = 0; i < newService.getParameterList().size(); i++) {
172                                 MicroserviceParameter param = newService.getParameterList().get(i);
173                                 param.setServiceId(serviceId);
174                                 getDataAccessService().saveDomainObject(param, null);
175                         }
176                 } catch (Exception e) {
177                         logger.error(EELFLoggerDelegate.errorLogger, "updateMicroservice failed", e);
178                         throw e;
179                 }
180                 saveServiceParameters(serviceId, newService.getParameterList());
181         }
182
183         @Override
184         @SuppressWarnings("unchecked")
185         public List<MicroserviceParameter> getParametersById(long serviceId) {
186                 List<Criterion> restrictionsList = new ArrayList<>();
187                 Criterion contextIdCrit = Restrictions.eq("serviceId", serviceId);
188                 restrictionsList.add(contextIdCrit);
189                 List<MicroserviceParameter> list = (List<MicroserviceParameter>) dataAccessService
190                                 .getList(MicroserviceParameter.class, null, restrictionsList, null);
191                 logger.debug(EELFLoggerDelegate.debugLogger,
192                                 "getParametersById: microservice parameters list size: " + list.size());
193                 return list;
194         }
195
196         private String decryptedPassword(String encryptedPwd) throws Exception {
197                 String result = "";
198                 if (encryptedPwd != null && !encryptedPwd.isEmpty()) {
199                         try {
200                                 result = CipherUtil.decryptPKC(encryptedPwd,
201                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
202                         } catch (Exception e) {
203                                 logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword failed", e);
204                                 throw e;
205                         }
206                 }
207                 return result;
208         }
209
210         private String encryptedPassword(String decryptedPwd) throws Exception {
211                 String result = "";
212                 if (decryptedPwd != null && !decryptedPwd.isEmpty()) {
213                         try {
214                                 result = CipherUtil.encryptPKC(decryptedPwd,
215                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
216                         } catch (Exception e) {
217                                 logger.error(EELFLoggerDelegate.errorLogger, "encryptedPassword failed", e);
218                                 throw e;
219                         }
220                 }
221                 return result;
222         }
223
224         public DataAccessService getDataAccessService() {
225                 return dataAccessService;
226         }
227
228 }