2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.actor.so;
23 import java.util.List;
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.ModelVer;
33 import org.onap.aai.domain.yang.ServiceInstance;
34 import org.onap.aai.domain.yang.Tenant;
35 import org.onap.policy.aai.AaiConstants;
36 import org.onap.policy.aai.AaiCqResponse;
37 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
38 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
39 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
40 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
41 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
42 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig;
43 import org.onap.policy.so.SoModelInfo;
44 import org.onap.policy.so.SoOperationType;
45 import org.onap.policy.so.SoRelatedInstance;
46 import org.onap.policy.so.SoRelatedInstanceListElement;
47 import org.onap.policy.so.SoRequest;
48 import org.onap.policy.so.SoRequestDetails;
49 import org.onap.policy.so.SoRequestParameters;
50 import org.onap.policy.so.SoResponse;
53 * Operation to create a VF Module. This gets the VF count from the A&AI Custom Query
54 * response and stores it in the context. It also passes the count+1 to the guard. Once
55 * the "create" completes successfully, it bumps the VF count that's stored in the
58 public class VfModuleCreate extends SoOperation {
59 public static final String NAME = "VF Module Create";
61 private static final String PATH_PREFIX = "/";
64 private static final List<String> PROPERTY_NAMES = List.of(
65 OperationProperties.AAI_SERVICE,
66 OperationProperties.AAI_SERVICE_MODEL,
67 OperationProperties.AAI_VNF,
68 OperationProperties.AAI_VNF_MODEL,
69 OperationProperties.AAI_DEFAULT_CLOUD_REGION,
70 OperationProperties.AAI_DEFAULT_TENANT,
71 OperationProperties.DATA_VF_COUNT);
75 * Constructs the object.
77 * @param params operation parameters
78 * @param config configuration for this operation
80 public VfModuleCreate(ControlLoopOperationParams params, HttpPollingConfig config) {
81 super(params, config, PROPERTY_NAMES);
83 // ensure we have the necessary parameters
88 * Ensures that A&AI custom query has been performed, and then runs the guard.
91 @SuppressWarnings("unchecked")
92 protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
93 if (params.isPreprocessed()) {
98 ControlLoopOperationParams cqParams = params.toBuilder().actor(AaiConstants.ACTOR_NAME)
99 .operation(AaiCqResponse.OPERATION).payload(null).retry(null).timeoutSec(null).build();
101 // run Custom Query, extract the VF count, and then run the Guard
104 return sequence(() -> params.getContext().obtain(AaiCqResponse.CONTEXT_KEY, cqParams),
105 this::obtainVfCount, this::startGuardAsync);
110 protected Map<String, Object> makeGuardPayload() {
111 Map<String, Object> payload = super.makeGuardPayload();
113 // run guard with the proposed vf count
114 payload.put(PAYLOAD_KEY_VF_COUNT, getVfCount() + 1);
120 protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
122 // starting a whole new attempt - reset the count
125 Pair<String, SoRequest> pair = makeRequest();
126 String path = getPath() + pair.getLeft();
127 SoRequest request = pair.getRight();
129 String url = getClient().getBaseUrl() + path;
131 String strRequest = prettyPrint(request);
132 logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest);
134 Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON);
136 Map<String, Object> headers = createSimpleHeaders();
138 return handleResponse(outcome, url, callback -> getClient().post(callback, path, entity, headers));
142 * Increments the VF count that's stored in the context, if the request was
146 protected Status detmStatus(Response rawResponse, SoResponse response) {
147 Status status = super.detmStatus(rawResponse, response);
149 if (status == Status.SUCCESS) {
150 setVfCount(getVfCount() + 1);
159 * @return a pair containing the request URL and the new request
161 protected Pair<String, SoRequest> makeRequest() {
162 final SoModelInfo soModelInfo = prepareSoModelInfo();
163 final GenericVnf vnfItem = getVnfItem(soModelInfo);
164 final ServiceInstance vnfServiceItem = getServiceInstance();
165 final Tenant tenantItem = getDefaultTenant();
166 final CloudRegion cloudRegionItem = getDefaultCloudRegion();
167 final ModelVer vnfModel = getVnfModel(vnfItem);
168 final ModelVer vnfServiceModel = getServiceModel(vnfServiceItem);
170 SoRequest request = new SoRequest();
171 request.setOperationType(SoOperationType.SCALE_OUT);
175 // Do NOT send SO the requestId, they do not support this field
177 request.setRequestDetails(new SoRequestDetails());
178 request.getRequestDetails().setRequestParameters(new SoRequestParameters());
179 request.getRequestDetails().getRequestParameters().setUserParams(null);
181 // cloudConfiguration
182 request.getRequestDetails().setCloudConfiguration(constructCloudConfigurationCq(tenantItem, cloudRegionItem));
185 request.getRequestDetails().setModelInfo(soModelInfo);
188 request.getRequestDetails().setRequestInfo(constructRequestInfo());
189 request.getRequestDetails().getRequestInfo().setInstanceName("vfModuleName");
191 // relatedInstanceList
192 SoRelatedInstanceListElement relatedInstanceListElement1 = new SoRelatedInstanceListElement();
193 SoRelatedInstanceListElement relatedInstanceListElement2 = new SoRelatedInstanceListElement();
194 relatedInstanceListElement1.setRelatedInstance(new SoRelatedInstance());
195 relatedInstanceListElement2.setRelatedInstance(new SoRelatedInstance());
198 relatedInstanceListElement1.getRelatedInstance().setInstanceId(vnfServiceItem.getServiceInstanceId());
199 relatedInstanceListElement1.getRelatedInstance().setModelInfo(new SoModelInfo());
200 relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelType("service");
201 relatedInstanceListElement1.getRelatedInstance().getModelInfo()
202 .setModelInvariantId(vnfServiceItem.getModelInvariantId());
203 relatedInstanceListElement1.getRelatedInstance().getModelInfo()
204 .setModelVersionId(vnfServiceItem.getModelVersionId());
205 relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelName(vnfModel.getModelName());
206 relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelVersion(vnfModel.getModelVersion());
209 relatedInstanceListElement2.getRelatedInstance().setInstanceId(vnfItem.getVnfId());
210 relatedInstanceListElement2.getRelatedInstance().setModelInfo(new SoModelInfo());
211 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelType("vnf");
212 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
213 .setModelInvariantId(vnfItem.getModelInvariantId());
214 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersionId(vnfItem.getModelVersionId());
216 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelName(vnfServiceModel.getModelName());
217 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
218 .setModelVersion(vnfServiceModel.getModelVersion());
220 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
221 .setModelCustomizationId(vnfItem.getModelCustomizationId());
223 // Insert the Service Item and VNF Item
224 request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement1);
225 request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2);
227 // Request Parameters
228 buildRequestParameters().ifPresent(request.getRequestDetails()::setRequestParameters);
230 // Configuration Parameters
231 buildConfigurationParameters().ifPresent(request.getRequestDetails()::setConfigurationParameters);
234 String path = PATH_PREFIX + vnfServiceItem.getServiceInstanceId() + "/vnfs/" + vnfItem.getVnfId()
235 + "/vfModules/scaleOut";
237 return Pair.of(path, request);