aboutsummaryrefslogtreecommitdiffstats
path: root/controlloop/common/controller-usecases/src/main/java/org/onap/policy/drools/apps/controller/usecases/step/GuardStep2.java
blob: b7247ce69efc2530c3b8c29c6fedcc2c9c5b5740 (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
/*-
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.apps.controller.usecases.step;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.onap.policy.controlloop.actor.guard.DecisionOperation;
import org.onap.policy.controlloop.actor.guard.GuardActor;
import org.onap.policy.controlloop.actor.so.VfModuleCreate;
import org.onap.policy.controlloop.actorserviceprovider.Operation;
import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;

/**
 * Wrapper for a Guard operation. Note: this makes a clone of the operation parameters,
 * replacing the payload. It overrides the operation's property names with that are
 * relevant for guards. In addition, it overrides the relevant loadXxx() methods to load
 * the data into the payload instead of into the operation's properties. It also
 * increments or decrements the VF Count, depending whether the operation is a "VF Module
 * Create" or not.
 */
public class GuardStep2 extends Step2 {
    public static final String PAYLOAD_KEY_TARGET_ENTITY = "target";
    public static final String PAYLOAD_KEY_VF_COUNT = "vfCount";

    private final Operation policyOper;


    /**
     * Constructs the object using information from another step.
     *
     * @param otherStep step whose information should be used
     */
    public GuardStep2(Step2 otherStep, String closedLoopControlName) {
        super(otherStep, GuardActor.NAME, DecisionOperation.NAME);

        if (!otherStep.isInitialized()) {
            throw new IllegalStateException("policy operation must be initialized before the guard operation");
        }

        this.policyOper = otherStep.getOperation();

        Map<String, Object> payload = new LinkedHashMap<>();
        payload.put("actor", otherStep.getActorName());
        payload.put("operation", otherStep.getOperationName());
        payload.put("requestId", params.getRequestId());
        payload.put("clname", closedLoopControlName);

        params = params.toBuilder().payload(payload).build();
    }

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

    /**
     * Builds the list of properties on the policy's actual operation.
     */
    @Override
    public List<String> getPropertyNames() {
        List<String> names = new ArrayList<>(1);

        // include VF Count if the policy's operation needs it
        if (policyOper.getPropertyNames().contains(OperationProperties.DATA_VF_COUNT)) {
            names.add(OperationProperties.DATA_VF_COUNT);
        }

        return names;
    }

    /**
     * Load the target entity into the payload instead of the operation's properties.
     */
    @Override
    protected void loadTargetEntity(String propName) {
        params.getPayload().put(PAYLOAD_KEY_TARGET_ENTITY, getTargetEntity());
    }

    /**
     * Load the VF Count into the payload instead of the operation's properties.
     * Increments the count for "VF Module Create". Decrements it otherwise.
     */
    @Override
    protected void loadVfCount(String propName) {
        // run guard with the proposed VF count
        int count = getVfCount();
        if (VfModuleCreate.NAME.equals(policyOper.getName())) {
            ++count;
        } else {
            --count;
        }

        params.getPayload().put(PAYLOAD_KEY_VF_COUNT, count);
    }
}