2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.pap.main.comm.msgdata;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.List;
28 import java.util.stream.Collectors;
30 import org.apache.commons.lang3.StringUtils;
31 import org.onap.policy.models.pdp.concepts.PdpMessage;
32 import org.onap.policy.models.pdp.concepts.PdpStatus;
33 import org.onap.policy.models.pdp.concepts.PdpUpdate;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
36 import org.onap.policy.pap.main.parameters.RequestParams;
42 public class UpdateReq extends RequestImpl {
45 * Policies to be undeployed if the request fails.
48 private Collection<ToscaPolicyIdentifier> undeployPolicies = Collections.emptyList();
51 * Constructs the object, and validates the parameters.
53 * @param params configuration parameters
54 * @param name the request name, used for logging purposes
55 * @param message the initial message
57 * @throws IllegalArgumentException if a required parameter is not set
59 public UpdateReq(RequestParams params, String name, PdpUpdate message) {
60 super(params, name, message);
64 public PdpUpdate getMessage() {
65 return (PdpUpdate) super.getMessage();
69 public String checkResponse(PdpStatus response) {
71 undeployPolicies = Collections.emptyList();
73 String reason = super.checkResponse(response);
75 // response isn't for this PDP - don't generate notifications
79 Set<ToscaPolicyIdentifier> actualSet = new HashSet<>(alwaysList(response.getPolicies()));
80 getNotifier().processResponse(response.getName(), actualSet);
82 PdpUpdate message = getMessage();
84 if (!StringUtils.equals(message.getPdpGroup(), response.getPdpGroup())) {
85 return "group does not match";
88 if (!StringUtils.equals(message.getPdpSubgroup(), response.getPdpSubgroup())) {
89 return "subgroup does not match";
92 if (message.getPdpSubgroup() == null) {
96 // see if the policies match
98 Set<ToscaPolicyIdentifier> expectedSet = new HashSet<>(alwaysList(message.getPolicies()).stream()
99 .map(ToscaPolicy::getIdentifier).collect(Collectors.toSet()));
101 if (!actualSet.equals(expectedSet)) {
102 // need to undeploy the policies that are expected, but missing from the
104 undeployPolicies = expectedSet;
105 undeployPolicies.removeAll(actualSet);
107 return "policies do not match";
114 public boolean reconfigure(PdpMessage newMessage) {
115 if (!(newMessage instanceof PdpUpdate)) {
116 // not an update - no change to this request
120 PdpUpdate update = (PdpUpdate) newMessage;
122 if (isSameContent(update)) {
123 // content hasn't changed - nothing more to do
127 reconfigure2(newMessage);
131 protected final boolean isSameContent(PdpUpdate second) {
132 PdpUpdate first = getMessage();
134 if (!StringUtils.equals(first.getPdpGroup(), second.getPdpGroup())) {
138 if (!StringUtils.equals(first.getPdpSubgroup(), second.getPdpSubgroup())) {
142 // see if the policies are the same
143 Set<ToscaPolicy> set1 = new HashSet<>(alwaysList(first.getPolicies()));
144 Set<ToscaPolicy> set2 = new HashSet<>(alwaysList(second.getPolicies()));
146 return set1.equals(set2);
150 * Always get a list, even if the original is {@code null}.
152 * @param list the original list, or {@code null}
153 * @return the list, or an empty list if the original was {@code null}
155 private <T> List<T> alwaysList(List<T> list) {
156 return (list != null ? list : Collections.emptyList());