1886f333a8b8cbdeec251bc7b65e5c23469b5d7b
[vid.git] / vid-app-common / src / main / java / org / onap / vid / roles / RoleProvider.java
1 package org.onap.vid.roles;
2
3 import com.fasterxml.jackson.core.JsonProcessingException;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
6 import org.onap.portalsdk.core.web.support.UserUtils;
7 import org.onap.vid.aai.AaiResponse;
8 import org.onap.vid.aai.exceptions.RoleParsingException;
9 import org.onap.vid.model.ModelConstants;
10 import org.onap.vid.model.Subscriber;
11 import org.onap.vid.model.SubscriberList;
12 import org.onap.vid.services.AaiService;
13 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.stereotype.Component;
15
16 import javax.annotation.PostConstruct;
17 import javax.servlet.http.HttpServletRequest;
18 import java.util.*;
19
20 //import org.codehaus.jackson.map.ObjectMapper;
21
22 /**
23  * Created by Oren on 7/1/17.
24  */
25
26 @Component
27 public class RoleProvider {
28
29     private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(RoleProvider.class);
30     final String readPermissionString = "read";
31     SubscriberList subscribers;
32     ObjectMapper om = new ObjectMapper();
33     @Autowired
34     private AaiService aaiService;
35
36     public static List<String> extractRoleFromSession(HttpServletRequest request) {
37
38         return new ArrayList<String>();
39
40     }
41
42     @PostConstruct
43     public void init() {
44         LOG.debug(EELFLoggerDelegate.debugLogger, "Role provider => init method started");
45         AaiResponse<SubscriberList> subscribersResponse = aaiService.getFullSubscriberList();
46         subscribers = subscribersResponse.getT();
47         LOG.debug(EELFLoggerDelegate.debugLogger, "Role provider => init method finished");
48     }
49
50     public List<Role> getUserRoles(HttpServletRequest request) throws JsonProcessingException {
51         String logPrefix = "Role Provider (" + UserUtils.getUserId(request) + ") ==>";
52
53         LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + "Entering to get user role for user " + UserUtils.getUserId(request));
54
55         List<Role> roleList = new ArrayList<>();
56         //Disable roles until AAF integration finishes
57         /*Map roles = UserUtils.getRoles(request);
58         for (Object role : roles.keySet()) {
59             org.onap.portalsdk.core.domain.Role sdkRol = (org.onap.portalsdk.core.domain.Role) roles.get(role);
60
61             LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + "Role " + sdkRol.getName() + " is being proccessed");
62             try {
63                 if (sdkRol.getName().contains(readPermissionString)) {
64                     LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + " Role " + sdkRol.getName() + " contain " + readPermissionString);
65
66                     continue;
67                 }
68                 String[] roleParts = splitRole((sdkRol.getName()), logPrefix);
69                 roleList.add(createRoleFromStringArr(roleParts, logPrefix));
70                 String msg = String.format(logPrefix + " User %s got permissions %s", UserUtils.getUserId(request), Arrays.toString(roleParts));
71                 LOG.debug(EELFLoggerDelegate.debugLogger, msg);
72             } catch (RoleParsingException e) {
73                 LOG.error(logPrefix + " Failed to parse permission");
74
75             }
76         }*/
77
78         return roleList;
79     }
80
81     public String[] splitRole(String roleAsString, String logPrefix) {
82         LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + "Spliting role = " + roleAsString + "With delimeter = " + ModelConstants.ROLE_DELIMITER);
83         return roleAsString.split(ModelConstants.ROLE_DELIMITER);
84     }
85
86     public boolean userPermissionIsReadOnly(List<Role> roles) {
87
88         return (!(roles.size() > 0));
89     }
90
91     public boolean userPermissionIsReadLogs(List<Role> roles){
92         for(Role role: roles){
93             if(role.getServiceType().equals("LOGS")){
94                 if(role.getTenant().equals("PERMITTED")){
95                     return true;
96                 }
97             }
98         }
99         return false;
100     }
101
102     private String replaceSubscriberNameToGlobalCustomerID(String subscriberName, String logPrefix) throws JsonProcessingException {
103         if (subscribers == null) {
104             LOG.debug(EELFLoggerDelegate.debugLogger, "replaceSubscriberNameToGlobalCustomerID calling init method");
105             init();
106         }
107         LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + "subscribers list size is  " + subscribers.customer.size() + " with the values " + om.writeValueAsString(subscribers.customer));
108         LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + "subscribers list size is  " + subscribers.customer.size() + " with the values " + om.writeValueAsString(subscribers.customer));
109
110
111         Optional<Subscriber> s = subscribers.customer.stream().filter(x -> x.subscriberName.equals(subscriberName)).findFirst();
112         //Fixing bug of logging "optional get" before isPresent
113         String replacement = s.isPresent() ? s.get().globalCustomerId : "";
114         LOG.debug(EELFLoggerDelegate.debugLogger, logPrefix + "Subscribername " + subscriberName + " changed to  " + replacement);
115         return replacement;
116     }
117
118     public Role createRoleFromStringArr(String[] roleParts, String rolePrefix) throws JsonProcessingException, RoleParsingException {
119         String globalCustomerID = replaceSubscriberNameToGlobalCustomerID(roleParts[0], rolePrefix);
120         try {
121             if (roleParts.length > 2) {
122                 return new Role(EcompRole.READ, globalCustomerID, roleParts[1], roleParts[2]);
123             } else {
124                 return new Role(EcompRole.READ, globalCustomerID, roleParts[1], null);
125             }
126         } catch (ArrayIndexOutOfBoundsException e) {
127             if (roleParts.length > 0)
128                 LOG.debug(EELFLoggerDelegate.debugLogger, "Could not parse role ", roleParts[0]);
129             else {
130                 LOG.debug(EELFLoggerDelegate.debugLogger, "Got empty role, Could not parse it ");
131
132             }
133             throw new RoleParsingException();
134         }
135
136     }
137
138 }
139