Reorganize dashboard into subfolders
[portal/ric-dashboard.git] / dashboard / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / DashboardUserManager.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;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.lang.invoke.MethodHandles;
25 import java.nio.file.Files;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
30 import org.onap.portalsdk.core.restful.domain.EcompUser;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import com.fasterxml.jackson.core.type.TypeReference;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36
37 /**
38  * Provides simple user-management services.
39  * 
40  * This first implementation serializes user details to a file.
41  * 
42  * Migrate to a database someday?
43  */
44 public class DashboardUserManager {
45
46         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
47
48         // This default value is only useful for development and testing.
49         public static final String USER_FILE_PATH = "dashboard-users.json";
50
51         private final File userFile;
52         private final List<EcompUser> users;
53
54         /**
55          * Development/test-only constructor that uses default file path.
56          * 
57          * @param clear
58          *                  If true, start empty and remove any existing file.
59          * 
60          * @throws IOException
61          *                         On file error
62          */
63         public DashboardUserManager(boolean clear) throws IOException {
64                 this(USER_FILE_PATH);
65                 if (clear) {
66                         logger.debug("ctor: removing file {}", userFile.getAbsolutePath());
67                         File f = new File(DashboardUserManager.USER_FILE_PATH);
68                         if (f.exists())
69                                 Files.delete(f.toPath());
70                         users.clear();
71                 }
72         }
73
74         /**
75          * Constructur that accepts a file path
76          * 
77          * @param userFilePath
78          *                         File path
79          * @throws IOException
80          *                         If file cannot be read
81          */
82         public DashboardUserManager(final String userFilePath) throws IOException {
83                 logger.debug("ctor: userfile {}", userFilePath);
84                 if (userFilePath == null)
85                         throw new IllegalArgumentException("Missing or empty user file property");
86                 userFile = new File(userFilePath);
87                 logger.debug("ctor: managing users in file {}", userFile.getAbsolutePath());
88                 if (userFile.exists()) {
89                         final ObjectMapper mapper = new ObjectMapper();
90                         users = mapper.readValue(userFile, new TypeReference<List<EcompUser>>() {
91                         });
92                 } else {
93                         users = new ArrayList<>();
94                 }
95         }
96
97         /**
98          * Gets the current users.
99          * 
100          * @return List of EcompUser objects, possibly empty
101          */
102         public List<EcompUser> getUsers() {
103                 return this.users;
104         }
105
106         /**
107          * Gets the user with the specified login Id
108          * 
109          * @param loginId
110          *                    Desired login Id
111          * @return User object; null if Id is not known
112          */
113         public EcompUser getUser(String loginId) {
114                 for (EcompUser u : this.users) {
115                         if (u.getLoginId().equals(loginId)) {
116                                 logger.debug("getUser: match on {}", loginId);
117                                 return u;
118                         }
119                 }
120                 logger.debug("getUser: no match on {}", loginId);
121                 return null;
122         }
123
124         private void saveUsers() throws IOException {
125                 final ObjectMapper mapper = new ObjectMapper();
126                 mapper.writeValue(userFile, users);
127         }
128
129         /*
130          * Allow at most one thread to create a user at one time.
131          */
132         public synchronized void createUser(EcompUser user) throws PortalAPIException {
133                 logger.debug("createUser: loginId {}", user.getLoginId());
134                 if (users.contains(user))
135                         throw new PortalAPIException("User exists: " + user.getLoginId());
136                 users.add(user);
137                 try {
138                         saveUsers();
139                 } catch (Exception ex) {
140                         throw new PortalAPIException("Save failed", ex);
141                 }
142         }
143
144         /*
145          * Allow at most one thread to modify a user at one time. We still have
146          * last-edit-wins of course.
147          */
148         public synchronized void updateUser(String loginId, EcompUser user) throws PortalAPIException {
149                 logger.debug("editUser: loginId {}", loginId);
150                 int index = users.indexOf(user);
151                 if (index < 0)
152                         throw new PortalAPIException("User does not exist: " + user.getLoginId());
153                 users.remove(index);
154                 users.add(user);
155                 try {
156                         saveUsers();
157                 } catch (Exception ex) {
158                         throw new PortalAPIException("Save failed", ex);
159                 }
160         }
161
162 }