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