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