353a903be6f58b606751372290917ba53ed6ec7a
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / openecomp / policy / model / PDPPolicyContainer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP 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.openecomp.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.openecomp.policy.utils.PolicyContainer;
31 import org.openecomp.policy.utils.PolicyItemSetChangeNotifier;
32 import org.openecomp.policy.common.logging.flexlogger.*;
33
34 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
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 org.openecomp.policy.xacml.std.pap.StdPDPPolicy;
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 String PROPERTY_ID = "Id";
50
51    /**
52      * String identifier of a file's "name" property.
53      */
54     public static String PROPERTY_NAME = "Name";
55
56     /**
57       * String identifier of a file's "name" property.
58       */
59      public static String PROPERTY_VERSION = "Version";
60      
61     /**
62      * String identifier of a file's "Description" property.
63      */
64     public static String PROPERTY_DESCRIPTION = "Description";
65     
66     /**
67      * String identifier of a file's "IsRoot" property.
68      */
69     public static String PROPERTY_ISROOT = "Root";
70
71     /**
72      * List of the string identifiers for the available properties.
73      */
74     public static Collection<String> PDPPOLICY_PROPERTIES;
75  
76     private final Object data;
77     private 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<PDPPolicy> (((PDPGroup) this.data).getPolicies());
85                 }
86                 if (this.data instanceof PDP) {
87                         policies = new ArrayList<PDPPolicy> (((PDP) this.data).getPolicies());
88                 }
89                 if (this.data instanceof Set) {
90                         policies = new ArrayList<PDPPolicy> ((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                         throws UnsupportedOperationException {
170                 return null;
171         }
172
173         @Override
174         public Collection<?> getContainerPropertyIds() {
175                 return PDPPOLICY_PROPERTIES;
176         }
177
178         @Override
179         public Collection<?> getItemIds() {
180                 final Collection<Object> items = new ArrayList<Object>();
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() throws UnsupportedOperationException {
224                 throw new UnsupportedOperationException("Cannot add an empty policy.");
225         }
226
227         @Override
228         public boolean removeItem(Object itemId)
229                         throws UnsupportedOperationException {
230                 if (LOGGER.isTraceEnabled()) {
231                         LOGGER.trace("removeItem: " + itemId);
232                 }
233                 ObjectMapper mapper = new ObjectMapper();
234                 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
235                 StdPDPPolicy pdpPolicy = null;
236                 try {
237                         pdpPolicy = mapper.readValue(itemId.toString() , StdPDPPolicy.class);
238                         for(int i = 0; i< policies.size(); i++){
239                                 if(policies.get(i).getId().equalsIgnoreCase(pdpPolicy.getId())){
240                                         return this.policies.remove(this.policies.get(i));
241                                 }
242                         }
243                 } catch (Exception e) {
244                         LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Exception Occured While Mapping the Removing Policy from PDP Group to Std Policy"+e);
245                 }       
246                 return this.policies.remove(itemId);
247         }
248
249         @Override
250         public boolean addContainerProperty(Object propertyId, Class<?> type,
251                         Object defaultValue) throws UnsupportedOperationException {
252                 return false;
253         }
254
255         @Override
256         public boolean removeContainerProperty(Object propertyId)
257                         throws UnsupportedOperationException {
258                 return false;
259         }
260
261         @Override
262         public boolean removeAllItems() throws UnsupportedOperationException {
263                 return false;
264         }
265
266         @Override
267         public int indexOfId(Object itemId) {
268                 if (LOGGER.isTraceEnabled()) {
269                         LOGGER.trace("indexOfId: " + itemId);
270                 }
271                 return this.policies.indexOf(itemId);
272         }
273
274         @Override
275         public Object getIdByIndex(int index) {
276                 if (LOGGER.isTraceEnabled()) {
277                         LOGGER.trace("getIdByIndex: " + index);
278                 }
279                 return this.policies.get(index);
280         }
281
282         @Override
283         public List<?> getItemIds(int startIndex, int numberOfItems) {
284                 if (LOGGER.isTraceEnabled()) {
285                         LOGGER.trace("getItemIds: " + startIndex + " " + numberOfItems);
286                 }
287                 if (numberOfItems < 0) {
288                         throw new IllegalArgumentException();
289                 }
290                 return this.policies.subList(startIndex, startIndex + numberOfItems);
291         }
292
293         @Override
294         public Object addItemAt(int index) throws UnsupportedOperationException {
295                 if (LOGGER.isTraceEnabled()) {
296                         LOGGER.trace("addItemAt: " + index);
297                 }
298                 return null;
299         }
300
301         public class PDPPolicyItem {
302                 private final PDPPolicy policy;
303                 
304                 public PDPPolicyItem(PDPPolicy itemId) {
305                         this.policy = itemId;
306                 }
307
308                 public String getId() {
309                         if (LOGGER.isTraceEnabled()) {
310                                 LOGGER.trace("getId: " + this.policy);
311                         }
312                         return this.policy.getId();
313                 }
314                 
315                 public String getName() {
316                         if (LOGGER.isTraceEnabled()) {
317                                 LOGGER.trace("getName: " + this.policy);
318                         }
319                         return this.policy.getName();
320                 }
321                 
322                 public String getVersion() {
323                         if (LOGGER.isTraceEnabled()) {
324                                 LOGGER.trace("getVersion: " + this.policy);
325                         }
326                         return this.policy.getVersion();
327                 }
328                 
329                 public String getDescription() {
330                         if (LOGGER.isTraceEnabled()) {
331                                 LOGGER.trace("getDescription: " + this.policy);
332                         }
333                         return this.policy.getDescription();
334                 }
335                 
336                 public boolean getRoot() {
337                         if (LOGGER.isTraceEnabled()) {
338                                 LOGGER.trace("isRoot: " + this.policy);
339                         }
340                         return this.policy.isRoot();
341                 }
342                 
343                 public void setRoot(Boolean root) {
344                         ((StdPDPPolicy)this.policy).setRoot(root);
345                 }
346         
347         }
348 }