aboutsummaryrefslogtreecommitdiffstats
path: root/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStateRunning.java
blob: 2d5c66b38cf288c2933d4d8b7d8c8f5d74fc74b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2019 Bell Canada.
 * Modifications Copyright (C) 2021, 2023 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.drools.lifecycle;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.BiPredicate;
import lombok.NonNull;
import org.apache.commons.lang3.tuple.Pair;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.drools.domain.models.artifact.NativeArtifactPolicy;
import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
import org.onap.policy.models.pdp.concepts.PdpStateChange;
import org.onap.policy.models.pdp.concepts.PdpUpdate;
import org.onap.policy.models.pdp.enums.PdpResponseStatus;
import org.onap.policy.models.pdp.enums.PdpState;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Support class for default functionality for running states.
 */
public abstract class LifecycleStateRunning extends LifecycleStateDefault {

    private static final Logger logger = LoggerFactory.getLogger(LifecycleStateRunning.class);

    protected LifecycleStateRunning(LifecycleFsm manager) {
        super(manager);
    }

    protected abstract boolean stateChangeToPassive(@NonNull PdpStateChange change);

    protected abstract boolean stateChangeToActive(@NonNull PdpStateChange change);

    protected abstract boolean deployPolicy(@NonNull PolicyTypeController controller, @NonNull ToscaPolicy policy);

    protected abstract boolean undeployPolicy(@NonNull PolicyTypeController controller, @NonNull ToscaPolicy policy);

    @Override
    public boolean start() {
        logger.warn("{}: start", this);
        return false;
    }

    @Override
    public boolean stop() {
        synchronized (fsm) {
            boolean success = fsm.statusAction(PdpState.TERMINATED, null);
            success = fsm.stopAction() && success;
            return transitionToState(new LifecycleStateTerminated(fsm)) && success;
        }
    }

    @Override
    public void shutdown() {
        synchronized (fsm) {
            stop();
            fsm.shutdownAction();
        }
    }

    @Override
    public boolean status() {
        synchronized (fsm) {
            if (!fsm.isMandatoryPolicyTypesCompliant()) {
                logger.info("Not all expected policy types are registered yet, current={}, expected={}",
                        fsm.getCurrentPolicyTypes(), fsm.getMandatoryPolicyTypes());
                return false;
            }

            return fsm.statusAction();
        }
    }

    @Override
    public boolean isAlive() {
        return true;
    }

    @Override
    public boolean stateChange(@NonNull PdpStateChange change) {
        synchronized (fsm) {
            if (change.getState() == PdpState.PASSIVE) {
                return stateChangeToPassive(change);
            }

            if (change.getState() == PdpState.ACTIVE) {
                return stateChangeToActive(change);
            }

            logger.warn("{}: state-change: {}", this, change);

            invalidStateChange(change);
            return false;
        }
    }

    @Override
    public boolean update(@NonNull PdpUpdate update) {
        synchronized (fsm) {
            if (update.getPdpHeartbeatIntervalMs() != null
                    && !fsm.setStatusIntervalAction(update.getPdpHeartbeatIntervalMs() / 1000)) {
                fsm.statusAction(response(update.getRequestId(), PdpResponseStatus.FAIL,
                    "invalid interval: " + update.getPdpHeartbeatIntervalMs() + " seconds"));
                return false;
            }

            // update subgroup if applicable per update message

            fsm.setSubGroup(update.getPdpSubgroup());

            // Compute the desired final policy set after processing this update.
            // Delta policies allows for the PAP to send us just the policies to deploy and undeploy
            // Note that in this mode of operation, there may be dependent policies in the
            // active inventory.  For example a request to remove a controller policy in a
            // delta request, may affect operational or artifact policies in use.

            List<ToscaPolicy> desiredPolicyInventory =
                fsm.mergePolicies(update.getPoliciesToBeDeployed(), update.getPoliciesToBeUndeployed());

            // snapshot the active policies previous to apply the new set of active
            // policies as given by the PAP in the update message

            List<ToscaPolicy> activePoliciesPreUpdate = fsm.getActivePolicies();
            Map<String, List<ToscaPolicy>> activePoliciesPreUpdateMap =
                    fsm.groupPoliciesByPolicyType(activePoliciesPreUpdate);

            Pair<List<ToscaPolicy>, List<ToscaPolicy>> results = updatePoliciesWithResults(desiredPolicyInventory);

            // summary message to return in the update response to the PAP

            List<ToscaPolicy> failedPolicies = new ArrayList<>(results.getLeft());
            failedPolicies.addAll(results.getRight());

            // If there are *new* native controller policies deployed, there may
            // be existing native artifact policies (previous to the update event
            // processing) that would need to be reapplied.   This requires
            // going through the list of those native artifact policies that
            // were neither deployed nor undeployed and re-apply them on top
            // of the controllers.

            failedPolicies.addAll(reApplyNativeArtifactPolicies(activePoliciesPreUpdate, activePoliciesPreUpdateMap));

            // If there are *new* native artifact policies deployed, there may be existing
            // non-native policies (previous to the update event processing)
            // that will need to be reapplied as the new controllers don't know about them.
            // This requires going through the list of those non-native policies
            // which neither were undeployed nor deployed and re-apply them on top of the
            // new "brained" controllers.

            failedPolicies.addAll(reApplyNonNativePolicies(activePoliciesPreUpdateMap));

            PdpResponseDetails response =
                response(update.getRequestId(),
                        (failedPolicies.isEmpty()) ? PdpResponseStatus.SUCCESS : PdpResponseStatus.FAIL,
                        fsm.getPolicyIdsMessage(failedPolicies));

            return fsm.statusAction(response) && failedPolicies.isEmpty();
        }
    }

