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