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