Unit/SONAR/Checkstyle in ONAP-REST
[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-2019 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 com.att.research.xacml.api.pap.PDPPolicy;
24
25 import java.util.Collection;
26 import java.util.HashSet;
27 import java.util.Set;
28
29 import org.onap.policy.rest.util.PdpPolicyContainer;
30 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
31 import org.onap.policy.xacml.std.pap.StdPDPGroup;
32
33 public class RemoveGroupPolicy {
34
35     // Container from where we are fetching the policies
36     private static PdpPolicyContainer policyContainer;
37
38     private StdPDPGroup updatedObject;
39     private final StdPDPGroup group;
40     private boolean isSaved = false;
41
42     /**
43      * Instantiates a new removes the group policy.
44      *
45      * @param group the group
46      */
47     public RemoveGroupPolicy(StdPDPGroup group) {
48
49         this.group = group;
50
51     }
52
53     /**
54      * Prepare to remove.
55      */
56     public void prepareToRemove() {
57         if (this.group == null) {
58             return;
59         }
60         setRemoveGroupPolicy(new PdpPolicyContainer(group));
61     }
62
63     /**
64      * Removes the policy.
65      *
66      * @param policy the policy
67      */
68     public void removePolicy(PDPPolicy policy) {
69         RemoveGroupPolicy.policyContainer.removeItem(policy);
70         this.doSave();
71         this.isSaved = true;
72     }
73
74     private static void setRemoveGroupPolicy(PdpPolicyContainer pdpPolicyContainer) {
75         RemoveGroupPolicy.policyContainer = pdpPolicyContainer;
76     }
77
78     @SuppressWarnings("unchecked")
79     protected void doSave() {
80         if (this.group == null) {
81             return;
82         }
83
84         StdPDPGroup updatedGroupObject =
85                 new StdPDPGroup(group.getId(), group.isDefaultGroup(), group.getName(), group.getDescription(), null);
86
87         // replace the original set of Policies with the set from the container (possibly modified
88         // by the user)
89         Set<PDPPolicy> changedPolicies = new HashSet<>();
90         changedPolicies.addAll((Collection<PDPPolicy>) RemoveGroupPolicy.policyContainer.getItemIds());
91         updatedGroupObject.setPolicies(changedPolicies);
92         updatedGroupObject.setOnapPdps(this.group.getOnapPdps());
93
94         // replace the original set of PIP Configs with the set from the container
95         updatedGroupObject.setPipConfigs(this.group.getPipConfigs());
96
97         // copy those things that the user cannot change from the original to the new object
98         updatedGroupObject.setStatus(this.group.getStatus());
99
100         this.updatedObject = updatedGroupObject;
101     }
102
103     public boolean isRemoved() {
104         return this.isSaved;
105     }
106
107     public OnapPDPGroup getUpdatedObject() {
108         return this.updatedObject;
109     }
110
111     /**
112      * Gets the group.
113      *
114      * @return the group
115      */
116     public StdPDPGroup getGroup() {
117         return group;
118     }
119
120 }