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;
116 public synchronized void refreshGroups() {
117 synchronized (this.groups) {
120 this.groups.addAll(this.papEngine.getOnapPDPGroups());
121 } catch (PAPException e) {
122 String message = "Unable to retrieve Groups from server: " + e;
123 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
125 LOGGER.info("refreshGroups");
128 // Notify that we have changed
130 this.fireItemSetChange();
133 public List<OnapPDPGroup> getGroups() {
134 return Collections.unmodifiableList(this.groups);
137 public void makeDefault(OnapPDPGroup group) {
139 this.papEngine.setDefaultGroup(group);
140 } catch (PAPException e) {
141 String message = "Unable to set Default Group on server: " + e;
142 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
147 public void removeGroup(OnapPDPGroup group, OnapPDPGroup newGroup) throws PAPException {
148 if (LOGGER.isTraceEnabled()) {
149 LOGGER.trace("removeGroup: " + group + " new group for PDPs: " + newGroup);
151 if (group.isDefaultGroup()) {
152 throw new UnsupportedOperationException("You can't remove the Default Group.");
155 this.papEngine.removeGroup(group, newGroup);
156 } catch (NullPointerException | PAPException e) {
157 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to removeGroup " + group.getId(), e);
158 throw new PAPException("Failed to remove group '" + group.getId() + "'", e);
162 public void removePDP(OnapPDP pdp, OnapPDPGroup group) throws PAPException {
163 if (LOGGER.isTraceEnabled()) {
164 LOGGER.trace("removePDP: " + pdp + " from group: " + group);
167 this.papEngine.removePDP(pdp);
168 } catch (PAPException e) {
169 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to removePDP " + pdp.getId(), e);
170 throw new PAPException("Failed to remove pdp '" + pdp.getId() + "'", e);
174 public void updatePDP(OnapPDP pdp) {
176 papEngine.updatePDP(pdp);
177 } catch (PAPException e) {
178 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
182 public void updateGroup(OnapPDPGroup group) {
184 papEngine.updateGroup(group);
185 } catch (PAPException e) {
186 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
193 * @param group the group
194 * @param userName the user name
196 public void updateGroup(OnapPDPGroup group, String userName) {
198 papEngine.updateGroup(group, userName);
199 } catch (PAPException e) {
200 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
205 public Collection<?> getContainerPropertyIds() {
206 return pDPProperties;
210 public Collection<?> getItemIds() {
211 final Collection<Object> items = new ArrayList<>();
212 items.addAll(this.groups);
213 if (LOGGER.isTraceEnabled()) {
214 LOGGER.trace("getItemIds: " + items);
216 return Collections.unmodifiableCollection(items);
220 public Class<?> getType(Object propertyId) {
221 if (propertyId.equals(PROPERTY_ID)) {
224 if (propertyId.equals(PROPERTY_NAME)) {
227 if (propertyId.equals(PROPERTY_DESCRIPTION)) {
230 if (propertyId.equals(PROPERTY_DEFAULT)) {
231 return Boolean.class;
233 if (propertyId.equals(PROPERTY_STATUS)) {
236 if (propertyId.equals(PROPERTY_PDPS)) {
239 if (propertyId.equals(PROPERTY_POLICIES)) {
242 if (propertyId.equals(PROPERTY_PIPCONFIG)) {
245 if (propertyId.equals(PROPERTY_SELECTED)) {
246 return Checkbox.class;
253 return this.groups.size();
257 public boolean containsId(Object itemId) {
258 if (LOGGER.isTraceEnabled()) {
259 LOGGER.trace("containsId: " + itemId);
261 if (!this.isSupported(itemId)) {
264 return this.groups.contains(itemId);
268 public Object addItem() {
269 throw new UnsupportedOperationException("PDP Container cannot add a given item.");
272 public void addNewGroup(String name, String description) throws PAPException {
273 if (LOGGER.isTraceEnabled()) {
274 LOGGER.trace("addNewGroup " + name + " " + description);
276 this.papEngine.newGroup(name, description);
279 public void addNewPDP(String id, OnapPDPGroup group, String name, String description, int jmxport)
280 throws PAPException {
281 if (LOGGER.isTraceEnabled()) {
282 LOGGER.trace("addNewPDP " + id + " " + name + " " + description + " " + jmxport);
284 this.papEngine.newPDP(id, group, name, description, jmxport);
287 public void movePDP(OnapPDP pdp, OnapPDPGroup group) {
289 this.papEngine.movePDP(pdp, group);
290 } catch (PAPException e) {
291 String message = "Unable to move PDP to new group on server: " + e;
292 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
298 public boolean addContainerProperty(Object propertyId, Class<?> type, Object defaultValue) {
299 throw new UnsupportedOperationException("Cannot add a container property.");
303 public boolean removeContainerProperty(Object propertyId) {
304 throw new UnsupportedOperationException("Cannot remove a container property.");
308 public boolean removeAllItems() {
309 throw new UnsupportedOperationException(
310 "PDP Container cannot remove all items. You must have at least the Default group.");
314 public void addItemSetChangeListener(ItemSetChangeListener listener) {
315 if (getItemSetChangeListeners() == null) {
316 setItemSetChangeListeners(new LinkedList<PolicyContainer.ItemSetChangeListener>());
318 getItemSetChangeListeners().add(listener);
322 public Object nextItemId(Object itemId) {
323 if (!this.isSupported(itemId)) {
326 int index = this.groups.indexOf(itemId);
329 // We don't know this group
334 // Is it the last one?
336 if (index == this.groups.size() - 1) {
343 // Return the next one
345 return this.groups.get(index + 1);
349 public Object prevItemId(Object itemId) {
350 if (!this.isSupported(itemId)) {
353 int index = this.groups.indexOf(itemId);
356 // We don't know this group
361 // Is it the first one?
370 // Return the previous one
372 return this.groups.get(index - 1);
376 public Object firstItemId() {
377 synchronized (this.groups) {
378 if (!this.groups.isEmpty()) {
379 return this.groups.get(0);
386 public Object lastItemId() {
387 synchronized (this.groups) {
388 if (!this.groups.isEmpty()) {
389 return this.groups.get(this.groups.size() - 1);
396 public boolean isFirstId(Object itemId) {
397 synchronized (this.groups) {
398 if (!this.groups.isEmpty()) {
399 return this.groups.get(0).equals(itemId);
406 public boolean isLastId(Object itemId) {
407 synchronized (this.groups) {
408 if (!this.groups.isEmpty()) {
409 return this.groups.get(this.groups.size() - 1).equals(itemId);
416 public Object addItemAfter(Object previousItemId) {
417 throw new UnsupportedOperationException("Cannot addItemAfter, there really is no real ordering.");
421 public int indexOfId(Object itemId) {
422 return this.groups.indexOf(itemId);
426 public Object getIdByIndex(int index) {
427 return this.groups.get(index);
431 public List<?> getItemIds(int startIndex, int numberOfItems) {
432 synchronized (this.groups) {
433 int endIndex = startIndex + numberOfItems;
434 if (endIndex > this.groups.size()) {
435 endIndex = this.groups.size() - 1;
437 return this.groups.subList(startIndex, endIndex);
442 public Object addItemAt(int index) {
443 throw new UnsupportedOperationException("Cannot addItemAt");
447 public boolean removeItem(Object itemId) {
448 if (LOGGER.isTraceEnabled()) {
449 LOGGER.trace("removeItem: " + itemId);
451 if (!this.isSupported(itemId)) {
455 // You cannot remove the default group
457 if (PROPERTY_DEFAULT.equals(((OnapPDPGroup) itemId).getId())) {
458 throw new UnsupportedOperationException("You can't remove the Default Group.");
461 // Remove PDPGroup and move any PDP's in it into the default group
464 this.papEngine.removeGroup((OnapPDPGroup) itemId, this.papEngine.getDefaultGroup());
466 } catch (NullPointerException | PAPException e) {
467 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to remove group", e);