aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org/openecomp/sdc/be/ecomp/PortalRestApiCentralServiceImpl.java
blob: 945bf7e0f238ff2ce06acd733c8865ce91a7419c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.openecomp.sdc.be.ecomp;

import fj.data.Either;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.onap.portalsdk.core.onboarding.crossapi.IPortalRestCentralService;
import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
import org.onap.portalsdk.core.onboarding.util.CipherUtil;
import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
import org.onap.portalsdk.core.restful.domain.EcompUser;
import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
import org.openecomp.sdc.be.config.BeEcompErrorManager;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.ecomp.converters.EcompUserConverter;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.user.UserBusinessLogic;
import org.openecomp.sdc.be.user.UserBusinessLogicExt;
import org.openecomp.sdc.common.api.Constants;
import org.openecomp.sdc.common.log.wrappers.Logger;
import org.openecomp.sdc.exception.ResponseFormat;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoader;

public final class PortalRestApiCentralServiceImpl implements IPortalRestCentralService {

    private static final String FAILED_TO_UPDATE_USER_CREDENTIALS = "Failed to update user credentials";
    private static final String FAILED_TO_DEACTIVATE_USER = "Failed to deactivate user {}";
    private static final String FAILED_TO_EDIT_USER = "Failed to edit user";
    private static final String EDIT_USER = "EditUser";
    private static final String CHECK_ROLES = "checkIfSingleRoleProvided";
    private static final String RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID = "Received null for argument loginId";
    private static final String RECEIVED_NULL_ROLES = "Received null roles for user";
    private static final String RECEIVED_MULTIPLE_ROLES = "Received multiple roles for user {}";
    private static final String RECEIVED_MULTIPLE_ROLES2 = "Received multiple roles for user";
    private static final String FAILED_TO_CREATE_USER = "Failed to create user {}";
    private static final String FAILED_TO_GET_USER_ID_HEADER = "Failed to get user_id header";
    private static final String JH0003 = "jh0003";
    private static final String PUSH_USER = "PushUser";
    private static final String RECEIVED_NULL_FOR_ARGUMENT_USER = "Received null for argument user";
    private static final Logger log = Logger.getLogger(PortalRestApiCentralServiceImpl.class);
    private UserBusinessLogic userBusinessLogic;
    private UserBusinessLogicExt userBusinessLogicExt;

    public PortalRestApiCentralServiceImpl() throws PortalAPIException {
        try {
            ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
            if (ctx == null) {
                log.debug("Failed to get CurrentWebApplicationContext.");
                BeEcompErrorManager.getInstance().logInternalUnexpectedError("constructor", "Failed to get CurrentWebApplicationContext. Can't get UserBusinessLogic and userBusinessLogicExt", BeEcompErrorManager.ErrorSeverity.ERROR);
                throw new PortalAPIException("CurrentWebApplicationContext is null. Failed to get Bean userBusinessLogic and userBusinessLogicExt");
            } else {
                userBusinessLogic = (UserBusinessLogic) ctx.getBean("userBusinessLogic");
                userBusinessLogicExt = (UserBusinessLogicExt) ctx.getBean("userBusinessLogicExt");
            }
        } catch (Exception e) {
            log.debug("Failed to get user UserBusinessLogic", e);
            BeEcompErrorManager.getInstance()
                .logInvalidInputError("constructor", "Exception thrown" + e.getMessage(), BeEcompErrorManager.ErrorSeverity.ERROR);
            throw new PortalAPIException("SDC Internal server error");
        }
        log.debug("PortalRestApiCentralServiceImpl Class Instantiated");
    }

    //For testing purposes
    PortalRestApiCentralServiceImpl(UserBusinessLogic ubl, UserBusinessLogicExt uble) {
        this.userBusinessLogic = ubl;
        this.userBusinessLogicExt = uble;
    }

