2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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 com.att.research.xacml.api.pap.PAPException;
25 import java.awt.Checkbox;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.LinkedList;
30 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.rest.util.PolicyContainer;
35 import org.onap.policy.rest.util.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 public class PDPGroupContainer extends PolicyItemSetChangeNotifier
42 implements PolicyContainer.Indexed, PolicyContainer.ItemSetChangeNotifier {
43 private static final long serialVersionUID = 1L;
44 private static final Logger LOGGER = FlexLogger.getLogger(PDPGroupContainer.class);
47 * String identifier of a file's "Id" property.
49 private static final String PROPERTY_ID = "Id";
52 * String identifier of a file's "name" property.
54 private static final String PROPERTY_NAME = "Name";
57 * String identifier of a file's "Description" property.
59 private static final String PROPERTY_DESCRIPTION = "Description";
62 * String identifier of a file's "Default" property.
64 private static final String PROPERTY_DEFAULT = "Default";
66 * String identifier of a file's "Status" property.
68 private static final String PROPERTY_STATUS = "Status";
71 * String identifier of a file's "PDPs" property.
73 private static final String PROPERTY_PDPS = "PDPs";
76 * String identifier of a file's "Policies" property.
78 private static final String PROPERTY_POLICIES = "Policies";
81 * String identifier of a file's "PIP Configurations" property.
83 private static final String PROPERTY_PIPCONFIG = "PIP Configurations";
86 * String identifier of a file's "Selected" property.
88 private static final String PROPERTY_SELECTED = "Selected";
91 * List of the string identifiers for the available properties.
93 private static Collection<String> pDPProperties;
95 private transient PAPPolicyEngine papEngine = null;
96 protected transient List<OnapPDPGroup> groups = Collections.synchronizedList(new ArrayList<OnapPDPGroup>());
98 public PDPGroupContainer(PAPPolicyEngine papPolicyEngine) {
100 this.setContainer(this);
104 this.papEngine = papPolicyEngine;
108 this.refreshGroups();
111 public boolean isSupported(Object itemId) {
112 return itemId instanceof OnapPDPGroup;
115 public synchronized void refreshGroups() {
116 synchronized (this.groups) {
119 this.groups.addAll(this.papEngine.getOnapPDPGroups());
120 } catch (PAPException e) {
121 String message = "Unable to retrieve Groups from server: " + e;
122 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
124 LOGGER.info("refreshGroups");
127 // Notify that we have changed
129 this.fireItemSetChange();
132 public List<OnapPDPGroup> getGroups() {
133 return Collections.unmodifiableList(this.groups);
136 public void makeDefault(OnapPDPGroup group) {
138 this.papEngine.setDefaultGroup(group);
139 } catch (PAPException e) {
140 String message = "Unable to set Default Group on server: " + e;
141 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
146 public void removeGroup(OnapPDPGroup group, OnapPDPGroup newGroup) throws PAPException {
147 if (LOGGER.isTraceEnabled()) {
148 LOGGER.trace("removeGroup: " + group + " new group for PDPs: " + newGroup);
150 if (group.isDefaultGroup()) {
151 throw new UnsupportedOperationException("You can't remove the Default Group.");
154 this.papEngine.removeGroup(group, newGroup);
155 } catch (NullPointerException | PAPException e) {
156 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to removeGroup " + group.getId(), e);
157 throw new PAPException("Failed to remove group '" + group.getId() + "'", e);
161 public void removePDP(OnapPDP pdp, OnapPDPGroup group) throws PAPException {
162 if (LOGGER.isTraceEnabled()) {
163 LOGGER.trace("removePDP: " + pdp + " from group: " + group);
166 this.papEngine.removePDP(pdp);
167 } catch (PAPException e) {
168 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to removePDP " + pdp.getId(), e);
169 throw new PAPException("Failed to remove pdp '" + pdp.getId() + "'", e);
173 public void updatePDP(OnapPDP pdp) {
175 papEngine.updatePDP(pdp);
176 } catch (PAPException e) {
177 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
181 public void updateGroup(OnapPDPGroup group) {
183 papEngine.updateGroup(group);
184 } catch (PAPException e) {
185 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
192 * @param group the group
193 * @param userName the user name
195 public void updateGroup(OnapPDPGroup group, String userName) {
197 papEngine.updateGroup(group, userName);
198 } catch (PAPException e) {
199 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
204 public Collection<?> getContainerPropertyIds() {
205 return pDPProperties;
209 public Collection<?> getItemIds() {
210 final Collection<Object> items = new ArrayList<>();
211 items.addAll(this.groups);
212 if (LOGGER.isTraceEnabled()) {
213 LOGGER.trace("getItemIds: " + items);
215 return Collections.unmodifiableCollection(items);
219 public Class<?> getType(Object propertyId) {
220 if (propertyId.equals(PROPERTY_ID)) {
223 if (propertyId.equals(PROPERTY_NAME)) {
226 if (propertyId.equals(PROPERTY_DESCRIPTION)) {
229 if (propertyId.equals(PROPERTY_DEFAULT)) {
230 return Boolean.class;
232 if (propertyId.equals(PROPERTY_STATUS)) {
235 if (propertyId.equals(PROPERTY_PDPS)) {
238 if (propertyId.equals(PROPERTY_POLICIES)) {
241 if (propertyId.equals(PROPERTY_PIPCONFIG)) {
244 if (propertyId.equals(PROPERTY_SELECTED)) {
245 return Checkbox.class;
252 return this.groups.size();
256 public boolean containsId(Object itemId) {
257 if (LOGGER.isTraceEnabled()) {
258 LOGGER.trace("containsId: " + itemId);
260 if (!this.isSupported(itemId)) {
263 return this.groups.contains(itemId);
267 public Object addItem() {
268 throw new UnsupportedOperationException("PDP Container cannot add a given item.");
271 public void addNewGroup(String name, String description) throws PAPException {
272 if (LOGGER.isTraceEnabled()) {
273 LOGGER.trace("addNewGroup " + name + " " + description);
275 this.papEngine.newGroup(name, description);
278 public void addNewPDP(String id, OnapPDPGroup group, String name, String description, int jmxport)
279 throws 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(OnapPDP pdp, OnapPDPGroup 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) {
298 throw new UnsupportedOperationException("Cannot add a container property.");
302 public boolean removeContainerProperty(Object propertyId) {
303 throw new UnsupportedOperationException("Cannot remove a container property.");
307 public boolean removeAllItems() {
308 throw new UnsupportedOperationException(
309 "PDP Container cannot remove all items. You must have at least the Default group.");
313 public void addItemSetChangeListener(ItemSetChangeListener listener) {
314 if (getItemSetChangeListeners() == null) {
315 setItemSetChangeListeners(new LinkedList<PolicyContainer.ItemSetChangeListener>());
317 getItemSetChangeListeners().add(listener);
321 public Object nextItemId(Object itemId) {
322 if (!this.isSupported(itemId)) {
325 int index = this.groups.indexOf(itemId);
328 // We don't know this group
333 // Is it the last one?
335 if (index == this.groups.size() - 1) {
342 // Return the next one
344 return this.groups.get(index + 1);
348 public Object prevItemId(Object itemId) {
349 if (!this.isSupported(itemId)) {
352 int index = this.groups.indexOf(itemId);
355 // We don't know this group
360 // Is it the first one?
369 // Return the previous one
371 return this.groups.get(index - 1);
375 public Object firstItemId() {
376 synchronized (this.groups) {
377 if (!this.groups.isEmpty()) {
378 return this.groups.get(0);
385 public Object lastItemId() {
386 synchronized (this.groups) {
387 if (!this.groups.isEmpty()) {
388 return this.groups.get(this.groups.size() - 1);
395 public boolean isFirstId(Object itemId) {
396 synchronized (this.groups) {
397 if (!this.groups.isEmpty()) {
398 return this.groups.get(0).equals(itemId);
405 public boolean isLastId(Object itemId) {
406 synchronized (this.groups) {
407 if (!this.groups.isEmpty()) {
408 return this.groups.get(this.groups.size() - 1).equals(itemId);
415 public Object addItemAfter(Object previousItemId) {
416 throw new UnsupportedOperationException("Cannot addItemAfter, there really is no real ordering.");
420 public int indexOfId(Object itemId) {
421 return this.groups.indexOf(itemId);
425 public Object getIdByIndex(int index) {
426 return this.groups.get(index);
430 public List<?> getItemIds(int startIndex, int numberOfItems) {
431 synchronized (this.groups) {
432 int endIndex = startIndex + numberOfItems;
433 if (endIndex > this.groups.size()) {
434 endIndex = this.groups.size() - 1;
436 return this.groups.subList(startIndex, endIndex);
441 public Object addItemAt(int index) {
442 throw new UnsupportedOperationException("Cannot addItemAt");
446 public boolean removeItem(Object itemId) {
447 if (LOGGER.isTraceEnabled()) {
448 LOGGER.trace("removeItem: " + itemId);
450 if (!this.isSupported(itemId)) {
454 // You cannot remove the default group
456 if (PROPERTY_DEFAULT.equals(((OnapPDPGroup) itemId).getId())) {
457 throw new UnsupportedOperationException("You can't remove the Default Group.");
460 // Remove PDPGroup and move any PDP's in it into the default group
463 this.papEngine.removeGroup((OnapPDPGroup) itemId, this.papEngine.getDefaultGroup());
465 } catch (NullPointerException | PAPException e) {
466 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to remove group", e);