summaryrefslogtreecommitdiffstats
path: root/cadi/aaf/src/main/java/org/onap/aaf/cadi/aaf/AAFPermission.java
blob: be1d39221e4460b1594dd432132ea18ca17432dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
 * ============LICENSE_START====================================================
 * org.onap.aaf
 * ===========================================================================
 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
 * ===========================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END====================================================
 *
 */

package org.onap.aaf.cadi.aaf;

import java.util.ArrayList;
import java.util.List;

import org.onap.aaf.cadi.Permission;
import org.onap.aaf.misc.env.util.Split;

/**
 * A Class that understands the AAF format of Permission (name/type/action)
 *  or String "name|type|action"
 * 
 * @author Jonathan
 *
 */
public class AAFPermission implements Permission {
    private static final List<String> NO_ROLES;
    protected String ns,type,instance,action,key;
    private List<String> roles;
    
    static {
        NO_ROLES = new ArrayList<>();
    }

    protected AAFPermission() {roles=NO_ROLES;}

    public AAFPermission(String ns, String name, String instance, String action) {
        this.ns = ns;
        type = name;
        this.instance = instance;
        this.action = action;
        if (ns==null) {
            key = type + '|' + instance + '|' + action;
        } else {
            key = ns + '|' + type + '|' + instance + '|' + action;
        }
        this.roles = NO_ROLES;

    }

    public AAFPermission(String ns, String name, String instance, String action, List<String> roles) {
        this.ns = ns;
        type = name;
        this.instance = instance;
        this.action = action;
        if (ns==null) {
            key = type + '|' + instance + '|' + action;
        } else {
            key = ns + '|' + type + '|' + instance + '|' + action;
        }
        this.roles = roles==null?NO_ROLES:roles;
    }
    
    /**
     * Match a Permission
     * if Permission is Fielded type "Permission", we use the fields
     * otherwise, we split the Permission with '|'
     * 
     * when the type or action starts with REGEX indicator character ( ! ),
     * then it is evaluated as a regular expression.
     * 
     * If you want a simple field comparison, it is faster without REGEX
     */
    public boolean match(Permission p) {
        String aafNS;
        String aafType;
        String aafInstance;
        String aafAction;
        if (p instanceof AAFPermission) {
            AAFPermission ap = (AAFPermission)p;
            // Note: In AAF > 1.0, Accepting "*" from name would violate multi-tenancy
            // Current solution is only allow direct match on Type.
            // 8/28/2014 Jonathan - added REGEX ability
            aafNS = ap.getNS();
            aafType = ap.getType();
            aafInstance = ap.getInstance();
            aafAction = ap.getAction();
        } else {
            // Permission is concatenated together: separated by 
            String[] aaf = Split.splitTrim('|', p.getKey());
            switch(aaf.length) {
                case 1:
                    aafNS = aaf[0];
                    aafType="";
                    aafInstance = aafAction = "*";
                    break;
                case 2:
                    aafNS = aaf[0];
                    aafType = aaf[1];
                    aafInstance = aafAction = "*";
                    break;
                case 3:
                    aafNS = aaf[0];
                    aafType = aaf[1];
                    aafInstance = aaf[2]; 
                    aafAction = "*";
                    break;
                default:
                    aafNS = aaf[0];
                    aafType = aaf[1];
                    aafInstance = aaf[2]; 
                    aafAction = aaf[3];
                break;
            }
        }
        boolean typeMatches;
        if (aafNS==null) {
            if (ns==null) {
                typeMatches = aafType.equals(type);
            } else {
                typeMatches = aafType.equals(ns+'.'+type);
            }
        } else if (ns==null) {
            typeMatches = type.equals(aafNS+'.'+aafType);
        } else if (aafNS.length() == ns.length()) {
            typeMatches = aafNS.equals(ns) && aafType.equals(type);
        } else { // Allow for restructuring of NS/Perm structure
            typeMatches = (aafNS+'.'+aafType).equals(ns+'.'+type);
        }
        return (typeMatches &&
                PermEval.evalInstance(instance, aafInstance) &&
                PermEval.evalAction(action, aafAction));
    }

    public String getNS() {
        return ns;
    }

    public String getType() {
        return type;
    }

    public String getFullType() {
        return ns + '.' + type;
    }
    
    public String getInstance() {
        return instance;
    }
    
    public String getAction() {
        return action;
    }
    
    public String getKey() {
        return key;
    }

    /* (non-Javadoc)
     * @see org.onap.aaf.cadi.Permission#permType()
     */
    public String permType() {
        return "AAF";
    }

    public List<String> roles() {
        return roles;
    }
    public String toString() {
        return "AAFPermission:" +
                "\n\tNS: " + ns +
                "\n\tType: " + type + 
                "\n\tInstance: " + instance +
                "\n\tAction: " + action +
                "\n\tKey: " + key;
    }
}