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