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