Simplify the user management
[clamp.git] / src / main / java / org / onap / clamp / authorization / SecureServicePermission.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.authorization;
25
26 /**
27  * Permission class that can be instantiated easily using constructor or factory
28  * methods.
29  */
30 public class SecureServicePermission {
31     public static final String ALL = "*";
32
33     private String             type;
34     private String             instance;
35     private String             action;
36
37     /**
38      * Factory method to create permission given type, instance, and action.
39      *
40      * @param type type of the permission
41      * @param instance instance of the permission
42      * @param action action of the permission
43      * @return instance of SecureServicePermission with type, instance and action
44      */
45     public static SecureServicePermission create(String type, String instance, String action) {
46         return new SecureServicePermission(type, instance, action);
47     }
48
49     /**
50      * Factory method to create permission given type and instance. Default
51      * action to ALL/*.
52      *
53      * @param type type of the permission
54      * @param instance instance of the permission
55      * @return instance of SecureServicePermission with type, instance and default action
56      */
57     public static SecureServicePermission create(String type, String instance) {
58         return new SecureServicePermission(type, instance, ALL);
59     }
60
61     /**
62      * Factory method to create permission given type. Default instance and
63      * action to ALL/*.
64      *
65      * @param type type of the permission
66      * @return instance of SecureServicePermission with type and default instance and action
67      */
68     public static SecureServicePermission create(String type) {
69         return new SecureServicePermission(type, ALL, ALL);
70     }
71
72     /**
73      * Instantiate permission given type, instance, and action.
74      *
75      * @param type type of the permission
76      * @param instance instance of the permission
77      * @param action action of the permission
78      */
79     public SecureServicePermission(String type, String instance, String action) {
80         this.type = type;
81         this.instance = instance;
82         this.action = action;
83     }
84
85     /**
86      * Instantiate permission given type from concatenated string
87      *
88      * @param concatenatedString
89      *            the string type|instance|action, less than 3 params can be
90      *            provided (e.g. "permission-type-cl", "permission-type-cl|dev",
91      *            "permission-type-cl|dev|update" )
92      */
93     public SecureServicePermission(String concatenatedString) {
94         String[] userInfo = concatenatedString.split("[|]");
95         // We should have at least 1 string
96         this.type = userInfo[0];
97         this.instance = (userInfo.length > 1 ? userInfo[1] : ALL);
98         this.action = (userInfo.length > 2 ? userInfo[2] : ALL);
99     }
100
101     /**
102      * Override toString - return permission in key format.
103      */
104     @Override
105     public String toString() {
106         return getKey();
107     }
108
109     /**
110      * Return Permission in Key format = type, instance, and action separate by
111      * pipe character.
112      *
113      * @return permission in key format
114      */
115     public String getKey() {
116         return type + "|" + instance + "|" + action;
117     }
118
119     /**
120      * Return Permission in Key format = type, all instance, and action separate
121      * by pipe character.
122      *
123      * @return permission in key format
124      */
125     public String getKeyAllInstance() {
126         return type + "|" + ALL + "|" + action;
127     }
128
129     /**
130      * Return Permission in Key format = type, all instance, and all action
131      * separate by pipe character.
132      *
133      * @return permission in key format
134      */
135     public String getKeyAllInstanceAction() {
136         return type + "|" + ALL + "|" + ALL;
137     }
138
139     /**
140      * Return Permission in Key format = type, instance, and all action separate
141      * by pipe character.
142      *
143      * @return permission in key format
144      */
145     public String getKeyAllAction() {
146         return type + "|" + instance + "|" + ALL;
147     }
148
149     /**
150      * Returns the permission type.
151      *
152      * @return the type
153      */
154     public String getType() {
155         return type;
156     }
157
158     /**
159      * Sets the type of permission.
160      *
161      * @param type the type to set
162      */
163     public void setType(String type) {
164         this.type = type;
165     }
166
167     /**
168      * Returns the instance of permission.
169      *
170      * @return the instance
171      */
172     public String getInstance() {
173         return instance;
174     }
175
176     /**
177      * Sets the instance of permission.
178      *
179      * @param instance the instance to set
180      */
181     public void setInstance(String instance) {
182         this.instance = instance;
183     }
184
185     /**
186      * Returns the action of permission.
187      *
188      * @return the action
189      */
190     public String getAction() {
191         return action;
192     }
193
194     /**
195      * Sets the action of permission.
196      *
197      * @param action the action to set
198      */
199     public void setAction(String action) {
200         this.action = action;
201     }
202
203 }