Skip preprocessor step in Actors
[policy/models.git] / models-interactions / model-actors / actor.so / src / main / java / org / onap / policy / controlloop / actor / so / VfModuleCreate.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.so;
22
23 import java.util.List;
24 import java.util.Map;
25 import java.util.concurrent.CompletableFuture;
26 import javax.ws.rs.client.Entity;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
29 import org.apache.commons.lang3.tuple.Pair;
30 import org.onap.aai.domain.yang.CloudRegion;
31 import org.onap.aai.domain.yang.GenericVnf;
32 import org.onap.aai.domain.yang.ServiceInstance;
33 import org.onap.aai.domain.yang.Tenant;
34 import org.onap.policy.aai.AaiConstants;
35 import org.onap.policy.aai.AaiCqResponse;
36 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
37 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
38 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
39 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
40 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
41 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig;
42 import org.onap.policy.so.SoModelInfo;
43 import org.onap.policy.so.SoOperationType;
44 import org.onap.policy.so.SoRelatedInstance;
45 import org.onap.policy.so.SoRelatedInstanceListElement;
46 import org.onap.policy.so.SoRequest;
47 import org.onap.policy.so.SoRequestDetails;
48 import org.onap.policy.so.SoRequestParameters;
49 import org.onap.policy.so.SoResponse;
50
51 /**
52  * Operation to create a VF Module. This gets the VF count from the A&AI Custom Query
53  * response and stores it in the context. It also passes the count+1 to the guard. Once
54  * the "create" completes successfully, it bumps the VF count that's stored in the
55  * context.
56  */
57 public class VfModuleCreate extends SoOperation {
58     public static final String NAME = "VF Module Create";
59
60     private static final String PATH_PREFIX = "/";
61
62     // @formatter:off
63     private static final List<String> PROPERTY_NAMES = List.of(
64                             OperationProperties.AAI_MODEL_SERVICE,
65                             OperationProperties.AAI_MODEL_VNF,
66                             OperationProperties.AAI_MODEL_CLOUD_REGION,
67                             OperationProperties.AAI_MODEL_TENANT,
68                             OperationProperties.DATA_VF_COUNT);
69     // @formatter:off
70
71     /**
72      * Constructs the object.
73      *
74      * @param params operation parameters
75      * @param config configuration for this operation
76      */
77     public VfModuleCreate(ControlLoopOperationParams params, HttpPollingConfig config) {
78         super(params, config, PROPERTY_NAMES);
79
80         // ensure we have the necessary parameters
81         validateTarget();
82     }
83
84     /**
85      * Ensures that A&AI custom query has been performed, and then runs the guard.
86      */
87     @Override
88     @SuppressWarnings("unchecked")
89     protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
90         if (params.isPreprocessed()) {
91             return null;
92         }
93
94         // need the VF count
95         ControlLoopOperationParams cqParams = params.toBuilder().actor(AaiConstants.ACTOR_NAME)
96                         .operation(AaiCqResponse.OPERATION).payload(null).retry(null).timeoutSec(null).build();
97
98         // run Custom Query, extract the VF count, and then run the Guard
99
100         // @formatter:off
101         return sequence(() -> params.getContext().obtain(AaiCqResponse.CONTEXT_KEY, cqParams),
102                         this::obtainVfCount, this::startGuardAsync);
103         // @formatter:on
104     }
105
106     @Override
107     protected Map<String, Object> makeGuardPayload() {
108         Map<String, Object> payload = super.makeGuardPayload();
109
110         // run guard with the proposed vf count
111         payload.put(PAYLOAD_KEY_VF_COUNT, getVfCount() + 1);
112
113         return payload;
114     }
115
116     @Override
117     protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
118
119         // starting a whole new attempt - reset the count
120         resetPollCount();
121
122         Pair<String, SoRequest> pair = makeRequest();
123         String path = getPath() + pair.getLeft();
124         SoRequest request = pair.getRight();
125
126         String url = getClient().getBaseUrl() + path;
127
128         String strRequest = prettyPrint(request);
129         logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest);
130
131         Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON);
132
133         Map<String, Object> headers = createSimpleHeaders();
134
135         return handleResponse(outcome, url, callback -> getClient().post(callback, path, entity, headers));
136     }
137
138     /**
139      * Increments the VF count that's stored in the context, if the request was
140      * successful.
141      */
142     @Override
143     protected Status detmStatus(Response rawResponse, SoResponse response) {
144         Status status = super.detmStatus(rawResponse, response);
145
146         if (status == Status.SUCCESS) {
147             setVfCount(getVfCount() + 1);
148         }
149
150         return status;
151     }
152
153     /**
154      * Makes a request.
155      *
156      * @return a pair containing the request URL and the new request
157      */
158     protected Pair<String, SoRequest> makeRequest() {
159         final AaiCqResponse aaiCqResponse = params.getContext().getProperty(AaiCqResponse.CONTEXT_KEY);
160         final SoModelInfo soModelInfo = prepareSoModelInfo();
161         final GenericVnf vnfItem = getVnfItem(aaiCqResponse, soModelInfo);
162         final ServiceInstance vnfServiceItem = getServiceInstance(aaiCqResponse);
163         final Tenant tenantItem = getDefaultTenant(aaiCqResponse);
164         final CloudRegion cloudRegionItem = getDefaultCloudRegion(aaiCqResponse);
165
166         SoRequest request = new SoRequest();
167         request.setOperationType(SoOperationType.SCALE_OUT);
168
169         //
170         //
171         // Do NOT send SO the requestId, they do not support this field
172         //
173         request.setRequestDetails(new SoRequestDetails());
174         request.getRequestDetails().setRequestParameters(new SoRequestParameters());
175         request.getRequestDetails().getRequestParameters().setUserParams(null);
176
177         // cloudConfiguration
178         request.getRequestDetails().setCloudConfiguration(constructCloudConfigurationCq(tenantItem, cloudRegionItem));
179
180         // modelInfo
181         request.getRequestDetails().setModelInfo(soModelInfo);
182
183         // requestInfo
184         request.getRequestDetails().setRequestInfo(constructRequestInfo());
185         request.getRequestDetails().getRequestInfo().setInstanceName("vfModuleName");
186
187         // relatedInstanceList
188         SoRelatedInstanceListElement relatedInstanceListElement1 = new SoRelatedInstanceListElement();
189         SoRelatedInstanceListElement relatedInstanceListElement2 = new SoRelatedInstanceListElement();
190         relatedInstanceListElement1.setRelatedInstance(new SoRelatedInstance());
191         relatedInstanceListElement2.setRelatedInstance(new SoRelatedInstance());
192
193         // Service Item
194         relatedInstanceListElement1.getRelatedInstance().setInstanceId(vnfServiceItem.getServiceInstanceId());
195         relatedInstanceListElement1.getRelatedInstance().setModelInfo(new SoModelInfo());
196         relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelType("service");
197         relatedInstanceListElement1.getRelatedInstance().getModelInfo()
198                         .setModelInvariantId(vnfServiceItem.getModelInvariantId());
199         relatedInstanceListElement1.getRelatedInstance().getModelInfo()
200                         .setModelVersionId(vnfServiceItem.getModelVersionId());
201         relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelName(
202                         aaiCqResponse.getModelVerByVersionId(vnfServiceItem.getModelVersionId()).getModelName());
203         relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelVersion(
204                         aaiCqResponse.getModelVerByVersionId(vnfServiceItem.getModelVersionId()).getModelVersion());
205
206         // VNF Item
207         relatedInstanceListElement2.getRelatedInstance().setInstanceId(vnfItem.getVnfId());
208         relatedInstanceListElement2.getRelatedInstance().setModelInfo(new SoModelInfo());
209         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelType("vnf");
210         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
211                         .setModelInvariantId(vnfItem.getModelInvariantId());
212         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersionId(vnfItem.getModelVersionId());
213
214         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
215                         .setModelName(aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelName());
216         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersion(
217                         aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelVersion());
218
219         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
220                         .setModelCustomizationId(vnfItem.getModelCustomizationId());
221
222         // Insert the Service Item and VNF Item
223         request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement1);
224         request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2);
225
226         // Request Parameters
227         buildRequestParameters().ifPresent(request.getRequestDetails()::setRequestParameters);
228
229         // Configuration Parameters
230         buildConfigurationParameters().ifPresent(request.getRequestDetails()::setConfigurationParameters);
231
232         // compute the path
233         String path = PATH_PREFIX + vnfServiceItem.getServiceInstanceId() + "/vnfs/" + vnfItem.getVnfId()
234                         + "/vfModules/scaleOut";
235
236         return Pair.of(path, request);
237     }
238 }