Sonar Fixes
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / model / PDPPolicyContainer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.xacml.rest.model;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Set;
28
29 import org.onap.policy.common.logging.flexlogger.FlexLogger;
30 import org.onap.policy.common.logging.flexlogger.Logger;
31 import org.onap.policy.pap.xacml.rest.util.PolicyContainer;
32 import org.onap.policy.pap.xacml.rest.util.PolicyItemSetChangeNotifier;
33 import org.onap.policy.xacml.api.XACMLErrorConstants;
34 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
35
36 import com.att.research.xacml.api.pap.PDP;
37 import com.att.research.xacml.api.pap.PDPGroup;
38 import com.att.research.xacml.api.pap.PDPPolicy;
39 import com.fasterxml.jackson.databind.DeserializationFeature;
40 import com.fasterxml.jackson.databind.ObjectMapper;
41
42 public class PDPPolicyContainer extends PolicyItemSetChangeNotifier implements PolicyContainer.Indexed {
43         private static final long serialVersionUID = 1L;
44         private static Logger logger    = FlexLogger.getLogger(PDPPolicyContainer.class);
45         
46          /**
47      * String identifier of a file's "Id" property.
48      */
49     public static final String PROPERTY_ID = "Id";
50
51    /**
52      * String identifier of a file's "name" property.
53      */
54     public static final String PROPERTY_NAME = "Name";
55
56     /**
57       * String identifier of a file's "name" property.
58       */
59      public static final String PROPERTY_VERSION = "Version";
60      
61     /**
62      * String identifier of a file's "Description" property.
63      */
64     public static final String PROPERTY_DESCRIPTION = "Description";
65     
66     /**
67      * String identifier of a file's "IsRoot" property.
68      */
69     public static final String PROPERTY_ISROOT = "Root";
70
71     /**
72      * List of the string identifiers for the available properties.
73      */
74     private static Collection<String> pdpPolicyProperties;
75  
76     private final transient Object data;
77     private transient List<PDPPolicy> policies;
78     
79         @SuppressWarnings("unchecked")
80         public PDPPolicyContainer(Object data) {
81                 super();
82                 this.data = data;
83                 if (this.data instanceof PDPGroup) {
84                         policies = new ArrayList<> (((PDPGroup) this.data).getPolicies());
85                 }
86                 if (this.data instanceof PDP) {
87                         policies = new ArrayList<> (((PDP) this.data).getPolicies());
88                 }
89                 if (this.data instanceof Set) {
90                         policies = new ArrayList<> ((Set<PDPPolicy>)data);
91                 }
92                 if (this.policies == null) {
93                         logger.info("NULL policies");
94                         throw new NullPointerException("PDPPolicyContainer created with unexpected Object type '" + data.getClass().getName() + "'");
95                 }
96                 this.setContainer(this);
97         }
98         
99         @Override
100         public Object nextItemId(Object itemId) {
101                 if (logger.isTraceEnabled()) {
102                         logger.trace("nextItemId: " + itemId);
103                 }
104                 int index = this.policies.indexOf(itemId);
105                 if (index == -1 || ((index + 1) >= this.policies.size())) {
106                         return null;
107                 }               
108                 return new PDPPolicyItem(this.policies.get(index + 1));
109         }
110
111         @Override
112         public Object prevItemId(Object itemId) {
113                 if (logger.isTraceEnabled()) {
114                         logger.trace("prevItemId: " + itemId);
115                 }
116                 int index = this.policies.indexOf(itemId);
117                 if (index <= 0) {
118                         return null;
119                 }
120                 return new PDPPolicyItem(this.policies.get(index - 1));
121         }
122
123         @Override
124         public Object firstItemId() {
125                 if (logger.isTraceEnabled()) {
126                         logger.trace("firstItemId: ");
127                 }
128                 if (this.policies.isEmpty()) {
129                         return null;
130                 }
131                 return new PDPPolicyItem(this.policies.get(0));
132         }
133
134         @Override
135         public Object lastItemId() {
136                 if (logger.isTraceEnabled()) {
137                         logger.trace("lastItemid: ");
138                 }
139                 if (this.policies.isEmpty()) {
140                         return null;
141                 }
142                 return new PDPPolicyItem(this.policies.get(this.policies.size() - 1));
143         }
144
145         @Override
146         public boolean isFirstId(Object itemId) {
147                 if (logger.isTraceEnabled()) {
148                         logger.trace("isFirstId: " + itemId);
149                 }
150                 if (this.policies.isEmpty()) {
151                         return false;
152                 }
153                 return (itemId.equals(this.policies.get(0)));
154         }
155
156         @Override
157         public boolean isLastId(Object itemId) {
158                 if (logger.isTraceEnabled()) {
159                         logger.trace("isLastId: " + itemId);
160                 }
161                 if (this.policies.isEmpty()) {
162                         return false;
163                 }
164                 return (itemId.equals(this.policies.get(this.policies.size() - 1)));
165         }
166
167         @Override
168         public Object addItemAfter(Object previousItemId){
169                 return null;
170         }
171
172         @Override
173         public Collection<?> getContainerPropertyIds() {
174                 return pdpPolicyProperties;
175         }
176
177         @Override
178         public Collection<?> getItemIds() {
179                 final Collection<Object> items = new ArrayList<>();
180                 items.addAll(this.policies);
181                 return Collections.unmodifiableCollection(items);
182         }
183         
184         
185         @Override
186         public Class<?> getType(Object propertyId) {
187         if (propertyId.equals(PROPERTY_ID)) {
188             return String.class;
189         }
190         if (propertyId.equals(PROPERTY_NAME)) {
191             return String.class;
192         }
193         if (propertyId.equals(PROPERTY_VERSION)) {
194             return String.class;
195         }
196         if (propertyId.equals(PROPERTY_DESCRIPTION)) {
197             return String.class;
198         }
199         if (propertyId.equals(PROPERTY_ISROOT)) {
200             return Boolean.class;
201         }
202                 return null;
203         }
204
205         @Override
206         public int size() {
207                 if (logger.isTraceEnabled()) {
208                         logger.trace("size: " + this.policies.size());
209                 }
210                 return this.policies.size();
211         }
212
213         @Override
214         public boolean containsId(Object itemId) {
215                 if (logger.isTraceEnabled()) {
216                         logger.trace("containsId: " + itemId);
217                 }
218                 return this.policies.contains(itemId);
219         }
220
221         @Override
222         public Object addItem(){
223                 throw new UnsupportedOperationException("Cannot add an empty policy.");
224         }
225
226         @Override
227         public boolean removeItem(Object itemId){
228                 if (logger.isTraceEnabled()) {
229                         logger.trace("removeItem: " + itemId);
230                 }
231                 ObjectMapper mapper = new ObjectMapper();
232                 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
233                 StdPDPPolicy pdpPolicy = null;
234                 try {
235                         pdpPolicy = mapper.readValue(itemId.toString() , StdPDPPolicy.class);
236                         for(int i = 0; i< policies.size(); i++){
237                                 if(policies.get(i).getId().equalsIgnoreCase(pdpPolicy.getId())){
238                                         return this.policies.remove(this.policies.get(i));
239                                 }
240                         }
241                 } catch (Exception e) {
242                         logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Exception Occured While Mapping the Removing Policy from PDP Group to Std Policy"+e);
243                 }       
244                 return this.policies.remove(itemId);
245         }
246
247         @Override
248         public boolean addContainerProperty(Object propertyId, Class<?> type,
249                         Object defaultValue){
250                 return false;
251         }
252
253         @Override
254         public boolean removeContainerProperty(Object propertyId){
255                 return false;
256         }
257
258         @Override
259         public boolean removeAllItems(){
260                 return false;
261         }
262
263         @Override
264         public int indexOfId(Object itemId) {
265                 if (logger.isTraceEnabled()) {
266                         logger.trace("indexOfId: " + itemId);
267                 }
268                 return this.policies.indexOf(itemId);
269         }
270
271         @Override
272         public Object getIdByIndex(int index) {
273                 if (logger.isTraceEnabled()) {
274                         logger.trace("getIdByIndex: " + index);
275                 }
276                 return this.policies.get(index);
277         }
278
279         @Override
280         public List<?> getItemIds(int startIndex, int numberOfItems) {
281                 if (logger.isTraceEnabled()) {
282                         logger.trace("getItemIds: " + startIndex + " " + numberOfItems);
283                 }
284                 if (numberOfItems < 0) {
285                         throw new IllegalArgumentException();
286                 }
287                 return this.policies.subList(startIndex, startIndex + numberOfItems);
288         }
289
290         @Override
291         public Object addItemAt(int index) {
292                 if (logger.isTraceEnabled()) {
293                         logger.trace("addItemAt: " + index);
294                 }
295                 return null;
296         }
297
298         public class PDPPolicyItem {
299                 private final PDPPolicy policy;
300                 
301                 public PDPPolicyItem(PDPPolicy itemId) {
302                         this.policy = itemId;
303                 }
304
305                 public String getId() {
306                         if (logger.isTraceEnabled()) {
307                                 logger.trace("getId: " + this.policy);
308                         }
309                         return this.policy.getId();
310                 }
311                 
312                 public String getName() {
313                         if (logger.isTraceEnabled()) {
314                                 logger.trace("getName: " + this.policy);
315                         }
316                         return this.policy.getName();
317                 }
318                 
319                 public String getVersion() {
320                         if (logger.isTraceEnabled()) {
321                                 logger.trace("getVersion: " + this.policy);
322                         }
323                         return this.policy.getVersion();
324                 }
325                 
326                 public String getDescription() {
327                         if (logger.isTraceEnabled()) {
328                                 logger.trace("getDescription: " + this.policy);
329                         }
330                         return this.policy.getDescription();
331                 }
332                 
333                 public boolean getRoot() {
334                         if (logger.isTraceEnabled()) {
335                                 logger.trace("isRoot: " + this.policy);
336                         }
337                         return this.policy.isRoot();
338                 }
339                 
340                 public void setRoot(Boolean root) {
341                         ((StdPDPPolicy)this.policy).setRoot(root);
342                 }
343         
344         }
345 }