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