Generate notifications when policies change
[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 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.Collections;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Set;
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;
34
35
36 /**
37  * Wraps an UPDATE.
38  */
39 public class UpdateReq extends RequestImpl {
40
41     /**
42      * Constructs the object, and validates the parameters.
43      *
44      * @param params configuration parameters
45      * @param name the request name, used for logging purposes
46      * @param message the initial message
47      *
48      * @throws IllegalArgumentException if a required parameter is not set
49      */
50     public UpdateReq(RequestParams params, String name, PdpUpdate message) {
51         super(params, name, message);
52     }
53
54     @Override
55     public PdpUpdate getMessage() {
56         return (PdpUpdate) super.getMessage();
57     }
58
59     @Override
60     public String checkResponse(PdpStatus response) {
61         String reason = super.checkResponse(response);
62         if (reason != null) {
63             // response isn't for this PDP - don't generate notifications
64             return reason;
65         }
66
67         Set<ToscaPolicyIdentifier> actualSet = new HashSet<>(alwaysList(response.getPolicies()));
68         getNotifier().processResponse(getName(), actualSet);
69
70         PdpUpdate message = getMessage();
71
72         if (!StringUtils.equals(message.getPdpGroup(), response.getPdpGroup())) {
73             return "group does not match";
74         }
75
76         if (!StringUtils.equals(message.getPdpSubgroup(), response.getPdpSubgroup())) {
77             return "subgroup does not match";
78         }
79
80         // see if the policies match
81
82         Set<ToscaPolicyIdentifier> expectedSet = new HashSet<>(alwaysList(message.getPolicies()).stream()
83                         .map(ToscaPolicy::getIdentifier).collect(Collectors.toSet()));
84
85         if (!actualSet.equals(expectedSet)) {
86             return "policies do not match";
87         }
88
89         return null;
90     }
91
92     @Override
93     public boolean isSameContent(Request other) {
94         if (!(other instanceof UpdateReq)) {
95             return false;
96         }
97
98         PdpUpdate first = getMessage();
99         PdpUpdate second = (PdpUpdate) other.getMessage();
100
101         if (!StringUtils.equals(first.getPdpGroup(), second.getPdpGroup())) {
102             return false;
103         }
104
105         if (!StringUtils.equals(first.getPdpSubgroup(), second.getPdpSubgroup())) {
106             return false;
107         }
108
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()));
112
113         return set1.equals(set2);
114     }
115
116     /**
117      * Always get a list, even if the original is {@code null}.
118      *
119      * @param list the original list, or {@code null}
120      * @return the list, or an empty list if the original was {@code null}
121      */
122     private <T> List<T> alwaysList(List<T> list) {
123         return (list != null ? list : Collections.emptyList());
124     }
125
126     @Override
127     public int getPriority() {
128         return 1;
129     }
130 }