Unit/SONAR/Checkstyle in ONAP-REST
[policy/engine.git] / ONAP-REST / src / main / java / org / onap / policy / rest / util / PdpPolicyContainer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nordix Foundation.
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 package org.onap.policy.rest.util;
23
24 import com.att.research.xacml.api.pap.PDP;
25 import com.att.research.xacml.api.pap.PDPGroup;
26 import com.att.research.xacml.api.pap.PDPPolicy;
27 import com.fasterxml.jackson.databind.DeserializationFeature;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.Set;
35
36 import org.onap.policy.common.logging.flexlogger.FlexLogger;
37 import org.onap.policy.common.logging.flexlogger.Logger;
38 import org.onap.policy.xacml.api.XACMLErrorConstants;
39 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
40
41 public class PdpPolicyContainer extends PolicyItemSetChangeNotifier implements PolicyContainer.Indexed {
42     private static final long serialVersionUID = 1L;
43     private static final Logger LOGGER = FlexLogger.getLogger(PdpPolicyContainer.class);
44
45     private static final String PROPERTY_ID = "Id";
46     private static final String PROPERTY_NAME = "Name";
47     private static final String PROPERTY_VERSION = "Version";
48     private static final String PROPERTY_DESCRIPTION = "Description";
49     private static final String PROPERTY_ISROOT = "Root";
50
51     /**
52      * List of the string identifiers for the available properties.
53      */
54     private static Collection<String> pDPPolicyProperties;
55
56     private final transient Object data;
57     private transient List<PDPPolicy> policies;
58
59     /**
60      * Instantiates a new pdp policy container.
61      *
62      * @param data the data
63      */
64     @SuppressWarnings("unchecked")
65     public PdpPolicyContainer(Object data) {
66         super();
67         this.data = data;
68         if (this.data instanceof PDPGroup) {
69             policies = new ArrayList<>(((PDPGroup) this.data).getPolicies());
70         }
71         if (this.data instanceof PDP) {
72             policies = new ArrayList<>(((PDP) this.data).getPolicies());
73         }
74         if (this.data instanceof Set) {
75             policies = new ArrayList<>((Set<PDPPolicy>) data);
76         }
77         if (this.policies == null) {
78             LOGGER.info("NULL policies");
79             throw new NullPointerException("PDPPolicyContainer created with unexpected Object type '"
80                             + data.getClass().getName() + "'");
81         }
82         this.setContainer(this);
83     }
84
85     /**
86      * Next item id.
87      *
88      * @param itemId the item id
89      * @return the object
90      */
91     @Override
92     public Object nextItemId(Object itemId) {
93         if (LOGGER.isTraceEnabled()) {
94             LOGGER.trace("nextItemId: " + itemId);
95         }
96         int index = this.policies.indexOf(itemId);
97         if (index == -1 || ((index + 1) >= this.policies.size())) {
98             return null;
99         }
100         return new PdpPolicyItem(this.policies.get(index + 1));
101     }
102
103     /**
104      * Prev item id.
105      *
106      * @param itemId the item id
107      * @return the object
108      */
109     @Override
110     public Object prevItemId(Object itemId) {
111         if (LOGGER.isTraceEnabled()) {
112             LOGGER.trace("prevItemId: " + itemId);
113         }
114         int index = this.policies.indexOf(itemId);
115         if (index <= 0) {
116             return null;
117         }
118         return new PdpPolicyItem(this.policies.get(index - 1));
119     }
120
121     /**
122      * First item id.
123      *
124      * @return the object
125      */
126     @Override
127     public Object firstItemId() {
128         if (LOGGER.isTraceEnabled()) {
129             LOGGER.trace("firstItemId: ");
130         }
131         if (this.policies.isEmpty()) {
132             return null;
133         }
134         return new PdpPolicyItem(this.policies.get(0));
135     }
136
137     /**
138      * Last item id.
139      *
140      * @return the object
141      */
142     @Override
143     public Object lastItemId() {
144         if (LOGGER.isTraceEnabled()) {
145             LOGGER.trace("lastItemid: ");
146         }
147         if (this.policies.isEmpty()) {
148             return null;
149         }
150         return new PdpPolicyItem(this.policies.get(this.policies.size() - 1));
151     }
152
153     /**
154      * Checks if is first id.
155      *
156      * @param itemId the item id
157      * @return true, if is first id
158      */
159     @Override
160     public boolean isFirstId(Object itemId) {
161         if (LOGGER.isTraceEnabled()) {
162             LOGGER.trace("isFirstId: " + itemId);
163         }
164         if (this.policies.isEmpty()) {
165             return false;
166         }
167         return itemId.equals(this.policies.get(0));
168     }
169
170     /**
171      * Checks if is last id.
172      *
173      * @param itemId the item id
174      * @return true, if is last id
175      */
176     @Override
177     public boolean isLastId(Object itemId) {
178         if (LOGGER.isTraceEnabled()) {
179             LOGGER.trace("isLastId: " + itemId);
180         }
181         if (this.policies.isEmpty()) {
182             return false;
183         }
184         return itemId.equals(this.policies.get(this.policies.size() - 1));
185     }
186
187     /**
188      * Adds the item after.
189      *
190      * @param previousItemId the previous item id
191      * @return the object
192      */
193     @Override
194     public Object addItemAfter(Object previousItemId) {
195         return null;
196     }
197
198     /**
199      * Gets the container property ids.
200      *
201      * @return the container property ids
202      */
203     @Override
204     public Collection<?> getContainerPropertyIds() {
205         return pDPPolicyProperties;
206     }
207
208     /**
209      * Gets the item ids.
210      *
211      * @return the item ids
212      */
213     @Override
214     public Collection<?> getItemIds() {
215         final Collection<Object> items = new ArrayList<>();
216         items.addAll(this.policies);
217         return Collections.unmodifiableCollection(items);
218     }
219
220     /**
221      * Gets the item ids.
222      *
223      * @param startIndex the start index
224      * @param numberOfItems the number of items
225      * @return the item ids
226      */
227     @Override
228     public List<?> getItemIds(int startIndex, int numberOfItems) {
229         if (LOGGER.isTraceEnabled()) {
230             LOGGER.trace("getItemIds: " + startIndex + " " + numberOfItems);
231         }
232         if (numberOfItems < 0) {
233             throw new IllegalArgumentException();
234         }
235         return this.policies.subList(startIndex, startIndex + numberOfItems);
236     }
237
238     /**
239      * Gets the type.
240      *
241      * @param propertyId the property id
242      * @return the type
243      */
244     @Override
245     public Class<?> getType(Object propertyId) {
246         if (propertyId.equals(PROPERTY_ID)) {
247             return String.class;
248         }
249         if (propertyId.equals(PROPERTY_NAME)) {
250             return String.class;
251         }
252         if (propertyId.equals(PROPERTY_VERSION)) {
253             return String.class;
254         }
255         if (propertyId.equals(PROPERTY_DESCRIPTION)) {
256             return String.class;
257         }
258         if (propertyId.equals(PROPERTY_ISROOT)) {
259             return Boolean.class;
260         }
261         return null;
262     }
263
264     /**
265      * Size.
266      *
267      * @return the int
268      */
269     @Override
270     public int size() {
271         if (LOGGER.isTraceEnabled()) {
272             LOGGER.trace("size: " + this.policies.size());
273         }
274         return this.policies.size();
275     }
276
277     /**
278      * Contains id.
279      *
280      * @param itemId the item id
281      * @return true, if successful
282      */
283     @Override
284     public boolean containsId(Object itemId) {
285         if (LOGGER.isTraceEnabled()) {
286             LOGGER.trace("containsId: " + itemId);
287         }
288         return this.policies.contains(itemId);
289     }
290
291     /**
292      * Adds the item.
293      *
294      * @return the object
295      */
296     @Override
297     public Object addItem() {
298         throw new UnsupportedOperationException("Cannot add an empty policy.");
299     }
300
301     /**
302      * Removes the item.
303      *
304      * @param itemId the item id
305      * @return true, if successful
306      */
307     @Override
308     public boolean removeItem(Object itemId) {
309         if (LOGGER.isTraceEnabled()) {
310             LOGGER.trace("removeItem: " + itemId);
311         }
312         ObjectMapper mapper = new ObjectMapper();
313         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
314         StdPDPPolicy pdpPolicy = null;
315         try {
316             pdpPolicy = mapper.readValue(itemId.toString(), StdPDPPolicy.class);
317             for (int i = 0; i < policies.size(); i++) {
318                 if (policies.get(i).getId().equalsIgnoreCase(pdpPolicy.getId())) {
319                     return this.policies.remove(this.policies.get(i));
320                 }
321             }
322         } catch (Exception e) {
323             LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE
324                             + "Exception Occured While Mapping the Removing Policy from PDP Group to Std Policy" + e);
325         }
326         return this.policies.remove(itemId);
327     }
328
329     /**
330      * Adds the container property.
331      *
332      * @param propertyId the property id
333      * @param type the type
334      * @param defaultValue the default value
335      * @return true, if successful
336      */
337     @Override
338     public boolean addContainerProperty(Object propertyId, Class<?> type, Object defaultValue) {
339         return false;
340     }
341
342     /**
343      * Removes the container property.
344      *
345      * @param propertyId the property id
346      * @return true, if successful
347      */
348     @Override
349     public boolean removeContainerProperty(Object propertyId) {
350         return false;
351     }
352
353     /**
354      * Removes the all items.
355      *
356      * @return true, if successful
357      */
358     @Override
359     public boolean removeAllItems() {
360         return false;
361     }
362
363     /**
364      * Index of id.
365      *
366      * @param itemId the item id
367      * @return the int
368      */
369     @Override
370     public int indexOfId(Object itemId) {
371         if (LOGGER.isTraceEnabled()) {
372             LOGGER.trace("indexOfId: " + itemId);
373         }
374         return this.policies.indexOf(itemId);
375     }
376
377     /**
378      * Gets the id by index.
379      *
380      * @param index the index
381      * @return the id by index
382      */
383     @Override
384     public Object getIdByIndex(int index) {
385         if (LOGGER.isTraceEnabled()) {
386             LOGGER.trace("getIdByIndex: " + index);
387         }
388         return this.policies.get(index);
389     }
390
391     /**
392      * Adds the item at.
393      *
394      * @param index the index
395      * @return the object
396      */
397     @Override
398     public Object addItemAt(int index) {
399         if (LOGGER.isTraceEnabled()) {
400             LOGGER.trace("addItemAt: " + index);
401         }
402         return null;
403     }
404
405     public class PdpPolicyItem {
406         private final PDPPolicy policy;
407
408         /**
409          * Instantiates a new PDP policy item.
410          *
411          * @param itemId the item id
412          */
413         public PdpPolicyItem(PDPPolicy itemId) {
414             this.policy = itemId;
415         }
416
417         /**
418          * Gets the id.
419          *
420          * @return the id
421          */
422         public String getId() {
423             if (LOGGER.isTraceEnabled()) {
424                 LOGGER.trace("getId: " + this.policy);
425             }
426             return this.policy.getId();
427         }
428
429         /**
430          * Gets the name.
431          *
432          * @return the name
433          */
434         public String getName() {
435             if (LOGGER.isTraceEnabled()) {
436                 LOGGER.trace("getName: " + this.policy);
437             }
438             return this.policy.getName();
439         }
440
441         /**
442          * Gets the version.
443          *
444          * @return the version
445          */
446         public String getVersion() {
447             if (LOGGER.isTraceEnabled()) {
448                 LOGGER.trace("getVersion: " + this.policy);
449             }
450             return this.policy.getVersion();
451         }
452
453         /**
454          * Gets the description.
455          *
456          * @return the description
457          */
458         public String getDescription() {
459             if (LOGGER.isTraceEnabled()) {
460                 LOGGER.trace("getDescription: " + this.policy);
461             }
462             return this.policy.getDescription();
463         }
464
465         /**
466          * Gets the root.
467          *
468          * @return the root
469          */
470         public boolean getRoot() {
471             if (LOGGER.isTraceEnabled()) {
472                 LOGGER.trace("isRoot: " + this.policy);
473             }
474             return this.policy.isRoot();
475         }
476
477         /**
478          * Sets the root.
479          *
480          * @param root the new root
481          */
482         public void setRoot(Boolean root) {
483             ((StdPDPPolicy) this.policy).setRoot(root);
484         }
485
486     }
487 }