451500d6473a64006d685453ed1211ee55cbfea4
[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                         logger.error(EELFLoggerDelegate.errorLogger, "deleteMicroservice failed", e);
140                         throw e;
141                 }
142         }
143
144         @Override
145         public void updateMicroservice(long serviceId, MicroserviceData newService) throws Exception {
146                 try {
147                         newService.setId(serviceId);
148                         if (newService.getPassword() != null){
149                                 if(newService.getPassword().equals(EPCommonSystemProperties.APP_DISPLAY_PASSWORD)){
150                                         MicroserviceData oldMS = getMicroserviceDataById(serviceId);
151                                         newService.setPassword(oldMS.getPassword()); // keep the old password
152                                 }else
153                                         newService.setPassword(encryptedPassword(newService.getPassword())); //new password
154                         }
155                         getDataAccessService().saveDomainObject(newService, null);
156                         List<MicroserviceParameter> oldService = getServiceParameters(serviceId);
157                         boolean foundParam;
158                         for (MicroserviceParameter microserviceParameter : oldService) {
159                                 foundParam = false;
160                                 for (int n = 0; n < newService.getParameterList().size(); n++) {
161                                         if (newService.getParameterList().get(n).getId().equals(microserviceParameter.getId())) {
162                                                 foundParam = true;
163                                                 break;
164                                         }
165                                 }
166                                 if (!foundParam) {
167                                         getDataAccessService().deleteDomainObject(microserviceParameter, null);
168                                 }
169                         }
170                         for (int i = 0; i < newService.getParameterList().size(); i++) {
171                                 MicroserviceParameter param = newService.getParameterList().get(i);
172                                 param.setServiceId(serviceId);
173                                 getDataAccessService().saveDomainObject(param, null);
174                         }
175                 } catch (Exception e) {
176                         logger.error(EELFLoggerDelegate.errorLogger, "updateMicroservice failed", e);
177                         throw e;
178                 }
179                 saveServiceParameters(serviceId, newService.getParameterList());
180         }
181
182         @Override
183         @SuppressWarnings("unchecked")
184         public List<MicroserviceParameter> getParametersById(long serviceId) {
185                 List<Criterion> restrictionsList = new ArrayList<>();
186                 Criterion contextIdCrit = Restrictions.eq("serviceId", serviceId);
187                 restrictionsList.add(contextIdCrit);
188                 List<MicroserviceParameter> list = (List<MicroserviceParameter>) dataAccessService
189                                 .getList(MicroserviceParameter.class, null, restrictionsList, null);
190                 logger.debug(EELFLoggerDelegate.debugLogger,
191                                 "getParametersById: microservice parameters list size: " + list.size());
192                 return list;
193         }
194
195         private String decryptedPassword(String encryptedPwd) throws Exception {
196                 String result = "";
197                 if (encryptedPwd != null && !encryptedPwd.isEmpty()) {
198                         try {
199                                 result = CipherUtil.decryptPKC(encryptedPwd,
200                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
201                         } catch (Exception e) {
202                                 logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword failed", e);
203                                 throw e;
204                         }
205                 }
206                 return result;
207         }
208
209         private String encryptedPassword(String decryptedPwd) throws Exception {
210                 String result = "";
211                 if (decryptedPwd != null && !decryptedPwd.isEmpty()) {
212                         try {
213                                 result = CipherUtil.encryptPKC(decryptedPwd,
214                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
215                         } catch (Exception e) {
216                                 logger.error(EELFLoggerDelegate.errorLogger, "encryptedPassword failed", e);
217                                 throw e;
218                         }
219                 }
220                 return result;
221         }
222
223         public DataAccessService getDataAccessService() {
224                 return dataAccessService;
225         }
226
227 }