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.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;
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
57 public class VfModuleCreate extends SoOperation {
58 public static final String NAME = "VF Module Create";
60 private static final String PATH_PREFIX = "/";
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);
72 * Constructs the object.
74 * @param params operation parameters
75 * @param config configuration for this operation
77 public VfModuleCreate(ControlLoopOperationParams params, HttpPollingConfig config) {
78 super(params, config, PROPERTY_NAMES);
80 // ensure we have the necessary parameters
85 * Ensures that A&AI custom query has been performed, and then runs the guard.
88 @SuppressWarnings("unchecked")
89 protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
90 if (params.isPreprocessed()) {
95 ControlLoopOperationParams cqParams = params.toBuilder().actor(AaiConstants.ACTOR_NAME)
96 .operation(AaiCqResponse.OPERATION).payload(null).retry(null).timeoutSec(null).build();
98 // run Custom Query, extract the VF count, and then run the Guard
101 return sequence(() -> params.getContext().obtain(AaiCqResponse.CONTEXT_KEY, cqParams),
102 this::obtainVfCount, this::startGuardAsync);
107 protected Map<String, Object> makeGuardPayload() {
108 Map<String, Object> payload = super.makeGuardPayload();
110 // run guard with the proposed vf count
111 payload.put(PAYLOAD_KEY_VF_COUNT, getVfCount() + 1);
117 protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
119 // starting a whole new attempt - reset the count
122 Pair<String, SoRequest> pair = makeRequest();
123 String path = getPath() + pair.getLeft();
124 SoRequest request = pair.getRight();
126 String url = getClient().getBaseUrl() + path;
128 String strRequest = prettyPrint(request);
129 logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest);
131 Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON);
133 Map<String, Object> headers = createSimpleHeaders();
135 return handleResponse(outcome, url, callback -> getClient().post(callback, path, entity, headers));
139 * Increments the VF count that's stored in the context, if the request was
143 protected Status detmStatus(Response rawResponse, SoResponse response) {
144 Status status = super.detmStatus(rawResponse, response);
146 if (status == Status.SUCCESS) {
147 setVfCount(getVfCount() + 1);
156 * @return a pair containing the request URL and the new request
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);
166 SoRequest request = new SoRequest();
167 request.setOperationType(SoOperationType.SCALE_OUT);
171 // Do NOT send SO the requestId, they do not support this field
173 request.setRequestDetails(new SoRequestDetails());
174 request.getRequestDetails().setRequestParameters(new SoRequestParameters());
175 request.getRequestDetails().getRequestParameters().setUserParams(null);
177 // cloudConfiguration
178 request.getRequestDetails().setCloudConfiguration(constructCloudConfigurationCq(tenantItem, cloudRegionItem));
181 request.getRequestDetails().setModelInfo(soModelInfo);
184 request.getRequestDetails().setRequestInfo(constructRequestInfo());
185 request.getRequestDetails().getRequestInfo().setInstanceName("vfModuleName");
187 // relatedInstanceList
188 SoRelatedInstanceListElement relatedInstanceListElement1 = new SoRelatedInstanceListElement();
189 SoRelatedInstanceListElement relatedInstanceListElement2 = new SoRelatedInstanceListElement();
190 relatedInstanceListElement1.setRelatedInstance(new SoRelatedInstance());
191 relatedInstanceListElement2.setRelatedInstance(new SoRelatedInstance());
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());
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());
214 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
215 .setModelName(aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelName());
216 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersion(
217 aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelVersion());
219 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
220 .setModelCustomizationId(vnfItem.getModelCustomizationId());
222 // Insert the Service Item and VNF Item
223 request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement1);
224 request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2);
226 // Request Parameters
227 buildRequestParameters().ifPresent(request.getRequestDetails()::setRequestParameters);
229 // Configuration Parameters
230 buildConfigurationParameters().ifPresent(request.getRequestDetails()::setConfigurationParameters);
233 String path = PATH_PREFIX + vnfServiceItem.getServiceInstanceId() + "/vnfs/" + vnfItem.getVnfId()
234 + "/vfModules/scaleOut";
236 return Pair.of(path, request);