2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property
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;
23 import java.io.IOException;
24 import java.lang.invoke.MethodHandles;
25 import java.util.ArrayList;
26 import java.util.HashSet;
27 import java.util.List;
30 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
31 import org.onap.portalsdk.core.restful.domain.EcompRole;
32 import org.onap.portalsdk.core.restful.domain.EcompUser;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import com.fasterxml.jackson.core.JsonGenerationException;
37 import com.fasterxml.jackson.core.type.TypeReference;
38 import com.fasterxml.jackson.databind.JsonMappingException;
39 import com.fasterxml.jackson.databind.ObjectMapper;
42 * Provides simple user-management services.
44 * This first implementation serializes user details to a file.
46 * TODO: migrate to a database.
48 public class DashboardUserManager {
50 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
52 // This default value is only useful for development and testing.
53 public static final String USER_FILE_PATH = "dashboard-users.json";
55 private final File userFile;
56 private final List<EcompUser> users;
59 * Development/test-only constructor that uses default file path.
62 * If true, start empty and remove any existing file.
67 public DashboardUserManager(boolean clear) throws IOException {
70 logger.debug("ctor: removing file {}", userFile.getAbsolutePath());
71 File f = new File(DashboardUserManager.USER_FILE_PATH);
79 * Constructur that accepts a file path
84 * If file cannot be read
86 public DashboardUserManager(final String userFilePath) throws IOException {
87 logger.debug("ctor: userfile {}", userFilePath);
88 if (userFilePath == null)
89 throw new IllegalArgumentException("Missing or empty user file property");
90 userFile = new File(userFilePath);
91 logger.debug("ctor: managing users in file {}", userFile.getAbsolutePath());
92 if (userFile.exists()) {
93 final ObjectMapper mapper = new ObjectMapper();
94 users = mapper.readValue(userFile, new TypeReference<List<EcompUser>>() {
97 users = new ArrayList<>();
102 * Gets the current users.
104 * @return List of EcompUser objects, possibly empty
106 public List<EcompUser> getUsers() {
111 * Gets the user with the specified login Id
115 * @return User object; null if Id is not known
117 public EcompUser getUser(String loginId) {
118 for (EcompUser u : this.users) {
119 if (u.getLoginId().equals(loginId)) {
120 logger.debug("getUser: match on {}", loginId);
124 logger.debug("getUser: no match on {}", loginId);
128 private void saveUsers() throws JsonGenerationException, JsonMappingException, IOException {
129 final ObjectMapper mapper = new ObjectMapper();
130 mapper.writeValue(userFile, users);
134 * Allow at most one thread to create a user at one time.
136 public synchronized void createUser(EcompUser user) throws PortalAPIException {
137 logger.debug("createUser: loginId is " + user.getLoginId());
138 if (users.contains(user))
139 throw new PortalAPIException("User exists: " + user.getLoginId());
143 } catch (Exception ex) {
144 throw new PortalAPIException("Save failed", ex);
149 * Allow at most one thread to modify a user at one time. We still have
150 * last-edit-wins of course.
152 public synchronized void updateUser(String loginId, EcompUser user) throws PortalAPIException {
153 logger.debug("editUser: loginId is " + loginId);
154 int index = users.indexOf(user);
156 throw new PortalAPIException("User does not exist: " + user.getLoginId());
161 } catch (Exception ex) {
162 throw new PortalAPIException("Save failed", ex);
166 // Test infrastructure
167 public static void main(String[] args) throws Exception {
168 DashboardUserManager dum = new DashboardUserManager(false);
169 EcompUser user = new EcompUser();
170 user.setActive(true);
171 user.setLoginId("demo");
172 user.setFirstName("First");
173 user.setLastName("Last");
174 EcompRole role = new EcompRole();
176 role.setName(DashboardConstants.ROLE_NAME_ADMIN);
177 Set<EcompRole> roles = new HashSet<>();
179 user.setRoles(roles);
180 dum.createUser(user);
181 logger.debug("Created user {}", user);