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