Merge "Modify Actors to use properties when provided"
[policy/models.git] / models-interactions / model-actors / actor.appc / src / main / java / org / onap / policy / controlloop / actor / appc / ModifyConfigOperation.java
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.controlloop.actor.appc;
22
23 import java.util.List;
24 import java.util.concurrent.CompletableFuture;
25 import org.onap.aai.domain.yang.GenericVnf;
26 import org.onap.policy.aai.AaiConstants;
27 import org.onap.policy.aai.AaiCqResponse;
28 import org.onap.policy.appc.Request;
29 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
30 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
31 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
32 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class ModifyConfigOperation extends AppcOperation {
37     private static final Logger logger = LoggerFactory.getLogger(ModifyConfigOperation.class);
38
39     public static final String NAME = "ModifyConfig";
40
41     private static final List<String> PROPERTY_NAMES = List.of(OperationProperties.AAI_RESOURCE_VNF);
42
43     /**
44      * Constructs the object.
45      *
46      * @param params operation parameters
47      * @param config configuration for this operation
48      */
49     public ModifyConfigOperation(ControlLoopOperationParams params, BidirectionalTopicConfig config) {
50         super(params, config, PROPERTY_NAMES);
51     }
52
53     /**
54      * Ensures that A&AI customer query has been performed, and then runs the guard query.
55      */
56     @Override
57     @SuppressWarnings("unchecked")
58     protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
59         if (params.isPreprocessed()) {
60             return null;
61         }
62
63         ControlLoopOperationParams cqParams = params.toBuilder().actor(AaiConstants.ACTOR_NAME)
64                         .operation(AaiCqResponse.OPERATION).payload(null).retry(null).timeoutSec(null).build();
65
66         // run Custom Query and Guard, in parallel
67         return allOf(() -> params.getContext().obtain(AaiCqResponse.CONTEXT_KEY, cqParams), this::startGuardAsync);
68     }
69
70     @Override
71     protected Request makeRequest(int attempt) {
72         return makeRequest(attempt, getVnfId());
73     }
74
75     protected String getVnfId() {
76         GenericVnf vnf = this.getProperty(OperationProperties.AAI_RESOURCE_VNF);
77         if (vnf != null) {
78             return vnf.getVnfId();
79         }
80
81         AaiCqResponse cq = params.getContext().getProperty(AaiCqResponse.CONTEXT_KEY);
82         if (cq == null) {
83             throw new IllegalStateException("target vnf-id could not be determined");
84         }
85
86         GenericVnf genvnf = cq.getGenericVnfByModelInvariantId(params.getTarget().getResourceID());
87         if (genvnf == null) {
88             logger.info("{}: target entity could not be found for {}", getFullName(), params.getRequestId());
89             throw new IllegalArgumentException("target vnf-id could not be found");
90         }
91
92         return genvnf.getVnfId();
93     }
94 }