Sonar cleanup in controllers etc
[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     /**
117      * refreshGroups.
118      */
119     public synchronized void refreshGroups() {
120         synchronized (this.groups) {
121             this.groups.clear();
122             try {
123                 this.groups.addAll(this.papEngine.getOnapPDPGroups());
124             } catch (PAPException e) {
125                 String message = "Unable to retrieve Groups from server: " + e;
126                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
127             }
128             LOGGER.info("refreshGroups");
129         }
130         //
131         // Notify that we have changed
132         //
133         this.fireItemSetChange();
134     }
135
136     public List<OnapPDPGroup> getGroups() {
137         return Collections.unmodifiableList(this.groups);
138     }
139
140     /**
141      * makeDefault.
142      *
143      * @param group OnapPDPGroup
144      */
145     public void makeDefault(OnapPDPGroup group) {
146         try {
147             this.papEngine.setDefaultGroup(group);
148         } catch (PAPException e) {
149             String message = "Unable to set Default Group on server: " + e;
150             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
151         }
152         return;
153     }
154
155     /**
156      * removeGroup.
157      *
158      * @param group OnapPDPGroup
159      * @param newGroup OnapPDPGroup
160      * @throws PAPException PAPException
161      */
162     public void removeGroup(OnapPDPGroup group, OnapPDPGroup newGroup) throws PAPException {
163         if (LOGGER.isTraceEnabled()) {
164             LOGGER.trace("removeGroup: " + group + " new group for PDPs: " + newGroup);
165         }
166         if (group.isDefaultGroup()) {
167             throw new UnsupportedOperationException("You can't remove the Default Group.");
168         }
169         try {
170             this.papEngine.removeGroup(group, newGroup);
171         } catch (NullPointerException | PAPException e) {
172             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to removeGroup " + group.getId(), e);
173             throw new PAPException("Failed to remove group '" + group.getId() + "'", e);
174         }
175     }
176
177     /**
178      * removePDP.
179      *
180      * @param pdp OnapPDP
181      * @param group OnapPDPGroup
182      * @throws PAPException PAPException
183      */
184     public void removePDP(OnapPDP pdp, OnapPDPGroup group) throws PAPException {
185         if (LOGGER.isTraceEnabled()) {
186             LOGGER.trace("removePDP: " + pdp + " from group: " + group);
187         }
188         try {
189             this.papEngine.removePDP(pdp);
190         } catch (PAPException e) {
191             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to removePDP " + pdp.getId(), e);
192             throw new PAPException("Failed to remove pdp '" + pdp.getId() + "'", e);
193         }
194     }
195
196     /**
197      * updatePDP.
198      *
199      * @param pdp OnapPDP
200      */
201     public void updatePDP(OnapPDP pdp) {
202         try {
203             papEngine.updatePDP(pdp);
204         } catch (PAPException e) {
205             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
206         }
207     }
208
209     /**
210      * updateGroup.
211      *
212      * @param group OnapPDPGroup
213      */
214     public void updateGroup(OnapPDPGroup group) {
215         try {
216             papEngine.updateGroup(group);
217         } catch (PAPException e) {
218             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
219         }
220     }
221
222     /**
223      * Update group.
224      *
225      * @param group the group
226      * @param userName the user name
227      */
228     public void updateGroup(OnapPDPGroup group, String userName) {
229         try {
230             papEngine.updateGroup(group, userName);
231         } catch (PAPException e) {
232             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
233         }
234     }
235
236     @Override
237     public Collection<?> getContainerPropertyIds() {
238         return pDPProperties;
239     }
240
241     @Override
242     public Collection<?> getItemIds() {
243         final Collection<Object> items = new ArrayList<>();
244         items.addAll(this.groups);
245         if (LOGGER.isTraceEnabled()) {
246             LOGGER.trace("getItemIds: " + items);
247         }
248         return Collections.unmodifiableCollection(items);
249     }
250
251     @Override
252     public List<?> getItemIds(int startIndex, int numberOfItems) {
253         synchronized (this.groups) {
254             int endIndex = startIndex + numberOfItems;
255             if (endIndex > this.groups.size()) {
256                 endIndex = this.groups.size() - 1;
257             }
258             return this.groups.subList(startIndex, endIndex);
259         }
260     }
261
262     @Override
263     public Class<?> getType(Object propertyId) {
264         if (propertyId.equals(PROPERTY_ID)) {
265             return String.class;
266         }
267         if (propertyId.equals(PROPERTY_NAME)) {
268             return String.class;
269         }
270         if (propertyId.equals(PROPERTY_DESCRIPTION)) {
271             return String.class;
272         }
273         if (propertyId.equals(PROPERTY_DEFAULT)) {
274             return Boolean.class;
275         }
276         if (propertyId.equals(PROPERTY_STATUS)) {
277             return String.class;
278         }
279         if (propertyId.equals(PROPERTY_PDPS)) {
280             return Set.class;
281         }
282         if (propertyId.equals(PROPERTY_POLICIES)) {
283             return Set.class;
284         }
285         if (propertyId.equals(PROPERTY_PIPCONFIG)) {
286             return Set.class;
287         }
288         if (propertyId.equals(PROPERTY_SELECTED)) {
289             return Checkbox.class;
290         }
291         return null;
292     }
293
294     @Override
295     public int size() {
296         return this.groups.size();
297     }
298
299     @Override
300     public boolean containsId(Object itemId) {
301         if (LOGGER.isTraceEnabled()) {
302             LOGGER.trace("containsId: " + itemId);
303         }
304         if (!this.isSupported(itemId)) {
305             return false;
306         }
307         return this.groups.contains(itemId);
308     }
309
310     @Override
311     public Object addItem() {
312         throw new UnsupportedOperationException("PDP Container cannot add a given item.");
313     }
314
315     /**
316      * addNewGroup.
317      *
318      * @param name String
319      * @param description String
320      * @throws PAPException PAPException
321      */
322     public void addNewGroup(String name, String description) throws PAPException {
323         if (LOGGER.isTraceEnabled()) {
324             LOGGER.trace("addNewGroup " + name + " " + description);
325         }
326         this.papEngine.newGroup(name, description);
327     }
328
329     /**
330      * addNewPDP.
331      *
332      * @param id String
333      * @param group OnapPDPGroup
334      * @param name String
335      * @param description String
336      * @param jmxport int
337      * @throws PAPException PAPException
338      */
339     public void addNewPDP(String id, OnapPDPGroup group, String name, String description, int jmxport)
340             throws PAPException {
341         if (LOGGER.isTraceEnabled()) {
342             LOGGER.trace("addNewPDP " + id + " " + name + " " + description + " " + jmxport);
343         }
344         this.papEngine.newPDP(id, group, name, description, jmxport);
345     }
346
347     /**
348      * movePDP.
349      *
350      * @param pdp OnapPDP
351      * @param group OnapPDPGroup
352      */
353     public void movePDP(OnapPDP pdp, OnapPDPGroup group) {
354         try {
355             this.papEngine.movePDP(pdp, group);
356         } catch (PAPException e) {
357             String message = "Unable to move PDP to new group on server: " + e;
358             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
359         }
360         return;
361     }
362
363     @Override
364     public boolean addContainerProperty(Object propertyId, Class<?> type, Object defaultValue) {
365         throw new UnsupportedOperationException("Cannot add a container property.");
366     }
367
368     @Override
369     public boolean removeContainerProperty(Object propertyId) {
370         throw new UnsupportedOperationException("Cannot remove a container property.");
371     }
372
373     @Override
374     public boolean removeAllItems() {
375         throw new UnsupportedOperationException(
376                 "PDP Container cannot remove all items. You must have at least the Default group.");
377     }
378
379     @Override
380     public void addItemSetChangeListener(ItemSetChangeListener listener) {
381         if (getItemSetChangeListeners() == null) {
382             setItemSetChangeListeners(new LinkedList<PolicyContainer.ItemSetChangeListener>());
383         }
384         getItemSetChangeListeners().add(listener);
385     }
386
387     @Override
388     public Object nextItemId(Object itemId) {
389         if (!this.isSupported(itemId)) {
390             return null;
391         }
392         int index = this.groups.indexOf(itemId);
393         if (index == -1) {
394             //
395             // We don't know this group
396             //
397             return null;
398         }
399         //
400         // Is it the last one?
401         //
402         if (index == this.groups.size() - 1) {
403             //
404             // Yes
405             //
406             return null;
407         }
408         //
409         // Return the next one
410         //
411         return this.groups.get(index + 1);
412     }
413
414     @Override
415     public Object prevItemId(Object itemId) {
416         if (!this.isSupported(itemId)) {
417             return null;
418         }
419         int index = this.groups.indexOf(itemId);
420         if (index == -1) {
421             //
422             // We don't know this group
423             //
424             return null;
425         }
426         //
427         // Is it the first one?
428         //
429         if (index == 0) {
430             //
431             // Yes
432             //
433             return null;
434         }
435         //
436         // Return the previous one
437         //
438         return this.groups.get(index - 1);
439     }
440
441     @Override
442     public Object firstItemId() {
443         synchronized (this.groups) {
444             if (!this.groups.isEmpty()) {
445                 return this.groups.get(0);
446             }
447         }
448         return null;
449     }
450
451     @Override
452     public Object lastItemId() {
453         synchronized (this.groups) {
454             if (!this.groups.isEmpty()) {
455                 return this.groups.get(this.groups.size() - 1);
456             }
457         }
458         return null;
459     }
460
461     @Override
462     public boolean isFirstId(Object itemId) {
463         synchronized (this.groups) {
464             if (!this.groups.isEmpty()) {
465                 return this.groups.get(0).equals(itemId);
466             }
467         }
468         return false;
469     }
470
471     @Override
472     public boolean isLastId(Object itemId) {
473         synchronized (this.groups) {
474             if (!this.groups.isEmpty()) {
475                 return this.groups.get(this.groups.size() - 1).equals(itemId);
476             }
477         }
478         return false;
479     }
480
481     @Override
482     public Object addItemAfter(Object previousItemId) {
483         throw new UnsupportedOperationException("Cannot addItemAfter, there really is no real ordering.");
484     }
485
486     @Override
487     public int indexOfId(Object itemId) {
488         return this.groups.indexOf(itemId);
489     }
490
491     @Override
492     public Object getIdByIndex(int index) {
493         return this.groups.get(index);
494     }
495
496     @Override
497     public Object addItemAt(int index) {
498         throw new UnsupportedOperationException("Cannot addItemAt");
499     }
500
501     @Override
502     public boolean removeItem(Object itemId) {
503         if (LOGGER.isTraceEnabled()) {
504             LOGGER.trace("removeItem: " + itemId);
505         }
506         if (!this.isSupported(itemId)) {
507             return false;
508         }
509         //
510         // You cannot remove the default group
511         //
512         if (PROPERTY_DEFAULT.equals(((OnapPDPGroup) itemId).getId())) {
513             throw new UnsupportedOperationException("You can't remove the Default Group.");
514         }
515         //
516         // Remove PDPGroup and move any PDP's in it into the default group
517         //
518         try {
519             this.papEngine.removeGroup((OnapPDPGroup) itemId, this.papEngine.getDefaultGroup());
520             return true;
521         } catch (NullPointerException | PAPException e) {
522             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Failed to remove group", e);
523         }
524         return false;
525     }
526 }