Merge "Auditing User Operations Push or Delete Policies."
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / model / PDPGroupContainer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.model;
22
23
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;
31 import java.util.Set;
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;
40
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);
45
46     /**
47      * String identifier of a file's "Id" property.
48      */
49     private static final String PROPERTY_ID = "Id";
50
51     /**
52      * String identifier of a file's "name" property.
53      */
54     private static final String PROPERTY_NAME = "Name";
55
56     /**
57      * String identifier of a file's "Description" property.
58      */
59     private static final String PROPERTY_DESCRIPTION = "Description";
60
61     /**
62      * String identifier of a file's "Default" property.
63      */
64     private static final String PROPERTY_DEFAULT = "Default";
65     /**
66      * String identifier of a file's "Status" property.
67      */
68     private static final String PROPERTY_STATUS = "Status";
69
70     /**
71      * String identifier of a file's "PDPs" property.
72      */
73     private static final String PROPERTY_PDPS = "PDPs";
74
75     /**
76      * String identifier of a file's "Policies" property.
77      */
78     private static final String PROPERTY_POLICIES = "Policies";
79
80     /**
81      * String identifier of a file's "PIP Configurations" property.
82      */
83     private static final String PROPERTY_PIPCONFIG = "PIP Configurations";
84
85     /**
86      * String identifier of a file's "Selected" property.
87      */
88     private static final String PROPERTY_SELECTED = "Selected";
89
90     /**
91      * List of the string identifiers for the available properties.
92      */
93     private static Collection<String> pDPProperties;
94
95     private transient PAPPolicyEngine papEngine = null;
96     protected transient List<OnapPDPGroup> groups = Collections.synchronizedList(new ArrayList<OnapPDPGroup>());
97
98     public PDPGroupContainer(PAPPolicyEngine papPolicyEngine) {
99         super();
100         this.setContainer(this);
101         //
102         //
103         //
104         this.papEngine = papPolicyEngine;
105         //
106         //
107         //
108         this.refreshGroups();
109     }
110
111     public boolean isSupported(Object itemId) {
112         return itemId instanceof OnapPDPGroup;
113     }
114
115     public synchronized void refreshGroups() {
116         synchronized (this.groups) {
117             this.groups.clear();
118             try {
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);
123             }
124             LOGGER.info("refreshGroups");
125         }
126         //
127         // Notify that we have changed
128         //
129         this.fireItemSetChange();
130     }
131
132     public List<OnapPDPGroup> getGroups() {
133         return Collections.unmodifiableList(this.groups);
134     }
135
136     public void makeDefault(OnapPDPGroup group) {
137         try {
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);
142         }
143         return;
144     }
145
146     public void removeGroup(OnapPDPGroup group, OnapPDPGroup newGroup) throws PAPException {
147         if (LOGGER.isTraceEnabled()) {
148             LOGGER.trace("removeGroup: " + group + " new group for PDPs: " + newGroup);
149         }
150         if (group.isDefaultGroup()) {
151             throw new UnsupportedOperationException("You can't remove the Default Group.");
152         }
153         try {
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);
158         }
159     }
160
161     public void removePDP(OnapPDP pdp, OnapPDPGroup group) throws PAPException {
162         if (LOGGER.isTraceEnabled()) {
163             LOGGER.trace("removePDP: " + pdp + " from group: " + group);
164         }
165         try {
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);
170         }
171     }
172
173     public void updatePDP(OnapPDP pdp) {
174         try {
175             papEngine.updatePDP(pdp);
176         } catch (PAPException e) {
177             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
178         }
179     }
180
181     public void updateGroup(OnapPDPGroup group) {
182         try {
183             papEngine.updateGroup(group);
184         } catch (PAPException e) {
185             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
186         }
187     }
188
189     /**
190      * Update group.
191      *
192      * @param group the group
193      * @param userName the user name
194      */
195     public void updateGroup(OnapPDPGroup group, String userName) {
196         try {
197             papEngine.updateGroup(group, userName);
198         } catch (PAPException e) {
199             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
200         }
201     }
202
203     @Override
204     public Collection<?> getContainerPropertyIds() {
205         return pDPProperties;
206     }
207
208     @Override
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);
214         }
215         return Collections.unmodifiableCollection(items);
216     }
217
218     @Override
219     public Class<?> getType(Object propertyId) {
220         if (propertyId.equals(PROPERTY_ID)) {
221             return String.class;
222         }
223         if (propertyId.equals(PROPERTY_NAME)) {
224             return String.class;
225         }
226         if (propertyId.equals(PROPERTY_DESCRIPTION)) {
227             return String.class;
228         }
229         if (propertyId.equals(PROPERTY_DEFAULT)) {
230             return Boolean.class;
231         }
232         if (propertyId.equals(PROPERTY_STATUS)) {
233             return String.class;
234         }
235         if (propertyId.equals(PROPERTY_PDPS)) {
236             return Set.class;
237         }
238         if (propertyId.equals(PROPERTY_POLICIES)) {
239             return Set.class;
240         }
241         if (propertyId.equals(PROPERTY_PIPCONFIG)) {
242             return Set.class;
243         }
244         if (propertyId.equals(PROPERTY_SELECTED)) {
245             return Checkbox.class;
246         }
247         return null;
248     }
249
250     @Override
251     public int size() {
252         return this.groups.size();
253     }
254
255     @Override
256     public boolean containsId(Object itemId) {
257         if (LOGGER.isTraceEnabled()) {
258             LOGGER.trace("containsId: " + itemId);
259         }
260         if (!this.isSupported(itemId)) {
261             return false;
262         }
263         return this.groups.contains(itemId);
264     }
265
266     @Override
267     public Object addItem() {
268         throw new UnsupportedOperationException("PDP Container cannot add a given item.");
269     }
270
271     public void addNewGroup(String name, String description) throws PAPException {
272         if (LOGGER.isTraceEnabled()) {
273             LOGGER.trace("addNewGroup " + name + " " + description);
274         }
275         this.papEngine.newGroup(name, description);
276     }
277
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);
282         }
283         this.papEngine.newPDP(id, group, name, description, jmxport);
284     }
285
286     public void movePDP(OnapPDP pdp, OnapPDPGroup group) {
287         try {
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);
292         }
293         return;
294     }
295
296     @Override
297     public boolean addContainerProperty(Object propertyId, Class<?> type, Object defaultValue) {
298         throw new UnsupportedOperationException("Cannot add a container property.");
299     }
300
301     @Override
302     public boolean removeContainerProperty(Object propertyId) {
303         throw new UnsupportedOperationException("Cannot remove a container property.");
304     }
305
306     @Override
307     public boolean removeAllItems() {
308         throw new UnsupportedOperationException(
309                 "PDP Container cannot remove all items. You must have at least the Default group.");
310     }
311
312     @Override
313     public void addItemSetChangeListener(ItemSetChangeListener listener) {
314         if (getItemSetChangeListeners() == null) {
315             setItemSetChangeListeners(new LinkedList<PolicyContainer.ItemSetChangeListener>());
316         }
317         getItemSetChangeListeners().add(listener);
318     }
319
320     @Override
321     public Object nextItemId(Object itemId) {
322         if (!this.isSupported(itemId)) {
323             return null;
324         }
325         int index = this.groups.indexOf(itemId);
326         if (index == -1) {
327             //
328             // We don't know this group
329             //
330             return null;
331         }
332         //
333         // Is it the last one?
334         //
335         if (index == this.groups.size() - 1) {
336             //
337             // Yes
338             //
339             return null;
340         }
341         //
342         // Return the next one
343         //
344         return this.groups.get(index + 1);
345     }
346
347     @Override
348     public Object prevItemId(Object itemId) {
349         if (!this.isSupported(itemId)) {
350             return null;
351         }
352         int index = this.groups.indexOf(itemId);
353         if (index == -1) {
354             //
355             // We don't know this group
356             //
357             return null;
358         }
359         //
360         // Is it the first one?
361         //
362         if (index == 0) {
363             //
364             // Yes
365             //
366             return null;
367         }
368         //
369         // Return the previous one
370         //
371         return this.groups.get(index - 1);
372     }
373
374     @Override
375     public Object firstItemId() {
376         synchronized (this.groups) {
377             if (!this.groups.isEmpty()) {
378                 return this.groups.get(0);
379             }
380         }
381         return null;
382     }
383
384     @Override
385     public Object lastItemId() {
386         synchronized (this.groups) {
387             if (!this.groups.isEmpty()) {
388                 return this.groups.get(this.groups.size() - 1);
389             }
390         }
391         return null;
392     }
393
394     @Override
395     public boolean isFirstId(Object itemId) {
396         synchronized (this.groups) {
397             if (!this.groups.isEmpty()) {
398                 return this.groups.get(0).equals(itemId);
399             }
400         }
401         return false;
402     }
403
404     @Override
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);
409             }
410         }
411         return false;
412     }
413
414     @Override
415     public Object addItemAfter(Object previousItemId) {
416         throw new UnsupportedOperationException("Cannot addItemAfter, there really is no real ordering.");
417     }
418
419     @Override
420     public int indexOfId(Object itemId) {
421         return this.groups.indexOf(itemId);
422     }
423
424     @Override
425     public Object getIdByIndex(int index) {
426         return this.groups.get(index);
427     }
428
429     @Override
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;
435             }
436             return this.groups.subList(startIndex, endIndex);
437         }
438     }
439
440     @Override
441     public Object addItemAt(int index) {
442         throw new UnsupportedOperationException("Cannot addItemAt");
443     }
444
445     @Override
446     public boolean removeItem(Object itemId) {
447         if (LOGGER.isTraceEnabled()) {
448             LOGGER.trace("removeItem: " + itemId);
449         }
450         if (!this.isSupported(itemId)) {
451             return false;
452         }
453         //
454         // You cannot remove the default group
455         //
456         if (PROPERTY_DEFAULT.equals(((OnapPDPGroup) itemId).getId())) {
457             throw new UnsupportedOperationException("You can't remove the Default Group.");
458         }
459         //
460         // Remove PDPGroup and move any PDP's in it into the default group
461         //
462         try {
463             this.papEngine.removeGroup((OnapPDPGroup) itemId, this.papEngine.getDefaultGroup());
464             return true;
465         } catch (NullPointerException | PAPException e) {
466             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to remove group", e);
467         }
468         return false;
469     }
470 }