Move CQ operation name from actor.aai to aai
[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.Map;
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.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;
47
48 /**
49  * Operation to create a VF Module. This gets the VF count from the A&AI Custom Query
50  * response and stores it in the context. It also passes the count+1 to the guard. Once
51  * the "create" completes successfully, it bumps the VF count that's stored in the
52  * context.
53  * <p/>
54  * Note: currently, this only supports storing the count for a single target VF.
55  */
56 public class VfModuleCreate extends SoOperation {
57     public static final String NAME = "VF Module Create";
58
59     public static final String PAYLOAD_KEY_VF_COUNT = "vfCount";
60
61     /**
62      * Constructs the object.
63      *
64      * @param params operation parameters
65      * @param config configuration for this operation
66      */
67     public VfModuleCreate(ControlLoopOperationParams params, HttpConfig config) {
68         super(params, config);
69
70         // ensure we have the necessary parameters
71         validateTarget();
72     }
73
74     /**
75      * Ensures that A&AI customer query has been performed, and then runs the guard.
76      */
77     @Override
78     @SuppressWarnings("unchecked")
79     protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
80         if (params.getContext().contains(SoConstants.CONTEXT_KEY_VF_COUNT)) {
81             return startGuardAsync();
82         }
83
84         // need the VF count
85         ControlLoopOperationParams cqParams = params.toBuilder().actor(AaiConstants.ACTOR_NAME)
86                         .operation(AaiCqResponse.OPERATION).payload(null).retry(null).timeoutSec(null).build();
87
88         // run Custom Query, extract the VF count, and then run the Guard
89         return sequence(() -> params.getContext().obtain(AaiCqResponse.CONTEXT_KEY, cqParams),
90                         this::storeVfCountRunGuard);
91     }
92
93     @Override
94     protected Map<String, Object> makeGuardPayload() {
95         Map<String, Object> payload = super.makeGuardPayload();
96
97         int vfcount = params.getContext().getProperty(SoConstants.CONTEXT_KEY_VF_COUNT);
98
99         // run guard with the proposed vf count
100         payload.put(PAYLOAD_KEY_VF_COUNT, vfcount + 1);
101
102         return payload;
103     }
104
105     @Override
106     protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
107
108         // starting a whole new attempt - reset the count
109         resetGetCount();
110
111         Pair<String, SoRequest> pair = makeRequest();
112         String path = pair.getLeft();
113         SoRequest request = pair.getRight();
114
115         Entity<SoRequest> entity = Entity.entity(request, MediaType.APPLICATION_JSON);
116         String url = getClient().getBaseUrl() + path;
117
118         logMessage(EventType.OUT, CommInfrastructure.REST, url, request);
119
120         // TODO should this use "path" or the full "url"?
121
122         return handleResponse(outcome, url, callback -> getClient().post(callback, path, entity, null));
123     }
124
125     /**
126      * Increments the VF count that's stored in the context.
127      */
128     @Override
129     protected void successfulCompletion() {
130         int vfcount = params.getContext().getProperty(SoConstants.CONTEXT_KEY_VF_COUNT);
131         params.getContext().setProperty(SoConstants.CONTEXT_KEY_VF_COUNT, vfcount + 1);
132     }
133
134     /**
135      * Makes a request.
136      *
137      * @return a pair containing the request URL and the new request
138      */
139     protected Pair<String, SoRequest> makeRequest() {
140         final AaiCqResponse aaiCqResponse = params.getContext().getProperty(AaiCqResponse.CONTEXT_KEY);
141         final SoModelInfo soModelInfo = prepareSoModelInfo();
142         final GenericVnf vnfItem = getVnfItem(aaiCqResponse, soModelInfo);
143         final ServiceInstance vnfServiceItem = getServiceInstance(aaiCqResponse);
144         final Tenant tenantItem = getDefaultTenant(aaiCqResponse);
145         final CloudRegion cloudRegionItem = getDefaultCloudRegion(aaiCqResponse);
146
147         SoRequest request = new SoRequest();
148         request.setOperationType(SoOperationType.SCALE_OUT);
149
150         //
151         //
152         // Do NOT send SO the requestId, they do not support this field
153         //
154         request.setRequestDetails(new SoRequestDetails());
155         request.getRequestDetails().setRequestParameters(new SoRequestParameters());
156         request.getRequestDetails().getRequestParameters().setUserParams(null);
157
158         // cloudConfiguration
159         request.getRequestDetails().setCloudConfiguration(constructCloudConfigurationCq(tenantItem, cloudRegionItem));
160
161         // modelInfo
162         request.getRequestDetails().setModelInfo(soModelInfo);
163
164         // requestInfo
165         request.getRequestDetails().setRequestInfo(constructRequestInfo());
166         request.getRequestDetails().getRequestInfo().setInstanceName("vfModuleName");
167
168         // relatedInstanceList
169         SoRelatedInstanceListElement relatedInstanceListElement1 = new SoRelatedInstanceListElement();
170         SoRelatedInstanceListElement relatedInstanceListElement2 = new SoRelatedInstanceListElement();
171         relatedInstanceListElement1.setRelatedInstance(new SoRelatedInstance());
172         relatedInstanceListElement2.setRelatedInstance(new SoRelatedInstance());
173
174         // Service Item
175         relatedInstanceListElement1.getRelatedInstance().setInstanceId(vnfServiceItem.getServiceInstanceId());
176         relatedInstanceListElement1.getRelatedInstance().setModelInfo(new SoModelInfo());
177         relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelType("service");
178         relatedInstanceListElement1.getRelatedInstance().getModelInfo()
179                         .setModelInvariantId(vnfServiceItem.getModelInvariantId());
180         relatedInstanceListElement1.getRelatedInstance().getModelInfo()
181                         .setModelVersionId(vnfServiceItem.getModelVersionId());
182         relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelName(
183                         aaiCqResponse.getModelVerByVersionId(vnfServiceItem.getModelVersionId()).getModelName());
184         relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelVersion(
185                         aaiCqResponse.getModelVerByVersionId(vnfServiceItem.getModelVersionId()).getModelVersion());
186
187         // VNF Item
188         relatedInstanceListElement2.getRelatedInstance().setInstanceId(vnfItem.getVnfId());
189         relatedInstanceListElement2.getRelatedInstance().setModelInfo(new SoModelInfo());
190         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelType("vnf");
191         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
192                         .setModelInvariantId(vnfItem.getModelInvariantId());
193         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersionId(vnfItem.getModelVersionId());
194
195         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
196                         .setModelName(aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelName());
197         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersion(
198                         aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelVersion());
199
200         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
201                         .setModelCustomizationId(vnfItem.getModelCustomizationId());
202
203         // Insert the Service Item and VNF Item
204         request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement1);
205         request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2);
206
207         // Request Parameters
208         request.getRequestDetails().setRequestParameters(buildRequestParameters());
209
210         // Configuration Parameters
211         request.getRequestDetails().setConfigurationParameters(buildConfigurationParameters());
212
213         // compute the path
214         String path = "/serviceInstantiation/v7/serviceInstances/" + vnfServiceItem.getServiceInstanceId() + "/vnfs/"
215                         + vnfItem.getVnfId() + "/vfModules/scaleOut";
216
217         return Pair.of(path, request);
218     }
219
220     /**
221      * Construct cloudConfiguration for the SO requestDetails. Overridden for custom
222      * query.
223      *
224      * @param tenantItem tenant item from A&AI named-query response
225      * @return SO cloud configuration
226      */
227     private SoCloudConfiguration constructCloudConfigurationCq(Tenant tenantItem, CloudRegion cloudRegionItem) {
228         SoCloudConfiguration cloudConfiguration = new SoCloudConfiguration();
229         cloudConfiguration.setTenantId(tenantItem.getTenantId());
230         cloudConfiguration.setLcpCloudRegionId(cloudRegionItem.getCloudRegionId());
231         return cloudConfiguration;
232     }
233 }