41e51be6dc53b08c0f95da464cc528bb4042c226
[portal.git] / ecomp-portal-BE-os / src / main / java / org / openecomp / portalapp / portal / service / EPAppServiceImpl.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
23 import java.security.GeneralSecurityException;
24 import java.util.ArrayList;
25 import java.util.LinkedList;
26 import java.util.List;
27 import java.util.TreeSet;
28 import java.util.UUID;
29
30 import javax.servlet.http.HttpServletResponse;
31
32 import org.hibernate.Session;
33 import org.hibernate.Transaction;
34 import org.openecomp.portalapp.portal.domain.EPApp;
35 import org.openecomp.portalapp.portal.domain.EPUser;
36 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
37 import org.openecomp.portalapp.portal.logging.format.EPAppMessagesEnum;
38 import org.openecomp.portalapp.portal.logging.logic.EPLogUtil;
39 import org.openecomp.portalapp.portal.transport.FieldsValidator;
40 import org.openecomp.portalapp.portal.transport.OnboardingApp;
41 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
42 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
43 import org.openecomp.portalsdk.core.service.DataAccessService;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.context.annotation.EnableAspectJAutoProxy;
46 import org.springframework.stereotype.Service;
47 import org.springframework.transaction.annotation.Transactional;
48
49 import com.att.nsa.cambria.client.CambriaClientFactory;
50 import com.att.nsa.cambria.client.CambriaTopicManager;
51
52 @Service("epAppService")
53 @Transactional
54 @org.springframework.context.annotation.Configuration
55 @EnableAspectJAutoProxy
56 @EPMetricsLog
57 public class EPAppServiceImpl extends EPAppCommonServiceImpl implements EPAppService {
58
59         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPAppServiceImpl.class);
60
61         private static Object syncRests = new Object();
62         
63         @Autowired
64         private DataAccessService dataAccessService;
65         
66         @Override
67         public List<EPApp> getUserRemoteApps(String id) {
68                 
69                         StringBuilder query = new StringBuilder();
70                 
71                         query.append("SELECT * FROM FN_APP join FN_USER_ROLE ON FN_USER_ROLE.APP_ID = FN_APP.APP_ID where ");
72                         query.append(
73                                                 "FN_USER_ROLE.USER_ID = " + id + " AND FN_USER_ROLE.ROLE_ID != " + SUPER_ADMIN_ROLE_ID);
74                         query.append(" AND FN_APP.ENABLED = 'Y'");
75
76                         TreeSet<EPApp> distinctApps = new TreeSet<EPApp>();
77
78                         @SuppressWarnings("unchecked")
79                         List<EPApp> adminApps = dataAccessService.executeSQLQuery(query.toString(), EPApp.class, null);
80                         for (EPApp app : adminApps) {
81                                 distinctApps.add(app);
82                         }
83
84                         List<EPApp> userApps = new ArrayList<EPApp>();
85                         userApps.addAll(distinctApps);
86                         return userApps;
87         
88         }
89         
90         protected void updateRestrictedApp(Long appId, OnboardingApp onboardingApp, FieldsValidator fieldsValidator,
91                         EPUser user) {
92                 synchronized (syncRests) {
93                         boolean result = false;
94                         Session localSession = null;
95                         Transaction transaction = null;
96                         try {
97                                 localSession = sessionFactory.openSession();
98                                 transaction = localSession.beginTransaction();
99                                 EPApp app;
100                                 if (appId == null) {
101                                         app = new EPApp();
102                                         
103                                         // In the parent class, the UEB code is responsible for generating the keys/secret/mailbox but UEB Messaging 
104                                         // is not actually being used currently; may be used in future at which point we can just remove this method and
105                                         // depend on parent class's method
106                                         // So, using UUID generator to generate the unique key instead.
107                                         String uuidStr = UUID.randomUUID().toString();
108                                         String appKey = uuidStr;
109                                         String appSecret = uuidStr;
110                                         String appMailboxName = "ECOMP-PORTAL-OUTBOX";
111                                         onboardingApp.setUebTopicName(appMailboxName);
112                                         onboardingApp.setUebKey(appKey);
113                                         onboardingApp.setUebSecret(appSecret);
114                                         
115                                 }else {
116                                         app = (EPApp) localSession.get(EPApp.class, appId);
117                                         if (app == null || app.getId() == null) {
118                                                 // App is already deleted!
119                                                 transaction.commit();
120                                                 localSession.close();
121                                                 fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_NOT_FOUND);
122                                                 return;
123                                         }
124                                 }
125                                 logger.debug(EELFLoggerDelegate.debugLogger, "LR: about to call createAppFromOnboarding");
126                                 createAppFromOnboarding(app, onboardingApp, localSession);
127                                 logger.debug(EELFLoggerDelegate.debugLogger,
128                                                 "LR: updateApp: finished calling createAppFromOnboarding");
129                                 localSession.saveOrUpdate(app);
130                                 logger.debug(EELFLoggerDelegate.debugLogger,
131                                                 "LR: updateApp: finished calling localSession.saveOrUpdate");
132                                 // Enable or disable all menu items associated with this app
133                                 setFunctionalMenuItemsEnabled(localSession, onboardingApp.isEnabled, appId);
134                                 logger.debug(EELFLoggerDelegate.debugLogger,
135                                                 "LR: updateApp: finished calling setFunctionalMenuItemsEnabled");
136                                 transaction.commit();
137                                 logger.debug(EELFLoggerDelegate.debugLogger, "LR: updateApp: finished calling transaction.commit");
138                                 result = true;
139                         } catch (Exception e) {
140                                 logger.error(EELFLoggerDelegate.errorLogger, "updateApp failed", e);
141                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebRegisterOnboardingAppError, e);
142                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e);
143                                 EcompPortalUtils.rollbackTransaction(transaction,
144                                                 "updateApp rollback, exception = " + EcompPortalUtils.getStackTrace(e));
145                         } finally {
146                                 EcompPortalUtils.closeLocalSession(localSession, "updateApp");
147                         }
148                         if (!result) {
149                                 fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
150                         }
151                 }
152
153         }
154         
155         public CambriaTopicManager getTopicManager(LinkedList<String> urlList, String key, String secret) throws GeneralSecurityException, Exception{
156                 return CambriaClientFactory.createTopicManager( null, urlList, key, secret);
157         }
158
159 }