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