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.awt.Checkbox;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.LinkedList;
29 import java.util.List;
32 import org.onap.policy.common.logging.flexlogger.FlexLogger;
33 import org.onap.policy.common.logging.flexlogger.Logger;
34 import org.onap.policy.utils.PolicyContainer;
35 import org.onap.policy.utils.PolicyItemSetChangeNotifier;
36 import org.onap.policy.xacml.api.XACMLErrorConstants;
37 import org.onap.policy.xacml.api.pap.OnapPDP;
38 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
39 import org.onap.policy.xacml.api.pap.PAPPolicyEngine;
41 import com.att.research.xacml.api.pap.PAPException;
42 import com.att.research.xacml.api.pap.PDP;
43 import com.att.research.xacml.api.pap.PDPPIPConfig;
44 import com.att.research.xacml.api.pap.PDPPolicy;
46 public class PDPGroupContainer extends PolicyItemSetChangeNotifier implements PolicyContainer.Indexed, PolicyContainer.ItemSetChangeNotifier {
47 private static final long serialVersionUID = 1L;
48 private static final Logger LOGGER = FlexLogger.getLogger(PDPGroupContainer.class);
51 * String identifier of a file's "Id" property.
53 private static final String PROPERTY_ID = "Id";
56 * String identifier of a file's "name" property.
58 private static final String PROPERTY_NAME = "Name";
61 * String identifier of a file's "Description" property.
63 private static final String PROPERTY_DESCRIPTION = "Description";
66 * String identifier of a file's "Default" property.
68 private static final String PROPERTY_DEFAULT = "Default";
70 * String identifier of a file's "Status" property.
72 private static final String PROPERTY_STATUS = "Status";
75 * String identifier of a file's "PDPs" property.
77 private static final String PROPERTY_PDPS = "PDPs";
80 * String identifier of a file's "Policies" property.
82 private static final String PROPERTY_POLICIES = "Policies";
85 * String identifier of a file's "PIP Configurations" property.
87 private static final String PROPERTY_PIPCONFIG = "PIP Configurations";
90 * String identifier of a file's "Selected" property.
92 private static final String PROPERTY_SELECTED = "Selected";
95 * List of the string identifiers for the available properties.
97 private static Collection<String> pDPProperties;
99 private transient PAPPolicyEngine papEngine = null;
100 protected transient List<OnapPDPGroup> groups = Collections.synchronizedList(new ArrayList<OnapPDPGroup>());
102 public PDPGroupContainer(PAPPolicyEngine papPolicyEngine) {
104 this.setContainer(this);
108 this.papEngine = papPolicyEngine;
112 this.refreshGroups();
115 public boolean isSupported(Object itemId) {
116 if (itemId instanceof OnapPDPGroup) {
122 public synchronized void refreshGroups() {
123 synchronized(this.groups) {
126 this.groups.addAll(this.papEngine.getOnapPDPGroups());
127 } catch (PAPException e) {
128 String message = "Unable to retrieve Groups from server: " + e;
129 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
131 LOGGER.info("refreshGroups");
134 // Notify that we have changed
136 this.fireItemSetChange();
139 public List<OnapPDPGroup> getGroups() {
140 return Collections.unmodifiableList(this.groups);
143 public void makeDefault(OnapPDPGroup group) {
145 this.papEngine.setDefaultGroup(group);
146 } catch (PAPException e) {
147 String message = "Unable to set Default Group on server: " + e;
148 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
153 public void removeGroup(OnapPDPGroup group, OnapPDPGroup newGroup) throws PAPException {
154 if (LOGGER.isTraceEnabled()) {
155 LOGGER.trace("removeGroup: " + group + " new group for PDPs: " + newGroup);
157 if (group.isDefaultGroup()) {
158 throw new UnsupportedOperationException("You can't remove the Default Group.");
161 this.papEngine.removeGroup(group, newGroup);
162 } catch (NullPointerException | PAPException e) {
163 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to removeGroup " + group.getId(), e);
164 throw new PAPException("Failed to remove group '" + group.getId()+ "'", e);
168 public void removePDP(OnapPDP pdp, OnapPDPGroup group) throws PAPException {
169 if (LOGGER.isTraceEnabled()) {
170 LOGGER.trace("removePDP: " + pdp + " from group: " + group);
173 this.papEngine.removePDP(pdp);
174 } catch (PAPException e) {
175 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to removePDP " + pdp.getId(), e);
176 throw new PAPException("Failed to remove pdp '" + pdp.getId()+ "'", e);
180 public void updatePDP(OnapPDP pdp) {
182 papEngine.updatePDP(pdp);
183 } catch (PAPException e) {
184 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
188 public void updateGroup(OnapPDPGroup group) {
190 papEngine.updateGroup(group);
191 } catch (PAPException e) {
192 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
197 public Collection<?> getContainerPropertyIds() {
198 return pDPProperties;
202 public Collection<?> getItemIds() {
203 final Collection<Object> items = new ArrayList<>();
204 items.addAll(this.groups);
205 if (LOGGER.isTraceEnabled()) {
206 LOGGER.trace("getItemIds: " + items);
208 return Collections.unmodifiableCollection(items);
212 public Class<?> getType(Object propertyId) {
213 if (propertyId.equals(PROPERTY_ID)) {
216 if (propertyId.equals(PROPERTY_NAME)) {
219 if (propertyId.equals(PROPERTY_DESCRIPTION)) {
222 if (propertyId.equals(PROPERTY_DEFAULT)) {
223 return Boolean.class;
225 if (propertyId.equals(PROPERTY_STATUS)) {
228 if (propertyId.equals(PROPERTY_PDPS)) {
231 if (propertyId.equals(PROPERTY_POLICIES)) {
234 if (propertyId.equals(PROPERTY_PIPCONFIG)) {
237 if (propertyId.equals(PROPERTY_SELECTED)) {
238 return Checkbox.class;
245 return this.groups.size();
249 public boolean containsId(Object itemId) {
250 if (LOGGER.isTraceEnabled()) {
251 LOGGER.trace("containsId: " + itemId);
253 if (this.isSupported(itemId) == false) {
256 return this.groups.contains(itemId);
260 public Object addItem() throws UnsupportedOperationException {
261 throw new UnsupportedOperationException("PDP Container cannot add a given item.");
264 public void addNewGroup(String name, String description) throws NullPointerException, PAPException {
265 if (LOGGER.isTraceEnabled()) {
266 LOGGER.trace("addNewGroup " + name + " " + description);
268 this.papEngine.newGroup(name, description);
271 public void addNewPDP(String id, OnapPDPGroup group, String name, String description, int jmxport) throws NullPointerException, PAPException {
272 if (LOGGER.isTraceEnabled()) {
273 LOGGER.trace("addNewPDP " + id + " " + name + " " + description + " " + jmxport);
275 this.papEngine.newPDP(id, group, name, description, jmxport);
278 public void movePDP(OnapPDP pdp, OnapPDPGroup group) {
280 this.papEngine.movePDP(pdp, group);
281 } catch (PAPException e) {
282 String message = "Unable to move PDP to new group on server: " + e;
283 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
289 public boolean addContainerProperty(Object propertyId, Class<?> type, Object defaultValue) throws UnsupportedOperationException {
290 throw new UnsupportedOperationException("Cannot add a container property.");
294 public boolean removeContainerProperty(Object propertyId) throws UnsupportedOperationException {
295 throw new UnsupportedOperationException("Cannot remove a container property.");
299 public boolean removeAllItems() throws UnsupportedOperationException {
300 throw new UnsupportedOperationException("PDP Container cannot remove all items. You must have at least the Default group.");
304 public void addItemSetChangeListener(ItemSetChangeListener listener) {
305 if (getItemSetChangeListeners() == null) {
306 setItemSetChangeListeners(new LinkedList<PolicyContainer.ItemSetChangeListener>());
308 getItemSetChangeListeners().add(listener);
312 public Object nextItemId(Object itemId) {
313 if (this.isSupported(itemId) == false) {
316 int index = this.groups.indexOf(itemId);
319 // We don't know this group
324 // Is it the last one?
326 if (index == this.groups.size() - 1) {
333 // Return the next one
335 return this.groups.get(index + 1);
339 public Object prevItemId(Object itemId) {
340 if (this.isSupported(itemId) == false) {
343 int index = this.groups.indexOf(itemId);
346 // We don't know this group
351 // Is it the first one?
360 // Return the previous one
362 return this.groups.get(index - 1);
366 public Object firstItemId() {
367 synchronized (this.groups) {
368 if (!this.groups.isEmpty()) {
369 return this.groups.get(0);
376 public Object lastItemId() {
377 synchronized (this.groups) {
378 if (!this.groups.isEmpty()) {
379 return this.groups.get(this.groups.size() - 1);
386 public boolean isFirstId(Object itemId) {
387 synchronized (this.groups) {
388 if (!this.groups.isEmpty()) {
389 return this.groups.get(0).equals(itemId);
396 public boolean isLastId(Object itemId) {
397 synchronized (this.groups) {
398 if (!this.groups.isEmpty()) {
399 return this.groups.get(this.groups.size() - 1).equals(itemId);
406 public Object addItemAfter(Object previousItemId) throws UnsupportedOperationException {
407 throw new UnsupportedOperationException("Cannot addItemAfter, there really is no real ordering.");
411 public int indexOfId(Object itemId) {
412 return this.groups.indexOf(itemId);
416 public Object getIdByIndex(int index) {
417 return this.groups.get(index);
421 public List<?> getItemIds(int startIndex, int numberOfItems) {
422 synchronized (this.groups) {
423 int endIndex = startIndex + numberOfItems;
424 if (endIndex > this.groups.size()) {
425 endIndex = this.groups.size() - 1;
427 return this.groups.subList(startIndex, endIndex);
432 public Object addItemAt(int index) throws UnsupportedOperationException {
433 throw new UnsupportedOperationException("Cannot addItemAt");
437 public boolean removeItem(Object itemId) throws UnsupportedOperationException {
438 if (LOGGER.isTraceEnabled()) {
439 LOGGER.trace("removeItem: " + itemId);
441 if (this.isSupported(itemId) == false) {
445 // You cannot remove the default group
447 if ("Default".equals(((OnapPDPGroup) itemId).getId())) {
448 throw new UnsupportedOperationException("You can't remove the Default Group.");
451 // Remove PDPGroup and move any PDP's in it into the default group
454 this.papEngine.removeGroup((OnapPDPGroup) itemId, this.papEngine.getDefaultGroup());
456 } catch (NullPointerException | PAPException e) {
457 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to remove group", e);
462 public class PDPGroupItem{
463 private final OnapPDPGroup group;
465 public PDPGroupItem(OnapPDPGroup itemId) {
469 public String getId() {
470 if (LOGGER.isTraceEnabled()) {
471 LOGGER.trace("getId: " + this.group);
473 return this.group.getId();
476 public String getName() {
477 if (LOGGER.isTraceEnabled()) {
478 LOGGER.trace("getName: " + this.group);
480 return this.group.getName();
483 public String getDescription() {
484 if (LOGGER.isTraceEnabled()) {
485 LOGGER.trace("getDescription: " + this.group);
487 return this.group.getDescription();
490 public Boolean getDefault() {
491 if (LOGGER.isTraceEnabled()) {
492 LOGGER.trace("getDefault: " + this.group);
494 return this.group.isDefaultGroup();
498 public String getStatus() {
499 return this.group.getStatus().getStatus().toString();
502 public Set<PDP> getPDPs() {
503 return Collections.unmodifiableSet(this.group.getPdps());
506 public Set<PDPPolicy> getPolicies() {
507 if (LOGGER.isTraceEnabled()) {
508 LOGGER.trace("getPolicies: " + this.group);
510 return this.group.getPolicies();
513 public Set<PDPPIPConfig> getPipConfigs() {
514 if (LOGGER.isTraceEnabled()) {
515 LOGGER.trace("getPIPConfigs: " + this.group);
517 return this.group.getPipConfigs();