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