Onboarding foundation changes
[sdc.git] / openecomp-be / lib / openecomp-core-lib / openecomp-session-lib / src / main / java / org / openecomp / sdc / common / session / impl / AsdcSessionContextProvider.java
1 package org.openecomp.sdc.common.session.impl;
2
3 import org.openecomp.sdc.common.errors.CoreException;
4 import org.openecomp.sdc.common.errors.ErrorCode;
5 import org.openecomp.sdc.common.session.SessionContext;
6 import org.openecomp.sdc.common.session.SessionContextProvider;
7 import org.openecomp.sdc.common.session.User;
8
9 public class AsdcSessionContextProvider implements SessionContextProvider {
10
11   private static final ThreadLocal<String> threadUserId = new ThreadLocal<>();
12   private static final ThreadLocal<String> threadTenant = new ThreadLocal<>();
13
14   @Override
15   public void create(String userId, String tenant) {
16     threadUserId.set(userId);
17     threadTenant.set(tenant);
18   }
19
20   @Override
21   public SessionContext get() {
22     if (threadUserId.get() == null) {
23       throw new CoreException(new ErrorCode.ErrorCodeBuilder().withMessage("UserId was not set "
24           + "for this thread").build());
25     }
26
27     if (threadTenant.get() == null) {
28       throw new CoreException(new ErrorCode.ErrorCodeBuilder().withMessage("Tenant was not set "
29           + "for this thread").build());
30     }
31
32     return new AsdcSessionContext(new User(threadUserId.get()), threadTenant.get());
33   }
34
35   @Override
36   public void close() {
37     threadUserId.remove();
38     threadTenant.remove();
39   }
40
41   private static class AsdcSessionContext implements SessionContext {
42
43     private final User user;
44     private final String tenant;
45
46     private AsdcSessionContext(User user, String tenant) {
47       this.user = user;
48       this.tenant = tenant;
49     }
50
51     @Override
52     public User getUser() {
53       return user;
54     }
55
56     @Override
57     public String getTenant() {
58       return tenant;
59     }
60   }
61 }