    @Override
    public boolean updatePolicies(List<ToscaPolicy> policies) {
        Pair<List<ToscaPolicy>, List<ToscaPolicy>> results = updatePoliciesWithResults(policies);
        return results.getLeft().isEmpty() && results.getRight().isEmpty();
    }

    protected Pair<List<ToscaPolicy>, List<ToscaPolicy>> updatePoliciesWithResults(List<ToscaPolicy> policies) {
        if (policies == null) {
            return Pair.of(Collections.emptyList(), Collections.emptyList());
        }

        // Note that PAP sends the list of all ACTIVE policies with every
        // UPDATE message.   First, we will undeploy all policies that are
        // running but are not present in this list.  This will include
        // policies that are overridden by a different version.   Second,
        // we will deploy those policies that are not installed but
        // included in this list.

        List<ToscaPolicy> failedUndeployPolicies = undeployPolicies(policies);
        if (!failedUndeployPolicies.isEmpty()) {
            logger.warn("update-policies: undeployment failures: {}", fsm.getPolicyIdsMessage(failedUndeployPolicies));
            failedUndeployPolicies.forEach(fsm::failedUndeployPolicyAction);
        }

        List<ToscaPolicy> failedDeployPolicies = deployPolicies(policies);
        if (!failedDeployPolicies.isEmpty()) {
            logger.warn("update-policies: deployment failures: {}", fsm.getPolicyIdsMessage(failedDeployPolicies));
            failedDeployPolicies.forEach(fsm::failedDeployPolicyAction);
        }

        return Pair.of(failedUndeployPolicies, failedDeployPolicies);
    }

    protected List<ToscaPolicy> reApplyNonNativePolicies(Map<String, List<ToscaPolicy>> preActivePoliciesMap) {
        // only need to re-apply non-native policies if there are new native artifact policies

        Map<String, List<ToscaPolicy>> activePoliciesByType = fsm.groupPoliciesByPolicyType(fsm.getActivePolicies());
        List<ToscaPolicy> activeNativeArtifactPolicies = fsm.getNativeArtifactPolicies(activePoliciesByType);

        activeNativeArtifactPolicies.removeAll(fsm.getNativeArtifactPolicies(preActivePoliciesMap));
        if (activeNativeArtifactPolicies.isEmpty()) {
            logger.info("reapply-non-native-policies: nothing to reapply, no new native artifact policies");
            return Collections.emptyList();
        }

        // need to re-apply non-native policies

        // get the non-native policies to be reapplied, this is just the intersection of
        // the original active set, and the new active set (i.e. policies that have not changed,
        // or in other words, have not been deployed or undeployed).

        List<ToscaPolicy> preNonNativePolicies = fsm.getNonNativePolicies(preActivePoliciesMap);
        preNonNativePolicies.retainAll(fsm.getNonNativePolicies(activePoliciesByType));

        logger.info("re-applying non-native policies {} because new native artifact policies have been found: {}",
                fsm.getPolicyIdsMessage(preNonNativePolicies), fsm.getPolicyIdsMessage(activeNativeArtifactPolicies));

        List<ToscaPolicy> failedPolicies = syncPolicies(preNonNativePolicies, this::deployPolicy);
        logger.info("re-applying non-native policies failures: {}", fsm.getPolicyIdsMessage(failedPolicies));

        return failedPolicies;
    }

