2d4de64493b12812db3f05fe6ec66aea9479d586
[clamp.git] / src / main / java / org / onap / clamp / clds / service / 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.clds.service;
25
26 /**
27  * Permission class that can be instantiated easily using constructor or factory
28  * methods.
29  */
30 public class SecureServicePermission {
31     public final static 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
41      * @param instance
42      * @param action
43      * @return
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
54      * @param instance
55      * @return
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
66      * @return
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
76      * @param instance
77      * @param action
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
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
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
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
144      */
145     public String getKeyAllAction() {
146         return type + "|" + instance + "|" + ALL;
147     }
148
149     /**
150      * @return the type
151      */
152     public String getType() {
153         return type;
154     }
155
156     /**
157      * @param type
158      *            the type to set
159      */
160     public void setType(String type) {
161         this.type = type;
162     }
163
164     /**
165      * @return the instance
166      */
167     public String getInstance() {
168         return instance;
169     }
170
171     /**
172      * @param instance
173      *            the instance to set
174      */
175     public void setInstance(String instance) {
176         this.instance = instance;
177     }
178
179     /**
180      * @return the action
181      */
182     public String getAction() {
183         return action;
184     }
185
186     /**
187      * @param action
188      *            the action to set
189      */
190     public void setAction(String action) {
191         this.action = action;
192     }
193
194 }