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.concurrent.CompletableFuture;
24 import javax.ws.rs.client.Entity;
25 import javax.ws.rs.core.MediaType;
26 import org.apache.commons.lang3.tuple.Pair;
27 import org.onap.aai.domain.yang.CloudRegion;
28 import org.onap.aai.domain.yang.GenericVnf;
29 import org.onap.aai.domain.yang.ServiceInstance;
30 import org.onap.aai.domain.yang.Tenant;
31 import org.onap.policy.aai.AaiConstants;
32 import org.onap.policy.aai.AaiCqResponse;
33 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
34 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
35 import org.onap.policy.controlloop.actor.aai.AaiCustomQueryOperation;
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;
48 public class VfModuleCreate extends SoOperation {
49 public static final String NAME = "VF Module Create";
51 public VfModuleCreate(ControlLoopOperationParams params, HttpConfig config) {
52 super(params, config);
56 * Ensures that A&AI customer query has been performed, and then runs the guard query.
59 @SuppressWarnings("unchecked")
60 protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
61 ControlLoopOperationParams cqParams = params.toBuilder().actor(AaiConstants.ACTOR_NAME)
62 .operation(AaiCustomQueryOperation.NAME).payload(null).retry(null).timeoutSec(null).build();
64 // run Custom Query and Guard, in parallel
65 return allOf(() -> params.getContext().obtain(AaiCqResponse.CONTEXT_KEY, cqParams), this::startGuardAsync);
69 protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
71 // starting a whole new attempt - reset the count
74 Pair<String, SoRequest> pair = makeRequest();
75 String path = pair.getLeft();
76 SoRequest request = pair.getRight();
78 Entity<SoRequest> entity = Entity.entity(request, MediaType.APPLICATION_JSON);
79 String url = getClient().getBaseUrl() + path;
81 logMessage(EventType.OUT, CommInfrastructure.REST, url, request);
83 // TODO should this use "path" or the full "url"?
85 return handleResponse(outcome, url, callback -> getClient().post(callback, path, entity, null));
91 * @return a pair containing the request URL and the new request
93 protected Pair<String, SoRequest> makeRequest() {
94 final AaiCqResponse aaiCqResponse = params.getContext().getProperty(AaiCqResponse.CONTEXT_KEY);
95 final SoModelInfo soModelInfo = prepareSoModelInfo();
96 final GenericVnf vnfItem = getVnfItem(aaiCqResponse, soModelInfo);
97 final ServiceInstance vnfServiceItem = getServiceInstance(aaiCqResponse);
98 final Tenant tenantItem = getDefaultTenant(aaiCqResponse);
99 final CloudRegion cloudRegionItem = getDefaultCloudRegion(aaiCqResponse);
101 SoRequest request = new SoRequest();
102 request.setOperationType(SoOperationType.SCALE_OUT);
106 // Do NOT send SO the requestId, they do not support this field
108 request.setRequestDetails(new SoRequestDetails());
109 request.getRequestDetails().setRequestParameters(new SoRequestParameters());
110 request.getRequestDetails().getRequestParameters().setUserParams(null);
112 // cloudConfiguration
113 request.getRequestDetails().setCloudConfiguration(constructCloudConfigurationCq(tenantItem, cloudRegionItem));
116 request.getRequestDetails().setModelInfo(soModelInfo);
119 request.getRequestDetails().setRequestInfo(constructRequestInfo());
120 request.getRequestDetails().getRequestInfo().setInstanceName("vfModuleName");
122 // relatedInstanceList
123 SoRelatedInstanceListElement relatedInstanceListElement1 = new SoRelatedInstanceListElement();
124 SoRelatedInstanceListElement relatedInstanceListElement2 = new SoRelatedInstanceListElement();
125 relatedInstanceListElement1.setRelatedInstance(new SoRelatedInstance());
126 relatedInstanceListElement2.setRelatedInstance(new SoRelatedInstance());
129 relatedInstanceListElement1.getRelatedInstance().setInstanceId(vnfServiceItem.getServiceInstanceId());
130 relatedInstanceListElement1.getRelatedInstance().setModelInfo(new SoModelInfo());
131 relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelType("service");
132 relatedInstanceListElement1.getRelatedInstance().getModelInfo()
133 .setModelInvariantId(vnfServiceItem.getModelInvariantId());
134 relatedInstanceListElement1.getRelatedInstance().getModelInfo()
135 .setModelVersionId(vnfServiceItem.getModelVersionId());
136 relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelName(
137 aaiCqResponse.getModelVerByVersionId(vnfServiceItem.getModelVersionId()).getModelName());
138 relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelVersion(
139 aaiCqResponse.getModelVerByVersionId(vnfServiceItem.getModelVersionId()).getModelVersion());
142 relatedInstanceListElement2.getRelatedInstance().setInstanceId(vnfItem.getVnfId());
143 relatedInstanceListElement2.getRelatedInstance().setModelInfo(new SoModelInfo());
144 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelType("vnf");
145 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
146 .setModelInvariantId(vnfItem.getModelInvariantId());
147 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersionId(vnfItem.getModelVersionId());
149 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
150 .setModelName(aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelName());
151 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersion(
152 aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelVersion());
154 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
155 .setModelCustomizationId(vnfItem.getModelCustomizationId());
157 // Insert the Service Item and VNF Item
158 request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement1);
159 request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2);
161 // Request Parameters
162 request.getRequestDetails().setRequestParameters(buildRequestParameters());
164 // Configuration Parameters
165 request.getRequestDetails().setConfigurationParameters(buildConfigurationParameters());
168 String path = "/serviceInstantiation/v7/serviceInstances/" + vnfServiceItem.getServiceInstanceId() + "/vnfs/"
169 + vnfItem.getVnfId() + "/vfModules/scaleOut";
171 return Pair.of(path, request);
175 * Construct cloudConfiguration for the SO requestDetails. Overridden for custom
178 * @param tenantItem tenant item from A&AI named-query response
179 * @return SO cloud configuration
181 private SoCloudConfiguration constructCloudConfigurationCq(Tenant tenantItem, CloudRegion cloudRegionItem) {
182 SoCloudConfiguration cloudConfiguration = new SoCloudConfiguration();
183 cloudConfiguration.setTenantId(tenantItem.getTenantId());
184 cloudConfiguration.setLcpCloudRegionId(cloudRegionItem.getCloudRegionId());
185 return cloudConfiguration;