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.openecomp.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.openecomp.policy.common.logging.flexlogger.FlexLogger;
33 import org.openecomp.policy.common.logging.flexlogger.Logger;
34 import org.openecomp.policy.utils.PolicyContainer;
35 import org.openecomp.policy.utils.PolicyItemSetChangeNotifier;
36 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
37 import org.openecomp.policy.xacml.api.pap.EcompPDP;
38 import org.openecomp.policy.xacml.api.pap.EcompPDPGroup;
39 import org.openecomp.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.PDP;
44 //import com.att.research.xacml.api.pap.PDPGroup;
45 import com.att.research.xacml.api.pap.PDPPIPConfig;
46 import com.att.research.xacml.api.pap.PDPPolicy;
48 public class PDPGroupContainer extends PolicyItemSetChangeNotifier implements PolicyContainer.Indexed, PolicyContainer.ItemSetChangeNotifier {
49 private static final long serialVersionUID = 1L;
50 private static Logger LOGGER = FlexLogger.getLogger(PDPGroupContainer.class);
53 * String identifier of a file's "Id" property.
55 private static String PROPERTY_ID = "Id";
58 * String identifier of a file's "name" property.
60 private static String PROPERTY_NAME = "Name";
63 * String identifier of a file's "Description" property.
65 private static String PROPERTY_DESCRIPTION = "Description";
68 * String identifier of a file's "Default" property.
70 private static String PROPERTY_DEFAULT = "Default";
72 * String identifier of a file's "Status" property.
74 private static String PROPERTY_STATUS = "Status";
77 * String identifier of a file's "PDPs" property.
79 private static String PROPERTY_PDPS = "PDPs";
82 * String identifier of a file's "Policies" property.
84 private static String PROPERTY_POLICIES = "Policies";
87 * String identifier of a file's "PIP Configurations" property.
89 private static String PROPERTY_PIPCONFIG = "PIP Configurations";
92 * String identifier of a file's "Selected" property.
94 private static String PROPERTY_SELECTED = "Selected";
97 * List of the string identifiers for the available properties.
99 private static Collection<String> PDP_PROPERTIES;
101 private PAPPolicyEngine papEngine = null;
102 protected List<EcompPDPGroup> groups = Collections.synchronizedList(new ArrayList<EcompPDPGroup>());
104 public PDPGroupContainer(PAPPolicyEngine papPolicyEngine) {
106 this.setContainer(this);
110 this.papEngine = (PAPPolicyEngine) papPolicyEngine;
114 this.refreshGroups();
117 public boolean isSupported(Object itemId) {
118 if (itemId instanceof EcompPDPGroup) {
124 public synchronized void refreshGroups() {
125 synchronized(this.groups) {
128 this.groups.addAll(this.papEngine.getEcompPDPGroups());
129 } catch (PAPException e) {
130 String message = "Unable to retrieve Groups from server: " + e;
131 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
133 LOGGER.info("refreshGroups");
136 // Notify that we have changed
138 this.fireItemSetChange();
141 public List<EcompPDPGroup> getGroups() {
142 return Collections.unmodifiableList(this.groups);
145 public void makeDefault(EcompPDPGroup 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);
155 public void removeGroup(EcompPDPGroup group, EcompPDPGroup newGroup) throws PAPException {
156 if (LOGGER.isTraceEnabled()) {
157 LOGGER.trace("removeGroup: " + group + " new group for PDPs: " + newGroup);
159 if (group.isDefaultGroup()) {
160 throw new UnsupportedOperationException("You can't remove the Default Group.");
163 this.papEngine.removeGroup(group, newGroup);
164 } catch (NullPointerException | PAPException e) {
165 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to removeGroup " + group.getId(), e);
166 throw new PAPException("Failed to remove group '" + group.getId()+ "'", e);
170 public void removePDP(EcompPDP pdp, EcompPDPGroup group) throws PAPException {
171 if (LOGGER.isTraceEnabled()) {
172 LOGGER.trace("removePDP: " + pdp + " from group: " + group);
175 this.papEngine.removePDP(pdp);
176 } catch (PAPException e) {
177 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to removePDP " + pdp.getId(), e);
178 throw new PAPException("Failed to remove pdp '" + pdp.getId()+ "'", e);
182 public void updatePDP(EcompPDP pdp) {
184 papEngine.updatePDP(pdp);
185 } catch (PAPException e) {
186 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
190 public void updateGroup(EcompPDPGroup group) {
192 papEngine.updateGroup(group);
193 } catch (PAPException e) {
194 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
199 public Collection<?> getContainerPropertyIds() {
200 return PDP_PROPERTIES;
204 public Collection<?> getItemIds() {
205 final Collection<Object> items = new ArrayList<>();
206 items.addAll(this.groups);
207 if (LOGGER.isTraceEnabled()) {
208 LOGGER.trace("getItemIds: " + items);
210 return Collections.unmodifiableCollection(items);
214 public Class<?> getType(Object propertyId) {
215 if (propertyId.equals(PROPERTY_ID)) {
218 if (propertyId.equals(PROPERTY_NAME)) {
221 if (propertyId.equals(PROPERTY_DESCRIPTION)) {
224 if (propertyId.equals(PROPERTY_DEFAULT)) {
225 return Boolean.class;
227 if (propertyId.equals(PROPERTY_STATUS)) {
230 if (propertyId.equals(PROPERTY_PDPS)) {
233 if (propertyId.equals(PROPERTY_POLICIES)) {
236 if (propertyId.equals(PROPERTY_PIPCONFIG)) {
239 if (propertyId.equals(PROPERTY_SELECTED)) {
240 return Checkbox.class;
247 return this.groups.size();
251 public boolean containsId(Object itemId) {
252 if (LOGGER.isTraceEnabled()) {
253 LOGGER.trace("containsId: " + itemId);
255 if (this.isSupported(itemId) == false) {
258 return this.groups.contains(itemId);
262 public Object addItem() throws UnsupportedOperationException {
263 throw new UnsupportedOperationException("PDP Container cannot add a given item.");
266 public void addNewGroup(String name, String description) throws NullPointerException, PAPException {
267 if (LOGGER.isTraceEnabled()) {
268 LOGGER.trace("addNewGroup " + name + " " + description);
270 this.papEngine.newGroup(name, description);
273 public void addNewPDP(String id, EcompPDPGroup group, String name, String description, int jmxport) throws NullPointerException, PAPException {
274 if (LOGGER.isTraceEnabled()) {
275 LOGGER.trace("addNewPDP " + id + " " + name + " " + description + " " + jmxport);
277 this.papEngine.newPDP(id, group, name, description, jmxport);
280 public void movePDP(EcompPDP pdp, EcompPDPGroup group) {
282 this.papEngine.movePDP(pdp, group);
283 } catch (PAPException e) {
284 String message = "Unable to move PDP to new group on server: " + e;
285 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
291 public boolean addContainerProperty(Object propertyId, Class<?> type, Object defaultValue) throws UnsupportedOperationException {
292 throw new UnsupportedOperationException("Cannot add a container property.");
296 public boolean removeContainerProperty(Object propertyId) throws UnsupportedOperationException {
297 throw new UnsupportedOperationException("Cannot remove a container property.");
301 public boolean removeAllItems() throws UnsupportedOperationException {
302 throw new UnsupportedOperationException("PDP Container cannot remove all items. You must have at least the Default group.");
306 public void addItemSetChangeListener(ItemSetChangeListener listener) {
307 if (getItemSetChangeListeners() == null) {
308 setItemSetChangeListeners(new LinkedList<PolicyContainer.ItemSetChangeListener>());
310 getItemSetChangeListeners().add(listener);
314 public Object nextItemId(Object itemId) {
315 if (this.isSupported(itemId) == false) {
318 int index = this.groups.indexOf(itemId);
321 // We don't know this group
326 // Is it the last one?
328 if (index == this.groups.size() - 1) {
335 // Return the next one
337 return this.groups.get(index + 1);
341 public Object prevItemId(Object itemId) {
342 if (this.isSupported(itemId) == false) {
345 int index = this.groups.indexOf(itemId);
348 // We don't know this group
353 // Is it the first one?
362 // Return the previous one
364 return this.groups.get(index - 1);
368 public Object firstItemId() {
369 synchronized (this.groups) {
370 if (this.groups.size() > 0) {
371 return this.groups.get(0);
378 public Object lastItemId() {
379 synchronized (this.groups) {
380 if (this.groups.size() > 0) {
381 return this.groups.get(this.groups.size() - 1);
388 public boolean isFirstId(Object itemId) {
389 synchronized (this.groups) {
390 if (this.groups.size() > 0) {
391 return (this.groups.get(0).equals(itemId));
398 public boolean isLastId(Object itemId) {
399 synchronized (this.groups) {
400 if (this.groups.size() > 0) {
401 return (this.groups.get(this.groups.size() - 1).equals(itemId));
408 public Object addItemAfter(Object previousItemId) throws UnsupportedOperationException {
409 throw new UnsupportedOperationException("Cannot addItemAfter, there really is no real ordering.");
413 public int indexOfId(Object itemId) {
414 return this.groups.indexOf(itemId);
418 public Object getIdByIndex(int index) {
419 return this.groups.get(index);
423 public List<?> getItemIds(int startIndex, int numberOfItems) {
424 synchronized (this.groups) {
425 int endIndex = startIndex + numberOfItems;
426 if (endIndex > this.groups.size()) {
427 endIndex = this.groups.size() - 1;
429 return this.groups.subList(startIndex, endIndex);
434 public Object addItemAt(int index) throws UnsupportedOperationException {
435 throw new UnsupportedOperationException("Cannot addItemAt");
439 public boolean removeItem(Object itemId) throws UnsupportedOperationException {
440 if (LOGGER.isTraceEnabled()) {
441 LOGGER.trace("removeItem: " + itemId);
443 if (this.isSupported(itemId) == false) {
447 // You cannot remove the default group
449 if (((EcompPDPGroup) itemId).getId().equals("Default")) {
450 throw new UnsupportedOperationException("You can't remove the Default Group.");
453 // Remove PDPGroup and move any PDP's in it into the default group
456 this.papEngine.removeGroup((EcompPDPGroup) itemId, this.papEngine.getDefaultGroup());
458 } catch (NullPointerException | PAPException e) {
459 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to remove group", e);
464 public class PDPGroupItem{
465 private final EcompPDPGroup group;
467 public PDPGroupItem(EcompPDPGroup itemId) {
471 public String getId() {
472 if (LOGGER.isTraceEnabled()) {
473 LOGGER.trace("getId: " + this.group);
475 return this.group.getId();
478 public String getName() {
479 if (LOGGER.isTraceEnabled()) {
480 LOGGER.trace("getName: " + this.group);
482 return this.group.getName();
485 public String getDescription() {
486 if (LOGGER.isTraceEnabled()) {
487 LOGGER.trace("getDescription: " + this.group);
489 return this.group.getDescription();
492 public Boolean getDefault() {
493 if (LOGGER.isTraceEnabled()) {
494 LOGGER.trace("getDefault: " + this.group);
496 return this.group.isDefaultGroup();
500 public String getStatus() {
501 return this.group.getStatus().getStatus().toString();
504 public Set<PDP> getPDPs() {
505 return Collections.unmodifiableSet(this.group.getPdps());
508 public Set<PDPPolicy> getPolicies() {
509 if (LOGGER.isTraceEnabled()) {
510 LOGGER.trace("getPolicies: " + this.group);
512 return this.group.getPolicies();
515 public Set<PDPPIPConfig> getPipConfigs() {
516 if (LOGGER.isTraceEnabled()) {
517 LOGGER.trace("getPIPConfigs: " + this.group);
519 return this.group.getPipConfigs();