2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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
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.Collections;
24 import java.util.HashSet;
25 import java.util.List;
27 import java.util.stream.Collectors;
28 import org.apache.commons.lang3.StringUtils;
29 import org.onap.policy.models.pdp.concepts.PdpStatus;
30 import org.onap.policy.models.pdp.concepts.PdpUpdate;
31 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
33 import org.onap.policy.pap.main.parameters.RequestParams;
39 public class UpdateReq extends RequestImpl {
42 * Constructs the object, and validates the parameters.
44 * @param params configuration parameters
45 * @param name the request name, used for logging purposes
46 * @param message the initial message
48 * @throws IllegalArgumentException if a required parameter is not set
50 public UpdateReq(RequestParams params, String name, PdpUpdate message) {
51 super(params, name, message);
55 public PdpUpdate getMessage() {
56 return (PdpUpdate) super.getMessage();
60 public String checkResponse(PdpStatus response) {
61 String reason = super.checkResponse(response);
63 // response isn't for this PDP - don't generate notifications
67 Set<ToscaPolicyIdentifier> actualSet = new HashSet<>(alwaysList(response.getPolicies()));
68 getNotifier().processResponse(getName(), actualSet);
70 PdpUpdate message = getMessage();
72 if (!StringUtils.equals(message.getPdpGroup(), response.getPdpGroup())) {
73 return "group does not match";
76 if (!StringUtils.equals(message.getPdpSubgroup(), response.getPdpSubgroup())) {
77 return "subgroup does not match";
80 // see if the policies match
82 Set<ToscaPolicyIdentifier> expectedSet = new HashSet<>(alwaysList(message.getPolicies()).stream()
83 .map(ToscaPolicy::getIdentifier).collect(Collectors.toSet()));
85 if (!actualSet.equals(expectedSet)) {
86 return "policies do not match";
93 public boolean isSameContent(Request other) {
94 if (!(other instanceof UpdateReq)) {
98 PdpUpdate first = getMessage();
99 PdpUpdate second = (PdpUpdate) other.getMessage();
101 if (!StringUtils.equals(first.getPdpGroup(), second.getPdpGroup())) {
105 if (!StringUtils.equals(first.getPdpSubgroup(), second.getPdpSubgroup())) {
109 // see if the policies are the same
110 Set<ToscaPolicy> set1 = new HashSet<>(alwaysList(first.getPolicies()));
111 Set<ToscaPolicy> set2 = new HashSet<>(alwaysList(second.getPolicies()));
113 return set1.equals(set2);
117 * Always get a list, even if the original is {@code null}.
119 * @param list the original list, or {@code null}
120 * @return the list, or an empty list if the original was {@code null}
122 private <T> List<T> alwaysList(List<T> list) {
123 return (list != null ? list : Collections.emptyList());
127 public int getPriority() {