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