Merge "Modify Actors to use properties when provided"
[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.List;
24 import java.util.Map;
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.ModelVer;
33 import org.onap.aai.domain.yang.ServiceInstance;
34 import org.onap.aai.domain.yang.Tenant;
35 import org.onap.policy.aai.AaiConstants;
36 import org.onap.policy.aai.AaiCqResponse;
37 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
38 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
39 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
40 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
41 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
42 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig;
43 import org.onap.policy.so.SoModelInfo;
44 import org.onap.policy.so.SoOperationType;
45 import org.onap.policy.so.SoRelatedInstance;
46 import org.onap.policy.so.SoRelatedInstanceListElement;
47 import org.onap.policy.so.SoRequest;
48 import org.onap.policy.so.SoRequestDetails;
49 import org.onap.policy.so.SoRequestParameters;
50 import org.onap.policy.so.SoResponse;
51
52 /**
53  * Operation to create a VF Module. This gets the VF count from the A&AI Custom Query
54  * response and stores it in the context. It also passes the count+1 to the guard. Once
55  * the "create" completes successfully, it bumps the VF count that's stored in the
56  * context.
57  */
58 public class VfModuleCreate extends SoOperation {
59     public static final String NAME = "VF Module Create";
60
61     private static final String PATH_PREFIX = "/";
62
63     // @formatter:off
64     private static final List<String> PROPERTY_NAMES = List.of(
65                             OperationProperties.AAI_SERVICE,
66                             OperationProperties.AAI_SERVICE_MODEL,
67                             OperationProperties.AAI_VNF,
68                             OperationProperties.AAI_VNF_MODEL,
69                             OperationProperties.AAI_DEFAULT_CLOUD_REGION,
70                             OperationProperties.AAI_DEFAULT_TENANT,
71                             OperationProperties.DATA_VF_COUNT);
72     // @formatter:off
73
74     /**
75      * Constructs the object.
76      *
77      * @param params operation parameters
78      * @param config configuration for this operation
79      */
80     public VfModuleCreate(ControlLoopOperationParams params, HttpPollingConfig config) {
81         super(params, config, PROPERTY_NAMES);
82
83         // ensure we have the necessary parameters
84         validateTarget();
85     }
86
87     /**
88      * Ensures that A&AI custom query has been performed, and then runs the guard.
89      */
90     @Override
91     @SuppressWarnings("unchecked")
92     protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
93         if (params.isPreprocessed()) {
94             return null;
95         }
96
97         // need the VF count
98         ControlLoopOperationParams cqParams = params.toBuilder().actor(AaiConstants.ACTOR_NAME)
99                         .operation(AaiCqResponse.OPERATION).payload(null).retry(null).timeoutSec(null).build();
100
101         // run Custom Query, extract the VF count, and then run the Guard
102
103         // @formatter:off
104         return sequence(() -> params.getContext().obtain(AaiCqResponse.CONTEXT_KEY, cqParams),
105                         this::obtainVfCount, this::startGuardAsync);
106         // @formatter:on
107     }
108
109     @Override
110     protected Map<String, Object> makeGuardPayload() {
111         Map<String, Object> payload = super.makeGuardPayload();
112
113         // run guard with the proposed vf count
114         payload.put(PAYLOAD_KEY_VF_COUNT, getVfCount() + 1);
115
116         return payload;
117     }
118
119     @Override
120     protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
121
122         // starting a whole new attempt - reset the count
123         resetPollCount();
124
125         Pair<String, SoRequest> pair = makeRequest();
126         String path = getPath() + pair.getLeft();
127         SoRequest request = pair.getRight();
128
129         String url = getClient().getBaseUrl() + path;
130
131         String strRequest = prettyPrint(request);
132         logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest);
133
134         Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON);
135
136         Map<String, Object> headers = createSimpleHeaders();
137
138         return handleResponse(outcome, url, callback -> getClient().post(callback, path, entity, headers));
139     }
140
141     /**
142      * Increments the VF count that's stored in the context, if the request was
143      * successful.
144      */
145     @Override
146     protected Status detmStatus(Response rawResponse, SoResponse response) {
147         Status status = super.detmStatus(rawResponse, response);
148
149         if (status == Status.SUCCESS) {
150             setVfCount(getVfCount() + 1);
151         }
152
153         return status;
154     }
155
156     /**
157      * Makes a request.
158      *
159      * @return a pair containing the request URL and the new request
160      */
161     protected Pair<String, SoRequest> makeRequest() {
162         final SoModelInfo soModelInfo = prepareSoModelInfo();
163         final GenericVnf vnfItem = getVnfItem(soModelInfo);
164         final ServiceInstance vnfServiceItem = getServiceInstance();
165         final Tenant tenantItem = getDefaultTenant();
166         final CloudRegion cloudRegionItem = getDefaultCloudRegion();
167         final ModelVer vnfModel = getVnfModel(vnfItem);
168         final ModelVer vnfServiceModel = getServiceModel(vnfServiceItem);
169
170         SoRequest request = new SoRequest();
171         request.setOperationType(SoOperationType.SCALE_OUT);
172
173         //
174         //
175         // Do NOT send SO the requestId, they do not support this field
176         //
177         request.setRequestDetails(new SoRequestDetails());
178         request.getRequestDetails().setRequestParameters(new SoRequestParameters());
179         request.getRequestDetails().getRequestParameters().setUserParams(null);
180
181         // cloudConfiguration
182         request.getRequestDetails().setCloudConfiguration(constructCloudConfigurationCq(tenantItem, cloudRegionItem));
183
184         // modelInfo
185         request.getRequestDetails().setModelInfo(soModelInfo);
186
187         // requestInfo
188         request.getRequestDetails().setRequestInfo(constructRequestInfo());
189         request.getRequestDetails().getRequestInfo().setInstanceName("vfModuleName");
190
191         // relatedInstanceList
192         SoRelatedInstanceListElement relatedInstanceListElement1 = new SoRelatedInstanceListElement();
193         SoRelatedInstanceListElement relatedInstanceListElement2 = new SoRelatedInstanceListElement();
194         relatedInstanceListElement1.setRelatedInstance(new SoRelatedInstance());
195         relatedInstanceListElement2.setRelatedInstance(new SoRelatedInstance());
196
197         // Service Item
198         relatedInstanceListElement1.getRelatedInstance().setInstanceId(vnfServiceItem.getServiceInstanceId());
199         relatedInstanceListElement1.getRelatedInstance().setModelInfo(new SoModelInfo());
200         relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelType("service");
201         relatedInstanceListElement1.getRelatedInstance().getModelInfo()
202                         .setModelInvariantId(vnfServiceItem.getModelInvariantId());
203         relatedInstanceListElement1.getRelatedInstance().getModelInfo()
204                         .setModelVersionId(vnfServiceItem.getModelVersionId());
205         relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelName(vnfModel.getModelName());
206         relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelVersion(vnfModel.getModelVersion());
207
208         // VNF Item
209         relatedInstanceListElement2.getRelatedInstance().setInstanceId(vnfItem.getVnfId());
210         relatedInstanceListElement2.getRelatedInstance().setModelInfo(new SoModelInfo());
211         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelType("vnf");
212         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
213                         .setModelInvariantId(vnfItem.getModelInvariantId());
214         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersionId(vnfItem.getModelVersionId());
215
216         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelName(vnfServiceModel.getModelName());
217         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
218                         .setModelVersion(vnfServiceModel.getModelVersion());
219
220         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
221                         .setModelCustomizationId(vnfItem.getModelCustomizationId());
222
223         // Insert the Service Item and VNF Item
224         request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement1);
225         request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2);
226
227         // Request Parameters
228         buildRequestParameters().ifPresent(request.getRequestDetails()::setRequestParameters);
229
230         // Configuration Parameters
231         buildConfigurationParameters().ifPresent(request.getRequestDetails()::setConfigurationParameters);
232
233         // compute the path
234         String path = PATH_PREFIX + vnfServiceItem.getServiceInstanceId() + "/vnfs/" + vnfItem.getVnfId()
235                         + "/vfModules/scaleOut";
236
237         return Pair.of(path, request);
238     }
239 }