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;
24 import java.util.concurrent.CompletableFuture;
25 import javax.ws.rs.client.Entity;
26 import javax.ws.rs.core.MediaType;
27 import org.apache.commons.lang3.tuple.Pair;
28 import org.onap.aai.domain.yang.CloudRegion;
29 import org.onap.aai.domain.yang.GenericVnf;
30 import org.onap.aai.domain.yang.ServiceInstance;
31 import org.onap.aai.domain.yang.Tenant;
32 import org.onap.policy.aai.AaiConstants;
33 import org.onap.policy.aai.AaiCqResponse;
34 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
35 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
36 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
37 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
38 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
39 import org.onap.policy.so.SoCloudConfiguration;
40 import org.onap.policy.so.SoModelInfo;
41 import org.onap.policy.so.SoOperationType;
42 import org.onap.policy.so.SoRelatedInstance;
43 import org.onap.policy.so.SoRelatedInstanceListElement;
44 import org.onap.policy.so.SoRequest;
45 import org.onap.policy.so.SoRequestDetails;
46 import org.onap.policy.so.SoRequestParameters;
49 * Operation to create a VF Module. This gets the VF count from the A&AI Custom Query
50 * response and stores it in the context. It also passes the count+1 to the guard. Once
51 * the "create" completes successfully, it bumps the VF count that's stored in the
54 * Note: currently, this only supports storing the count for a single target VF.
56 public class VfModuleCreate extends SoOperation {
57 public static final String NAME = "VF Module Create";
59 public static final String PAYLOAD_KEY_VF_COUNT = "vfCount";
62 * Constructs the object.
64 * @param params operation parameters
65 * @param config configuration for this operation
67 public VfModuleCreate(ControlLoopOperationParams params, HttpConfig config) {
68 super(params, config);
70 // ensure we have the necessary parameters
75 * Ensures that A&AI customer query has been performed, and then runs the guard.
78 @SuppressWarnings("unchecked")
79 protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
82 ControlLoopOperationParams cqParams = params.toBuilder().actor(AaiConstants.ACTOR_NAME)
83 .operation(AaiCqResponse.OPERATION).payload(null).retry(null).timeoutSec(null).build();
85 // run Custom Query, extract the VF count, and then run the Guard
88 return sequence(() -> params.getContext().obtain(AaiCqResponse.CONTEXT_KEY, cqParams),
89 this::obtainVfCount, this::startGuardAsync);
94 protected Map<String, Object> makeGuardPayload() {
95 Map<String, Object> payload = super.makeGuardPayload();
97 // run guard with the proposed vf count
98 payload.put(PAYLOAD_KEY_VF_COUNT, getVfCount() + 1);
104 protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
106 // starting a whole new attempt - reset the count
109 Pair<String, SoRequest> pair = makeRequest();
110 String path = getPath() + pair.getLeft();
111 SoRequest request = pair.getRight();
113 Entity<SoRequest> entity = Entity.entity(request, MediaType.APPLICATION_JSON);
114 String url = getClient().getBaseUrl() + path;
116 logMessage(EventType.OUT, CommInfrastructure.REST, url, request);
118 return handleResponse(outcome, url, callback -> getClient().post(callback, path, entity, null));
122 * Increments the VF count that's stored in the context.
125 protected void successfulCompletion() {
126 setVfCount(getVfCount() + 1);
132 * @return a pair containing the request URL and the new request
134 protected Pair<String, SoRequest> makeRequest() {
135 final AaiCqResponse aaiCqResponse = params.getContext().getProperty(AaiCqResponse.CONTEXT_KEY);
136 final SoModelInfo soModelInfo = prepareSoModelInfo();
137 final GenericVnf vnfItem = getVnfItem(aaiCqResponse, soModelInfo);
138 final ServiceInstance vnfServiceItem = getServiceInstance(aaiCqResponse);
139 final Tenant tenantItem = getDefaultTenant(aaiCqResponse);
140 final CloudRegion cloudRegionItem = getDefaultCloudRegion(aaiCqResponse);
142 SoRequest request = new SoRequest();
143 request.setOperationType(SoOperationType.SCALE_OUT);
147 // Do NOT send SO the requestId, they do not support this field
149 request.setRequestDetails(new SoRequestDetails());
150 request.getRequestDetails().setRequestParameters(new SoRequestParameters());
151 request.getRequestDetails().getRequestParameters().setUserParams(null);
153 // cloudConfiguration
154 request.getRequestDetails().setCloudConfiguration(constructCloudConfigurationCq(tenantItem, cloudRegionItem));
157 request.getRequestDetails().setModelInfo(soModelInfo);
160 request.getRequestDetails().setRequestInfo(constructRequestInfo());
161 request.getRequestDetails().getRequestInfo().setInstanceName("vfModuleName");
163 // relatedInstanceList
164 SoRelatedInstanceListElement relatedInstanceListElement1 = new SoRelatedInstanceListElement();
165 SoRelatedInstanceListElement relatedInstanceListElement2 = new SoRelatedInstanceListElement();
166 relatedInstanceListElement1.setRelatedInstance(new SoRelatedInstance());
167 relatedInstanceListElement2.setRelatedInstance(new SoRelatedInstance());
170 relatedInstanceListElement1.getRelatedInstance().setInstanceId(vnfServiceItem.getServiceInstanceId());
171 relatedInstanceListElement1.getRelatedInstance().setModelInfo(new SoModelInfo());
172 relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelType("service");
173 relatedInstanceListElement1.getRelatedInstance().getModelInfo()
174 .setModelInvariantId(vnfServiceItem.getModelInvariantId());
175 relatedInstanceListElement1.getRelatedInstance().getModelInfo()
176 .setModelVersionId(vnfServiceItem.getModelVersionId());
177 relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelName(
178 aaiCqResponse.getModelVerByVersionId(vnfServiceItem.getModelVersionId()).getModelName());
179 relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelVersion(
180 aaiCqResponse.getModelVerByVersionId(vnfServiceItem.getModelVersionId()).getModelVersion());
183 relatedInstanceListElement2.getRelatedInstance().setInstanceId(vnfItem.getVnfId());
184 relatedInstanceListElement2.getRelatedInstance().setModelInfo(new SoModelInfo());
185 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelType("vnf");
186 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
187 .setModelInvariantId(vnfItem.getModelInvariantId());
188 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersionId(vnfItem.getModelVersionId());
190 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
191 .setModelName(aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelName());
192 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersion(
193 aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelVersion());
195 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
196 .setModelCustomizationId(vnfItem.getModelCustomizationId());
198 // Insert the Service Item and VNF Item
199 request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement1);
200 request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2);
202 // Request Parameters
203 buildRequestParameters().ifPresent(request.getRequestDetails()::setRequestParameters);
205 // Configuration Parameters
206 buildConfigurationParameters().ifPresent(request.getRequestDetails()::setConfigurationParameters);
209 String path = "/serviceInstances/" + vnfServiceItem.getServiceInstanceId() + "/vnfs/" + vnfItem.getVnfId()
210 + "/vfModules/scaleOut";
212 return Pair.of(path, request);
216 * Construct cloudConfiguration for the SO requestDetails. Overridden for custom
219 * @param tenantItem tenant item from A&AI named-query response
220 * @return SO cloud configuration
222 private SoCloudConfiguration constructCloudConfigurationCq(Tenant tenantItem, CloudRegion cloudRegionItem) {
223 SoCloudConfiguration cloudConfiguration = new SoCloudConfiguration();
224 cloudConfiguration.setTenantId(tenantItem.getTenantId());
225 cloudConfiguration.setLcpCloudRegionId(cloudRegionItem.getCloudRegionId());
226 return cloudConfiguration;