Merge "Minor fix to avoid ConcurrentModificationException in policy-models"
[policy/models.git] / models-interactions / model-actors / actor.so / src / main / java / org / onap / policy / controlloop / actor / so / VfModuleCreate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.actor.so;
22
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.so.SoCloudConfiguration;
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;
46
47 public class VfModuleCreate extends SoOperation {
48     public static final String NAME = "VF Module Create";
49
50     public VfModuleCreate(ControlLoopOperationParams params, SoOperator operator) {
51         super(params, operator);
52     }
53
54     /**
55      * Ensures that A&AI customer query has been performed, and then runs the guard query.
56      */
57     @Override
58     @SuppressWarnings("unchecked")
59     protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
60         ControlLoopOperationParams cqParams = params.toBuilder().actor(AaiConstants.ACTOR_NAME)
61                         .operation(AaiCustomQueryOperation.NAME).payload(null).retry(null).timeoutSec(null).build();
62
63         // run Custom Query and Guard, in parallel
64         return allOf(() -> params.getContext().obtain(AaiCqResponse.CONTEXT_KEY, cqParams), this::startGuardAsync);
65     }
66
67     @Override
68     protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
69
70         // starting a whole new attempt - reset the count
71         resetGetCount();
72
73         Pair<String, SoRequest> pair = makeRequest();
74         String path = pair.getLeft();
75         SoRequest request = pair.getRight();
76
77         Entity<SoRequest> entity = Entity.entity(request, MediaType.APPLICATION_JSON);
78         String url = getOperator().getClient().getBaseUrl() + path;
79
80         logMessage(EventType.OUT, CommInfrastructure.REST, url, request);
81
82         // TODO should this use "path" or the full "url"?
83
84         return handleResponse(outcome, url, callback -> getOperator().getClient().post(callback, path, entity, null));
85     }
86
87     /**
88      * Makes a request.
89      *
90      * @return a pair containing the request URL and the new request
91      */
92     protected Pair<String, SoRequest> makeRequest() {
93         final AaiCqResponse aaiCqResponse = params.getContext().getProperty(AaiCqResponse.CONTEXT_KEY);
94         final SoModelInfo soModelInfo = prepareSoModelInfo();
95         final GenericVnf vnfItem = getVnfItem(aaiCqResponse, soModelInfo);
96         final ServiceInstance vnfServiceItem = getServiceInstance(aaiCqResponse);
97         final Tenant tenantItem = getDefaultTenant(aaiCqResponse);
98         final CloudRegion cloudRegionItem = getDefaultCloudRegion(aaiCqResponse);
99
100         SoRequest request = new SoRequest();
101         request.setOperationType(SoOperationType.SCALE_OUT);
102
103         //
104         //
105         // Do NOT send SO the requestId, they do not support this field
106         //
107         request.setRequestDetails(new SoRequestDetails());
108         request.getRequestDetails().setRequestParameters(new SoRequestParameters());
109         request.getRequestDetails().getRequestParameters().setUserParams(null);
110
111         // cloudConfiguration
112         request.getRequestDetails().setCloudConfiguration(constructCloudConfigurationCq(tenantItem, cloudRegionItem));
113
114         // modelInfo
115         request.getRequestDetails().setModelInfo(soModelInfo);
116
117         // requestInfo
118         request.getRequestDetails().setRequestInfo(constructRequestInfo());
119         request.getRequestDetails().getRequestInfo().setInstanceName("vfModuleName");
120
121         // relatedInstanceList
122         SoRelatedInstanceListElement relatedInstanceListElement1 = new SoRelatedInstanceListElement();
123         SoRelatedInstanceListElement relatedInstanceListElement2 = new SoRelatedInstanceListElement();
124         relatedInstanceListElement1.setRelatedInstance(new SoRelatedInstance());
125         relatedInstanceListElement2.setRelatedInstance(new SoRelatedInstance());
126
127         // Service Item
128         relatedInstanceListElement1.getRelatedInstance().setInstanceId(vnfServiceItem.getServiceInstanceId());
129         relatedInstanceListElement1.getRelatedInstance().setModelInfo(new SoModelInfo());
130         relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelType("service");
131         relatedInstanceListElement1.getRelatedInstance().getModelInfo()
132                         .setModelInvariantId(vnfServiceItem.getModelInvariantId());
133         relatedInstanceListElement1.getRelatedInstance().getModelInfo()
134                         .setModelVersionId(vnfServiceItem.getModelVersionId());
135         relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelName(
136                         aaiCqResponse.getModelVerByVersionId(vnfServiceItem.getModelVersionId()).getModelName());
137         relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelVersion(
138                         aaiCqResponse.getModelVerByVersionId(vnfServiceItem.getModelVersionId()).getModelVersion());
139
140         // VNF Item
141         relatedInstanceListElement2.getRelatedInstance().setInstanceId(vnfItem.getVnfId());
142         relatedInstanceListElement2.getRelatedInstance().setModelInfo(new SoModelInfo());
143         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelType("vnf");
144         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
145                         .setModelInvariantId(vnfItem.getModelInvariantId());
146         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersionId(vnfItem.getModelVersionId());
147
148         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
149                         .setModelName(aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelName());
150         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersion(
151                         aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelVersion());
152
153         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
154                         .setModelCustomizationId(vnfItem.getModelCustomizationId());
155
156         // Insert the Service Item and VNF Item
157         request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement1);
158         request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2);
159
160         // Request Parameters
161         request.getRequestDetails().setRequestParameters(buildRequestParameters());
162
163         // Configuration Parameters
164         request.getRequestDetails().setConfigurationParameters(buildConfigurationParameters());
165
166         // compute the path
167         String path = "/serviceInstantiation/v7/serviceInstances/" + vnfServiceItem.getServiceInstanceId() + "/vnfs/"
168                         + vnfItem.getVnfId() + "/vfModules/scaleOut";
169
170         return Pair.of(path, request);
171     }
172
173     /**
174      * Construct cloudConfiguration for the SO requestDetails. Overridden for custom
175      * query.
176      *
177      * @param tenantItem tenant item from A&AI named-query response
178      * @return SO cloud configuration
179      */
180     private SoCloudConfiguration constructCloudConfigurationCq(Tenant tenantItem, CloudRegion cloudRegionItem) {
181         SoCloudConfiguration cloudConfiguration = new SoCloudConfiguration();
182         cloudConfiguration.setTenantId(tenantItem.getTenantId());
183         cloudConfiguration.setLcpCloudRegionId(cloudRegionItem.getCloudRegionId());
184         return cloudConfiguration;
185     }
186 }