    public static void checkIfSingleRoleProvided(EcompUser user) throws PortalAPIException {
        if (user.getRoles() == null) {
            log.debug(RECEIVED_NULL_ROLES, user);
            BeEcompErrorManager.getInstance().logInvalidInputError(CHECK_ROLES, RECEIVED_NULL_ROLES, BeEcompErrorManager.ErrorSeverity.ERROR);
            throw new PortalAPIException(RECEIVED_NULL_ROLES + user);
        } else if (user.getRoles().size() > 1) {
            log.debug(RECEIVED_MULTIPLE_ROLES, user);
            BeEcompErrorManager.getInstance().logInvalidInputError(CHECK_ROLES, RECEIVED_MULTIPLE_ROLES2, BeEcompErrorManager.ErrorSeverity.ERROR);
            throw new PortalAPIException(RECEIVED_MULTIPLE_ROLES2 + user);
        }
    }

    @Override
    public Map<String, String> getAppCredentials() throws PortalAPIException {
        Map<String, String> credMap = new HashMap<>();
        String portalUser = PortalApiProperties.getProperty(PortalPropertiesEnum.USER.value());
        String password = PortalApiProperties.getProperty(PortalPropertiesEnum.PASSWORD.value());
        String appName = PortalApiProperties.getProperty(PortalPropertiesEnum.APP_NAME.value());
        try {
            credMap.put("username", CipherUtil.decryptPKC(portalUser));
            credMap.put("password", CipherUtil.decryptPKC(password));
            credMap.put("appName", CipherUtil.decryptPKC(appName));
        } catch (CipherUtilException e) {
            log.debug("User authentication failed - Decryption failed", e);
            throw new PortalAPIException("Failed to decrypt" + e.getMessage());
        }
        log.debug("the credentials map for portal is {}", credMap);
        return credMap;
    }

    @Override
    public void pushUser(EcompUser user) throws PortalAPIException {
        log.debug("Start handle request of ECOMP pushUser");
        if (user == null) {
            BeEcompErrorManager.getInstance()
                .logInvalidInputError(PUSH_USER, RECEIVED_NULL_FOR_ARGUMENT_USER, BeEcompErrorManager.ErrorSeverity.INFO);
            log.debug(RECEIVED_NULL_FOR_ARGUMENT_USER);
            throw new PortalAPIException(RECEIVED_NULL_FOR_ARGUMENT_USER);
        }
        checkIfSingleRoleProvided(user);
        final String modifierAttId = JH0003;
        log.debug("modifier id is {}", modifierAttId);
        User convertedAsdcUser = EcompUserConverter.convertEcompUserToUser(user);
        try {
            log.debug("Before creating ecomp user {} sdc user {}", user, convertedAsdcUser);
            userBusinessLogic.createUser(modifierAttId, convertedAsdcUser);
        } catch (Exception e) {
            log.debug(FAILED_TO_CREATE_USER, user, e);
            BeEcompErrorManager.getInstance().logInvalidInputError(PUSH_USER, FAILED_TO_CREATE_USER, BeEcompErrorManager.ErrorSeverity.ERROR);
            throw new PortalAPIException(FAILED_TO_CREATE_USER + e.getMessage());
        }
        log.debug("User created ecomp user {} sdc user {}", user, convertedAsdcUser);
    }

