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)
170 throws UnsupportedOperationException {
175 public Collection<?> getContainerPropertyIds() {
176 return pDPPolicyProperties;
180 public Collection<?> getItemIds() {
181 final Collection<Object> items = new ArrayList<>();
182 items.addAll(this.policies);
183 return Collections.unmodifiableCollection(items);
188 public Class<?> getType(Object propertyId) {
189 if (propertyId.equals(PROPERTY_ID)) {
192 if (propertyId.equals(PROPERTY_NAME)) {
195 if (propertyId.equals(PROPERTY_VERSION)) {
198 if (propertyId.equals(PROPERTY_DESCRIPTION)) {
201 if (propertyId.equals(PROPERTY_ISROOT)) {
202 return Boolean.class;
209 if (LOGGER.isTraceEnabled()) {
210 LOGGER.trace("size: " + this.policies.size());
212 return this.policies.size();
216 public boolean containsId(Object itemId) {
217 if (LOGGER.isTraceEnabled()) {
218 LOGGER.trace("containsId: " + itemId);
220 return this.policies.contains(itemId);
224 public Object addItem() throws UnsupportedOperationException {
225 throw new UnsupportedOperationException("Cannot add an empty policy.");
229 public boolean removeItem(Object itemId)
230 throws UnsupportedOperationException {
231 if (LOGGER.isTraceEnabled()) {
232 LOGGER.trace("removeItem: " + itemId);
234 ObjectMapper mapper = new ObjectMapper();
235 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
236 StdPDPPolicy pdpPolicy = null;
238 pdpPolicy = mapper.readValue(itemId.toString() , StdPDPPolicy.class);
239 for(int i = 0; i< policies.size(); i++){
240 if(policies.get(i).getId().equalsIgnoreCase(pdpPolicy.getId())){
241 return this.policies.remove(this.policies.get(i));
244 } catch (Exception e) {
245 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Exception Occured While Mapping the Removing Policy from PDP Group to Std Policy"+e);
247 return this.policies.remove(itemId);
251 public boolean addContainerProperty(Object propertyId, Class<?> type,
252 Object defaultValue) throws UnsupportedOperationException {
257 public boolean removeContainerProperty(Object propertyId)
258 throws UnsupportedOperationException {
263 public boolean removeAllItems() throws UnsupportedOperationException {
268 public int indexOfId(Object itemId) {
269 if (LOGGER.isTraceEnabled()) {
270 LOGGER.trace("indexOfId: " + itemId);
272 return this.policies.indexOf(itemId);
276 public Object getIdByIndex(int index) {
277 if (LOGGER.isTraceEnabled()) {
278 LOGGER.trace("getIdByIndex: " + index);
280 return this.policies.get(index);
284 public List<?> getItemIds(int startIndex, int numberOfItems) {
285 if (LOGGER.isTraceEnabled()) {
286 LOGGER.trace("getItemIds: " + startIndex + " " + numberOfItems);
288 if (numberOfItems < 0) {
289 throw new IllegalArgumentException();
291 return this.policies.subList(startIndex, startIndex + numberOfItems);
295 public Object addItemAt(int index) throws UnsupportedOperationException {
296 if (LOGGER.isTraceEnabled()) {
297 LOGGER.trace("addItemAt: " + index);
302 public class PDPPolicyItem {
303 private final PDPPolicy policy;
305 public PDPPolicyItem(PDPPolicy itemId) {
306 this.policy = itemId;
309 public String getId() {
310 if (LOGGER.isTraceEnabled()) {
311 LOGGER.trace("getId: " + this.policy);
313 return this.policy.getId();
316 public String getName() {
317 if (LOGGER.isTraceEnabled()) {
318 LOGGER.trace("getName: " + this.policy);
320 return this.policy.getName();
323 public String getVersion() {
324 if (LOGGER.isTraceEnabled()) {
325 LOGGER.trace("getVersion: " + this.policy);
327 return this.policy.getVersion();
330 public String getDescription() {
331 if (LOGGER.isTraceEnabled()) {
332 LOGGER.trace("getDescription: " + this.policy);
334 return this.policy.getDescription();
337 public boolean getRoot() {
338 if (LOGGER.isTraceEnabled()) {
339 LOGGER.trace("isRoot: " + this.policy);
341 return this.policy.isRoot();
344 public void setRoot(Boolean root) {
345 ((StdPDPPolicy)this.policy).setRoot(root);