First version of NonRT RIC Controlpanel
[portal/nonrtric-controlpanel.git] / webapp-backend / src / main / java / org / oransc / portal / nonrtric / controlpanel / portalapi / PortalRestCentralServiceImpl.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
6  * Modifications Copyright (C) 2020 Nordix Foundation
7  * %%
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ========================LICENSE_END===================================
20  */
21 package org.oransc.portal.nonrtric.controlpanel.portalapi;
22
23 import java.io.IOException;
24 import java.lang.invoke.MethodHandles;
25 import java.util.Map;
26
27 import javax.servlet.http.HttpServletRequest;
28
29 import org.onap.portalsdk.core.onboarding.crossapi.IPortalRestCentralService;
30 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
31 import org.onap.portalsdk.core.restful.domain.EcompUser;
32 import org.oransc.portal.nonrtric.controlpanel.ControlpanelUserManager;
33 import org.oransc.portal.nonrtric.controlpanel.config.SpringContextCache;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.context.ApplicationContext;
38
39 /**
40  * Implements the contract used by the Portal to transmit user details to this
41  * on-boarded application. The requests are intercepted first by a servlet in
42  * the EPSDK-FW library, which proxies the calls to these methods.
43  *
44  * An instance of this class is created upon first request to the API. But this
45  * class is found and instantiated via Class.forName(), so cannot use Spring
46  * annotations.
47  */
48 public class PortalRestCentralServiceImpl implements IPortalRestCentralService {
49
50     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
51
52     @Autowired
53     private SpringContextCache springContextCache;
54     private final PortalAuthManager authManager;
55     private final ControlpanelUserManager userManager;
56
57     public PortalRestCentralServiceImpl() throws IOException, PortalAPIException {
58         final ApplicationContext context = springContextCache.getApplicationContext();
59         authManager = context.getBean(PortalAuthManager.class);
60         userManager = context.getBean(ControlpanelUserManager.class);
61     }
62
63     /*
64      * Answers the Portal API credentials.
65      */
66     @Override
67     public Map<String, String> getAppCredentials() throws PortalAPIException {
68         logger.debug("getAppCredentials");
69         return authManager.getAppCredentials();
70     }
71
72     /*
73      * Extracts the user ID from a cookie in the header
74      */
75     @Override
76     public String getUserId(HttpServletRequest request) throws PortalAPIException {
77         logger.debug("getuserId");
78         return authManager.validateEcompSso(request);
79     }
80
81     @Override
82     public void pushUser(EcompUser user) throws PortalAPIException {
83         logger.debug("pushUser: {}", user);
84         userManager.createUser(user);
85     }
86
87     @Override
88     public void editUser(String loginId, EcompUser user) throws PortalAPIException {
89         logger.debug("editUser: {}", user);
90         userManager.updateUser(loginId, user);
91     }
92
93 }