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() {
92 ControlLoopOperationParams cqParams = params.toBuilder().actor(AaiConstants.ACTOR_NAME)
93 .operation(AaiCqResponse.OPERATION).payload(null).retry(null).timeoutSec(null).build();
95 // run Custom Query, extract the VF count, and then run the Guard
98 return sequence(() -> params.getContext().obtain(AaiCqResponse.CONTEXT_KEY, cqParams),
99 this::obtainVfCount, this::startGuardAsync);
104 protected Map<String, Object> makeGuardPayload() {
105 Map<String, Object> payload = super.makeGuardPayload();
107 // run guard with the proposed vf count
108 payload.put(PAYLOAD_KEY_VF_COUNT, getVfCount() + 1);
114 protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
116 // starting a whole new attempt - reset the count
119 Pair<String, SoRequest> pair = makeRequest();
120 String path = getPath() + pair.getLeft();
121 SoRequest request = pair.getRight();
123 String url = getClient().getBaseUrl() + path;
125 String strRequest = prettyPrint(request);
126 logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest);
128 Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON);
130 Map<String, Object> headers = createSimpleHeaders();
132 return handleResponse(outcome, url, callback -> getClient().post(callback, path, entity, headers));
136 * Increments the VF count that's stored in the context, if the request was
140 protected Status detmStatus(Response rawResponse, SoResponse response) {
141 Status status = super.detmStatus(rawResponse, response);
143 if (status == Status.SUCCESS) {
144 setVfCount(getVfCount() + 1);
153 * @return a pair containing the request URL and the new request
155 protected Pair<String, SoRequest> makeRequest() {
156 final AaiCqResponse aaiCqResponse = params.getContext().getProperty(AaiCqResponse.CONTEXT_KEY);
157 final SoModelInfo soModelInfo = prepareSoModelInfo();
158 final GenericVnf vnfItem = getVnfItem(aaiCqResponse, soModelInfo);
159 final ServiceInstance vnfServiceItem = getServiceInstance(aaiCqResponse);
160 final Tenant tenantItem = getDefaultTenant(aaiCqResponse);
161 final CloudRegion cloudRegionItem = getDefaultCloudRegion(aaiCqResponse);
163 SoRequest request = new SoRequest();
164 request.setOperationType(SoOperationType.SCALE_OUT);
168 // Do NOT send SO the requestId, they do not support this field
170 request.setRequestDetails(new SoRequestDetails());
171 request.getRequestDetails().setRequestParameters(new SoRequestParameters());
172 request.getRequestDetails().getRequestParameters().setUserParams(null);
174 // cloudConfiguration
175 request.getRequestDetails().setCloudConfiguration(constructCloudConfigurationCq(tenantItem, cloudRegionItem));
178 request.getRequestDetails().setModelInfo(soModelInfo);
181 request.getRequestDetails().setRequestInfo(constructRequestInfo());
182 request.getRequestDetails().getRequestInfo().setInstanceName("vfModuleName");
184 // relatedInstanceList
185 SoRelatedInstanceListElement relatedInstanceListElement1 = new SoRelatedInstanceListElement();
186 SoRelatedInstanceListElement relatedInstanceListElement2 = new SoRelatedInstanceListElement();
187 relatedInstanceListElement1.setRelatedInstance(new SoRelatedInstance());
188 relatedInstanceListElement2.setRelatedInstance(new SoRelatedInstance());
191 relatedInstanceListElement1.getRelatedInstance().setInstanceId(vnfServiceItem.getServiceInstanceId());
192 relatedInstanceListElement1.getRelatedInstance().setModelInfo(new SoModelInfo());
193 relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelType("service");
194 relatedInstanceListElement1.getRelatedInstance().getModelInfo()
195 .setModelInvariantId(vnfServiceItem.getModelInvariantId());
196 relatedInstanceListElement1.getRelatedInstance().getModelInfo()
197 .setModelVersionId(vnfServiceItem.getModelVersionId());
198 relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelName(
199 aaiCqResponse.getModelVerByVersionId(vnfServiceItem.getModelVersionId()).getModelName());
200 relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelVersion(
201 aaiCqResponse.getModelVerByVersionId(vnfServiceItem.getModelVersionId()).getModelVersion());
204 relatedInstanceListElement2.getRelatedInstance().setInstanceId(vnfItem.getVnfId());
205 relatedInstanceListElement2.getRelatedInstance().setModelInfo(new SoModelInfo());
206 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelType("vnf");
207 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
208 .setModelInvariantId(vnfItem.getModelInvariantId());
209 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersionId(vnfItem.getModelVersionId());
211 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
212 .setModelName(aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelName());
213 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersion(
214 aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelVersion());
216 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
217 .setModelCustomizationId(vnfItem.getModelCustomizationId());
219 // Insert the Service Item and VNF Item
220 request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement1);
221 request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2);
223 // Request Parameters
224 buildRequestParameters().ifPresent(request.getRequestDetails()::setRequestParameters);
226 // Configuration Parameters
227 buildConfigurationParameters().ifPresent(request.getRequestDetails()::setConfigurationParameters);
230 String path = PATH_PREFIX + vnfServiceItem.getServiceInstanceId() + "/vnfs/" + vnfItem.getVnfId()
231 + "/vfModules/scaleOut";
233 return Pair.of(path, request);