2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2020 Wipro Limited.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.actor.so;
24 import java.util.List;
26 import java.util.concurrent.CompletableFuture;
27 import javax.ws.rs.client.Entity;
28 import javax.ws.rs.core.MediaType;
29 import javax.ws.rs.core.Response;
30 import org.apache.commons.lang3.tuple.Pair;
31 import org.onap.aai.domain.yang.CloudRegion;
32 import org.onap.aai.domain.yang.GenericVnf;
33 import org.onap.aai.domain.yang.ModelVer;
34 import org.onap.aai.domain.yang.ServiceInstance;
35 import org.onap.aai.domain.yang.Tenant;
36 import org.onap.policy.aai.AaiConstants;
37 import org.onap.policy.aai.AaiCqResponse;
38 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
39 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
40 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
41 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
42 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
43 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig;
44 import org.onap.policy.so.SoModelInfo;
45 import org.onap.policy.so.SoOperationType;
46 import org.onap.policy.so.SoRelatedInstance;
47 import org.onap.policy.so.SoRelatedInstanceListElement;
48 import org.onap.policy.so.SoRequest;
49 import org.onap.policy.so.SoRequestDetails;
50 import org.onap.policy.so.SoRequestParameters;
51 import org.onap.policy.so.SoResponse;
54 * Operation to create a VF Module. This gets the VF count from the A&AI Custom Query
55 * response and stores it in the context. It also passes the count+1 to the guard. Once
56 * the "create" completes successfully, it bumps the VF count that's stored in the
59 public class VfModuleCreate extends SoOperation {
60 public static final String NAME = "VF Module Create";
62 private static final String PATH_PREFIX = "/";
65 private static final List<String> PROPERTY_NAMES = List.of(
66 OperationProperties.AAI_SERVICE,
67 OperationProperties.AAI_SERVICE_MODEL,
68 OperationProperties.AAI_VNF,
69 OperationProperties.AAI_VNF_MODEL,
70 OperationProperties.AAI_DEFAULT_CLOUD_REGION,
71 OperationProperties.AAI_DEFAULT_TENANT,
72 OperationProperties.DATA_VF_COUNT);
76 * Constructs the object.
78 * @param params operation parameters
79 * @param config configuration for this operation
81 public VfModuleCreate(ControlLoopOperationParams params, HttpPollingConfig config) {
82 super(params, config, PROPERTY_NAMES, params.getTargetEntityIds());
85 // ensure we have the necessary parameters
90 * Ensures that A&AI custom query has been performed, and then runs the guard.
93 @SuppressWarnings("unchecked")
94 protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
95 if (params.isPreprocessed()) {
100 ControlLoopOperationParams cqParams = params.toBuilder().actor(AaiConstants.ACTOR_NAME)
101 .operation(AaiCqResponse.OPERATION).payload(null).retry(null).timeoutSec(null).build();
103 // run Custom Query, extract the VF count, and then run the Guard
106 return sequence(() -> params.getContext().obtain(AaiCqResponse.CONTEXT_KEY, cqParams),
107 this::obtainVfCount, this::startGuardAsync);
112 protected Map<String, Object> makeGuardPayload() {
113 Map<String, Object> payload = super.makeGuardPayload();
115 // run guard with the proposed vf count
116 payload.put(PAYLOAD_KEY_VF_COUNT, getVfCount() + 1);
122 protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
124 // starting a whole new attempt - reset the count
127 Pair<String, SoRequest> pair = makeRequest();
128 String path = getPath() + pair.getLeft();
129 SoRequest request = pair.getRight();
131 String url = getClient().getBaseUrl() + path;
133 String strRequest = prettyPrint(request);
134 logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest);
136 Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON);
138 Map<String, Object> headers = createSimpleHeaders();
140 return handleResponse(outcome, url, callback -> getClient().post(callback, path, entity, headers));
144 * Increments the VF count that's stored in the context, if the request was
148 protected Status detmStatus(Response rawResponse, SoResponse response) {
149 Status status = super.detmStatus(rawResponse, response);
151 if (status == Status.SUCCESS) {
152 setVfCount(getVfCount() + 1);
161 * @return a pair containing the request URL and the new request
163 protected Pair<String, SoRequest> makeRequest() {
164 final SoModelInfo soModelInfo = prepareSoModelInfo();
165 final GenericVnf vnfItem = getVnfItem(soModelInfo);
166 final ServiceInstance vnfServiceItem = getServiceInstance();
167 final Tenant tenantItem = getDefaultTenant();
168 final CloudRegion cloudRegionItem = getDefaultCloudRegion();
169 final ModelVer vnfModel = getVnfModel(vnfItem);
170 final ModelVer vnfServiceModel = getServiceModel(vnfServiceItem);
172 SoRequest request = new SoRequest();
173 request.setOperationType(SoOperationType.SCALE_OUT);
177 // Do NOT send SO the requestId, they do not support this field
179 request.setRequestDetails(new SoRequestDetails());
180 request.getRequestDetails().setRequestParameters(new SoRequestParameters());
181 request.getRequestDetails().getRequestParameters().setUserParams(null);
183 // cloudConfiguration
184 request.getRequestDetails().setCloudConfiguration(constructCloudConfigurationCq(tenantItem, cloudRegionItem));
187 request.getRequestDetails().setModelInfo(soModelInfo);
190 request.getRequestDetails().setRequestInfo(constructRequestInfo());
191 request.getRequestDetails().getRequestInfo().setInstanceName("vfModuleName");
193 // relatedInstanceList
194 SoRelatedInstanceListElement relatedInstanceListElement1 = new SoRelatedInstanceListElement();
195 SoRelatedInstanceListElement relatedInstanceListElement2 = new SoRelatedInstanceListElement();
196 relatedInstanceListElement1.setRelatedInstance(new SoRelatedInstance());
197 relatedInstanceListElement2.setRelatedInstance(new SoRelatedInstance());
200 relatedInstanceListElement1.getRelatedInstance().setInstanceId(vnfServiceItem.getServiceInstanceId());
201 relatedInstanceListElement1.getRelatedInstance().setModelInfo(new SoModelInfo());
202 relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelType("service");
203 relatedInstanceListElement1.getRelatedInstance().getModelInfo()
204 .setModelInvariantId(vnfServiceItem.getModelInvariantId());
205 relatedInstanceListElement1.getRelatedInstance().getModelInfo()
206 .setModelVersionId(vnfServiceItem.getModelVersionId());
207 relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelName(vnfModel.getModelName());
208 relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelVersion(vnfModel.getModelVersion());
211 relatedInstanceListElement2.getRelatedInstance().setInstanceId(vnfItem.getVnfId());
212 relatedInstanceListElement2.getRelatedInstance().setModelInfo(new SoModelInfo());
213 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelType("vnf");
214 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
215 .setModelInvariantId(vnfItem.getModelInvariantId());
216 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersionId(vnfItem.getModelVersionId());
218 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelName(vnfServiceModel.getModelName());
219 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
220 .setModelVersion(vnfServiceModel.getModelVersion());
222 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
223 .setModelCustomizationId(vnfItem.getModelCustomizationId());
225 // Insert the Service Item and VNF Item
226 request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement1);
227 request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2);
229 // Request Parameters
230 buildRequestParameters().ifPresent(request.getRequestDetails()::setRequestParameters);
232 // Configuration Parameters
233 buildConfigurationParameters().ifPresent(request.getRequestDetails()::setConfigurationParameters);
236 String path = PATH_PREFIX + vnfServiceItem.getServiceInstanceId() + "/vnfs/" + vnfItem.getVnfId()
237 + "/vfModules/scaleOut";
239 return Pair.of(path, request);