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.utils.PolicyContainer;
33 import org.openecomp.policy.utils.PolicyItemSetChangeNotifier;
35 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
36 import org.openecomp.policy.xacml.api.pap.EcompPDP;
37 import org.openecomp.policy.xacml.api.pap.EcompPDPGroup;
38 import org.openecomp.policy.xacml.api.pap.PAPPolicyEngine;
40 import com.att.research.xacml.api.pap.PAPEngine;
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 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
49 import org.openecomp.policy.common.logging.flexlogger.Logger;
51 public class PDPGroupContainer extends PolicyItemSetChangeNotifier implements PolicyContainer.Indexed, PolicyContainer.ItemSetChangeNotifier {
52 private static final long serialVersionUID = 1L;
53 private static Logger logger = FlexLogger.getLogger(PDPGroupContainer.class);
56 * String identifier of a file's "Id" property.
58 public static String PROPERTY_ID = "Id";
61 * String identifier of a file's "name" property.
63 public static String PROPERTY_NAME = "Name";
66 * String identifier of a file's "Description" property.
68 public static String PROPERTY_DESCRIPTION = "Description";
71 * String identifier of a file's "Default" property.
73 public static String PROPERTY_DEFAULT = "Default";
76 * String identifier of a file's "icon" property.
78 public static String PROPERTY_ICON = "Icon";
81 * String identifier of a file's "Status" property.
83 public static String PROPERTY_STATUS = "Status";
86 * String identifier of a file's "PDPs" property.
88 public static String PROPERTY_PDPS = "PDPs";
91 * String identifier of a file's "Policies" property.
93 public static String PROPERTY_POLICIES = "Policies";
96 * String identifier of a file's "PIP Configurations" property.
98 public static String PROPERTY_PIPCONFIG = "PIP Configurations";
101 * String identifier of a file's "Selected" property.
103 public static String PROPERTY_SELECTED = "Selected";
106 * List of the string identifiers for the available properties.
108 public static Collection<String> PDP_PROPERTIES;
110 private PAPPolicyEngine papEngine = null;
111 protected List<EcompPDPGroup> groups = Collections.synchronizedList(new ArrayList<EcompPDPGroup>());
113 public PDPGroupContainer(PAPPolicyEngine papPolicyEngine) {
115 this.setContainer(this);
119 this.papEngine = (PAPPolicyEngine) papPolicyEngine;
123 this.refreshGroups();
126 public boolean isSupported(Object itemId) {
127 if (itemId instanceof EcompPDPGroup) {
133 public synchronized void refreshGroups() {
134 synchronized(this.groups) {
137 this.groups.addAll(this.papEngine.getEcompPDPGroups());
138 } catch (PAPException e) {
139 String message = "Unable to retrieve Groups from server: " + e;
140 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
142 logger.info("refreshGroups");
145 // Notify that we have changed
147 this.fireItemSetChange();
150 public List<EcompPDPGroup> getGroups() {
151 return Collections.unmodifiableList(this.groups);
154 public void makeDefault(EcompPDPGroup group) {
156 this.papEngine.SetDefaultGroup(group);
157 } catch (PAPException e) {
158 String message = "Unable to set Default Group on server: " + e;
159 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
164 public void removeGroup(EcompPDPGroup group, EcompPDPGroup newGroup) throws PAPException {
165 if (logger.isTraceEnabled()) {
166 logger.trace("removeGroup: " + group + " new group for PDPs: " + newGroup);
168 if (group.isDefaultGroup()) {
169 throw new UnsupportedOperationException("You can't remove the Default Group.");
172 this.papEngine.removeGroup(group, newGroup);
173 } catch (NullPointerException | PAPException e) {
174 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to removeGroup " + group.getId(), e);
175 throw new PAPException("Failed to remove group '" + group.getId()+ "'", e);
179 public void removePDP(EcompPDP pdp, EcompPDPGroup group) throws PAPException {
180 if (logger.isTraceEnabled()) {
181 logger.trace("removePDP: " + pdp + " from group: " + group);
184 this.papEngine.removePDP(pdp);
185 } catch (PAPException e) {
186 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to removePDP " + pdp.getId(), e);
187 throw new PAPException("Failed to remove pdp '" + pdp.getId()+ "'", e);
191 public void updatePDP(EcompPDP pdp) {
193 papEngine.updatePDP(pdp);
194 } catch (PAPException e) {
195 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
199 public void updateGroup(EcompPDPGroup group) {
201 papEngine.updateGroup(group);
202 } catch (PAPException e) {
203 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
208 public Collection<?> getContainerPropertyIds() {
209 return PDP_PROPERTIES;
213 public Collection<?> getItemIds() {
214 final Collection<Object> items = new ArrayList<Object>();
215 items.addAll(this.groups);
216 if (logger.isTraceEnabled()) {
217 logger.trace("getItemIds: " + items);
219 return Collections.unmodifiableCollection(items);
223 public Class<?> getType(Object propertyId) {
224 if (propertyId.equals(PROPERTY_ID)) {
227 if (propertyId.equals(PROPERTY_NAME)) {
230 if (propertyId.equals(PROPERTY_DESCRIPTION)) {
233 if (propertyId.equals(PROPERTY_DEFAULT)) {
234 return Boolean.class;
236 if (propertyId.equals(PROPERTY_STATUS)) {
239 if (propertyId.equals(PROPERTY_PDPS)) {
242 if (propertyId.equals(PROPERTY_POLICIES)) {
245 if (propertyId.equals(PROPERTY_PIPCONFIG)) {
248 if (propertyId.equals(PROPERTY_SELECTED)) {
249 return Checkbox.class;
256 return this.groups.size();
260 public boolean containsId(Object itemId) {
261 if (logger.isTraceEnabled()) {
262 logger.trace("containsId: " + itemId);
264 if (this.isSupported(itemId) == false) {
267 return this.groups.contains(itemId);
271 public Object addItem() throws UnsupportedOperationException {
272 throw new UnsupportedOperationException("PDP Container cannot add a given item.");
275 public void addNewGroup(String name, String description) throws NullPointerException, PAPException {
276 if (logger.isTraceEnabled()) {
277 logger.trace("addNewGroup " + name + " " + description);
279 this.papEngine.newGroup(name, description);
282 public void addNewPDP(String id, EcompPDPGroup group, String name, String description, int jmxport) throws NullPointerException, PAPException {
283 if (logger.isTraceEnabled()) {
284 logger.trace("addNewPDP " + id + " " + name + " " + description + " " + jmxport);
286 this.papEngine.newPDP(id, group, name, description, jmxport);
289 public void movePDP(EcompPDP pdp, EcompPDPGroup group) {
291 this.papEngine.movePDP(pdp, group);
292 } catch (PAPException e) {
293 String message = "Unable to move PDP to new group on server: " + e;
294 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
300 public boolean addContainerProperty(Object propertyId, Class<?> type, Object defaultValue) throws UnsupportedOperationException {
301 throw new UnsupportedOperationException("Cannot add a container property.");
305 public boolean removeContainerProperty(Object propertyId) throws UnsupportedOperationException {
306 throw new UnsupportedOperationException("Cannot remove a container property.");
310 public boolean removeAllItems() throws UnsupportedOperationException {
311 throw new UnsupportedOperationException("PDP Container cannot remove all items. You must have at least the Default group.");
315 public void addItemSetChangeListener(ItemSetChangeListener listener) {
316 if (getItemSetChangeListeners() == null) {
317 setItemSetChangeListeners(new LinkedList<PolicyContainer.ItemSetChangeListener>());
319 getItemSetChangeListeners().add(listener);
323 public Object nextItemId(Object itemId) {
324 if (this.isSupported(itemId) == false) {
327 int index = this.groups.indexOf(itemId);
330 // We don't know this group
335 // Is it the last one?
337 if (index == this.groups.size() - 1) {
344 // Return the next one
346 return this.groups.get(index + 1);
350 public Object prevItemId(Object itemId) {
351 if (this.isSupported(itemId) == false) {
354 int index = this.groups.indexOf(itemId);
357 // We don't know this group
362 // Is it the first one?
371 // Return the previous one
373 return this.groups.get(index - 1);
377 public Object firstItemId() {
378 synchronized (this.groups) {
379 if (this.groups.size() > 0) {
380 return this.groups.get(0);
387 public Object lastItemId() {
388 synchronized (this.groups) {
389 if (this.groups.size() > 0) {
390 return this.groups.get(this.groups.size() - 1);
397 public boolean isFirstId(Object itemId) {
398 synchronized (this.groups) {
399 if (this.groups.size() > 0) {
400 return (this.groups.get(0).equals(itemId));
407 public boolean isLastId(Object itemId) {
408 synchronized (this.groups) {
409 if (this.groups.size() > 0) {
410 return (this.groups.get(this.groups.size() - 1).equals(itemId));
417 public Object addItemAfter(Object previousItemId) throws UnsupportedOperationException {
418 throw new UnsupportedOperationException("Cannot addItemAfter, there really is no real ordering.");
422 public int indexOfId(Object itemId) {
423 return this.groups.indexOf(itemId);
427 public Object getIdByIndex(int index) {
428 return this.groups.get(index);
432 public List<?> getItemIds(int startIndex, int numberOfItems) {
433 synchronized (this.groups) {
434 int endIndex = startIndex + numberOfItems;
435 if (endIndex > this.groups.size()) {
436 endIndex = this.groups.size() - 1;
438 return this.groups.subList(startIndex, endIndex);
443 public Object addItemAt(int index) throws UnsupportedOperationException {
444 throw new UnsupportedOperationException("Cannot addItemAt");
448 public boolean removeItem(Object itemId) throws UnsupportedOperationException {
449 if (logger.isTraceEnabled()) {
450 logger.trace("removeItem: " + itemId);
452 if (this.isSupported(itemId) == false) {
456 // You cannot remove the default group
458 if (((EcompPDPGroup) itemId).getId().equals("Default")) {
459 throw new UnsupportedOperationException("You can't remove the Default Group.");
462 // Remove PDPGroup and move any PDP's in it into the default group
465 this.papEngine.removeGroup((EcompPDPGroup) itemId, this.papEngine.getDefaultGroup());
467 } catch (NullPointerException | PAPException e) {
468 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to remove group", e);
473 public class PDPGroupItem{
474 private final EcompPDPGroup group;
476 public PDPGroupItem(EcompPDPGroup itemId) {
480 public String getId() {
481 if (logger.isTraceEnabled()) {
482 logger.trace("getId: " + this.group);
484 return this.group.getId();
487 public String getName() {
488 if (logger.isTraceEnabled()) {
489 logger.trace("getName: " + this.group);
491 return this.group.getName();
494 public String getDescription() {
495 if (logger.isTraceEnabled()) {
496 logger.trace("getDescription: " + this.group);
498 return this.group.getDescription();
501 public Boolean getDefault() {
502 if (logger.isTraceEnabled()) {
503 logger.trace("getDefault: " + this.group);
505 return this.group.isDefaultGroup();
509 public String getStatus() {
510 return this.group.getStatus().getStatus().toString();
513 public Set<PDP> getPDPs() {
514 return Collections.unmodifiableSet(this.group.getPdps());
517 public Set<PDPPolicy> getPolicies() {
518 if (logger.isTraceEnabled()) {
519 logger.trace("getPolicies: " + this.group);
521 return this.group.getPolicies();
524 public Set<PDPPIPConfig> getPipConfigs() {
525 if (logger.isTraceEnabled()) {
526 logger.trace("getPIPConfigs: " + this.group);
528 return this.group.getPipConfigs();