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