Release dashboard image at version 2.1.0
[portal/ric-dashboard.git] / dashboard / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / portalapi / PortalRestCentralServiceImpl.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 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  * ========================LICENSE_END===================================
19  */
20 package org.oransc.ric.portal.dashboard.portalapi;
21
22 import java.io.IOException;
23 import java.lang.invoke.MethodHandles;
24 import java.util.Map;
25
26 import javax.servlet.http.HttpServletRequest;
27
28 import org.onap.portalsdk.core.onboarding.crossapi.IPortalRestCentralService;
29 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
30 import org.onap.portalsdk.core.restful.domain.EcompUser;
31 import org.oransc.ric.portal.dashboard.DashboardUserManager;
32 import org.oransc.ric.portal.dashboard.config.SpringContextCache;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.context.ApplicationContext;
36
37 /**
38  * Implements the contract used by the Portal to transmit user details to this
39  * on-boarded application. The requests are intercepted first by a servlet in
40  * the EPSDK-FW library, which proxies the calls to these methods.
41  * 
42  * An instance of this class is created upon first request to the API. But this
43  * class is found and instantiated via Class.forName(), so cannot use Spring
44  * annotations.
45  */
46 public class PortalRestCentralServiceImpl implements IPortalRestCentralService {
47
48         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
49
50         private final PortalAuthManager authManager;
51         private final DashboardUserManager userManager;
52
53         public PortalRestCentralServiceImpl() throws IOException, PortalAPIException {
54                 try {
55                         final ApplicationContext context = SpringContextCache.getApplicationContext();
56                         authManager = context.getBean(PortalAuthManager.class);
57                         userManager = context.getBean(DashboardUserManager.class);
58                         logger.debug("ctor: authManager fetched credentials for app {}",
59                                         authManager.getAppCredentials().get(IPortalRestCentralService.CREDENTIALS_APP));
60                         logger.debug("ctor: userManager has list size {}", userManager.getUsers().size());
61                 } catch (Exception ex) {
62                         // Log the exception before it's discarded by class
63                         // PortalRestAPICentralServiceImpl
64                         logger.error("ctor failed", ex);
65                         throw ex;
66                 }
67         }
68
69         /*
70          * Answers the Portal API credentials.
71          */
72         @Override
73         public Map<String, String> getAppCredentials() throws PortalAPIException {
74                 logger.debug("getAppCredentials");
75                 return authManager.getAppCredentials();
76         }
77
78         /*
79          * Extracts the user ID from a cookie in the header
80          */
81         @Override
82         public String getUserId(HttpServletRequest request) throws PortalAPIException {
83                 logger.debug("getUserId");
84                 return authManager.validateEcompSso(request);
85         }
86
87         @Override
88         public void pushUser(EcompUser user) throws PortalAPIException {
89                 logger.debug("pushUser: {}", user);
90                 userManager.createUser(user);
91         }
92
93         @Override
94         public void editUser(String loginId, EcompUser user) throws PortalAPIException {
95                 logger.debug("editUser: {}", user);
96                 userManager.updateUser(loginId, user);
97         }
98
99 }