2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.pap.main.comm.msgdata;
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;
31 import java.util.stream.Collectors;
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;
47 public class UpdateReq extends RequestImpl {
49 private static final Logger logger = LoggerFactory.getLogger(RequestImpl.class);
52 * Policies to be undeployed if the request fails.
55 private Collection<ToscaConceptIdentifier> undeployPolicies = Collections.emptyList();
58 * Constructs the object, and validates the parameters.
60 * @param params configuration parameters
61 * @param name the request name, used for logging purposes
62 * @param message the initial message
64 * @throws IllegalArgumentException if a required parameter is not set
66 public UpdateReq(RequestParams params, String name, PdpUpdate message) {
67 super(params, name, message);
71 public PdpUpdate getMessage() {
72 return (PdpUpdate) super.getMessage();
76 public String checkResponse(PdpStatus response) {
78 undeployPolicies = Collections.emptyList();
80 String reason = super.checkResponse(response);
82 // response isn't for this PDP - don't generate notifications
86 PdpUpdate message = getMessage();
88 if (!StringUtils.equals(message.getPdpGroup(), response.getPdpGroup())) {
89 return "group does not match";
92 if (!StringUtils.equals(message.getPdpSubgroup(), response.getPdpSubgroup())) {
93 return "subgroup does not match";
96 if (message.getPdpSubgroup() == null) {
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()));
104 getNotifier().processResponse(response.getName(), message.getPdpGroup(), expectedSet, actualSet);
106 Set<ToscaConceptIdentifier> expectedUndeploySet =
107 new HashSet<>(alwaysList(message.getPoliciesToBeUndeployed()));
109 if (actualSet.stream().anyMatch(expectedUndeploySet::contains)) {
110 logger.info("some policies have failed to undeploy");
113 if (!actualSet.containsAll(expectedSet)) {
114 // need to undeploy the policies that are expected, but missing from the
116 undeployPolicies = expectedSet;
117 undeployPolicies.removeAll(actualSet);
119 return "policies do not match";
126 public boolean reconfigure(PdpMessage newMessage) {
127 if (!(newMessage instanceof PdpUpdate)) {
128 // not an update - no change to this request
132 PdpUpdate update = (PdpUpdate) newMessage;
134 if (isSameContent(update)) {
135 // content hasn't changed - nothing more to do
139 Map<ToscaConceptIdentifier, ToscaPolicy> newDeployMap = update.getPoliciesToBeDeployed().stream()
140 .collect(Collectors.toMap(ToscaPolicy::getIdentifier, policy -> policy));
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);
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());
155 // Set lists in update
156 update.setPoliciesToBeDeployed(policiesToBeDeployed);
157 update.setPoliciesToBeUndeployed(policiestoBeUndeployed);
159 reconfigure2(update);
163 protected final boolean isSameContent(PdpUpdate second) {
164 PdpUpdate first = getMessage();
166 if (!StringUtils.equals(first.getPdpGroup(), second.getPdpGroup())) {
170 if (!StringUtils.equals(first.getPdpSubgroup(), second.getPdpSubgroup())) {
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()));
178 if (!(set1.equals(set2))) {
182 Set<ToscaConceptIdentifier> undep1 = new HashSet<>(alwaysList(first.getPoliciesToBeUndeployed()));
183 Set<ToscaConceptIdentifier> undep2 = new HashSet<>(alwaysList(second.getPoliciesToBeUndeployed()));
185 return undep1.equals(undep2);
189 * Always get a list, even if the original is {@code null}.
191 * @param list the original list, or {@code null}
192 * @return the list, or an empty list if the original was {@code null}
194 private <T> List<T> alwaysList(List<T> list) {
195 return (list != null ? list : Collections.emptyList());