2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.rest.util;
24 import com.att.research.xacml.api.pap.PDP;
25 import com.att.research.xacml.api.pap.PDPGroup;
26 import com.att.research.xacml.api.pap.PDPPolicy;
27 import com.fasterxml.jackson.databind.DeserializationFeature;
28 import com.fasterxml.jackson.databind.ObjectMapper;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.List;
36 import org.onap.policy.common.logging.flexlogger.FlexLogger;
37 import org.onap.policy.common.logging.flexlogger.Logger;
38 import org.onap.policy.xacml.api.XACMLErrorConstants;
39 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
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);
45 private static final String PROPERTY_ID = "Id";
46 private static final String PROPERTY_NAME = "Name";
47 private static final String PROPERTY_VERSION = "Version";
48 private static final String PROPERTY_DESCRIPTION = "Description";
49 private static final String PROPERTY_ISROOT = "Root";
52 * List of the string identifiers for the available properties.
54 private static Collection<String> pDPPolicyProperties;
56 private final transient Object data;
57 private transient List<PDPPolicy> policies;
60 * Instantiates a new pdp policy container.
62 * @param data the data
64 @SuppressWarnings("unchecked")
65 public PdpPolicyContainer(Object data) {
68 if (this.data instanceof PDPGroup) {
69 policies = new ArrayList<>(((PDPGroup) this.data).getPolicies());
71 if (this.data instanceof PDP) {
72 policies = new ArrayList<>(((PDP) this.data).getPolicies());
74 if (this.data instanceof Set) {
75 policies = new ArrayList<>((Set<PDPPolicy>) data);
77 if (this.policies == null) {
78 LOGGER.info("NULL policies");
79 throw new NullPointerException("PDPPolicyContainer created with unexpected Object type '"
80 + data.getClass().getName() + "'");
82 this.setContainer(this);
88 * @param itemId the item id
92 public Object nextItemId(Object itemId) {
93 if (LOGGER.isTraceEnabled()) {
94 LOGGER.trace("nextItemId: " + itemId);
96 int index = this.policies.indexOf(itemId);
97 if (index == -1 || ((index + 1) >= this.policies.size())) {
100 return new PdpPolicyItem(this.policies.get(index + 1));
106 * @param itemId the item id
110 public Object prevItemId(Object itemId) {
111 if (LOGGER.isTraceEnabled()) {
112 LOGGER.trace("prevItemId: " + itemId);
114 int index = this.policies.indexOf(itemId);
118 return new PdpPolicyItem(this.policies.get(index - 1));
127 public Object firstItemId() {
128 if (LOGGER.isTraceEnabled()) {
129 LOGGER.trace("firstItemId: ");
131 if (this.policies.isEmpty()) {
134 return new PdpPolicyItem(this.policies.get(0));
143 public Object lastItemId() {
144 if (LOGGER.isTraceEnabled()) {
145 LOGGER.trace("lastItemid: ");
147 if (this.policies.isEmpty()) {
150 return new PdpPolicyItem(this.policies.get(this.policies.size() - 1));
154 * Checks if is first id.
156 * @param itemId the item id
157 * @return true, if is first id
160 public boolean isFirstId(Object itemId) {
161 if (LOGGER.isTraceEnabled()) {
162 LOGGER.trace("isFirstId: " + itemId);
164 if (this.policies.isEmpty()) {
167 return itemId.equals(this.policies.get(0));
171 * Checks if is last id.
173 * @param itemId the item id
174 * @return true, if is last id
177 public boolean isLastId(Object itemId) {
178 if (LOGGER.isTraceEnabled()) {
179 LOGGER.trace("isLastId: " + itemId);
181 if (this.policies.isEmpty()) {
184 return itemId.equals(this.policies.get(this.policies.size() - 1));
188 * Adds the item after.
190 * @param previousItemId the previous item id
194 public Object addItemAfter(Object previousItemId) {
199 * Gets the container property ids.
201 * @return the container property ids
204 public Collection<?> getContainerPropertyIds() {
205 return pDPPolicyProperties;
211 * @return the item ids
214 public Collection<?> getItemIds() {
215 final Collection<Object> items = new ArrayList<>();
216 items.addAll(this.policies);
217 return Collections.unmodifiableCollection(items);
223 * @param startIndex the start index
224 * @param numberOfItems the number of items
225 * @return the item ids
228 public List<?> getItemIds(int startIndex, int numberOfItems) {
229 if (LOGGER.isTraceEnabled()) {
230 LOGGER.trace("getItemIds: " + startIndex + " " + numberOfItems);
232 if (numberOfItems < 0) {
233 throw new IllegalArgumentException();
235 return this.policies.subList(startIndex, startIndex + numberOfItems);
241 * @param propertyId the property id
245 public Class<?> getType(Object propertyId) {
246 if (propertyId.equals(PROPERTY_ID)) {
249 if (propertyId.equals(PROPERTY_NAME)) {
252 if (propertyId.equals(PROPERTY_VERSION)) {
255 if (propertyId.equals(PROPERTY_DESCRIPTION)) {
258 if (propertyId.equals(PROPERTY_ISROOT)) {
259 return Boolean.class;
271 if (LOGGER.isTraceEnabled()) {
272 LOGGER.trace("size: " + this.policies.size());
274 return this.policies.size();
280 * @param itemId the item id
281 * @return true, if successful
284 public boolean containsId(Object itemId) {
285 if (LOGGER.isTraceEnabled()) {
286 LOGGER.trace("containsId: " + itemId);
288 return this.policies.contains(itemId);
297 public Object addItem() {
298 throw new UnsupportedOperationException("Cannot add an empty policy.");
304 * @param itemId the item id
305 * @return true, if successful
308 public boolean removeItem(Object itemId) {
309 if (LOGGER.isTraceEnabled()) {
310 LOGGER.trace("removeItem: " + itemId);
312 ObjectMapper mapper = new ObjectMapper();
313 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
314 StdPDPPolicy pdpPolicy = null;
316 pdpPolicy = mapper.readValue(itemId.toString(), StdPDPPolicy.class);
317 for (int i = 0; i < policies.size(); i++) {
318 if (policies.get(i).getId().equalsIgnoreCase(pdpPolicy.getId())) {
319 return this.policies.remove(this.policies.get(i));
322 } catch (Exception e) {
323 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE
324 + "Exception Occured While Mapping the Removing Policy from PDP Group to Std Policy" + e);
326 return this.policies.remove(itemId);
330 * Adds the container property.
332 * @param propertyId the property id
333 * @param type the type
334 * @param defaultValue the default value
335 * @return true, if successful
338 public boolean addContainerProperty(Object propertyId, Class<?> type, Object defaultValue) {
343 * Removes the container property.
345 * @param propertyId the property id
346 * @return true, if successful
349 public boolean removeContainerProperty(Object propertyId) {
354 * Removes the all items.
356 * @return true, if successful
359 public boolean removeAllItems() {
366 * @param itemId the item id
370 public int indexOfId(Object itemId) {
371 if (LOGGER.isTraceEnabled()) {
372 LOGGER.trace("indexOfId: " + itemId);
374 return this.policies.indexOf(itemId);
378 * Gets the id by index.
380 * @param index the index
381 * @return the id by index
384 public Object getIdByIndex(int index) {
385 if (LOGGER.isTraceEnabled()) {
386 LOGGER.trace("getIdByIndex: " + index);
388 return this.policies.get(index);
394 * @param index the index
398 public Object addItemAt(int index) {
399 if (LOGGER.isTraceEnabled()) {
400 LOGGER.trace("addItemAt: " + index);
405 public class PdpPolicyItem {
406 private final PDPPolicy policy;
409 * Instantiates a new PDP policy item.
411 * @param itemId the item id
413 public PdpPolicyItem(PDPPolicy itemId) {
414 this.policy = itemId;
422 public String getId() {
423 if (LOGGER.isTraceEnabled()) {
424 LOGGER.trace("getId: " + this.policy);
426 return this.policy.getId();
434 public String getName() {
435 if (LOGGER.isTraceEnabled()) {
436 LOGGER.trace("getName: " + this.policy);
438 return this.policy.getName();
444 * @return the version
446 public String getVersion() {
447 if (LOGGER.isTraceEnabled()) {
448 LOGGER.trace("getVersion: " + this.policy);
450 return this.policy.getVersion();
454 * Gets the description.
456 * @return the description
458 public String getDescription() {
459 if (LOGGER.isTraceEnabled()) {
460 LOGGER.trace("getDescription: " + this.policy);
462 return this.policy.getDescription();
470 public boolean getRoot() {
471 if (LOGGER.isTraceEnabled()) {
472 LOGGER.trace("isRoot: " + this.policy);
474 return this.policy.isRoot();
480 * @param root the new root
482 public void setRoot(Boolean root) {
483 ((StdPDPPolicy) this.policy).setRoot(root);