    protected List<ToscaPolicy> reApplyNativeArtifactPolicies(List<ToscaPolicy> preActivePolicies,
            Map<String, List<ToscaPolicy>> preActivePoliciesMap) {
        // only need to re-apply native artifact policies if there are new native controller policies

        Map<String, List<ToscaPolicy>> activePoliciesByType = fsm.groupPoliciesByPolicyType(fsm.getActivePolicies());
        List<ToscaPolicy> activeNativeControllerPolicies = fsm.getNativeControllerPolicies(activePoliciesByType);

        activeNativeControllerPolicies.removeAll(fsm.getNativeControllerPolicies(preActivePoliciesMap));
        if (activeNativeControllerPolicies.isEmpty()) {
            logger.info("reapply-native-artifact-policies: nothing to reapply, no new native controller policies");
            return Collections.emptyList();
        }

        // need to re-apply native artifact policies

        // get the native artifact policies to be reapplied, this is just the intersection of
        // the original active set, and the new active set (i.e. policies that have not changed,
        // or in other words, have not been deployed or undeployed).

        List<ToscaPolicy> preNativeArtifactPolicies = fsm.getNativeArtifactPolicies(preActivePoliciesMap);
        preNativeArtifactPolicies.retainAll(fsm.getNativeArtifactPolicies(activePoliciesByType));

        logger.info("reapply candidate native artifact policies {} as new native controller policies {} were found",
                fsm.getPolicyIdsMessage(preNativeArtifactPolicies),
                fsm.getPolicyIdsMessage(activeNativeControllerPolicies));

        // from the intersection, only need to reapply those for which there is a new native
        // controller policy

        List<ToscaPolicy> preNativeArtifactPoliciesToApply = new ArrayList<>();
        for (ToscaPolicy preNativeArtifactPolicy : preNativeArtifactPolicies) {
            NativeArtifactPolicy nativeArtifactPolicy;
            try {
                nativeArtifactPolicy =
                    fsm.getDomainMaker().convertTo(preNativeArtifactPolicy, NativeArtifactPolicy.class);
            } catch (CoderException | RuntimeException ex) {
                logger.warn("reapply-native-artifact-policy {}: (unexpected) non conformant: ignoring",
                        preNativeArtifactPolicy.getIdentifier(), ex);
                continue;
            }

            String controllerName = nativeArtifactPolicy.getProperties().getController().getName();
            for (ToscaPolicy policy : activeNativeControllerPolicies) {
                if (controllerName.equals(policy.getProperties().get("controllerName"))) {
                    preNativeArtifactPoliciesToApply.add(preNativeArtifactPolicy);
                }
            }
        }

        logger.info("reapply set of native artifact policies {} as new native controller policies {} were found",
                fsm.getPolicyIdsMessage(preNativeArtifactPoliciesToApply),
                fsm.getPolicyIdsMessage(activeNativeControllerPolicies));

        List<ToscaPolicy> failedPolicies = syncPolicies(preNativeArtifactPoliciesToApply, this::deployPolicy);
        logger.info("re-applying native artifact policies failures: {}", fsm.getPolicyIdsMessage(failedPolicies));

        // since we want non-native policies to be reapplied when a new native artifact policy has been
        // reapplied here, remove it from the preActivePolicies, so it is detected as new.

        if (!preNativeArtifactPoliciesToApply.isEmpty()) {
            preActivePolicies.removeAll(preNativeArtifactPoliciesToApply);
            preActivePoliciesMap
                    .getOrDefault(LifecycleFsm.POLICY_TYPE_DROOLS_NATIVE_RULES.getName(), Collections.emptyList())
                    .removeAll(preNativeArtifactPoliciesToApply);
        }
        return failedPolicies;
    }

    protected List<ToscaPolicy> deployPolicies(List<ToscaPolicy> policies) {
        return syncPolicies(fsm.getDeployablePoliciesAction(policies), this::deployPolicy);
    }

    protected List<ToscaPolicy> undeployPolicies(List<ToscaPolicy> policies) {
        return syncPolicies(fsm.getUndeployablePoliciesAction(policies), this::undeployPolicy);
    }

    protected List<ToscaPolicy> syncPolicies(List<ToscaPolicy> policies,
                                   BiPredicate<PolicyTypeController, ToscaPolicy> sync) {
        List<ToscaPolicy> failedPolicies = new ArrayList<>();
        var domain = fsm.getDomainMaker();
        for (ToscaPolicy policy : policies) {
            ToscaConceptIdentifier policyType = policy.getTypeIdentifier();
            PolicyTypeController controller = fsm.getController(policyType);
            if (controller == null) {
                logger.warn("no controller found for {}", policyType);
                failedPolicies.add(policy);
                continue;
            }

            if (domain.isRegistered(policy.getTypeIdentifier())) {
                if (!domain.isConformant(policy) || !sync.test(controller, policy)) {
                    failedPolicies.add(policy);
                }
            } else {
                logger.info("no validator registered for policy type {}", policy.getTypeIdentifier());
                if (!sync.test(controller, policy)) {
                    failedPolicies.add(policy);
                }
            }
        }
        return failedPolicies;
    }

    private void invalidStateChange(PdpStateChange change) {
        logger.warn("{}: state-change: {}", this, change);
        fsm.statusAction(response(change.getRequestId(), PdpResponseStatus.FAIL,
                 "invalid state change to " + change.getState()));
    }

    protected PdpResponseDetails response(String requestId, PdpResponseStatus responseStatus, String message) {
        var response = new PdpResponseDetails();
        response.setResponseTo(requestId);
        response.setResponseStatus(responseStatus);
        if (message != null) {
            response.setResponseMessage(message);
        }

        return response;
    }
}