520bef98c1fbac0bd667b30803c4aef9b97fbd40
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / MicroserviceServiceImpl.java
1 /*-
2  * ================================================================================
3  * ECOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.service;
21
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import javax.crypto.BadPaddingException;
28
29 import org.hibernate.criterion.Criterion;
30 import org.hibernate.criterion.Restrictions;
31 import org.openecomp.portalapp.portal.domain.MicroserviceData;
32 import org.openecomp.portalapp.portal.domain.MicroserviceParameter;
33 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
34 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
35 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
36 import org.openecomp.portalsdk.core.service.DataAccessService;
37 import org.openecomp.portalsdk.core.util.SystemProperties;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.context.annotation.EnableAspectJAutoProxy;
40 import org.springframework.stereotype.Service;
41
42 @Service("microserviceService")
43 @EnableAspectJAutoProxy
44 @EPMetricsLog
45 public class MicroserviceServiceImpl implements MicroserviceService {
46
47         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MicroserviceServiceImpl.class);
48
49         @Autowired
50         private DataAccessService dataAccessService;
51
52         public Long saveMicroservice(MicroserviceData newService) throws Exception {
53                 if (newService.getPassword() != null)
54                         newService.setPassword(encryptedPassword(newService.getPassword()));
55                 getDataAccessService().saveDomainObject(newService, null);
56                 return newService.getId();
57         }
58
59         public void saveServiceParameters(long serviceId, List<MicroserviceParameter> list) throws Exception {
60                 for (int i = 0; i < list.size(); i++) {
61                         MicroserviceParameter para = list.get(i);
62                         para.setServiceId(serviceId);
63                         getDataAccessService().saveDomainObject(para, null);
64                 }
65         }
66
67         @Override
68         public MicroserviceData getMicroserviceDataById(long id) {
69                 MicroserviceData data = null;
70                 try {
71                         data = (MicroserviceData) dataAccessService
72                                         .getList(MicroserviceData.class, " where id = '" + id + "'", null, null).get(0);
73                         data.setParameterList(getServiceParameters(id));
74                 } catch (Exception e) {
75                         logger.error(EELFLoggerDelegate.errorLogger, "getMicroserviceDataById failed", e);
76                         throw e;
77                 }
78                 return data;
79         }
80
81         @SuppressWarnings("unchecked")
82         @Override
83         public List<MicroserviceData> getMicroserviceData() throws Exception {
84                 List<MicroserviceData> list = (List<MicroserviceData>) dataAccessService.getList(MicroserviceData.class, null);
85                 for (int i = 0; i < list.size(); i++) {
86                         if (list.get(i).getPassword() != null)
87                                 try{
88                                         list.get(i).setPassword(decryptedPassword(list.get(i).getPassword()));
89                                 } catch(BadPaddingException bpe){
90                                         logger.error(EELFLoggerDelegate.errorLogger, "Couldn't decrypt - Check decryption key in system.properties - looks wrong. Still going ahead with list population though", bpe);
91                                 }
92                         list.get(i).setParameterList(getServiceParameters(list.get(i).getId()));
93                 }
94                 return list;
95         }
96
97         @SuppressWarnings("unchecked")
98         private List<MicroserviceParameter> getServiceParameters(long serviceId) {
99                 List<MicroserviceParameter> list = (List<MicroserviceParameter>) dataAccessService
100                                 .getList(MicroserviceParameter.class, " where service_id = '" + serviceId + "'", null, null);
101                 return list;
102         }
103
104         @Override
105         public void deleteMicroservice(long serviceId) throws Exception {
106
107                 try {
108                         Map<String, String> params = new HashMap<String, String>();
109                         params.put("serviceId", Long.toString(serviceId));
110
111                         dataAccessService.executeNamedQuery("deleteMicroserviceParameter", params, null);
112                         dataAccessService.executeNamedQuery("deleteMicroservice", params, null);
113
114                 } catch (Exception e) {
115                         e.printStackTrace();
116                         logger.error(EELFLoggerDelegate.errorLogger, "deleteMicroservice failed", e);
117                         throw e;
118                 }
119         }
120
121         @SuppressWarnings("unchecked")
122         @Override
123         public void updateMicroservice(long serviceId, MicroserviceData newService) throws Exception {
124                 try {
125                         newService.setId(serviceId);
126                         if (newService.getPassword() != null)
127                                 newService.setPassword(encryptedPassword(newService.getPassword()));
128                         getDataAccessService().saveDomainObject(newService, null);
129                         List<MicroserviceParameter> oldService = getServiceParameters(serviceId);
130                         boolean foundParam;
131                         for (int i = 0; i < oldService.size(); i++) {
132                                 foundParam = false;
133                                 for (int n = 0; n < newService.getParameterList().size(); n++) {
134                                         if (newService.getParameterList().get(n).getId().equals(oldService.get(i).getId())) {
135                                                 foundParam = true;
136                                                 break;
137                                         }
138                                 }
139                                 if (foundParam == false) {
140                                         MicroserviceParameter pd = oldService.get(i);
141                                         getDataAccessService().deleteDomainObject(pd, null);
142                                 }
143                         }
144                         for (int i = 0; i < newService.getParameterList().size(); i++) {
145                                 MicroserviceParameter param = newService.getParameterList().get(i);
146                                 param.setServiceId(serviceId);
147                                 getDataAccessService().saveDomainObject(param, null);
148                         }
149                 } catch (Exception e) {
150                         logger.error(EELFLoggerDelegate.errorLogger, "updateMicroservice failed", e);
151                         throw e;
152                 }
153                 saveServiceParameters(serviceId, newService.getParameterList());
154         }
155
156         @Override
157         @SuppressWarnings("unchecked")
158         public List<MicroserviceParameter> getParametersById(long serviceId) {
159                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
160                 Criterion contextIdCrit = Restrictions.eq("serviceId", serviceId);
161                 restrictionsList.add(contextIdCrit);
162                 List<MicroserviceParameter> list = (List<MicroserviceParameter>) dataAccessService
163                                 .getList(MicroserviceParameter.class, null, restrictionsList, null);
164                 logger.debug(EELFLoggerDelegate.debugLogger,
165                                 "getParametersById: microservice parameters list size: " + list.size());
166                 return list;
167         }
168
169         private String decryptedPassword(String encryptedPwd) throws Exception {
170                 String result = "";
171                 if (encryptedPwd != null & encryptedPwd.length() > 0) {
172                         try {
173                                 result = CipherUtil.decrypt(encryptedPwd,
174                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
175                         } catch (Exception e) {
176                                 logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword failed", e);
177                                 throw e;
178                         }
179                 }
180                 return result;
181         }
182
183         private String encryptedPassword(String decryptedPwd) throws Exception {
184                 String result = "";
185                 if (decryptedPwd != null & decryptedPwd.length() > 0) {
186                         try {
187                                 result = CipherUtil.encrypt(decryptedPwd,
188                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
189                         } catch (Exception e) {
190                                 logger.error(EELFLoggerDelegate.errorLogger, "encryptedPassword failed", e);
191                                 throw e;
192                         }
193                 }
194                 return result;
195         }
196
197         public DataAccessService getDataAccessService() {
198                 return dataAccessService;
199         }
200
201 }