2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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;
23 import com.att.research.xacml.api.pap.PAPException;
25 import java.awt.Checkbox;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.LinkedList;
30 import java.util.List;
33 import org.onap.policy.common.logging.flexlogger.FlexLogger;
34 import org.onap.policy.common.logging.flexlogger.Logger;
35 import org.onap.policy.rest.util.PolicyContainer;
36 import org.onap.policy.rest.util.PolicyItemSetChangeNotifier;
37 import org.onap.policy.xacml.api.XACMLErrorConstants;
38 import org.onap.policy.xacml.api.pap.OnapPDP;
39 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
40 import org.onap.policy.xacml.api.pap.PAPPolicyEngine;
42 public class PDPGroupContainer extends PolicyItemSetChangeNotifier
43 implements PolicyContainer.Indexed, PolicyContainer.ItemSetChangeNotifier {
44 private static final long serialVersionUID = 1L;
45 private static final Logger LOGGER = FlexLogger.getLogger(PDPGroupContainer.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 "Description" property.
60 private static final String PROPERTY_DESCRIPTION = "Description";
63 * String identifier of a file's "Default" property.
65 private static final String PROPERTY_DEFAULT = "Default";
67 * String identifier of a file's "Status" property.
69 private static final String PROPERTY_STATUS = "Status";
72 * String identifier of a file's "PDPs" property.
74 private static final String PROPERTY_PDPS = "PDPs";
77 * String identifier of a file's "Policies" property.
79 private static final String PROPERTY_POLICIES = "Policies";
82 * String identifier of a file's "PIP Configurations" property.
84 private static final String PROPERTY_PIPCONFIG = "PIP Configurations";
87 * String identifier of a file's "Selected" property.
89 private static final String PROPERTY_SELECTED = "Selected";
92 * List of the string identifiers for the available properties.
94 private static Collection<String> pDPProperties;
96 private transient PAPPolicyEngine papEngine = null;
97 protected transient List<OnapPDPGroup> groups = Collections.synchronizedList(new ArrayList<OnapPDPGroup>());
99 public PDPGroupContainer(PAPPolicyEngine papPolicyEngine) {
101 this.setContainer(this);
105 this.papEngine = papPolicyEngine;
109 this.refreshGroups();
112 public boolean isSupported(Object itemId) {
113 return itemId instanceof OnapPDPGroup;
119 public synchronized void refreshGroups() {
120 synchronized (this.groups) {
123 this.groups.addAll(this.papEngine.getOnapPDPGroups());
124 } catch (PAPException e) {
125 String message = "Unable to retrieve Groups from server: " + e;
126 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
128 LOGGER.info("refreshGroups");
131 // Notify that we have changed
133 this.fireItemSetChange();
136 public List<OnapPDPGroup> getGroups() {
137 return Collections.unmodifiableList(this.groups);
143 * @param group OnapPDPGroup
145 public void makeDefault(OnapPDPGroup group) {
147 this.papEngine.setDefaultGroup(group);
148 } catch (PAPException e) {
149 String message = "Unable to set Default Group on server: " + e;
150 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
158 * @param group OnapPDPGroup
159 * @param newGroup OnapPDPGroup
160 * @throws PAPException PAPException
162 public void removeGroup(OnapPDPGroup group, OnapPDPGroup newGroup) throws PAPException {
163 if (LOGGER.isTraceEnabled()) {
164 LOGGER.trace("removeGroup: " + group + " new group for PDPs: " + newGroup);
166 if (group.isDefaultGroup()) {
167 throw new UnsupportedOperationException("You can't remove the Default Group.");
170 this.papEngine.removeGroup(group, newGroup);
171 } catch (NullPointerException | PAPException e) {
172 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to removeGroup " + group.getId(), e);
173 throw new PAPException("Failed to remove group '" + group.getId() + "'", e);
181 * @param group OnapPDPGroup
182 * @throws PAPException PAPException
184 public void removePDP(OnapPDP pdp, OnapPDPGroup group) throws PAPException {
185 if (LOGGER.isTraceEnabled()) {
186 LOGGER.trace("removePDP: " + pdp + " from group: " + group);
189 this.papEngine.removePDP(pdp);
190 } catch (PAPException e) {
191 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to removePDP " + pdp.getId(), e);
192 throw new PAPException("Failed to remove pdp '" + pdp.getId() + "'", e);
201 public void updatePDP(OnapPDP pdp) {
203 papEngine.updatePDP(pdp);
204 } catch (PAPException e) {
205 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
212 * @param group OnapPDPGroup
214 public void updateGroup(OnapPDPGroup group) {
216 papEngine.updateGroup(group);
217 } catch (PAPException e) {
218 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
225 * @param group the group
226 * @param userName the user name
228 public void updateGroup(OnapPDPGroup group, String userName) {
230 papEngine.updateGroup(group, userName);
231 } catch (PAPException e) {
232 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
237 public Collection<?> getContainerPropertyIds() {
238 return pDPProperties;
242 public Collection<?> getItemIds() {
243 final Collection<Object> items = new ArrayList<>();
244 items.addAll(this.groups);
245 if (LOGGER.isTraceEnabled()) {
246 LOGGER.trace("getItemIds: " + items);
248 return Collections.unmodifiableCollection(items);
252 public List<?> getItemIds(int startIndex, int numberOfItems) {
253 synchronized (this.groups) {
254 int endIndex = startIndex + numberOfItems;
255 if (endIndex > this.groups.size()) {
256 endIndex = this.groups.size() - 1;
258 return this.groups.subList(startIndex, endIndex);
263 public Class<?> getType(Object propertyId) {
264 if (propertyId.equals(PROPERTY_ID)) {
267 if (propertyId.equals(PROPERTY_NAME)) {
270 if (propertyId.equals(PROPERTY_DESCRIPTION)) {
273 if (propertyId.equals(PROPERTY_DEFAULT)) {
274 return Boolean.class;
276 if (propertyId.equals(PROPERTY_STATUS)) {
279 if (propertyId.equals(PROPERTY_PDPS)) {
282 if (propertyId.equals(PROPERTY_POLICIES)) {
285 if (propertyId.equals(PROPERTY_PIPCONFIG)) {
288 if (propertyId.equals(PROPERTY_SELECTED)) {
289 return Checkbox.class;
296 return this.groups.size();
300 public boolean containsId(Object itemId) {
301 if (LOGGER.isTraceEnabled()) {
302 LOGGER.trace("containsId: " + itemId);
304 if (!this.isSupported(itemId)) {
307 return this.groups.contains(itemId);
311 public Object addItem() {
312 throw new UnsupportedOperationException("PDP Container cannot add a given item.");
319 * @param description String
320 * @throws PAPException PAPException
322 public void addNewGroup(String name, String description) throws PAPException {
323 if (LOGGER.isTraceEnabled()) {
324 LOGGER.trace("addNewGroup " + name + " " + description);
326 this.papEngine.newGroup(name, description);
333 * @param group OnapPDPGroup
335 * @param description String
337 * @throws PAPException PAPException
339 public void addNewPDP(String id, OnapPDPGroup group, String name, String description, int jmxport)
340 throws PAPException {
341 if (LOGGER.isTraceEnabled()) {
342 LOGGER.trace("addNewPDP " + id + " " + name + " " + description + " " + jmxport);
344 this.papEngine.newPDP(id, group, name, description, jmxport);
351 * @param group OnapPDPGroup
353 public void movePDP(OnapPDP pdp, OnapPDPGroup group) {
355 this.papEngine.movePDP(pdp, group);
356 } catch (PAPException e) {
357 String message = "Unable to move PDP to new group on server: " + e;
358 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
364 public boolean addContainerProperty(Object propertyId, Class<?> type, Object defaultValue) {
365 throw new UnsupportedOperationException("Cannot add a container property.");
369 public boolean removeContainerProperty(Object propertyId) {
370 throw new UnsupportedOperationException("Cannot remove a container property.");
374 public boolean removeAllItems() {
375 throw new UnsupportedOperationException(
376 "PDP Container cannot remove all items. You must have at least the Default group.");
380 public void addItemSetChangeListener(ItemSetChangeListener listener) {
381 if (getItemSetChangeListeners() == null) {
382 setItemSetChangeListeners(new LinkedList<PolicyContainer.ItemSetChangeListener>());
384 getItemSetChangeListeners().add(listener);
388 public Object nextItemId(Object itemId) {
389 if (!this.isSupported(itemId)) {
392 int index = this.groups.indexOf(itemId);
395 // We don't know this group
400 // Is it the last one?
402 if (index == this.groups.size() - 1) {
409 // Return the next one
411 return this.groups.get(index + 1);
415 public Object prevItemId(Object itemId) {
416 if (!this.isSupported(itemId)) {
419 int index = this.groups.indexOf(itemId);
422 // We don't know this group
427 // Is it the first one?
436 // Return the previous one
438 return this.groups.get(index - 1);
442 public Object firstItemId() {
443 synchronized (this.groups) {
444 if (!this.groups.isEmpty()) {
445 return this.groups.get(0);
452 public Object lastItemId() {
453 synchronized (this.groups) {
454 if (!this.groups.isEmpty()) {
455 return this.groups.get(this.groups.size() - 1);
462 public boolean isFirstId(Object itemId) {
463 synchronized (this.groups) {
464 if (!this.groups.isEmpty()) {
465 return this.groups.get(0).equals(itemId);
472 public boolean isLastId(Object itemId) {
473 synchronized (this.groups) {
474 if (!this.groups.isEmpty()) {
475 return this.groups.get(this.groups.size() - 1).equals(itemId);
482 public Object addItemAfter(Object previousItemId) {
483 throw new UnsupportedOperationException("Cannot addItemAfter, there really is no real ordering.");
487 public int indexOfId(Object itemId) {
488 return this.groups.indexOf(itemId);
492 public Object getIdByIndex(int index) {
493 return this.groups.get(index);
497 public Object addItemAt(int index) {
498 throw new UnsupportedOperationException("Cannot addItemAt");
502 public boolean removeItem(Object itemId) {
503 if (LOGGER.isTraceEnabled()) {
504 LOGGER.trace("removeItem: " + itemId);
506 if (!this.isSupported(itemId)) {
510 // You cannot remove the default group
512 if (PROPERTY_DEFAULT.equals(((OnapPDPGroup) itemId).getId())) {
513 throw new UnsupportedOperationException("You can't remove the Default Group.");
516 // Remove PDPGroup and move any PDP's in it into the default group
519 this.papEngine.removeGroup((OnapPDPGroup) itemId, this.papEngine.getDefaultGroup());
521 } catch (NullPointerException | PAPException e) {
522 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to remove group", e);