b7247ce69efc2530c3b8c29c6fedcc2c9c5b5740
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 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.drools.apps.controller.usecases.step;
22
23 import java.util.ArrayList;
24 import java.util.LinkedHashMap;
25 import java.util.List;
26 import java.util.Map;
27 import org.onap.policy.controlloop.actor.guard.DecisionOperation;
28 import org.onap.policy.controlloop.actor.guard.GuardActor;
29 import org.onap.policy.controlloop.actor.so.VfModuleCreate;
30 import org.onap.policy.controlloop.actorserviceprovider.Operation;
31 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
32
33 /**
34  * Wrapper for a Guard operation. Note: this makes a clone of the operation parameters,
35  * replacing the payload. It overrides the operation's property names with that are
36  * relevant for guards. In addition, it overrides the relevant loadXxx() methods to load
37  * the data into the payload instead of into the operation's properties. It also
38  * increments or decrements the VF Count, depending whether the operation is a "VF Module
39  * Create" or not.
40  */
41 public class GuardStep2 extends Step2 {
42     public static final String PAYLOAD_KEY_TARGET_ENTITY = "target";
43     public static final String PAYLOAD_KEY_VF_COUNT = "vfCount";
44
45     private final Operation policyOper;
46
47
48     /**
49      * Constructs the object using information from another step.
50      *
51      * @param otherStep step whose information should be used
52      */
53     public GuardStep2(Step2 otherStep, String closedLoopControlName) {
54         super(otherStep, GuardActor.NAME, DecisionOperation.NAME);
55
56         if (!otherStep.isInitialized()) {
57             throw new IllegalStateException("policy operation must be initialized before the guard operation");
58         }
59
60         this.policyOper = otherStep.getOperation();
61
62         Map<String, Object> payload = new LinkedHashMap<>();
63         payload.put("actor", otherStep.getActorName());
64         payload.put("operation", otherStep.getOperationName());
65         payload.put("requestId", params.getRequestId());
66         payload.put("clname", closedLoopControlName);
67
68         params = params.toBuilder().payload(payload).build();
69     }
70
71     @Override
72     public boolean acceptsEvent() {
73         return true;
74     }
75
76     /**
77      * Builds the list of properties on the policy's actual operation.
78      */
79     @Override
80     public List<String> getPropertyNames() {
81         List<String> names = new ArrayList<>(1);
82
83         // include VF Count if the policy's operation needs it
84         if (policyOper.getPropertyNames().contains(OperationProperties.DATA_VF_COUNT)) {
85             names.add(OperationProperties.DATA_VF_COUNT);
86         }
87
88         return names;
89     }
90
91     /**
92      * Load the target entity into the payload instead of the operation's properties.
93      */
94     @Override
95     protected void loadTargetEntity(String propName) {
96         params.getPayload().put(PAYLOAD_KEY_TARGET_ENTITY, getTargetEntity());
97     }
98
99     /**
100      * Load the VF Count into the payload instead of the operation's properties.
101      * Increments the count for "VF Module Create". Decrements it otherwise.
102      */
103     @Override
104     protected void loadVfCount(String propName) {
105         // run guard with the proposed VF count
106         int count = getVfCount();
107         if (VfModuleCreate.NAME.equals(policyOper.getName())) {
108             ++count;
109         } else {
110             --count;
111         }
112
113         params.getPayload().put(PAYLOAD_KEY_VF_COUNT, count);
114     }
115 }