[PORTAL-16 PORTAL-18] Widget ms; staging
[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 org.hibernate.criterion.Criterion;
28 import org.hibernate.criterion.Restrictions;
29 import org.openecomp.portalapp.portal.domain.MicroserviceData;
30 import org.openecomp.portalapp.portal.domain.MicroserviceParameter;
31 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
32 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
33 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
34 import org.openecomp.portalsdk.core.service.DataAccessService;
35 import org.openecomp.portalsdk.core.util.SystemProperties;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.context.annotation.EnableAspectJAutoProxy;
38 import org.springframework.stereotype.Service;
39
40 @Service("microserviceService")
41 @EnableAspectJAutoProxy
42 @EPMetricsLog
43 public class MicroserviceServiceImpl implements MicroserviceService {
44
45         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MicroserviceServiceImpl.class);
46
47         @Autowired
48         private DataAccessService dataAccessService;
49
50         public Long saveMicroservice(MicroserviceData newService) throws Exception {
51                 if (newService.getPassword() != null)
52                         newService.setPassword(encryptedPassword(newService.getPassword()));
53                 getDataAccessService().saveDomainObject(newService, null);
54                 return newService.getId();
55         }
56
57         public void saveServiceParameters(long serviceId, List<MicroserviceParameter> list) throws Exception {
58                 for (int i = 0; i < list.size(); i++) {
59                         MicroserviceParameter para = list.get(i);
60                         para.setServiceId(serviceId);
61                         getDataAccessService().saveDomainObject(para, null);
62                 }
63         }
64
65         @Override
66         public MicroserviceData getMicroserviceDataById(long id) {
67                 MicroserviceData data = null;
68                 try {
69                         data = (MicroserviceData) dataAccessService
70                                         .getList(MicroserviceData.class, " where id = '" + id + "'", null, null).get(0);
71                         data.setParameterList(getServiceParameters(id));
72                 } catch (Exception e) {
73                         logger.error(EELFLoggerDelegate.errorLogger, "getMicroserviceDataById failed", e);
74                         throw e;
75                 }
76                 return data;
77         }
78
79         @SuppressWarnings("unchecked")
80         @Override
81         public List<MicroserviceData> getMicroserviceData() throws Exception {
82                 List<MicroserviceData> list = (List<MicroserviceData>) dataAccessService.getList(MicroserviceData.class, null);
83                 for (int i = 0; i < list.size(); i++) {
84                         if (list.get(i).getPassword() != null)
85                                 list.get(i).setPassword(decryptedPassword(list.get(i).getPassword()));
86                         list.get(i).setParameterList(getServiceParameters(list.get(i).getId()));
87                 }
88                 return list;
89         }
90
91         @SuppressWarnings("unchecked")
92         private List<MicroserviceParameter> getServiceParameters(long serviceId) {
93                 List<MicroserviceParameter> list = (List<MicroserviceParameter>) dataAccessService
94                                 .getList(MicroserviceParameter.class, " where service_id = '" + serviceId + "'", null, null);
95                 return list;
96         }
97
98         @Override
99         public void deleteMicroservice(long serviceId) throws Exception {
100
101                 try {
102                         Map<String, String> params = new HashMap<String, String>();
103                         params.put("serviceId", Long.toString(serviceId));
104
105                         dataAccessService.executeNamedQuery("deleteMicroserviceParameter", params, null);
106                         dataAccessService.executeNamedQuery("deleteMicroservice", params, null);
107
108                 } catch (Exception e) {
109                         e.printStackTrace();
110                         logger.error(EELFLoggerDelegate.errorLogger, "deleteMicroservice failed", e);
111                         throw e;
112                 }
113         }
114
115         @SuppressWarnings("unchecked")
116         @Override
117         public void updateMicroservice(long serviceId, MicroserviceData newService) throws Exception {
118                 try {
119                         newService.setId(serviceId);
120                         if (newService.getPassword() != null)
121                                 newService.setPassword(encryptedPassword(newService.getPassword()));
122                         getDataAccessService().saveDomainObject(newService, null);
123                         List<MicroserviceParameter> oldService = getServiceParameters(serviceId);
124                         boolean foundParam;
125                         for (int i = 0; i < oldService.size(); i++) {
126                                 foundParam = false;
127                                 for (int n = 0; n < newService.getParameterList().size(); n++) {
128                                         if (newService.getParameterList().get(n).getId().equals(oldService.get(i).getId())) {
129                                                 foundParam = true;
130                                                 break;
131                                         }
132                                 }
133                                 if (foundParam == false) {
134                                         MicroserviceParameter pd = oldService.get(i);
135                                         getDataAccessService().deleteDomainObject(pd, null);
136                                 }
137                         }
138                         for (int i = 0; i < newService.getParameterList().size(); i++) {
139                                 MicroserviceParameter param = newService.getParameterList().get(i);
140                                 param.setServiceId(serviceId);
141                                 getDataAccessService().saveDomainObject(param, null);
142                         }
143                 } catch (Exception e) {
144                         logger.error(EELFLoggerDelegate.errorLogger, "updateMicroservice failed", e);
145                         throw e;
146                 }
147                 saveServiceParameters(serviceId, newService.getParameterList());
148         }
149
150         @Override
151         @SuppressWarnings("unchecked")
152         public List<MicroserviceParameter> getParametersById(long serviceId) {
153                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
154                 Criterion contextIdCrit = Restrictions.eq("serviceId", serviceId);
155                 restrictionsList.add(contextIdCrit);
156                 List<MicroserviceParameter> list = (List<MicroserviceParameter>) dataAccessService
157                                 .getList(MicroserviceParameter.class, null, restrictionsList, null);
158                 logger.debug(EELFLoggerDelegate.debugLogger,
159                                 "getParametersById: microservice parameters list size: " + list.size());
160                 return list;
161         }
162
163         private String decryptedPassword(String encryptedPwd) throws Exception {
164                 String result = "";
165                 if (encryptedPwd != null & encryptedPwd.length() > 0) {
166                         try {
167                                 result = CipherUtil.decrypt(encryptedPwd,
168                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
169                         } catch (Exception e) {
170                                 logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword failed", e);
171                                 throw e;
172                         }
173                 }
174                 return result;
175         }
176
177         private String encryptedPassword(String decryptedPwd) throws Exception {
178                 String result = "";
179                 if (decryptedPwd != null & decryptedPwd.length() > 0) {
180                         try {
181                                 result = CipherUtil.encrypt(decryptedPwd,
182                                                 SystemProperties.getProperty(SystemProperties.Decryption_Key));
183                         } catch (Exception e) {
184                                 logger.error(EELFLoggerDelegate.errorLogger, "encryptedPassword failed", e);
185                                 throw e;
186                         }
187                 }
188                 return result;
189         }
190
191         public DataAccessService getDataAccessService() {
192                 return dataAccessService;
193         }
194
195 }