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         String url = getClient().getBaseUrl() + path;
 
 112         String strRequest = prettyPrint(request);
 
 113         logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest);
 
 115         Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON);
 
 117         Map<String, Object> headers = createSimpleHeaders();
 
 119         return handleResponse(outcome, url, callback -> getClient().post(callback, path, entity, headers));
 
 123      * Increments the VF count that's stored in the context.
 
 126     protected void successfulCompletion() {
 
 127         setVfCount(getVfCount() + 1);
 
 133      * @return a pair containing the request URL and the new request
 
 135     protected Pair<String, SoRequest> makeRequest() {
 
 136         final AaiCqResponse aaiCqResponse = params.getContext().getProperty(AaiCqResponse.CONTEXT_KEY);
 
 137         final SoModelInfo soModelInfo = prepareSoModelInfo();
 
 138         final GenericVnf vnfItem = getVnfItem(aaiCqResponse, soModelInfo);
 
 139         final ServiceInstance vnfServiceItem = getServiceInstance(aaiCqResponse);
 
 140         final Tenant tenantItem = getDefaultTenant(aaiCqResponse);
 
 141         final CloudRegion cloudRegionItem = getDefaultCloudRegion(aaiCqResponse);
 
 143         SoRequest request = new SoRequest();
 
 144         request.setOperationType(SoOperationType.SCALE_OUT);
 
 148         // Do NOT send SO the requestId, they do not support this field
 
 150         request.setRequestDetails(new SoRequestDetails());
 
 151         request.getRequestDetails().setRequestParameters(new SoRequestParameters());
 
 152         request.getRequestDetails().getRequestParameters().setUserParams(null);
 
 154         // cloudConfiguration
 
 155         request.getRequestDetails().setCloudConfiguration(constructCloudConfigurationCq(tenantItem, cloudRegionItem));
 
 158         request.getRequestDetails().setModelInfo(soModelInfo);
 
 161         request.getRequestDetails().setRequestInfo(constructRequestInfo());
 
 162         request.getRequestDetails().getRequestInfo().setInstanceName("vfModuleName");
 
 164         // relatedInstanceList
 
 165         SoRelatedInstanceListElement relatedInstanceListElement1 = new SoRelatedInstanceListElement();
 
 166         SoRelatedInstanceListElement relatedInstanceListElement2 = new SoRelatedInstanceListElement();
 
 167         relatedInstanceListElement1.setRelatedInstance(new SoRelatedInstance());
 
 168         relatedInstanceListElement2.setRelatedInstance(new SoRelatedInstance());
 
 171         relatedInstanceListElement1.getRelatedInstance().setInstanceId(vnfServiceItem.getServiceInstanceId());
 
 172         relatedInstanceListElement1.getRelatedInstance().setModelInfo(new SoModelInfo());
 
 173         relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelType("service");
 
 174         relatedInstanceListElement1.getRelatedInstance().getModelInfo()
 
 175                         .setModelInvariantId(vnfServiceItem.getModelInvariantId());
 
 176         relatedInstanceListElement1.getRelatedInstance().getModelInfo()
 
 177                         .setModelVersionId(vnfServiceItem.getModelVersionId());
 
 178         relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelName(
 
 179                         aaiCqResponse.getModelVerByVersionId(vnfServiceItem.getModelVersionId()).getModelName());
 
 180         relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelVersion(
 
 181                         aaiCqResponse.getModelVerByVersionId(vnfServiceItem.getModelVersionId()).getModelVersion());
 
 184         relatedInstanceListElement2.getRelatedInstance().setInstanceId(vnfItem.getVnfId());
 
 185         relatedInstanceListElement2.getRelatedInstance().setModelInfo(new SoModelInfo());
 
 186         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelType("vnf");
 
 187         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
 
 188                         .setModelInvariantId(vnfItem.getModelInvariantId());
 
 189         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersionId(vnfItem.getModelVersionId());
 
 191         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
 
 192                         .setModelName(aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelName());
 
 193         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersion(
 
 194                         aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelVersion());
 
 196         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
 
 197                         .setModelCustomizationId(vnfItem.getModelCustomizationId());
 
 199         // Insert the Service Item and VNF Item
 
 200         request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement1);
 
 201         request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2);
 
 203         // Request Parameters
 
 204         buildRequestParameters().ifPresent(request.getRequestDetails()::setRequestParameters);
 
 206         // Configuration Parameters
 
 207         buildConfigurationParameters().ifPresent(request.getRequestDetails()::setConfigurationParameters);
 
 210         String path = PATH_PREFIX + vnfServiceItem.getServiceInstanceId() + "/vnfs/" + vnfItem.getVnfId()
 
 211                         + "/vfModules/scaleOut";
 
 213         return Pair.of(path, request);