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 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);
140 public void makeDefault(OnapPDPGroup group) {
142 this.papEngine.setDefaultGroup(group);
143 } catch (PAPException e) {
144 String message = "Unable to set Default Group on server: " + e;
145 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
150 public void removeGroup(OnapPDPGroup group, OnapPDPGroup newGroup) throws PAPException {
151 if (LOGGER.isTraceEnabled()) {
152 LOGGER.trace("removeGroup: " + group + " new group for PDPs: " + newGroup);
154 if (group.isDefaultGroup()) {
155 throw new UnsupportedOperationException("You can't remove the Default Group.");
158 this.papEngine.removeGroup(group, newGroup);
159 } catch (NullPointerException | PAPException e) {
160 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to removeGroup " + group.getId(), e);
161 throw new PAPException("Failed to remove group '" + group.getId()+ "'", e);
165 public void removePDP(OnapPDP pdp, OnapPDPGroup group) throws PAPException {
166 if (LOGGER.isTraceEnabled()) {
167 LOGGER.trace("removePDP: " + pdp + " from group: " + group);
170 this.papEngine.removePDP(pdp);
171 } catch (PAPException e) {
172 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to removePDP " + pdp.getId(), e);
173 throw new PAPException("Failed to remove pdp '" + pdp.getId()+ "'", e);
177 public void updatePDP(OnapPDP pdp) {
179 papEngine.updatePDP(pdp);
180 } catch (PAPException e) {
181 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
185 public void updateGroup(OnapPDPGroup group) {
187 papEngine.updateGroup(group);
188 } catch (PAPException e) {
189 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
194 public Collection<?> getContainerPropertyIds() {
195 return pDPProperties;
199 public Collection<?> getItemIds() {
200 final Collection<Object> items = new ArrayList<>();
201 items.addAll(this.groups);
202 if (LOGGER.isTraceEnabled()) {
203 LOGGER.trace("getItemIds: " + items);
205 return Collections.unmodifiableCollection(items);
209 public Class<?> getType(Object propertyId) {
210 if (propertyId.equals(PROPERTY_ID)) {
213 if (propertyId.equals(PROPERTY_NAME)) {
216 if (propertyId.equals(PROPERTY_DESCRIPTION)) {
219 if (propertyId.equals(PROPERTY_DEFAULT)) {
220 return Boolean.class;
222 if (propertyId.equals(PROPERTY_STATUS)) {
225 if (propertyId.equals(PROPERTY_PDPS)) {
228 if (propertyId.equals(PROPERTY_POLICIES)) {
231 if (propertyId.equals(PROPERTY_PIPCONFIG)) {
234 if (propertyId.equals(PROPERTY_SELECTED)) {
235 return Checkbox.class;
242 return this.groups.size();
246 public boolean containsId(Object itemId) {
247 if (LOGGER.isTraceEnabled()) {
248 LOGGER.trace("containsId: " + itemId);
250 if (! this.isSupported(itemId)) {
253 return this.groups.contains(itemId);
257 public Object addItem() {
258 throw new UnsupportedOperationException("PDP Container cannot add a given item.");
261 public void addNewGroup(String name, String description) throws PAPException {
262 if (LOGGER.isTraceEnabled()) {
263 LOGGER.trace("addNewGroup " + name + " " + description);
265 this.papEngine.newGroup(name, description);
268 public void addNewPDP(String id, OnapPDPGroup group, String name, String description, int jmxport) throws PAPException {
269 if (LOGGER.isTraceEnabled()) {
270 LOGGER.trace("addNewPDP " + id + " " + name + " " + description + " " + jmxport);
272 this.papEngine.newPDP(id, group, name, description, jmxport);
275 public void movePDP(OnapPDP pdp, OnapPDPGroup group) {
277 this.papEngine.movePDP(pdp, group);
278 } catch (PAPException e) {
279 String message = "Unable to move PDP to new group on server: " + e;
280 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
286 public boolean addContainerProperty(Object propertyId, Class<?> type, Object defaultValue) {
287 throw new UnsupportedOperationException("Cannot add a container property.");
291 public boolean removeContainerProperty(Object propertyId) {
292 throw new UnsupportedOperationException("Cannot remove a container property.");
296 public boolean removeAllItems() {
297 throw new UnsupportedOperationException("PDP Container cannot remove all items. You must have at least the Default group.");
301 public void addItemSetChangeListener(ItemSetChangeListener listener) {
302 if (getItemSetChangeListeners() == null) {
303 setItemSetChangeListeners(new LinkedList<PolicyContainer.ItemSetChangeListener>());
305 getItemSetChangeListeners().add(listener);
309 public Object nextItemId(Object itemId) {
310 if (! this.isSupported(itemId)) {
313 int index = this.groups.indexOf(itemId);
316 // We don't know this group
321 // Is it the last one?
323 if (index == this.groups.size() - 1) {
330 // Return the next one
332 return this.groups.get(index + 1);
336 public Object prevItemId(Object itemId) {
337 if (! this.isSupported(itemId)) {
340 int index = this.groups.indexOf(itemId);
343 // We don't know this group
348 // Is it the first one?
357 // Return the previous one
359 return this.groups.get(index - 1);
363 public Object firstItemId() {
364 synchronized (this.groups) {
365 if (!this.groups.isEmpty()) {
366 return this.groups.get(0);
373 public Object lastItemId() {
374 synchronized (this.groups) {
375 if (!this.groups.isEmpty()) {
376 return this.groups.get(this.groups.size() - 1);
383 public boolean isFirstId(Object itemId) {
384 synchronized (this.groups) {
385 if (!this.groups.isEmpty()) {
386 return this.groups.get(0).equals(itemId);
393 public boolean isLastId(Object itemId) {
394 synchronized (this.groups) {
395 if (!this.groups.isEmpty()) {
396 return this.groups.get(this.groups.size() - 1).equals(itemId);
403 public Object addItemAfter(Object previousItemId) {
404 throw new UnsupportedOperationException("Cannot addItemAfter, there really is no real ordering.");
408 public int indexOfId(Object itemId) {
409 return this.groups.indexOf(itemId);
413 public Object getIdByIndex(int index) {
414 return this.groups.get(index);
418 public List<?> getItemIds(int startIndex, int numberOfItems) {
419 synchronized (this.groups) {
420 int endIndex = startIndex + numberOfItems;
421 if (endIndex > this.groups.size()) {
422 endIndex = this.groups.size() - 1;
424 return this.groups.subList(startIndex, endIndex);
429 public Object addItemAt(int index) {
430 throw new UnsupportedOperationException("Cannot addItemAt");
434 public boolean removeItem(Object itemId) {
435 if (LOGGER.isTraceEnabled()) {
436 LOGGER.trace("removeItem: " + itemId);
438 if (! this.isSupported(itemId)) {
442 // You cannot remove the default group
444 if (PROPERTY_DEFAULT.equals(((OnapPDPGroup) itemId).getId())) {
445 throw new UnsupportedOperationException("You can't remove the Default Group.");
448 // Remove PDPGroup and move any PDP's in it into the default group
451 this.papEngine.removeGroup((OnapPDPGroup) itemId, this.papEngine.getDefaultGroup());
453 } catch (NullPointerException | PAPException e) {
454 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to remove group", e);
459 public class PDPGroupItem{
460 private final OnapPDPGroup group;
462 public PDPGroupItem(OnapPDPGroup itemId) {
466 public String getId() {
467 if (LOGGER.isTraceEnabled()) {
468 LOGGER.trace("getId: " + this.group);
470 return this.group.getId();
473 public String getName() {
474 if (LOGGER.isTraceEnabled()) {
475 LOGGER.trace("getName: " + this.group);
477 return this.group.getName();
480 public String getDescription() {
481 if (LOGGER.isTraceEnabled()) {
482 LOGGER.trace("getDescription: " + this.group);
484 return this.group.getDescription();
487 public Boolean getDefault() {
488 if (LOGGER.isTraceEnabled()) {
489 LOGGER.trace("getDefault: " + this.group);
491 return this.group.isDefaultGroup();
495 public String getStatus() {
496 return this.group.getStatus().getStatus().toString();
499 public Set<PDP> getPDPs() {
500 return Collections.unmodifiableSet(this.group.getPdps());
503 public Set<PDPPolicy> getPolicies() {
504 if (LOGGER.isTraceEnabled()) {
505 LOGGER.trace("getPolicies: " + this.group);
507 return this.group.getPolicies();
510 public Set<PDPPIPConfig> getPipConfigs() {
511 if (LOGGER.isTraceEnabled()) {
512 LOGGER.trace("getPIPConfigs: " + this.group);
514 return this.group.getPipConfigs();