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.SoModelInfo;
40 import org.onap.policy.so.SoOperationType;
41 import org.onap.policy.so.SoRelatedInstance;
42 import org.onap.policy.so.SoRelatedInstanceListElement;
43 import org.onap.policy.so.SoRequest;
44 import org.onap.policy.so.SoRequestDetails;
45 import org.onap.policy.so.SoRequestParameters;
48 * Operation to create a VF Module. This gets the VF count from the A&AI Custom Query
49 * response and stores it in the context. It also passes the count+1 to the guard. Once
50 * the "create" completes successfully, it bumps the VF count that's stored in the
53 public class VfModuleCreate extends SoOperation {
54 public static final String NAME = "VF Module Create";
56 private static final String PATH_PREFIX = "/";
59 * Constructs the object.
61 * @param params operation parameters
62 * @param config configuration for this operation
64 public VfModuleCreate(ControlLoopOperationParams params, HttpConfig config) {
65 super(params, config);
67 // ensure we have the necessary parameters
72 * Ensures that A&AI custom query has been performed, and then runs the guard.
75 @SuppressWarnings("unchecked")
76 protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
79 ControlLoopOperationParams cqParams = params.toBuilder().actor(AaiConstants.ACTOR_NAME)
80 .operation(AaiCqResponse.OPERATION).payload(null).retry(null).timeoutSec(null).build();
82 // run Custom Query, extract the VF count, and then run the Guard
85 return sequence(() -> params.getContext().obtain(AaiCqResponse.CONTEXT_KEY, cqParams),
86 this::obtainVfCount, this::startGuardAsync);
91 protected Map<String, Object> makeGuardPayload() {
92 Map<String, Object> payload = super.makeGuardPayload();
94 // run guard with the proposed vf count
95 payload.put(PAYLOAD_KEY_VF_COUNT, getVfCount() + 1);
101 protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
103 // starting a whole new attempt - reset the count
106 Pair<String, SoRequest> pair = makeRequest();
107 String path = getPath() + pair.getLeft();
108 SoRequest request = pair.getRight();
110 Entity<SoRequest> entity = Entity.entity(request, MediaType.APPLICATION_JSON);
111 String url = getClient().getBaseUrl() + path;
113 logMessage(EventType.OUT, CommInfrastructure.REST, url, request);
115 Map<String, Object> headers = createSimpleHeaders();
117 return handleResponse(outcome, url, callback -> getClient().post(callback, path, entity, headers));
121 * Increments the VF count that's stored in the context.
124 protected void successfulCompletion() {
125 setVfCount(getVfCount() + 1);
131 * @return a pair containing the request URL and the new request
133 protected Pair<String, SoRequest> makeRequest() {
134 final AaiCqResponse aaiCqResponse = params.getContext().getProperty(AaiCqResponse.CONTEXT_KEY);
135 final SoModelInfo soModelInfo = prepareSoModelInfo();
136 final GenericVnf vnfItem = getVnfItem(aaiCqResponse, soModelInfo);
137 final ServiceInstance vnfServiceItem = getServiceInstance(aaiCqResponse);
138 final Tenant tenantItem = getDefaultTenant(aaiCqResponse);
139 final CloudRegion cloudRegionItem = getDefaultCloudRegion(aaiCqResponse);
141 SoRequest request = new SoRequest();
142 request.setOperationType(SoOperationType.SCALE_OUT);
146 // Do NOT send SO the requestId, they do not support this field
148 request.setRequestDetails(new SoRequestDetails());
149 request.getRequestDetails().setRequestParameters(new SoRequestParameters());
150 request.getRequestDetails().getRequestParameters().setUserParams(null);
152 // cloudConfiguration
153 request.getRequestDetails().setCloudConfiguration(constructCloudConfigurationCq(tenantItem, cloudRegionItem));
156 request.getRequestDetails().setModelInfo(soModelInfo);
159 request.getRequestDetails().setRequestInfo(constructRequestInfo());
160 request.getRequestDetails().getRequestInfo().setInstanceName("vfModuleName");
162 // relatedInstanceList
163 SoRelatedInstanceListElement relatedInstanceListElement1 = new SoRelatedInstanceListElement();
164 SoRelatedInstanceListElement relatedInstanceListElement2 = new SoRelatedInstanceListElement();
165 relatedInstanceListElement1.setRelatedInstance(new SoRelatedInstance());
166 relatedInstanceListElement2.setRelatedInstance(new SoRelatedInstance());
169 relatedInstanceListElement1.getRelatedInstance().setInstanceId(vnfServiceItem.getServiceInstanceId());
170 relatedInstanceListElement1.getRelatedInstance().setModelInfo(new SoModelInfo());
171 relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelType("service");
172 relatedInstanceListElement1.getRelatedInstance().getModelInfo()
173 .setModelInvariantId(vnfServiceItem.getModelInvariantId());
174 relatedInstanceListElement1.getRelatedInstance().getModelInfo()
175 .setModelVersionId(vnfServiceItem.getModelVersionId());
176 relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelName(
177 aaiCqResponse.getModelVerByVersionId(vnfServiceItem.getModelVersionId()).getModelName());
178 relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelVersion(
179 aaiCqResponse.getModelVerByVersionId(vnfServiceItem.getModelVersionId()).getModelVersion());
182 relatedInstanceListElement2.getRelatedInstance().setInstanceId(vnfItem.getVnfId());
183 relatedInstanceListElement2.getRelatedInstance().setModelInfo(new SoModelInfo());
184 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelType("vnf");
185 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
186 .setModelInvariantId(vnfItem.getModelInvariantId());
187 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersionId(vnfItem.getModelVersionId());
189 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
190 .setModelName(aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelName());
191 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersion(
192 aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelVersion());
194 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
195 .setModelCustomizationId(vnfItem.getModelCustomizationId());
197 // Insert the Service Item and VNF Item
198 request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement1);
199 request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2);
201 // Request Parameters
202 buildRequestParameters().ifPresent(request.getRequestDetails()::setRequestParameters);
204 // Configuration Parameters
205 buildConfigurationParameters().ifPresent(request.getRequestDetails()::setConfigurationParameters);
208 String path = PATH_PREFIX + vnfServiceItem.getServiceInstanceId() + "/vnfs/" + vnfItem.getVnfId()
209 + "/vfModules/scaleOut";
211 return Pair.of(path, request);