Fix build failure and remove unused imports
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / comm / msgdata / UpdateReq.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
4  * ================================================================================
5  * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2021 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.comm.msgdata;
23
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.HashSet;
27 import java.util.LinkedList;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Set;
31 import java.util.stream.Collectors;
32 import lombok.Getter;
33 import org.apache.commons.lang3.StringUtils;
34 import org.onap.policy.models.pdp.concepts.PdpMessage;
35 import org.onap.policy.models.pdp.concepts.PdpStatus;
36 import org.onap.policy.models.pdp.concepts.PdpUpdate;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
39 import org.onap.policy.pap.main.parameters.RequestParams;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43
44 /**
45  * Wraps an UPDATE.
46  */
47 public class UpdateReq extends RequestImpl {
48
49     private static final Logger logger = LoggerFactory.getLogger(RequestImpl.class);
50
51     /**
52      * Policies to be undeployed if the request fails.
53      */
54     @Getter
55     private Collection<ToscaConceptIdentifier> undeployPolicies = Collections.emptyList();
56
57     /**
58      * Constructs the object, and validates the parameters.
59      *
60      * @param params configuration parameters
61      * @param name the request name, used for logging purposes
62      * @param message the initial message
63      *
64      * @throws IllegalArgumentException if a required parameter is not set
65      */
66     public UpdateReq(RequestParams params, String name, PdpUpdate message) {
67         super(params, name, message);
68     }
69
70     @Override
71     public PdpUpdate getMessage() {
72         return (PdpUpdate) super.getMessage();
73     }
74
75     @Override
76     public String checkResponse(PdpStatus response) {
77         // reset the list
78         undeployPolicies = Collections.emptyList();
79
80         String reason = super.checkResponse(response);
81         if (reason != null) {
82             // response isn't for this PDP - don't generate notifications
83             return reason;
84         }
85
86         PdpUpdate message = getMessage();
87
88         if (!StringUtils.equals(message.getPdpGroup(), response.getPdpGroup())) {
89             return "group does not match";
90         }
91
92         if (!StringUtils.equals(message.getPdpSubgroup(), response.getPdpSubgroup())) {
93             return "subgroup does not match";
94         }
95
96         if (message.getPdpSubgroup() == null) {
97             return null;
98         }
99
100         Set<ToscaConceptIdentifier> actualSet = new HashSet<>(alwaysList(response.getPolicies()));
101         Set<ToscaConceptIdentifier> expectedSet = new HashSet<>(alwaysList(message.getPoliciesToBeDeployed()).stream()
102                         .map(ToscaPolicy::getIdentifier).collect(Collectors.toSet()));
103
104         getNotifier().processResponse(response.getName(), message.getPdpGroup(), expectedSet, actualSet);
105
106         Set<ToscaConceptIdentifier> expectedUndeploySet =
107                 new HashSet<>(alwaysList(message.getPoliciesToBeUndeployed()));
108
109         if (actualSet.stream().anyMatch(expectedUndeploySet::contains)) {
110             logger.info("some policies have failed to undeploy");
111         }
112
113         if (!actualSet.containsAll(expectedSet)) {
114             // need to undeploy the policies that are expected, but missing from the
115             // response
116             undeployPolicies = expectedSet;
117             undeployPolicies.removeAll(actualSet);
118
119             return "policies do not match";
120         }
121
122         return null;
123     }
124
125     @Override
126     public boolean reconfigure(PdpMessage newMessage) {
127         if (!(newMessage instanceof PdpUpdate)) {
128             // not an update - no change to this request
129             return false;
130         }
131
132         PdpUpdate update = (PdpUpdate) newMessage;
133
134         if (isSameContent(update)) {
135             // content hasn't changed - nothing more to do
136             return true;
137         }
138
139         Map<ToscaConceptIdentifier, ToscaPolicy> newDeployMap = update.getPoliciesToBeDeployed().stream()
140                 .collect(Collectors.toMap(ToscaPolicy::getIdentifier, policy -> policy));
141
142         // Merge undpeloy lists
143         Set<ToscaConceptIdentifier> policiesToBeUndeployedSet = new HashSet<>(getMessage().getPoliciesToBeUndeployed());
144         policiesToBeUndeployedSet.removeAll(newDeployMap.keySet());
145         policiesToBeUndeployedSet.addAll(update.getPoliciesToBeUndeployed());
146         final List<ToscaConceptIdentifier> policiestoBeUndeployed = new LinkedList<>(policiesToBeUndeployedSet);
147
148         // Merge deploy lists
149         Map<ToscaConceptIdentifier, ToscaPolicy> policiesToBeDeployedMap = getMessage().getPoliciesToBeDeployed()
150                 .stream().collect(Collectors.toMap(ToscaPolicy::getIdentifier, policy -> policy));
151         policiesToBeDeployedMap.keySet().removeAll(update.getPoliciesToBeUndeployed());
152         policiesToBeDeployedMap.putAll(newDeployMap);
153         final List<ToscaPolicy> policiesToBeDeployed = new LinkedList<>(policiesToBeDeployedMap.values());
154
155         // Set lists in update
156         update.setPoliciesToBeDeployed(policiesToBeDeployed);
157         update.setPoliciesToBeUndeployed(policiestoBeUndeployed);
158
159         reconfigure2(update);
160         return true;
161     }
162
163     protected final boolean isSameContent(PdpUpdate second) {
164         PdpUpdate first = getMessage();
165
166         if (!StringUtils.equals(first.getPdpGroup(), second.getPdpGroup())) {
167             return false;
168         }
169
170         if (!StringUtils.equals(first.getPdpSubgroup(), second.getPdpSubgroup())) {
171             return false;
172         }
173
174         // see if the policies are the same
175         Set<ToscaPolicy> set1 = new HashSet<>(alwaysList(first.getPoliciesToBeDeployed()));
176         Set<ToscaPolicy> set2 = new HashSet<>(alwaysList(second.getPoliciesToBeDeployed()));
177
178         if (!(set1.equals(set2))) {
179             return false;
180         }
181
182         Set<ToscaConceptIdentifier> undep1 = new HashSet<>(alwaysList(first.getPoliciesToBeUndeployed()));
183         Set<ToscaConceptIdentifier> undep2 = new HashSet<>(alwaysList(second.getPoliciesToBeUndeployed()));
184
185         return undep1.equals(undep2);
186     }
187
188     /**
189      * Always get a list, even if the original is {@code null}.
190      *
191      * @param list the original list, or {@code null}
192      * @return the list, or an empty list if the original was {@code null}
193      */
194     private <T> List<T> alwaysList(List<T> list) {
195         return (list != null ? list : Collections.emptyList());
196     }
197 }