2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.pap.xacml.rest.model;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.List;
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;
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;
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);
47 * String identifier of a file's "Id" property.
49 public static final String PROPERTY_ID = "Id";
52 * String identifier of a file's "name" property.
54 public static final String PROPERTY_NAME = "Name";
57 * String identifier of a file's "name" property.
59 public static final String PROPERTY_VERSION = "Version";
62 * String identifier of a file's "Description" property.
64 public static final String PROPERTY_DESCRIPTION = "Description";
67 * String identifier of a file's "IsRoot" property.
69 public static final String PROPERTY_ISROOT = "Root";
72 * List of the string identifiers for the available properties.
74 private static Collection<String> pdpPolicyProperties;
76 private final transient Object data;
77 private transient List<PDPPolicy> policies;
79 @SuppressWarnings("unchecked")
80 public PDPPolicyContainer(Object data) {
83 if (this.data instanceof PDPGroup) {
84 policies = new ArrayList<> (((PDPGroup) this.data).getPolicies());
86 if (this.data instanceof PDP) {
87 policies = new ArrayList<> (((PDP) this.data).getPolicies());
89 if (this.data instanceof Set) {
90 policies = new ArrayList<> ((Set<PDPPolicy>)data);
92 if (this.policies == null) {
93 logger.info("NULL policies");
94 throw new NullPointerException("PDPPolicyContainer created with unexpected Object type '" + data.getClass().getName() + "'");
96 this.setContainer(this);
100 public Object nextItemId(Object itemId) {
101 if (logger.isTraceEnabled()) {
102 logger.trace("nextItemId: " + itemId);
104 int index = this.policies.indexOf(itemId);
105 if (index == -1 || ((index + 1) >= this.policies.size())) {
108 return new PDPPolicyItem(this.policies.get(index + 1));
112 public Object prevItemId(Object itemId) {
113 if (logger.isTraceEnabled()) {
114 logger.trace("prevItemId: " + itemId);
116 int index = this.policies.indexOf(itemId);
120 return new PDPPolicyItem(this.policies.get(index - 1));
124 public Object firstItemId() {
125 if (logger.isTraceEnabled()) {
126 logger.trace("firstItemId: ");
128 if (this.policies.isEmpty()) {
131 return new PDPPolicyItem(this.policies.get(0));
135 public Object lastItemId() {
136 if (logger.isTraceEnabled()) {
137 logger.trace("lastItemid: ");
139 if (this.policies.isEmpty()) {
142 return new PDPPolicyItem(this.policies.get(this.policies.size() - 1));
146 public boolean isFirstId(Object itemId) {
147 if (logger.isTraceEnabled()) {
148 logger.trace("isFirstId: " + itemId);
150 if (this.policies.isEmpty()) {
153 return (itemId.equals(this.policies.get(0)));
157 public boolean isLastId(Object itemId) {
158 if (logger.isTraceEnabled()) {
159 logger.trace("isLastId: " + itemId);
161 if (this.policies.isEmpty()) {
164 return (itemId.equals(this.policies.get(this.policies.size() - 1)));
168 public Object addItemAfter(Object previousItemId){
173 public Collection<?> getContainerPropertyIds() {
174 return pdpPolicyProperties;
178 public Collection<?> getItemIds() {
179 final Collection<Object> items = new ArrayList<>();
180 items.addAll(this.policies);
181 return Collections.unmodifiableCollection(items);
186 public Class<?> getType(Object propertyId) {
187 if (propertyId.equals(PROPERTY_ID)) {
190 if (propertyId.equals(PROPERTY_NAME)) {
193 if (propertyId.equals(PROPERTY_VERSION)) {
196 if (propertyId.equals(PROPERTY_DESCRIPTION)) {
199 if (propertyId.equals(PROPERTY_ISROOT)) {
200 return Boolean.class;
207 if (logger.isTraceEnabled()) {
208 logger.trace("size: " + this.policies.size());
210 return this.policies.size();
214 public boolean containsId(Object itemId) {
215 if (logger.isTraceEnabled()) {
216 logger.trace("containsId: " + itemId);
218 return this.policies.contains(itemId);
222 public Object addItem(){
223 throw new UnsupportedOperationException("Cannot add an empty policy.");
227 public boolean removeItem(Object itemId){
228 if (logger.isTraceEnabled()) {
229 logger.trace("removeItem: " + itemId);
231 ObjectMapper mapper = new ObjectMapper();
232 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
233 StdPDPPolicy pdpPolicy = null;
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));
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);
244 return this.policies.remove(itemId);
248 public boolean addContainerProperty(Object propertyId, Class<?> type,
249 Object defaultValue){
254 public boolean removeContainerProperty(Object propertyId){
259 public boolean removeAllItems(){
264 public int indexOfId(Object itemId) {
265 if (logger.isTraceEnabled()) {
266 logger.trace("indexOfId: " + itemId);
268 return this.policies.indexOf(itemId);
272 public Object getIdByIndex(int index) {
273 if (logger.isTraceEnabled()) {
274 logger.trace("getIdByIndex: " + index);
276 return this.policies.get(index);
280 public List<?> getItemIds(int startIndex, int numberOfItems) {
281 if (logger.isTraceEnabled()) {
282 logger.trace("getItemIds: " + startIndex + " " + numberOfItems);
284 if (numberOfItems < 0) {
285 throw new IllegalArgumentException();
287 return this.policies.subList(startIndex, startIndex + numberOfItems);
291 public Object addItemAt(int index) {
292 if (logger.isTraceEnabled()) {
293 logger.trace("addItemAt: " + index);
298 public class PDPPolicyItem {
299 private final PDPPolicy policy;
301 public PDPPolicyItem(PDPPolicy itemId) {
302 this.policy = itemId;
305 public String getId() {
306 if (logger.isTraceEnabled()) {
307 logger.trace("getId: " + this.policy);
309 return this.policy.getId();
312 public String getName() {
313 if (logger.isTraceEnabled()) {
314 logger.trace("getName: " + this.policy);
316 return this.policy.getName();
319 public String getVersion() {
320 if (logger.isTraceEnabled()) {
321 logger.trace("getVersion: " + this.policy);
323 return this.policy.getVersion();
326 public String getDescription() {
327 if (logger.isTraceEnabled()) {
328 logger.trace("getDescription: " + this.policy);
330 return this.policy.getDescription();
333 public boolean getRoot() {
334 if (logger.isTraceEnabled()) {
335 logger.trace("isRoot: " + this.policy);
337 return this.policy.isRoot();
340 public void setRoot(Boolean root) {
341 ((StdPDPPolicy)this.policy).setRoot(root);