2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property and Nokia
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
20 package org.oransc.ric.portal.dashboard.portalapi;
23 import java.io.IOException;
24 import java.lang.invoke.MethodHandles;
25 import java.util.ArrayList;
26 import java.util.List;
28 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
29 import org.onap.portalsdk.core.restful.domain.EcompUser;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import com.fasterxml.jackson.core.JsonGenerationException;
34 import com.fasterxml.jackson.core.type.TypeReference;
35 import com.fasterxml.jackson.databind.JsonMappingException;
36 import com.fasterxml.jackson.databind.ObjectMapper;
39 * Provides user-management services.
41 * This first implementation serializes user details to a file. TODO: migrate to
44 public class DashboardUserManager {
46 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
48 private final File userFile;
49 private final List<EcompUser> users;
51 public DashboardUserManager(final String userFilePath) throws IOException {
52 logger.debug("ctor: userfile {}", userFilePath);
53 if (userFilePath == null)
54 throw new IllegalArgumentException("Missing or empty user file property");
55 userFile = new File(userFilePath);
56 logger.debug("ctor: managing users in file {}", userFile.getAbsolutePath());
57 if (userFile.exists()) {
58 final ObjectMapper mapper = new ObjectMapper();
59 users = mapper.readValue(userFile, new TypeReference<List<EcompUser>>() {
62 users = new ArrayList<>();
67 * Gets the user with the specified login Id
71 * @return User object; null if Id is not known
73 public EcompUser getUser(String loginId) {
74 for (EcompUser u : this.users) {
75 if (u.getLoginId().equals(loginId)) {
76 logger.debug("getUser: match on {}", loginId);
80 logger.debug("getUser: no match on {}", loginId);
84 private void saveUsers() throws JsonGenerationException, JsonMappingException, IOException {
85 final ObjectMapper mapper = new ObjectMapper();
86 mapper.writeValue(userFile, users);
90 * Allow at most one thread to create a user at one time.
92 public synchronized void createUser(EcompUser user) throws PortalAPIException {
93 logger.debug("createUser: loginId is " + user.getLoginId());
94 if (users.contains(user))
95 throw new PortalAPIException("User exists: " + user.getLoginId());
99 } catch (Exception ex) {
100 throw new PortalAPIException("Save failed", ex);
105 * Allow at most one thread to modify a user at one time. We still have
106 * last-edit-wins of course.
108 public synchronized void updateUser(String loginId, EcompUser user) throws PortalAPIException {
109 logger.debug("editUser: loginId is " + loginId);
110 int index = users.indexOf(user);
112 throw new PortalAPIException("User does not exist: " + user.getLoginId());
117 } catch (Exception ex) {
118 throw new PortalAPIException("Save failed", ex);