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