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