Reduce technical debt
[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                 return null;
171         }
172
173         @Override
174         public Collection<?> getContainerPropertyIds() {
175                 return pDPPolicyProperties;
176         }
177
178         @Override
179         public Collection<?> getItemIds() {
180                 final Collection<Object> items = new ArrayList<>();
181                 items.addAll(this.policies);
182                 return Collections.unmodifiableCollection(items);
183         }
184         
185         
186         @Override
187         public Class<?> getType(Object propertyId) {
188         if (propertyId.equals(PROPERTY_ID)) {
189             return String.class;
190         }
191         if (propertyId.equals(PROPERTY_NAME)) {
192             return String.class;
193         }
194         if (propertyId.equals(PROPERTY_VERSION)) {
195             return String.class;
196         }
197         if (propertyId.equals(PROPERTY_DESCRIPTION)) {
198             return String.class;
199         }
200         if (propertyId.equals(PROPERTY_ISROOT)) {
201             return Boolean.class;
202         }
203                 return null;
204         }
205
206         @Override
207         public int size() {
208                 if (LOGGER.isTraceEnabled()) {
209                         LOGGER.trace("size: " + this.policies.size());
210                 }
211                 return this.policies.size();
212         }
213
214         @Override
215         public boolean containsId(Object itemId) {
216                 if (LOGGER.isTraceEnabled()) {
217                         LOGGER.trace("containsId: " + itemId);
218                 }
219                 return this.policies.contains(itemId);
220         }
221
222         @Override
223         public Object addItem() {
224                 throw new UnsupportedOperationException("Cannot add an empty policy.");
225         }
226
227         @Override
228         public boolean removeItem(Object itemId) {
229                 if (LOGGER.isTraceEnabled()) {
230                         LOGGER.trace("removeItem: " + itemId);
231                 }
232                 ObjectMapper mapper = new ObjectMapper();
233                 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
234                 StdPDPPolicy pdpPolicy = null;
235                 try {
236                         pdpPolicy = mapper.readValue(itemId.toString() , StdPDPPolicy.class);
237                         for(int i = 0; i< policies.size(); i++){
238                                 if(policies.get(i).getId().equalsIgnoreCase(pdpPolicy.getId())){
239                                         return this.policies.remove(this.policies.get(i));
240                                 }
241                         }
242                 } catch (Exception e) {
243                         LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Exception Occured While Mapping the Removing Policy from PDP Group to Std Policy"+e);
244                 }       
245                 return this.policies.remove(itemId);
246         }
247
248         @Override
249         public boolean addContainerProperty(Object propertyId, Class<?> type,
250                         Object defaultValue) {
251                 return false;
252         }
253
254         @Override
255         public boolean removeContainerProperty(Object propertyId) {
256                 return false;
257         }
258
259         @Override
260         public boolean removeAllItems() {
261                 return false;
262         }
263
264         @Override
265         public int indexOfId(Object itemId) {
266                 if (LOGGER.isTraceEnabled()) {
267                         LOGGER.trace("indexOfId: " + itemId);
268                 }
269                 return this.policies.indexOf(itemId);
270         }
271
272         @Override
273         public Object getIdByIndex(int index) {
274                 if (LOGGER.isTraceEnabled()) {
275                         LOGGER.trace("getIdByIndex: " + index);
276                 }
277                 return this.policies.get(index);
278         }
279
280         @Override
281         public List<?> getItemIds(int startIndex, int numberOfItems) {
282                 if (LOGGER.isTraceEnabled()) {
283                         LOGGER.trace("getItemIds: " + startIndex + " " + numberOfItems);
284                 }
285                 if (numberOfItems < 0) {
286                         throw new IllegalArgumentException();
287                 }
288                 return this.policies.subList(startIndex, startIndex + numberOfItems);
289         }
290
291         @Override
292         public Object addItemAt(int index) {
293                 if (LOGGER.isTraceEnabled()) {
294                         LOGGER.trace("addItemAt: " + index);
295                 }
296                 return null;
297         }
298
299         public class PDPPolicyItem {
300                 private final PDPPolicy policy;
301                 
302                 public PDPPolicyItem(PDPPolicy itemId) {
303                         this.policy = itemId;
304                 }
305
306                 public String getId() {
307                         if (LOGGER.isTraceEnabled()) {
308                                 LOGGER.trace("getId: " + this.policy);
309                         }
310                         return this.policy.getId();
311                 }
312                 
313                 public String getName() {
314                         if (LOGGER.isTraceEnabled()) {
315                                 LOGGER.trace("getName: " + this.policy);
316                         }
317                         return this.policy.getName();
318                 }
319                 
320                 public String getVersion() {
321                         if (LOGGER.isTraceEnabled()) {
322                                 LOGGER.trace("getVersion: " + this.policy);
323                         }
324                         return this.policy.getVersion();
325                 }
326                 
327                 public String getDescription() {
328                         if (LOGGER.isTraceEnabled()) {
329                                 LOGGER.trace("getDescription: " + this.policy);
330                         }
331                         return this.policy.getDescription();
332                 }
333                 
334                 public boolean getRoot() {
335                         if (LOGGER.isTraceEnabled()) {
336                                 LOGGER.trace("isRoot: " + this.policy);
337                         }
338                         return this.policy.isRoot();
339                 }
340                 
341                 public void setRoot(Boolean root) {
342                         ((StdPDPPolicy)this.policy).setRoot(root);
343                 }
344         
345         }
346 }