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 public static String PROPERTY_ID = "Id";
58 * String identifier of a file's "name" property.
60 public static String PROPERTY_NAME = "Name";
63 * String identifier of a file's "Description" property.
65 public static String PROPERTY_DESCRIPTION = "Description";
68 * String identifier of a file's "Default" property.
70 public static String PROPERTY_DEFAULT = "Default";
73 * String identifier of a file's "icon" property.
75 public static String PROPERTY_ICON = "Icon";
78 * String identifier of a file's "Status" property.
80 public static String PROPERTY_STATUS = "Status";
83 * String identifier of a file's "PDPs" property.
85 public static String PROPERTY_PDPS = "PDPs";
88 * String identifier of a file's "Policies" property.
90 public static String PROPERTY_POLICIES = "Policies";
93 * String identifier of a file's "PIP Configurations" property.
95 public static String PROPERTY_PIPCONFIG = "PIP Configurations";
98 * String identifier of a file's "Selected" property.
100 public static String PROPERTY_SELECTED = "Selected";
103 * List of the string identifiers for the available properties.
105 public static Collection<String> PDP_PROPERTIES;
107 private PAPPolicyEngine papEngine = null;
108 protected List<EcompPDPGroup> groups = Collections.synchronizedList(new ArrayList<EcompPDPGroup>());
110 public PDPGroupContainer(PAPPolicyEngine papPolicyEngine) {
112 this.setContainer(this);
116 this.papEngine = (PAPPolicyEngine) papPolicyEngine;
120 this.refreshGroups();
123 public boolean isSupported(Object itemId) {
124 if (itemId instanceof EcompPDPGroup) {
130 public synchronized void refreshGroups() {
131 synchronized(this.groups) {
134 this.groups.addAll(this.papEngine.getEcompPDPGroups());
135 } catch (PAPException e) {
136 String message = "Unable to retrieve Groups from server: " + e;
137 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
139 LOGGER.info("refreshGroups");
142 // Notify that we have changed
144 this.fireItemSetChange();
147 public List<EcompPDPGroup> getGroups() {
148 return Collections.unmodifiableList(this.groups);
151 public void makeDefault(EcompPDPGroup group) {
153 this.papEngine.SetDefaultGroup(group);
154 } catch (PAPException e) {
155 String message = "Unable to set Default Group on server: " + e;
156 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
161 public void removeGroup(EcompPDPGroup group, EcompPDPGroup newGroup) throws PAPException {
162 if (LOGGER.isTraceEnabled()) {
163 LOGGER.trace("removeGroup: " + group + " new group for PDPs: " + newGroup);
165 if (group.isDefaultGroup()) {
166 throw new UnsupportedOperationException("You can't remove the Default Group.");
169 this.papEngine.removeGroup(group, newGroup);
170 } catch (NullPointerException | PAPException e) {
171 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to removeGroup " + group.getId(), e);
172 throw new PAPException("Failed to remove group '" + group.getId()+ "'", e);
176 public void removePDP(EcompPDP pdp, EcompPDPGroup group) throws PAPException {
177 if (LOGGER.isTraceEnabled()) {
178 LOGGER.trace("removePDP: " + pdp + " from group: " + group);
181 this.papEngine.removePDP(pdp);
182 } catch (PAPException e) {
183 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to removePDP " + pdp.getId(), e);
184 throw new PAPException("Failed to remove pdp '" + pdp.getId()+ "'", e);
188 public void updatePDP(EcompPDP pdp) {
190 papEngine.updatePDP(pdp);
191 } catch (PAPException e) {
192 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
196 public void updateGroup(EcompPDPGroup group) {
198 papEngine.updateGroup(group);
199 } catch (PAPException e) {
200 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
205 public Collection<?> getContainerPropertyIds() {
206 return PDP_PROPERTIES;
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) == false) {
264 return this.groups.contains(itemId);
268 public Object addItem() throws UnsupportedOperationException {
269 throw new UnsupportedOperationException("PDP Container cannot add a given item.");
272 public void addNewGroup(String name, String description) throws NullPointerException, PAPException {
273 if (LOGGER.isTraceEnabled()) {
274 LOGGER.trace("addNewGroup " + name + " " + description);
276 this.papEngine.newGroup(name, description);
279 public void addNewPDP(String id, EcompPDPGroup group, String name, String description, int jmxport) throws NullPointerException, PAPException {
280 if (LOGGER.isTraceEnabled()) {
281 LOGGER.trace("addNewPDP " + id + " " + name + " " + description + " " + jmxport);
283 this.papEngine.newPDP(id, group, name, description, jmxport);
286 public void movePDP(EcompPDP pdp, EcompPDPGroup group) {
288 this.papEngine.movePDP(pdp, group);
289 } catch (PAPException e) {
290 String message = "Unable to move PDP to new group on server: " + e;
291 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
297 public boolean addContainerProperty(Object propertyId, Class<?> type, Object defaultValue) throws UnsupportedOperationException {
298 throw new UnsupportedOperationException("Cannot add a container property.");
302 public boolean removeContainerProperty(Object propertyId) throws UnsupportedOperationException {
303 throw new UnsupportedOperationException("Cannot remove a container property.");
307 public boolean removeAllItems() throws UnsupportedOperationException {
308 throw new UnsupportedOperationException("PDP Container cannot remove all items. You must have at least the Default group.");
312 public void addItemSetChangeListener(ItemSetChangeListener listener) {
313 if (getItemSetChangeListeners() == null) {
314 setItemSetChangeListeners(new LinkedList<PolicyContainer.ItemSetChangeListener>());
316 getItemSetChangeListeners().add(listener);
320 public Object nextItemId(Object itemId) {
321 if (this.isSupported(itemId) == false) {
324 int index = this.groups.indexOf(itemId);
327 // We don't know this group
332 // Is it the last one?
334 if (index == this.groups.size() - 1) {
341 // Return the next one
343 return this.groups.get(index + 1);
347 public Object prevItemId(Object itemId) {
348 if (this.isSupported(itemId) == false) {
351 int index = this.groups.indexOf(itemId);
354 // We don't know this group
359 // Is it the first one?
368 // Return the previous one
370 return this.groups.get(index - 1);
374 public Object firstItemId() {
375 synchronized (this.groups) {
376 if (this.groups.size() > 0) {
377 return this.groups.get(0);
384 public Object lastItemId() {
385 synchronized (this.groups) {
386 if (this.groups.size() > 0) {
387 return this.groups.get(this.groups.size() - 1);
394 public boolean isFirstId(Object itemId) {
395 synchronized (this.groups) {
396 if (this.groups.size() > 0) {
397 return (this.groups.get(0).equals(itemId));
404 public boolean isLastId(Object itemId) {
405 synchronized (this.groups) {
406 if (this.groups.size() > 0) {
407 return (this.groups.get(this.groups.size() - 1).equals(itemId));
414 public Object addItemAfter(Object previousItemId) throws UnsupportedOperationException {
415 throw new UnsupportedOperationException("Cannot addItemAfter, there really is no real ordering.");
419 public int indexOfId(Object itemId) {
420 return this.groups.indexOf(itemId);
424 public Object getIdByIndex(int index) {
425 return this.groups.get(index);
429 public List<?> getItemIds(int startIndex, int numberOfItems) {
430 synchronized (this.groups) {
431 int endIndex = startIndex + numberOfItems;
432 if (endIndex > this.groups.size()) {
433 endIndex = this.groups.size() - 1;
435 return this.groups.subList(startIndex, endIndex);
440 public Object addItemAt(int index) throws UnsupportedOperationException {
441 throw new UnsupportedOperationException("Cannot addItemAt");
445 public boolean removeItem(Object itemId) throws UnsupportedOperationException {
446 if (LOGGER.isTraceEnabled()) {
447 LOGGER.trace("removeItem: " + itemId);
449 if (this.isSupported(itemId) == false) {
453 // You cannot remove the default group
455 if (((EcompPDPGroup) itemId).getId().equals("Default")) {
456 throw new UnsupportedOperationException("You can't remove the Default Group.");
459 // Remove PDPGroup and move any PDP's in it into the default group
462 this.papEngine.removeGroup((EcompPDPGroup) itemId, this.papEngine.getDefaultGroup());
464 } catch (NullPointerException | PAPException e) {
465 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to remove group", e);
470 public class PDPGroupItem{
471 private final EcompPDPGroup group;
473 public PDPGroupItem(EcompPDPGroup itemId) {
477 public String getId() {
478 if (LOGGER.isTraceEnabled()) {
479 LOGGER.trace("getId: " + this.group);
481 return this.group.getId();
484 public String getName() {
485 if (LOGGER.isTraceEnabled()) {
486 LOGGER.trace("getName: " + this.group);
488 return this.group.getName();
491 public String getDescription() {
492 if (LOGGER.isTraceEnabled()) {
493 LOGGER.trace("getDescription: " + this.group);
495 return this.group.getDescription();
498 public Boolean getDefault() {
499 if (LOGGER.isTraceEnabled()) {
500 LOGGER.trace("getDefault: " + this.group);
502 return this.group.isDefaultGroup();
506 public String getStatus() {
507 return this.group.getStatus().getStatus().toString();
510 public Set<PDP> getPDPs() {
511 return Collections.unmodifiableSet(this.group.getPdps());
514 public Set<PDPPolicy> getPolicies() {
515 if (LOGGER.isTraceEnabled()) {
516 LOGGER.trace("getPolicies: " + this.group);
518 return this.group.getPolicies();
521 public Set<PDPPIPConfig> getPipConfigs() {
522 if (LOGGER.isTraceEnabled()) {
523 LOGGER.trace("getPIPConfigs: " + this.group);
525 return this.group.getPipConfigs();