    @Override
    public void editUser(String loginId, EcompUser user) throws PortalAPIException {
        if (user == null) {
            log.debug(RECEIVED_NULL_FOR_ARGUMENT_USER);
            BeEcompErrorManager.getInstance()
                .logInvalidInputError(EDIT_USER, RECEIVED_NULL_FOR_ARGUMENT_USER, BeEcompErrorManager.ErrorSeverity.INFO);
            throw new PortalAPIException(RECEIVED_NULL_FOR_ARGUMENT_USER);
        } else if (loginId == null) {
            log.debug(RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID);
            BeEcompErrorManager.getInstance()
                .logInvalidInputError(EDIT_USER, RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID, BeEcompErrorManager.ErrorSeverity.INFO);
            throw new PortalAPIException(RECEIVED_NULL_FOR_ARGUMENT_LOGIN_ID);
        }
        log.debug("Start handle request of ECOMP editUser {} with loginId {} with follopwing roles {}", user, loginId, user.getRoles());
        final String modifierAttId = JH0003;
        log.debug("modifier id is {}", modifierAttId);
        if (user.getLoginId() != null && !user.getLoginId().equals(loginId)) {
            log.debug("loginId and user loginId not equal");
            BeEcompErrorManager.getInstance()
                .logInvalidInputError(EDIT_USER, "loginId and user loginId not equal", BeEcompErrorManager.ErrorSeverity.INFO);
            throw new PortalAPIException("loginId not equals to the user loginId field");
        } else if (user.getLoginId() == null) {
            user.setLoginId(loginId);
        }
        Either<User, ActionStatus> verifyNewUser;
        try {
            verifyNewUser = userBusinessLogic.verifyNewUserForPortal(user.getLoginId());
        } catch (ComponentException e) {
            log.debug("Failed to verify new user", e);
            throw new PortalAPIException(e.getCause());
        }
        if (verifyNewUser.isRight() && (ActionStatus.USER_NOT_FOUND.equals(verifyNewUser.right().value()) || ActionStatus.USER_INACTIVE
            .equals(verifyNewUser.right().value()))) {
            log.debug("Edit user for user that not exist in DB, executing push user flow {}", user);
            pushUser(user);
            return;
        }
        User asdcUser = EcompUserConverter.convertEcompUserToUser(user);
        log.debug("Before editing ecomp user {} sdc user {}", user, asdcUser);
        Either<User, ResponseFormat> updateUserCredentialsResponse = userBusinessLogic.updateUserCredentials(asdcUser);
        if (updateUserCredentialsResponse.isRight()) {
            log.debug(FAILED_TO_UPDATE_USER_CREDENTIALS);
            BeEcompErrorManager.getInstance()
                .logInvalidInputError(EDIT_USER, FAILED_TO_UPDATE_USER_CREDENTIALS, BeEcompErrorManager.ErrorSeverity.ERROR);
            throw new PortalAPIException(FAILED_TO_EDIT_USER + updateUserCredentialsResponse.right().value());
        }
        if (user.getRoles() == null || user.getRoles().isEmpty()) {
            try {
                log.debug("Before deactivating ecomp user {} sdc user {}", user, asdcUser);
                userBusinessLogicExt.deActivateUser(modifierAttId, loginId);
            } catch (Exception e) {
                log.debug("Error: Failed to deactivate user {}", loginId);
                BeEcompErrorManager.getInstance()
                    .logInvalidInputError(FAILED_TO_DEACTIVATE_USER, "Failed to deactivate user", BeEcompErrorManager.ErrorSeverity.INFO);
                throw new PortalAPIException("Error: Failed to deactivate user" + e);
            }
        } else {
            checkIfSingleRoleProvided(user);
            try {
                log.debug("Before updating ecomp user {} sdc user {}", user, asdcUser);
                userBusinessLogic.updateUserRole(modifierAttId, loginId, asdcUser.getRole());
            } catch (Exception e) {
                log.debug("Error: Failed to update user role {}", loginId);
                BeEcompErrorManager.getInstance()
                    .logInvalidInputError(FAILED_TO_EDIT_USER, "Failed to update user role", BeEcompErrorManager.ErrorSeverity.INFO);
                throw new PortalAPIException("Error: Failed to update user role" + e);
            }
        }
        log.debug("user updated ecomp user {} sdc user {}", user, asdcUser);
    }

    @Override
    public String getUserId(HttpServletRequest request) throws PortalAPIException {
        String header = request.getHeader(Constants.USER_ID_HEADER);
        if (header == null) {
            log.debug(FAILED_TO_GET_USER_ID_HEADER);
            BeEcompErrorManager.getInstance()
                .logInvalidInputError("getUserId", FAILED_TO_GET_USER_ID_HEADER, BeEcompErrorManager.ErrorSeverity.ERROR);
            throw new PortalAPIException(FAILED_TO_GET_USER_ID_HEADER);
        }
        return header;
    }
}