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.actor.aai.AaiCustomQueryOperation;
37 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
38 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
39 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
40 import org.onap.policy.so.SoCloudConfiguration;
41 import org.onap.policy.so.SoModelInfo;
42 import org.onap.policy.so.SoOperationType;
43 import org.onap.policy.so.SoRelatedInstance;
44 import org.onap.policy.so.SoRelatedInstanceListElement;
45 import org.onap.policy.so.SoRequest;
46 import org.onap.policy.so.SoRequestDetails;
47 import org.onap.policy.so.SoRequestParameters;
50 * Operation to create a VF Module. This gets the VF count from the A&AI Custom Query
51 * response and stores it in the context. It also passes the count+1 to the guard. Once
52 * the "create" completes successfully, it bumps the VF count that's stored in the
55 * Note: currently, this only supports storing the count for a single target VF.
57 public class VfModuleCreate extends SoOperation {
58 public static final String NAME = "VF Module Create";
60 public static final String PAYLOAD_KEY_VF_COUNT = "vfCount";
63 * Constructs the object.
65 * @param params operation parameters
66 * @param config configuration for this operation
68 public VfModuleCreate(ControlLoopOperationParams params, HttpConfig config) {
69 super(params, config);
71 // ensure we have the necessary parameters
76 * Ensures that A&AI customer query has been performed, and then runs the guard.
79 @SuppressWarnings("unchecked")
80 protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
81 if (params.getContext().contains(SoConstants.CONTEXT_KEY_VF_COUNT)) {
82 return startGuardAsync();
86 ControlLoopOperationParams cqParams = params.toBuilder().actor(AaiConstants.ACTOR_NAME)
87 .operation(AaiCustomQueryOperation.NAME).payload(null).retry(null).timeoutSec(null).build();
89 // run Custom Query, extract the VF count, and then run the Guard
90 return sequence(() -> params.getContext().obtain(AaiCqResponse.CONTEXT_KEY, cqParams),
91 this::storeVfCountRunGuard);
95 protected Map<String, Object> makeGuardPayload() {
96 Map<String, Object> payload = super.makeGuardPayload();
98 int vfcount = params.getContext().getProperty(SoConstants.CONTEXT_KEY_VF_COUNT);
100 // run guard with the proposed vf count
101 payload.put(PAYLOAD_KEY_VF_COUNT, vfcount + 1);
107 protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
109 // starting a whole new attempt - reset the count
112 Pair<String, SoRequest> pair = makeRequest();
113 String path = pair.getLeft();
114 SoRequest request = pair.getRight();
116 Entity<SoRequest> entity = Entity.entity(request, MediaType.APPLICATION_JSON);
117 String url = getClient().getBaseUrl() + path;
119 logMessage(EventType.OUT, CommInfrastructure.REST, url, request);
121 // TODO should this use "path" or the full "url"?
123 return handleResponse(outcome, url, callback -> getClient().post(callback, path, entity, null));
127 * Increments the VF count that's stored in the context.
130 protected void successfulCompletion() {
131 int vfcount = params.getContext().getProperty(SoConstants.CONTEXT_KEY_VF_COUNT);
132 params.getContext().setProperty(SoConstants.CONTEXT_KEY_VF_COUNT, vfcount + 1);
138 * @return a pair containing the request URL and the new request
140 protected Pair<String, SoRequest> makeRequest() {
141 final AaiCqResponse aaiCqResponse = params.getContext().getProperty(AaiCqResponse.CONTEXT_KEY);
142 final SoModelInfo soModelInfo = prepareSoModelInfo();
143 final GenericVnf vnfItem = getVnfItem(aaiCqResponse, soModelInfo);
144 final ServiceInstance vnfServiceItem = getServiceInstance(aaiCqResponse);
145 final Tenant tenantItem = getDefaultTenant(aaiCqResponse);
146 final CloudRegion cloudRegionItem = getDefaultCloudRegion(aaiCqResponse);
148 SoRequest request = new SoRequest();
149 request.setOperationType(SoOperationType.SCALE_OUT);
153 // Do NOT send SO the requestId, they do not support this field
155 request.setRequestDetails(new SoRequestDetails());
156 request.getRequestDetails().setRequestParameters(new SoRequestParameters());
157 request.getRequestDetails().getRequestParameters().setUserParams(null);
159 // cloudConfiguration
160 request.getRequestDetails().setCloudConfiguration(constructCloudConfigurationCq(tenantItem, cloudRegionItem));
163 request.getRequestDetails().setModelInfo(soModelInfo);
166 request.getRequestDetails().setRequestInfo(constructRequestInfo());
167 request.getRequestDetails().getRequestInfo().setInstanceName("vfModuleName");
169 // relatedInstanceList
170 SoRelatedInstanceListElement relatedInstanceListElement1 = new SoRelatedInstanceListElement();
171 SoRelatedInstanceListElement relatedInstanceListElement2 = new SoRelatedInstanceListElement();
172 relatedInstanceListElement1.setRelatedInstance(new SoRelatedInstance());
173 relatedInstanceListElement2.setRelatedInstance(new SoRelatedInstance());
176 relatedInstanceListElement1.getRelatedInstance().setInstanceId(vnfServiceItem.getServiceInstanceId());
177 relatedInstanceListElement1.getRelatedInstance().setModelInfo(new SoModelInfo());
178 relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelType("service");
179 relatedInstanceListElement1.getRelatedInstance().getModelInfo()
180 .setModelInvariantId(vnfServiceItem.getModelInvariantId());
181 relatedInstanceListElement1.getRelatedInstance().getModelInfo()
182 .setModelVersionId(vnfServiceItem.getModelVersionId());
183 relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelName(
184 aaiCqResponse.getModelVerByVersionId(vnfServiceItem.getModelVersionId()).getModelName());
185 relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelVersion(
186 aaiCqResponse.getModelVerByVersionId(vnfServiceItem.getModelVersionId()).getModelVersion());
189 relatedInstanceListElement2.getRelatedInstance().setInstanceId(vnfItem.getVnfId());
190 relatedInstanceListElement2.getRelatedInstance().setModelInfo(new SoModelInfo());
191 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelType("vnf");
192 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
193 .setModelInvariantId(vnfItem.getModelInvariantId());
194 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersionId(vnfItem.getModelVersionId());
196 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
197 .setModelName(aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelName());
198 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersion(
199 aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelVersion());
201 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
202 .setModelCustomizationId(vnfItem.getModelCustomizationId());
204 // Insert the Service Item and VNF Item
205 request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement1);
206 request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2);
208 // Request Parameters
209 request.getRequestDetails().setRequestParameters(buildRequestParameters());
211 // Configuration Parameters
212 request.getRequestDetails().setConfigurationParameters(buildConfigurationParameters());
215 String path = "/serviceInstantiation/v7/serviceInstances/" + vnfServiceItem.getServiceInstanceId() + "/vnfs/"
216 + vnfItem.getVnfId() + "/vfModules/scaleOut";
218 return Pair.of(path, request);
222 * Construct cloudConfiguration for the SO requestDetails. Overridden for custom
225 * @param tenantItem tenant item from A&AI named-query response
226 * @return SO cloud configuration
228 private SoCloudConfiguration constructCloudConfigurationCq(Tenant tenantItem, CloudRegion cloudRegionItem) {
229 SoCloudConfiguration cloudConfiguration = new SoCloudConfiguration();
230 cloudConfiguration.setTenantId(tenantItem.getTenantId());
231 cloudConfiguration.setLcpCloudRegionId(cloudRegionItem.getCloudRegionId());
232 return cloudConfiguration;