MicroserviceParameter class DB constraints
[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 javax.crypto.BadPaddingException;
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) throws Exception {
79                 for (int i = 0; i < list.size(); i++) {
80                         MicroserviceParameter para = list.get(i);
81                         para.setServiceId(serviceId);
82                         getDataAccessService().saveDomainObject(para, null);
83                 }
84         }
85
86         @Override
87         public MicroserviceData getMicroserviceDataById(long id) {
88                 MicroserviceData data = null;
89                 try {
90                         List<Criterion> restrictionsList = new ArrayList<Criterion>();
91                         Criterion idCriterion = Restrictions.eq("id", id);
92                         restrictionsList.add(idCriterion);
93                         data = (MicroserviceData) dataAccessService.getList(MicroserviceData.class, null, restrictionsList, null).get(0);
94                         
95                         data.setParameterList(getServiceParameters(id));
96                 } catch (Exception e) {
97                         logger.error(EELFLoggerDelegate.errorLogger, "getMicroserviceDataById failed", e);
98                         throw e;
99                 }
100                 return data;
101         }
102
103         @SuppressWarnings("unchecked")
104         @Override
105         public List<MicroserviceData> getMicroserviceData() throws Exception {
106                 List<MicroserviceData> list = (List<MicroserviceData>) dataAccessService.getList(MicroserviceData.class, null);
107                 for (int i = 0; i < list.size(); i++) {
108                         if (list.get(i).getPassword() != null)
109                                 list.get(i).setPassword(EPCommonSystemProperties.APP_DISPLAY_PASSWORD);  //to hide password from get request
110                         list.get(i).setParameterList(getServiceParameters(list.get(i).getId()));
111                 }
112                 return list;
113         }
114
115         private List<MicroserviceParameter> getServiceParameters(long serviceId) {
116                 List<MicroserviceParameter> list = getMicroServiceParametersList(serviceId);
117                 return list;
118         }
119
120         @SuppressWarnings("unchecked")
121         private List<MicroserviceParameter> getMicroServiceParametersList(long serviceId) {
122                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
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) throws Exception {
130
131                 try {
132                         Map<String, String> params = new HashMap<String, String>();
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 (int i = 0; i < oldService.size(); i++) {
160                                 foundParam = false;
161                                 for (int n = 0; n < newService.getParameterList().size(); n++) {
162                                         if (newService.getParameterList().get(n).getId().equals(oldService.get(i).getId())) {
163                                                 foundParam = true;
164                                                 break;
165                                         }
166                                 }
167                                 if (foundParam == false) {
168                                         MicroserviceParameter pd = oldService.get(i);
169                                         getDataAccessService().deleteDomainObject(pd, 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<Criterion>();
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.length() > 0) {
200                         try {
201                                 result = CipherUtil.decryptPKC(encryptedPwd,
202                                                 SystemProperties.getProperty(SystemProperties.Decryption_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.length() > 0) {
214                         try {
215                                 result = CipherUtil.encryptPKC(decryptedPwd,
216                                                 SystemProperties.getProperty(SystemProperties.Decryption_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 }