Policy notifications appear to be reversed
[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-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
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.main.comm.msgdata;
22
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Set;
28 import java.util.stream.Collectors;
29 import lombok.Getter;
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;
37
38
39 /**
40  * Wraps an UPDATE.
41  */
42 public class UpdateReq extends RequestImpl {
43
44     /**
45      * Policies to be undeployed if the request fails.
46      */
47     @Getter
48     private Collection<ToscaPolicyIdentifier> undeployPolicies = Collections.emptyList();
49
50     /**
51      * Constructs the object, and validates the parameters.
52      *
53      * @param params configuration parameters
54      * @param name the request name, used for logging purposes
55      * @param message the initial message
56      *
57      * @throws IllegalArgumentException if a required parameter is not set
58      */
59     public UpdateReq(RequestParams params, String name, PdpUpdate message) {
60         super(params, name, message);
61     }
62
63     @Override
64     public PdpUpdate getMessage() {
65         return (PdpUpdate) super.getMessage();
66     }
67
68     @Override
69     public String checkResponse(PdpStatus response) {
70         // reset the list
71         undeployPolicies = Collections.emptyList();
72
73         String reason = super.checkResponse(response);
74         if (reason != null) {
75             // response isn't for this PDP - don't generate notifications
76             return reason;
77         }
78
79         Set<ToscaPolicyIdentifier> actualSet = new HashSet<>(alwaysList(response.getPolicies()));
80         getNotifier().processResponse(response.getName(), actualSet);
81
82         PdpUpdate message = getMessage();
83
84         if (!StringUtils.equals(message.getPdpGroup(), response.getPdpGroup())) {
85             return "group does not match";
86         }
87
88         if (!StringUtils.equals(message.getPdpSubgroup(), response.getPdpSubgroup())) {
89             return "subgroup does not match";
90         }
91
92         if (message.getPdpSubgroup() == null) {
93             return null;
94         }
95
96         // see if the policies match
97
98         Set<ToscaPolicyIdentifier> expectedSet = new HashSet<>(alwaysList(message.getPolicies()).stream()
99                         .map(ToscaPolicy::getIdentifier).collect(Collectors.toSet()));
100
101         if (!actualSet.equals(expectedSet)) {
102             // need to undeploy the policies that are expected, but missing from the
103             // response
104             undeployPolicies = expectedSet;
105             undeployPolicies.removeAll(actualSet);
106
107             return "policies do not match";
108         }
109
110         return null;
111     }
112
113     @Override
114     public boolean reconfigure(PdpMessage newMessage) {
115         if (!(newMessage instanceof PdpUpdate)) {
116             // not an update - no change to this request
117             return false;
118         }
119
120         PdpUpdate update = (PdpUpdate) newMessage;
121
122         if (isSameContent(update)) {
123             // content hasn't changed - nothing more to do
124             return true;
125         }
126
127         reconfigure2(newMessage);
128         return true;
129     }
130
131     protected final boolean isSameContent(PdpUpdate second) {
132         PdpUpdate first = getMessage();
133
134         if (!StringUtils.equals(first.getPdpGroup(), second.getPdpGroup())) {
135             return false;
136         }
137
138         if (!StringUtils.equals(first.getPdpSubgroup(), second.getPdpSubgroup())) {
139             return false;
140         }
141
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()));
145
146         return set1.equals(set2);
147     }
148
149     /**
150      * Always get a list, even if the original is {@code null}.
151      *
152      * @param list the original list, or {@code null}
153      * @return the list, or an empty list if the original was {@code null}
154      */
155     private <T> List<T> alwaysList(List<T> list) {
156         return (list != null ? list : Collections.emptyList());
157     }
158 }