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