aboutsummaryrefslogtreecommitdiffstats
path: root/ONAP-REST/src/main/java/org/onap/policy/rest/util/PdpPolicyContainer.java
blob: d6bd3f831ed2495c816dabfd838b52ef123337c5 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*-
 * ============LICENSE_START=======================================================
 * ONAP Policy Engine
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2019 Nordix Foundation.
 * ================================================================================
 * 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.policy.rest.util;

import com.att.research.xacml.api.pap.PDP;
import com.att.research.xacml.api.pap.PDPGroup;
import com.att.research.xacml.api.pap.PDPPolicy;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.onap.policy.common.logging.flexlogger.FlexLogger;
import org.onap.policy.common.logging.flexlogger.Logger;
import org.onap.policy.xacml.api.XACMLErrorConstants;
import org.onap.policy.xacml.std.pap.StdPDPPolicy;

public class PdpPolicyContainer extends PolicyItemSetChangeNotifier implements PolicyContainer.Indexed {
    private static final long serialVersionUID = 1L;
    private static final Logger LOGGER = FlexLogger.getLogger(PdpPolicyContainer.class);

    private static final String PROPERTY_ID = "Id";
    private static final String PROPERTY_NAME = "Name";
    private static final String PROPERTY_VERSION = "Version";
    private static final String PROPERTY_DESCRIPTION = "Description";
    private static final String PROPERTY_ISROOT = "Root";

    /**
     * List of the string identifiers for the available properties.
     */
    private static Collection<String> pDPPolicyProperties;

    private final transient Object data;
    private transient List<PDPPolicy> policies;

    /**
     * Instantiates a new pdp policy container.
     *
     * @param data the data
     */
    @SuppressWarnings("unchecked")
    public PdpPolicyContainer(Object data) {
        super();
        this.data = data;
        if (this.data instanceof PDPGroup) {
            policies = new ArrayList<>(((PDPGroup) this.data).getPolicies());
        }
        if (this.data instanceof PDP) {
            policies = new ArrayList<>(((PDP) this.data).getPolicies());
        }
        if (this.data instanceof Set) {
            policies = new ArrayList<>((Set<PDPPolicy>) data);
        }
        if (this.policies == null) {
            LOGGER.info("NULL policies");
            throw new NullPointerException("PDPPolicyContainer created with unexpected Object type '"
                            + data.getClass().getName() + "'");
        }
        this.setContainer(this);
    }

