Initial OpenECOMP policy/engine commit
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / model / RemoveGroupPolicy.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PAP-REST
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.pap.xacml.rest.model;
22
23 import java.util.Collection;
24 import java.util.HashSet;
25 import java.util.Set;
26
27 import org.openecomp.policy.pap.xacml.rest.model.PDPPolicyContainer;
28
29 import com.att.research.xacml.api.pap.PDPGroup;
30 import com.att.research.xacml.api.pap.PDPPolicy;
31
32 import org.openecomp.policy.xacml.api.pap.EcompPDPGroup;
33 import org.openecomp.policy.xacml.std.pap.StdPDPGroup;
34
35 public class RemoveGroupPolicy {
36
37         
38         //Container from where we are fetching the policies
39         private static PDPPolicyContainer policyContainer;
40                 
41         private final RemoveGroupPolicy self = this;
42         private StdPDPGroup updatedObject;
43         private final StdPDPGroup group;
44         private boolean isSaved = false;
45         
46         public RemoveGroupPolicy(StdPDPGroup group) {
47                 
48                 this.group = group;
49                 
50         }
51         
52         public void prepareToRemove(PDPPolicy policy) {
53                 
54                 if (this.group == null) {
55                         return;
56                 }
57
58                 RemoveGroupPolicy.policyContainer = new PDPPolicyContainer(group);
59
60                 RemoveGroupPolicy.policyContainer.removeItem(policy);
61                                                                         
62                 self.doSave();
63                 
64                 self.isSaved = true;
65                 
66         }
67         
68         @SuppressWarnings("unchecked")
69         protected void doSave() {
70                 if (this.group == null) {
71                         return;
72                 }
73                 
74                 //StdPDPGroup pdpGroup = (StdPDPGroup) group;
75                 StdPDPGroup updatedGroupObject = new StdPDPGroup(
76                                 group.getId(), 
77                                 group.isDefaultGroup(), 
78                                 group.getName(), 
79                                 group.getDescription(), 
80                                 null);
81                 
82                 // replace the original set of Policies with the set from the container (possibly modified by the user)
83                 Set<PDPPolicy> changedPolicies = new HashSet<PDPPolicy>();
84                 changedPolicies.addAll((Collection<PDPPolicy>) RemoveGroupPolicy.policyContainer.getItemIds());
85                 updatedGroupObject.setPolicies(changedPolicies);
86                 updatedGroupObject.setEcompPdps(this.group.getEcompPdps());
87                 
88                 // replace the original set of PIP Configs with the set from the container
89                 updatedGroupObject.setPipConfigs(this.group.getPipConfigs());
90                 
91                 // copy those things that the user cannot change from the original to the new object
92                 updatedGroupObject.setStatus(this.group.getStatus());
93                 
94                 this.updatedObject = updatedGroupObject;                        
95         }
96         
97         public boolean isRemoved() {
98                 return this.isSaved;
99         }
100                 
101         public EcompPDPGroup getUpdatedObject() {
102                 return this.updatedObject;
103         }
104
105 }