2 * ============LICENSE_START=======================================================
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
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.rest.util;
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.xacml.api.XACMLErrorConstants;
33 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
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;
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);
46 * String identifier of a file's "Id" property.
48 private static final String PROPERTY_ID = "Id";
51 * String identifier of a file's "name" property.
53 private static final String PROPERTY_NAME = "Name";
56 * String identifier of a file's "name" property.
58 private static final String PROPERTY_VERSION = "Version";
61 * String identifier of a file's "Description" property.
63 private static final String PROPERTY_DESCRIPTION = "Description";
66 * String identifier of a file's "IsRoot" property.
68 private static final String PROPERTY_ISROOT = "Root";
71 * List of the string identifiers for the available properties.
73 private static Collection<String> pDPPolicyProperties;
75 private final transient Object data;
76 private transient List<PDPPolicy> policies;
78 @SuppressWarnings("unchecked")
79 public PDPPolicyContainer(Object data) {
82 if (this.data instanceof PDPGroup) {
83 policies = new ArrayList<> (((PDPGroup) this.data).getPolicies());
85 if (this.data instanceof PDP) {
86 policies = new ArrayList<> (((PDP) this.data).getPolicies());
88 if (this.data instanceof Set) {
89 policies = new ArrayList<> ((Set<PDPPolicy>)data);
91 if (this.policies == null) {
92 LOGGER.info("NULL policies");
93 throw new NullPointerException("PDPPolicyContainer created with unexpected Object type '" + data.getClass().getName() + "'");
95 this.setContainer(this);
99 public Object nextItemId(Object itemId) {
100 if (LOGGER.isTraceEnabled()) {
101 LOGGER.trace("nextItemId: " + itemId);
103 int index = this.policies.indexOf(itemId);
104 if (index == -1 || ((index + 1) >= this.policies.size())) {
107 return new PDPPolicyItem(this.policies.get(index + 1));
111 public Object prevItemId(Object itemId) {
112 if (LOGGER.isTraceEnabled()) {
113 LOGGER.trace("prevItemId: " + itemId);
115 int index = this.policies.indexOf(itemId);
119 return new PDPPolicyItem(this.policies.get(index - 1));
123 public Object firstItemId() {
124 if (LOGGER.isTraceEnabled()) {
125 LOGGER.trace("firstItemId: ");
127 if (this.policies.isEmpty()) {
130 return new PDPPolicyItem(this.policies.get(0));
134 public Object lastItemId() {
135 if (LOGGER.isTraceEnabled()) {
136 LOGGER.trace("lastItemid: ");
138 if (this.policies.isEmpty()) {
141 return new PDPPolicyItem(this.policies.get(this.policies.size() - 1));
145 public boolean isFirstId(Object itemId) {
146 if (LOGGER.isTraceEnabled()) {
147 LOGGER.trace("isFirstId: " + itemId);
149 if (this.policies.isEmpty()) {
152 return itemId.equals(this.policies.get(0));
156 public boolean isLastId(Object itemId) {
157 if (LOGGER.isTraceEnabled()) {
158 LOGGER.trace("isLastId: " + itemId);
160 if (this.policies.isEmpty()) {
163 return itemId.equals(this.policies.get(this.policies.size() - 1));
167 public Object addItemAfter(Object previousItemId) {
172 public Collection<?> getContainerPropertyIds() {
173 return pDPPolicyProperties;
177 public Collection<?> getItemIds() {
178 final Collection<Object> items = new ArrayList<>();
179 items.addAll(this.policies);
180 return Collections.unmodifiableCollection(items);
185 public Class<?> getType(Object propertyId) {
186 if (propertyId.equals(PROPERTY_ID)) {
189 if (propertyId.equals(PROPERTY_NAME)) {
192 if (propertyId.equals(PROPERTY_VERSION)) {
195 if (propertyId.equals(PROPERTY_DESCRIPTION)) {
198 if (propertyId.equals(PROPERTY_ISROOT)) {
199 return Boolean.class;
206 if (LOGGER.isTraceEnabled()) {
207 LOGGER.trace("size: " + this.policies.size());
209 return this.policies.size();
213 public boolean containsId(Object itemId) {
214 if (LOGGER.isTraceEnabled()) {
215 LOGGER.trace("containsId: " + itemId);
217 return this.policies.contains(itemId);
221 public Object addItem() {
222 throw new UnsupportedOperationException("Cannot add an empty policy.");
226 public boolean removeItem(Object itemId) {
227 if (LOGGER.isTraceEnabled()) {
228 LOGGER.trace("removeItem: " + itemId);
230 ObjectMapper mapper = new ObjectMapper();
231 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
232 StdPDPPolicy pdpPolicy = null;
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));
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);
243 return this.policies.remove(itemId);
247 public boolean addContainerProperty(Object propertyId, Class<?> type,
248 Object defaultValue) {
253 public boolean removeContainerProperty(Object propertyId) {
258 public boolean removeAllItems() {
263 public int indexOfId(Object itemId) {
264 if (LOGGER.isTraceEnabled()) {
265 LOGGER.trace("indexOfId: " + itemId);
267 return this.policies.indexOf(itemId);
271 public Object getIdByIndex(int index) {
272 if (LOGGER.isTraceEnabled()) {
273 LOGGER.trace("getIdByIndex: " + index);
275 return this.policies.get(index);
279 public List<?> getItemIds(int startIndex, int numberOfItems) {
280 if (LOGGER.isTraceEnabled()) {
281 LOGGER.trace("getItemIds: " + startIndex + " " + numberOfItems);
283 if (numberOfItems < 0) {
284 throw new IllegalArgumentException();
286 return this.policies.subList(startIndex, startIndex + numberOfItems);
290 public Object addItemAt(int index) {
291 if (LOGGER.isTraceEnabled()) {
292 LOGGER.trace("addItemAt: " + index);
297 public class PDPPolicyItem {
298 private final PDPPolicy policy;
300 public PDPPolicyItem(PDPPolicy itemId) {
301 this.policy = itemId;
304 public String getId() {
305 if (LOGGER.isTraceEnabled()) {
306 LOGGER.trace("getId: " + this.policy);
308 return this.policy.getId();
311 public String getName() {
312 if (LOGGER.isTraceEnabled()) {
313 LOGGER.trace("getName: " + this.policy);
315 return this.policy.getName();
318 public String getVersion() {
319 if (LOGGER.isTraceEnabled()) {
320 LOGGER.trace("getVersion: " + this.policy);
322 return this.policy.getVersion();
325 public String getDescription() {
326 if (LOGGER.isTraceEnabled()) {
327 LOGGER.trace("getDescription: " + this.policy);
329 return this.policy.getDescription();
332 public boolean getRoot() {
333 if (LOGGER.isTraceEnabled()) {
334 LOGGER.trace("isRoot: " + this.policy);
336 return this.policy.isRoot();
339 public void setRoot(Boolean root) {
340 ((StdPDPPolicy)this.policy).setRoot(root);