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.model;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.List;
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;
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;
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);
48 * String identifier of a file's "Id" property.
50 private static final String PROPERTY_ID = "Id";
53 * String identifier of a file's "name" property.
55 private static final String PROPERTY_NAME = "Name";
58 * String identifier of a file's "name" property.
60 private static final String PROPERTY_VERSION = "Version";
63 * String identifier of a file's "Description" property.
65 private static final String PROPERTY_DESCRIPTION = "Description";
68 * String identifier of a file's "IsRoot" property.
70 private static final String PROPERTY_ISROOT = "Root";
73 * List of the string identifiers for the available properties.
75 private static Collection<String> pDPPolicyProperties;
77 private final transient Object data;
78 private transient List<PDPPolicy> policies;
80 @SuppressWarnings("unchecked")
81 public PDPPolicyContainer(Object data) {
84 if (this.data instanceof PDPGroup) {
85 policies = new ArrayList<> (((PDPGroup) this.data).getPolicies());
87 if (this.data instanceof PDP) {
88 policies = new ArrayList<> (((PDP) this.data).getPolicies());
90 if (this.data instanceof Set) {
91 policies = new ArrayList<> ((Set<PDPPolicy>)data);
93 if (this.policies == null) {
94 LOGGER.info("NULL policies");
95 throw new NullPointerException("PDPPolicyContainer created with unexpected Object type '" + data.getClass().getName() + "'");
97 this.setContainer(this);
101 public Object nextItemId(Object itemId) {
102 if (LOGGER.isTraceEnabled()) {
103 LOGGER.trace("nextItemId: " + itemId);
105 int index = this.policies.indexOf(itemId);
106 if (index == -1 || ((index + 1) >= this.policies.size())) {
109 return new PDPPolicyItem(this.policies.get(index + 1));
113 public Object prevItemId(Object itemId) {
114 if (LOGGER.isTraceEnabled()) {
115 LOGGER.trace("prevItemId: " + itemId);
117 int index = this.policies.indexOf(itemId);
121 return new PDPPolicyItem(this.policies.get(index - 1));
125 public Object firstItemId() {
126 if (LOGGER.isTraceEnabled()) {
127 LOGGER.trace("firstItemId: ");
129 if (this.policies.isEmpty()) {
132 return new PDPPolicyItem(this.policies.get(0));
136 public Object lastItemId() {
137 if (LOGGER.isTraceEnabled()) {
138 LOGGER.trace("lastItemid: ");
140 if (this.policies.isEmpty()) {
143 return new PDPPolicyItem(this.policies.get(this.policies.size() - 1));
147 public boolean isFirstId(Object itemId) {
148 if (LOGGER.isTraceEnabled()) {
149 LOGGER.trace("isFirstId: " + itemId);
151 if (this.policies.isEmpty()) {
154 return itemId.equals(this.policies.get(0));
158 public boolean isLastId(Object itemId) {
159 if (LOGGER.isTraceEnabled()) {
160 LOGGER.trace("isLastId: " + itemId);
162 if (this.policies.isEmpty()) {
165 return itemId.equals(this.policies.get(this.policies.size() - 1));
169 public Object addItemAfter(Object previousItemId) {
174 public Collection<?> getContainerPropertyIds() {
175 return pDPPolicyProperties;
179 public Collection<?> getItemIds() {
180 final Collection<Object> items = new ArrayList<>();
181 items.addAll(this.policies);
182 return Collections.unmodifiableCollection(items);
187 public Class<?> getType(Object propertyId) {
188 if (propertyId.equals(PROPERTY_ID)) {
191 if (propertyId.equals(PROPERTY_NAME)) {
194 if (propertyId.equals(PROPERTY_VERSION)) {
197 if (propertyId.equals(PROPERTY_DESCRIPTION)) {
200 if (propertyId.equals(PROPERTY_ISROOT)) {
201 return Boolean.class;
208 if (LOGGER.isTraceEnabled()) {
209 LOGGER.trace("size: " + this.policies.size());
211 return this.policies.size();
215 public boolean containsId(Object itemId) {
216 if (LOGGER.isTraceEnabled()) {
217 LOGGER.trace("containsId: " + itemId);
219 return this.policies.contains(itemId);
223 public Object addItem() {
224 throw new UnsupportedOperationException("Cannot add an empty policy.");
228 public boolean removeItem(Object itemId) {
229 if (LOGGER.isTraceEnabled()) {
230 LOGGER.trace("removeItem: " + itemId);
232 ObjectMapper mapper = new ObjectMapper();
233 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
234 StdPDPPolicy pdpPolicy = null;
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));
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);
245 return this.policies.remove(itemId);
249 public boolean addContainerProperty(Object propertyId, Class<?> type,
250 Object defaultValue) {
255 public boolean removeContainerProperty(Object propertyId) {
260 public boolean removeAllItems() {
265 public int indexOfId(Object itemId) {
266 if (LOGGER.isTraceEnabled()) {
267 LOGGER.trace("indexOfId: " + itemId);
269 return this.policies.indexOf(itemId);
273 public Object getIdByIndex(int index) {
274 if (LOGGER.isTraceEnabled()) {
275 LOGGER.trace("getIdByIndex: " + index);
277 return this.policies.get(index);
281 public List<?> getItemIds(int startIndex, int numberOfItems) {
282 if (LOGGER.isTraceEnabled()) {
283 LOGGER.trace("getItemIds: " + startIndex + " " + numberOfItems);
285 if (numberOfItems < 0) {
286 throw new IllegalArgumentException();
288 return this.policies.subList(startIndex, startIndex + numberOfItems);
292 public Object addItemAt(int index) {
293 if (LOGGER.isTraceEnabled()) {
294 LOGGER.trace("addItemAt: " + index);
299 public class PDPPolicyItem {
300 private final PDPPolicy policy;
302 public PDPPolicyItem(PDPPolicy itemId) {
303 this.policy = itemId;
306 public String getId() {
307 if (LOGGER.isTraceEnabled()) {
308 LOGGER.trace("getId: " + this.policy);
310 return this.policy.getId();
313 public String getName() {
314 if (LOGGER.isTraceEnabled()) {
315 LOGGER.trace("getName: " + this.policy);
317 return this.policy.getName();
320 public String getVersion() {
321 if (LOGGER.isTraceEnabled()) {
322 LOGGER.trace("getVersion: " + this.policy);
324 return this.policy.getVersion();
327 public String getDescription() {
328 if (LOGGER.isTraceEnabled()) {
329 LOGGER.trace("getDescription: " + this.policy);
331 return this.policy.getDescription();
334 public boolean getRoot() {
335 if (LOGGER.isTraceEnabled()) {
336 LOGGER.trace("isRoot: " + this.policy);
338 return this.policy.isRoot();
341 public void setRoot(Boolean root) {
342 ((StdPDPPolicy)this.policy).setRoot(root);