    /**
     * Next item id.
     *
     * @param itemId the item id
     * @return the object
     */
    @Override
    public Object nextItemId(Object itemId) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("nextItemId: " + itemId);
        }
        int index = this.policies.indexOf(itemId);
        if (index == -1 || ((index + 1) >= this.policies.size())) {
            return null;
        }
        return new PdpPolicyItem(this.policies.get(index + 1));
    }

    /**
     * Prev item id.
     *
     * @param itemId the item id
     * @return the object
     */
    @Override
    public Object prevItemId(Object itemId) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("prevItemId: " + itemId);
        }
        int index = this.policies.indexOf(itemId);
        if (index <= 0) {
            return null;
        }
        return new PdpPolicyItem(this.policies.get(index - 1));
    }

    /**
     * First item id.
     *
     * @return the object
     */
    @Override
    public Object firstItemId() {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("firstItemId: ");
        }
        if (this.policies.isEmpty()) {
            return null;
        }
        return new PdpPolicyItem(this.policies.get(0));
    }

    /**
     * Last item id.
     *
     * @return the object
     */
    @Override
    public Object lastItemId() {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("lastItemid: ");
        }
        if (this.policies.isEmpty()) {
            return null;
        }
        return new PdpPolicyItem(this.policies.get(this.policies.size() - 1));
    }

    /**
     * Checks if is first id.
     *
     * @param itemId the item id
     * @return true, if is first id
     */
    @Override
    public boolean isFirstId(Object itemId) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("isFirstId: " + itemId);
        }
        if (this.policies.isEmpty()) {
            return false;
        }
        return itemId.equals(this.policies.get(0));
    }

    /**
     * Checks if is last id.
     *
     * @param itemId the item id
     * @return true, if is last id
     */
    @Override
    public boolean isLastId(Object itemId) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("isLastId: " + itemId);
        }
        if (this.policies.isEmpty()) {
            return false;
        }
        return itemId.equals(this.policies.get(this.policies.size() - 1));
    }

    /**
     * Adds the item after.
     *
     * @param previousItemId the previous item id
     * @return the object
     */
    @Override
    public Object addItemAfter(Object previousItemId) {
        return null;
    }

    /**
     * Gets the container property ids.
     *
     * @return the container property ids
     */
    @Override
    public Collection<?> getContainerPropertyIds() {
        return pDPPolicyProperties;
    }

    /**
     * Gets the item ids.
     *
     * @return the item ids
     */
    @Override
    public Collection<?> getItemIds() {
        final Collection<Object> items = new ArrayList<>();
        items.addAll(this.policies);
        return Collections.unmodifiableCollection(items);
    }

    /**
     * Gets the item ids.
     *
     * @param startIndex the start index
     * @param numberOfItems the number of items
     * @return the item ids
     */
    @Override
    public List<?> getItemIds(int startIndex, int numberOfItems) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("getItemIds: " + startIndex + " " + numberOfItems);
        }
        if (numberOfItems < 0) {
            throw new IllegalArgumentException();
        }
        return this.policies.subList(startIndex, startIndex + numberOfItems);
    }

    /**
     * Gets the type.
     *
     * @param propertyId the property id
     * @return the type
     */
    @Override
    public Class<?> getType(Object propertyId) {
        if (propertyId.equals(PROPERTY_ID)) {
            return String.class;
        }
        if (propertyId.equals(PROPERTY_NAME)) {
            return String.class;
        }
        if (propertyId.equals(PROPERTY_VERSION)) {
            return String.class;
        }
        if (propertyId.equals(PROPERTY_DESCRIPTION)) {
            return String.class;
        }
        if (propertyId.equals(PROPERTY_ISROOT)) {
            return Boolean.class;
        }
        return null;
    }

    /**
     * Size.
     *
     * @return the int
     */
    @Override
    public int size() {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("size: " + this.policies.size());
        }
        return this.policies.size();
    }

    /**
     * Contains id.
     *
     * @param itemId the item id
     * @return true, if successful
     */
    @Override
    public boolean containsId(Object itemId) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("containsId: " + itemId);
        }
        return this.policies.contains(itemId);
    }

    /**
     * Adds the item.
     *
     * @return the object
     */
    @Override
    public Object addItem() {
        throw new UnsupportedOperationException("Cannot add an empty policy.");
    }

    /**
     * Removes the item.
     *
     * @param itemId the item id
     * @return true, if successful
     */
    @Override
    public boolean removeItem(Object itemId) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("removeItem: " + itemId);
        }
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        StdPDPPolicy pdpPolicy = null;
        try {
            pdpPolicy = mapper.readValue(itemId.toString(), StdPDPPolicy.class);
            for (int i = 0; i < policies.size(); i++) {
                if (policies.get(i).getId().equalsIgnoreCase(pdpPolicy.getId())) {
                    return this.policies.remove(this.policies.get(i));
                }
            }
        } catch (Exception e) {
            LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE
                            + "Exception Occured While Mapping the Removing Policy from PDP Group to Std Policy" + e);
        }
        return this.policies.remove(itemId);
    }

    /**
     * Adds the container property.
     *
     * @param propertyId the property id
     * @param type the type
     * @param defaultValue the default value
     * @return true, if successful
     */
    @Override
    public boolean addContainerProperty(Object propertyId, Class<?> type, Object defaultValue) {
        return false;
    }

    /**
     * Removes the container property.
     *
     * @param propertyId the property id
     * @return true, if successful
     */
    @Override
    public boolean removeContainerProperty(Object propertyId) {
        return false;
    }

    /**
     * Removes the all items.
     *
     * @return true, if successful
     */
    @Override
    public boolean removeAllItems() {
        return false;
    }

    /**
     * Index of id.
     *
     * @param itemId the item id
     * @return the int
     */
    @Override
    public int indexOfId(Object itemId) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("indexOfId: " + itemId);
        }
        return this.policies.indexOf(itemId);
    }

    /**
     * Gets the id by index.
     *
     * @param index the index
     * @return the id by index
     */
    @Override
    public Object getIdByIndex(int index) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("getIdByIndex: " + index);
        }
        return this.policies.get(index);
    }

    /**
     * Adds the item at.
     *
     * @param index the index
     * @return the object
     */
    @Override
    public Object addItemAt(int index) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("addItemAt: " + index);
        }
        return null;
    }

    public class PdpPolicyItem {
        private final PDPPolicy policy;

        /**
         * Instantiates a new PDP policy item.
         *
         * @param itemId the item id
         */
        public PdpPolicyItem(PDPPolicy itemId) {
            this.policy = itemId;
        }

        /**
         * Gets the id.
         *
         * @return the id
         */
        public String getId() {
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("getId: " + this.policy);
            }
            return this.policy.getId();
        }

        /**
         * Gets the name.
         *
         * @return the name
         */
        public String getName() {
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("getName: " + this.policy);
            }
            return this.policy.getName();
        }

        /**
         * Gets the version.
         *
         * @return the version
         */
        public String getVersion() {
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("getVersion: " + this.policy);
            }
            return this.policy.getVersion();
        }

        /**
         * Gets the description.
         *
         * @return the description
         */
        public String getDescription() {
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("getDescription: " + this.policy);
            }
            return this.policy.getDescription();
        }

        /**
         * Gets the root.
         *
         * @return the root
         */
        public boolean getRoot() {
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("isRoot: " + this.policy);
            }
            return this.policy.isRoot();
        }

        /**
         * Sets the root.
         *
         * @param root the new root
         */
        public void setRoot(Boolean root) {
            ((StdPDPPolicy) this.policy).setRoot(root);
        }

    }
}