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 public static Collection<String> PDPPOLICY_PROPERTIES;
76 private final Object data;
77 private List<PDPPolicy> policies;
79 @SuppressWarnings("unchecked")
80 public PDPPolicyContainer(Object data) {
83 if (this.data instanceof PDPGroup) {
84 policies = new ArrayList<PDPPolicy> (((PDPGroup) this.data).getPolicies());
86 if (this.data instanceof PDP) {
87 policies = new ArrayList<PDPPolicy> (((PDP) this.data).getPolicies());
89 if (this.data instanceof Set) {
90 policies = new ArrayList<PDPPolicy> ((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)
169 throws UnsupportedOperationException {
174 public Collection<?> getContainerPropertyIds() {
175 return PDPPOLICY_PROPERTIES;
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() throws UnsupportedOperationException {
224 throw new UnsupportedOperationException("Cannot add an empty policy.");
228 public boolean removeItem(Object itemId)
229 throws UnsupportedOperationException {
230 if (logger.isTraceEnabled()) {
231 logger.trace("removeItem: " + itemId);
233 ObjectMapper mapper = new ObjectMapper();
234 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
235 StdPDPPolicy pdpPolicy = null;
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));
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);
246 return this.policies.remove(itemId);
250 public boolean addContainerProperty(Object propertyId, Class<?> type,
251 Object defaultValue) throws UnsupportedOperationException {
256 public boolean removeContainerProperty(Object propertyId)
257 throws UnsupportedOperationException {
262 public boolean removeAllItems() throws UnsupportedOperationException {
267 public int indexOfId(Object itemId) {
268 if (logger.isTraceEnabled()) {
269 logger.trace("indexOfId: " + itemId);
271 return this.policies.indexOf(itemId);
275 public Object getIdByIndex(int index) {
276 if (logger.isTraceEnabled()) {
277 logger.trace("getIdByIndex: " + index);
279 return this.policies.get(index);
283 public List<?> getItemIds(int startIndex, int numberOfItems) {
284 if (logger.isTraceEnabled()) {
285 logger.trace("getItemIds: " + startIndex + " " + numberOfItems);
287 if (numberOfItems < 0) {
288 throw new IllegalArgumentException();
290 return this.policies.subList(startIndex, startIndex + numberOfItems);
294 public Object addItemAt(int index) throws UnsupportedOperationException {
295 if (logger.isTraceEnabled()) {
296 logger.trace("addItemAt: " + index);
301 public class PDPPolicyItem {
302 private final PDPPolicy policy;
304 public PDPPolicyItem(PDPPolicy itemId) {
305 this.policy = itemId;
308 public String getId() {
309 if (logger.isTraceEnabled()) {
310 logger.trace("getId: " + this.policy);
312 return this.policy.getId();
315 public String getName() {
316 if (logger.isTraceEnabled()) {
317 logger.trace("getName: " + this.policy);
319 return this.policy.getName();
322 public String getVersion() {
323 if (logger.isTraceEnabled()) {
324 logger.trace("getVersion: " + this.policy);
326 return this.policy.getVersion();
329 public String getDescription() {
330 if (logger.isTraceEnabled()) {
331 logger.trace("getDescription: " + this.policy);
333 return this.policy.getDescription();
336 public boolean getRoot() {
337 if (logger.isTraceEnabled()) {
338 logger.trace("isRoot: " + this.policy);
340 return this.policy.isRoot();
343 public void setRoot(Boolean root) {
344 ((StdPDPPolicy)this.policy).setRoot(root);