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