40c8396767690d0f065a9725f08aafed3fe1f74d
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / utils / UserUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
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.onap.policy.utils;
21
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.onap.policy.model.Roles;
30
31 public final class UserUtils {
32
33     private UserUtils() {
34         // Empty Constructor
35     }
36
37     public static class Pair<T, U> {
38         public final T t;
39         public final U u;
40
41         public Pair(T t, U u) {
42             this.t = t;
43             this.u = u;
44         }
45     }
46
47     /**
48      * Check Role and its Scopes.
49      * 
50      * @param userRoles list of UserRoles.
51      * @return return role and scope from UserRole Object.
52      */
53     public static Pair<Set<String>, List<String>> checkRoleAndScope(List<Object> userRoles) {
54         Set<String> scopes;
55         List<String> roles;
56         // Check if the Role and Scope Size are Null get the values from db.
57         roles = new ArrayList<>();
58         scopes = new HashSet<>();
59         for (Object role : userRoles) {
60             Roles userRole = (Roles) role;
61             roles.add(userRole.getRole());
62             if (userRole.getScope() != null) {
63                 if (userRole.getScope().contains(",")) {
64                     String[] multipleScopes = userRole.getScope().split(",");
65                     for (int i = 0; i < multipleScopes.length; i++) {
66                         scopes.add(trimScope(multipleScopes[i]));
67                     }
68                 } else {
69                     scopes.add(trimScope(userRole.getScope()));
70                 }
71             }
72         }
73         return new Pair<>(scopes, roles);
74     }
75
76     /**
77      * Get Role by Scope based on UserRole Object.
78      * 
79      * @param userRoles list of UserRoles.
80      * @return return the map<scope, role>.
81      */
82     public static Map<String, String> getRoleByScope(List<Object> userRoles) {
83         Map<String, String> rolesList = new HashMap<>();
84         for (Object role : userRoles) {
85             Roles userRole = (Roles) role;
86             if (!userRole.getRole().startsWith("super-")) {
87                 rolesList = addNonSuperUserScopes(userRole, rolesList);
88             } else {
89                 rolesList.put("@All@", userRole.getRole());
90             }
91         }
92         return rolesList;
93     }
94
95     /**
96      * Read non super role scopes and add to map.
97      * 
98      * @param userRole  Role Object.
99      * @param rolesList roleList Object.
100      * @return return the map<scope, role>.
101      */
102     private static Map<String, String> addNonSuperUserScopes(Roles userRole, Map<String, String> rolesList) {
103         if (userRole.getScope() != null && !(userRole.getScope().trim().isEmpty())) {
104             if (userRole.getScope().contains(",")) {
105                 String[] multipleScopes = userRole.getScope().split(",");
106                 for (int i = 0; i < multipleScopes.length; i++) {
107                     rolesList.put(trimScope(multipleScopes[i]), userRole.getRole());
108                 }
109             } else {
110                 rolesList.put(trimScope(userRole.getScope()), userRole.getRole());
111             }
112         }
113         return rolesList;
114     }
115
116     /**
117      * Trim Scope Value.
118      * 
119      * @param scope string scope name.
120      * @return trim scope.
121      */
122     private static String trimScope(String scope) {
123         return scope.replace("[", "").replace("]", "").replace("\"", "").trim();
124     }
125
126 }