Address sonar issues 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  * 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.common.endpoints.event.comm.Topic.CommInfrastructure;
37 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
38 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
39 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
40 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
41 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig;
42 import org.onap.policy.so.SoModelInfo;
43 import org.onap.policy.so.SoOperationType;
44 import org.onap.policy.so.SoRelatedInstance;
45 import org.onap.policy.so.SoRelatedInstanceListElement;
46 import org.onap.policy.so.SoRequest;
47 import org.onap.policy.so.SoRequestDetails;
48 import org.onap.policy.so.SoRequestParameters;
49 import org.onap.policy.so.SoResponse;
50
51 /**
52  * Operation to create a VF Module. When this completes successfully, it increments its VF
53  * Count property.
54  */
55 public class VfModuleCreate extends SoOperation {
56     public static final String NAME = "VF Module Create";
57
58     private static final String PATH_PREFIX = "/";
59
60     // @formatter:off
61     private static final List<String> PROPERTY_NAMES = List.of(
62                             OperationProperties.AAI_SERVICE,
63                             OperationProperties.AAI_SERVICE_MODEL,
64                             OperationProperties.AAI_VNF,
65                             OperationProperties.AAI_VNF_MODEL,
66                             OperationProperties.AAI_DEFAULT_CLOUD_REGION,
67                             OperationProperties.AAI_DEFAULT_TENANT,
68                             OperationProperties.DATA_VF_COUNT);
69     // @formatter:off
70
71     /**
72      * Constructs the object.
73      *
74      * @param params operation parameters
75      * @param config configuration for this operation
76      */
77     public VfModuleCreate(ControlLoopOperationParams params, HttpPollingConfig config) {
78         super(params, config, PROPERTY_NAMES, params.getTargetEntityIds());
79
80         setUsePolling();
81         // ensure we have the necessary parameters
82         validateTarget();
83     }
84
85     @Override
86     protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
87
88         // starting a whole new attempt - reset the count
89         resetPollCount();
90
91         Pair<String, SoRequest> pair = makeRequest();
92         String path = getPath() + pair.getLeft();
93         SoRequest request = pair.getRight();
94
95         String url = getClient().getBaseUrl() + path;
96
97         String strRequest = prettyPrint(request);
98         logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest);
99
100         Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON);
101
102         Map<String, Object> headers = createSimpleHeaders();
103
104         return handleResponse(outcome, url, callback -> getClient().post(callback, path, entity, headers));
105     }
106
107     /**
108      * Increments the VF count that's stored in the context, if the request was
109      * successful.
110      */
111     @Override
112     protected Status detmStatus(Response rawResponse, SoResponse response) {
113         Status status = super.detmStatus(rawResponse, response);
114
115         if (status == Status.SUCCESS) {
116             setVfCount(getVfCount() + 1);
117         }
118
119         return status;
120     }
121
122     /**
123      * Makes a request.
124      *
125      * @return a pair containing the request URL and the new request
126      */
127     protected Pair<String, SoRequest> makeRequest() {
128         final SoModelInfo soModelInfo = prepareSoModelInfo();
129         final GenericVnf vnfItem = getVnfItem();
130         final ServiceInstance vnfServiceItem = getServiceInstance();
131         final Tenant tenantItem = getDefaultTenant();
132         final CloudRegion cloudRegionItem = getDefaultCloudRegion();
133         final ModelVer vnfModel = getVnfModel();
134         final ModelVer vnfServiceModel = getServiceModel();
135
136         SoRequest request = new SoRequest();
137         request.setOperationType(SoOperationType.SCALE_OUT);
138
139         //
140         //
141         // Do NOT send SO the requestId, they do not support this field
142         //
143         request.setRequestDetails(new SoRequestDetails());
144         request.getRequestDetails().setRequestParameters(new SoRequestParameters());
145         request.getRequestDetails().getRequestParameters().setUserParams(null);
146
147         // cloudConfiguration
148         request.getRequestDetails().setCloudConfiguration(constructCloudConfiguration(tenantItem, cloudRegionItem));
149
150         // modelInfo
151         request.getRequestDetails().setModelInfo(soModelInfo);
152
153         // requestInfo
154         request.getRequestDetails().setRequestInfo(constructRequestInfo());
155         request.getRequestDetails().getRequestInfo().setInstanceName("vfModuleName");
156
157         // relatedInstanceList
158         SoRelatedInstanceListElement relatedInstanceListElement1 = new SoRelatedInstanceListElement();
159         SoRelatedInstanceListElement relatedInstanceListElement2 = new SoRelatedInstanceListElement();
160         relatedInstanceListElement1.setRelatedInstance(new SoRelatedInstance());
161         relatedInstanceListElement2.setRelatedInstance(new SoRelatedInstance());
162
163         // Service Item
164         relatedInstanceListElement1.getRelatedInstance().setInstanceId(vnfServiceItem.getServiceInstanceId());
165         relatedInstanceListElement1.getRelatedInstance().setModelInfo(new SoModelInfo());
166         relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelType("service");
167         relatedInstanceListElement1.getRelatedInstance().getModelInfo()
168                         .setModelInvariantId(vnfServiceItem.getModelInvariantId());
169         relatedInstanceListElement1.getRelatedInstance().getModelInfo()
170                         .setModelVersionId(vnfServiceItem.getModelVersionId());
171         relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelName(vnfModel.getModelName());
172         relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelVersion(vnfModel.getModelVersion());
173
174         // VNF Item
175         relatedInstanceListElement2.getRelatedInstance().setInstanceId(vnfItem.getVnfId());
176         relatedInstanceListElement2.getRelatedInstance().setModelInfo(new SoModelInfo());
177         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelType("vnf");
178         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
179                         .setModelInvariantId(vnfItem.getModelInvariantId());
180         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersionId(vnfItem.getModelVersionId());
181
182         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelName(vnfServiceModel.getModelName());
183         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
184                         .setModelVersion(vnfServiceModel.getModelVersion());
185
186         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
187                         .setModelCustomizationId(vnfItem.getModelCustomizationId());
188
189         // Insert the Service Item and VNF Item
190         request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement1);
191         request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2);
192
193         // Request Parameters
194         buildRequestParameters().ifPresent(request.getRequestDetails()::setRequestParameters);
195
196         // Configuration Parameters
197         buildConfigurationParameters().ifPresent(request.getRequestDetails()::setConfigurationParameters);
198
199         // compute the path
200         String path = PATH_PREFIX + vnfServiceItem.getServiceInstanceId() + "/vnfs/" + vnfItem.getVnfId()
201                         + "/vfModules/scaleOut";
202
203         return Pair.of(path, request);
204     }
205 }