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