Fixed SO request creation
[policy/models.git] / models-interactions / model-actors / actor.so / src / main / java / org / onap / policy / controlloop / actor / so / SoActorServiceProvider.java
1 /*
2  * ============LICENSE_START=======================================================
3  * SOActorServiceProvider
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nordix Foundation.
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 com.google.common.collect.ImmutableList;
25 import com.google.common.collect.ImmutableMap;
26 import com.google.gson.reflect.TypeToken;
27 import java.lang.reflect.Type;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.Map;
31 import org.onap.aai.domain.yang.CloudRegion;
32 import org.onap.aai.domain.yang.GenericVnf;
33 import org.onap.aai.domain.yang.ServiceInstance;
34 import org.onap.aai.domain.yang.Tenant;
35 import org.onap.policy.aai.AaiCqResponse;
36 import org.onap.policy.aai.AaiNqExtraProperty;
37 import org.onap.policy.aai.AaiNqInventoryResponseItem;
38 import org.onap.policy.aai.AaiNqResponseWrapper;
39 import org.onap.policy.controlloop.ControlLoopOperation;
40 import org.onap.policy.controlloop.VirtualControlLoopEvent;
41 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
42 import org.onap.policy.controlloop.policy.Policy;
43 import org.onap.policy.so.SoCloudConfiguration;
44 import org.onap.policy.so.SoManager;
45 import org.onap.policy.so.SoModelInfo;
46 import org.onap.policy.so.SoOperationType;
47 import org.onap.policy.so.SoRelatedInstance;
48 import org.onap.policy.so.SoRelatedInstanceListElement;
49 import org.onap.policy.so.SoRequest;
50 import org.onap.policy.so.SoRequestDetails;
51 import org.onap.policy.so.SoRequestInfo;
52 import org.onap.policy.so.SoRequestParameters;
53 import org.onap.policy.so.util.Serialization;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 public class SoActorServiceProvider implements Actor {
58     private static final Logger logger = LoggerFactory.getLogger(SoActorServiceProvider.class);
59
60     // Strings for SO Actor
61     private static final String SO_ACTOR = "SO";
62
63     // Strings for targets
64     private static final String TARGET_VFC = "VFC";
65
66     // Strings for recipes
67     private static final String RECIPE_VF_MODULE_CREATE = "VF Module Create";
68     private static final String RECIPE_VF_MODULE_DELETE = "VF Module Delete";
69
70     private static final ImmutableList<String> recipes =
71             ImmutableList.of(RECIPE_VF_MODULE_CREATE, RECIPE_VF_MODULE_DELETE);
72     private static final ImmutableMap<String, List<String>> targets =
73             new ImmutableMap.Builder<String, List<String>>().put(RECIPE_VF_MODULE_CREATE, ImmutableList.of(TARGET_VFC))
74                     .put(RECIPE_VF_MODULE_DELETE, ImmutableList.of(TARGET_VFC)).build();
75
76     // name of request parameters within policy payload
77     public static final String REQ_PARAM_NM = "requestParameters";
78
79     // name of configuration parameters within policy payload
80     public static final String CONFIG_PARAM_NM = "configurationParameters";
81
82     private static final String MODEL_NAME_PROPERTY_KEY = "model-ver.model-name";
83     private static final String MODEL_VERSION_PROPERTY_KEY = "model-ver.model-version";
84     private static final String MODEL_VERSION_ID_PROPERTY_KEY = "model-ver.model-version-id";
85
86     // used to decode configuration parameters via gson
87     private static final Type CONFIG_TYPE = new TypeToken<List<Map<String, String>>>() {}.getType();
88
89     // Static variables required to hold the IDs of the last service item, VNF item and VF Module.
90     // Note that in
91     // a multithreaded deployment this WILL break
92     private static String lastVNFItemVnfId;
93     private static String lastServiceItemServiceInstanceId;
94     private static String lastVfModuleItemVfModuleInstanceId;
95
96     @Override
97     public String actor() {
98         return SO_ACTOR;
99     }
100
101     @Override
102     public List<String> recipes() {
103         return ImmutableList.copyOf(recipes);
104     }
105
106     @Override
107     public List<String> recipeTargets(String recipe) {
108         return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
109     }
110
111     @Override
112     public List<String> recipePayloads(String recipe) {
113         return Collections.emptyList();
114     }
115
116     /**
117      * Constructs a SO request conforming to the lcm API. The actual request is constructed and then placed in a wrapper
118      * object used to send through DMAAP.
119      *
120      * @param onset the event that is reporting the alert for policy to perform an action
121      * @param operation the control loop operation specifying the actor, operation, target, etc.
122      * @param policy the policy the was specified from the yaml generated by CLAMP or through the Policy GUI/API
123      * @param aaiResponseWrapper wrapper for AAI vserver named-query response
124      * @return a SO request conforming to the lcm API using the DMAAP wrapper
125      */
126     public SoRequest constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation, Policy policy,
127             AaiNqResponseWrapper aaiResponseWrapper) {
128         if (!SO_ACTOR.equals(policy.getActor()) || !recipes().contains(policy.getRecipe())) {
129             return null;
130         }
131
132         // A&AI named query should have been performed by now. If not, return null
133         if (aaiResponseWrapper == null) {
134             return null;
135         }
136
137         AaiNqInventoryResponseItem vnfItem;
138         AaiNqInventoryResponseItem vnfServiceItem;
139         AaiNqInventoryResponseItem tenantItem;
140
141         // Extract the items we're interested in from the response
142         try {
143             vnfItem = aaiResponseWrapper.getAaiNqResponse().getInventoryResponseItems().get(0).getItems()
144                     .getInventoryResponseItems().get(0);
145         } catch (Exception e) {
146             logger.error("VNF Item not found in AAI response {}", Serialization.gsonPretty.toJson(aaiResponseWrapper),
147                     e);
148             return null;
149         }
150
151         try {
152             vnfServiceItem = vnfItem.getItems().getInventoryResponseItems().get(0);
153         } catch (Exception e) {
154             logger.error("VNF Service Item not found in AAI response {}",
155                     Serialization.gsonPretty.toJson(aaiResponseWrapper), e);
156             return null;
157         }
158
159         try {
160             tenantItem = aaiResponseWrapper.getAaiNqResponse().getInventoryResponseItems().get(0).getItems()
161                     .getInventoryResponseItems().get(1);
162         } catch (Exception e) {
163             logger.error("Tenant Item not found in AAI response {}",
164                     Serialization.gsonPretty.toJson(aaiResponseWrapper), e);
165             return null;
166         }
167
168         // Find the index for base vf module and non-base vf module
169         AaiNqInventoryResponseItem baseItem = findVfModule(aaiResponseWrapper, true);
170
171         SoModelInfo soModelInfo = prepareSoModelInfo(policy);
172
173         // Report the error if either base vf module or non-base vf module is not found
174         if (baseItem == null || soModelInfo == null) {
175             logger.error("Either base or non-base vf module is not found from AAI response.");
176             return null;
177         }
178
179         // Construct SO Request for a policy's recipe
180         if (RECIPE_VF_MODULE_CREATE.equals(policy.getRecipe())) {
181             return constructCreateRequest(aaiResponseWrapper, policy, tenantItem, vnfItem, vnfServiceItem, soModelInfo);
182         } else if (RECIPE_VF_MODULE_DELETE.equals(policy.getRecipe())) {
183             return constructDeleteRequest(tenantItem, vnfItem, vnfServiceItem, soModelInfo, policy);
184         } else {
185             return null;
186         }
187     }
188
189     private SoModelInfo prepareSoModelInfo(Policy policy) {
190
191         SoModelInfo soModelInfo = new SoModelInfo();
192         if ((policy.getTarget() != null && (policy.getTarget().getModelCustomizationId() != null))
193                 && (policy.getTarget().getModelInvariantId() != null) && (policy.getTarget().getModelName() != null)
194                 && (policy.getTarget().getModelVersion() != null) && (policy.getTarget().getModelVersionId() != null)) {
195
196             soModelInfo.setModelCustomizationId(policy.getTarget().getModelCustomizationId());
197             soModelInfo.setModelInvariantId(policy.getTarget().getModelInvariantId());
198             soModelInfo.setModelName(policy.getTarget().getModelName());
199             soModelInfo.setModelVersion(policy.getTarget().getModelVersion());
200             soModelInfo.setModelVersionId(policy.getTarget().getModelVersionId());
201             soModelInfo.setModelType("vfModule");
202             return soModelInfo;
203         } else {
204             return null;
205         }
206     }
207
208     /**
209      * Construct SO request to create vf-module.
210      *
211      * @param aaiResponseWrapper the AAI response containing the VF modules
212      * @param policy the policy
213      * @param tenantItem tenant item from A&AI named-query response
214      * @param vnfItem vnf item from A&AI named-query response
215      * @param vnfServiceItem vnf service item from A&AI named-query response
216      * @param vfModuleItem vf module item from A&AI named-query response
217      * @return SO create vf-module request
218      */
219     private SoRequest constructCreateRequest(AaiNqResponseWrapper aaiResponseWrapper, Policy policy,
220             AaiNqInventoryResponseItem tenantItem, AaiNqInventoryResponseItem vnfItem,
221             AaiNqInventoryResponseItem vnfServiceItem, SoModelInfo vfModuleItem) {
222         SoRequest request = new SoRequest();
223         request.setOperationType(SoOperationType.SCALE_OUT);
224         //
225         //
226         // Do NOT send So the requestId, they do not support this field
227         //
228         request.setRequestDetails(new SoRequestDetails());
229         request.getRequestDetails().setRequestParameters(new SoRequestParameters());
230         request.getRequestDetails().getRequestParameters().setUserParams(null);
231
232         // cloudConfiguration
233         request.getRequestDetails().setCloudConfiguration(constructCloudConfiguration(tenantItem));
234         // modelInfo
235         request.getRequestDetails().setModelInfo(vfModuleItem);
236
237         // requestInfo
238         request.getRequestDetails().setRequestInfo(constructRequestInfo());
239         String vfModuleName = aaiResponseWrapper.genVfModuleName();
240         request.getRequestDetails().getRequestInfo().setInstanceName(vfModuleName);
241
242         // relatedInstanceList
243         SoRelatedInstanceListElement relatedInstanceListElement1 = new SoRelatedInstanceListElement();
244         SoRelatedInstanceListElement relatedInstanceListElement2 = new SoRelatedInstanceListElement();
245         relatedInstanceListElement1.setRelatedInstance(new SoRelatedInstance());
246         relatedInstanceListElement2.setRelatedInstance(new SoRelatedInstance());
247
248         // Service Item
249         relatedInstanceListElement1.getRelatedInstance()
250                 .setInstanceId(vnfServiceItem.getServiceInstance().getServiceInstanceId());
251         relatedInstanceListElement1.getRelatedInstance().setModelInfo(new SoModelInfo());
252         relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelType("service");
253         relatedInstanceListElement1.getRelatedInstance().getModelInfo()
254                 .setModelInvariantId(vnfServiceItem.getServiceInstance().getModelInvariantId());
255         for (AaiNqExtraProperty prop : vnfServiceItem.getExtraProperties().getExtraProperty()) {
256             if (prop.getPropertyName().equals(MODEL_NAME_PROPERTY_KEY)) {
257                 relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelName(prop.getPropertyValue());
258             } else if (prop.getPropertyName().equals(MODEL_VERSION_PROPERTY_KEY)) {
259                 relatedInstanceListElement1.getRelatedInstance().getModelInfo()
260                         .setModelVersion(prop.getPropertyValue());
261             } else if (prop.getPropertyName().equals(MODEL_VERSION_ID_PROPERTY_KEY)) {
262                 relatedInstanceListElement1.getRelatedInstance().getModelInfo()
263                         .setModelVersionId(prop.getPropertyValue());
264             }
265         }
266
267         // VNF Item
268         relatedInstanceListElement2.getRelatedInstance().setInstanceId(vnfItem.getGenericVnf().getVnfId());
269         relatedInstanceListElement2.getRelatedInstance().setModelInfo(new SoModelInfo());
270         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelType("vnf");
271         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
272                 .setModelInvariantId(vnfItem.getGenericVnf().getModelInvariantId());
273         for (AaiNqExtraProperty prop : vnfItem.getExtraProperties().getExtraProperty()) {
274             if (prop.getPropertyName().equals(MODEL_NAME_PROPERTY_KEY)) {
275                 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelName(prop.getPropertyValue());
276             } else if (prop.getPropertyName().equals(MODEL_VERSION_PROPERTY_KEY)) {
277                 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
278                         .setModelVersion(prop.getPropertyValue());
279             } else if (prop.getPropertyName().equals(MODEL_VERSION_ID_PROPERTY_KEY)) {
280                 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
281                         .setModelVersionId(prop.getPropertyValue());
282             }
283         }
284
285         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelCustomizationName(vnfItem
286                 .getGenericVnf().getVnfType().substring(vnfItem.getGenericVnf().getVnfType().lastIndexOf('/') + 1));
287         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
288                 .setModelCustomizationId(vnfItem.getGenericVnf().getModelCustomizationId());
289
290         // Insert the Service Item and VNF Item
291         request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement1);
292         request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2);
293
294         // Request Parameters
295         buildRequestParameters(policy, request.getRequestDetails());
296
297         // Configuration Parameters
298         buildConfigurationParameters(policy, request.getRequestDetails());
299         // Save the instance IDs for the VNF and service to static fields
300         // vfModuleId is not required for the create vf-module
301         preserveInstanceIds(vnfItem.getGenericVnf().getVnfId(),
302                 vnfServiceItem.getServiceInstance().getServiceInstanceId(), null);
303         if (logger.isDebugEnabled()) {
304             logger.debug("Constructed SO request: {}", Serialization.gsonPretty.toJson(request));
305         }
306         return request;
307     }
308
309     /**
310      * Construct SO request to delete vf-module.
311      *
312      * @param tenantItem tenant item from A&AI named-query response
313      * @param vnfItem vnf item from A&AI named-query response
314      * @param vnfServiceItem vnf service item from A&AI named-query response
315      * @param vfModuleItem vf module item from A&AI named-query response
316      * @return SO delete vf-module request
317      */
318     private SoRequest constructDeleteRequest(AaiNqInventoryResponseItem tenantItem, AaiNqInventoryResponseItem vnfItem,
319             AaiNqInventoryResponseItem vnfServiceItem, SoModelInfo vfModuleItem, Policy policy) {
320         SoRequest request = new SoRequest();
321         request.setOperationType(SoOperationType.DELETE_VF_MODULE);
322         request.setRequestDetails(new SoRequestDetails());
323         request.getRequestDetails().setRelatedInstanceList(null);
324         request.getRequestDetails().setConfigurationParameters(null);
325
326         // cloudConfiguration
327         request.getRequestDetails().setCloudConfiguration(constructCloudConfiguration(tenantItem));
328         // modelInfo
329         request.getRequestDetails().setModelInfo(prepareSoModelInfo(policy));
330         // requestInfo
331         request.getRequestDetails().setRequestInfo(constructRequestInfo());
332         // Save the instance IDs for the VNF, service and vfModule to static fields
333         preserveInstanceIds(vnfItem.getGenericVnf().getVnfId(),
334                 vnfServiceItem.getServiceInstance().getServiceInstanceId(), null);
335
336         if (logger.isDebugEnabled()) {
337             logger.debug("Constructed SO request: {}", Serialization.gsonPretty.toJson(request));
338         }
339         return request;
340     }
341
342     /**
343      * Construct requestInfo for the SO requestDetails.
344      *
345      * @return SO request information
346      */
347     private SoRequestInfo constructRequestInfo() {
348         SoRequestInfo soRequestInfo = new SoRequestInfo();
349         soRequestInfo.setSource("POLICY");
350         soRequestInfo.setSuppressRollback(false);
351         soRequestInfo.setRequestorId("policy");
352         return soRequestInfo;
353     }
354
355     /**
356      * Construct cloudConfiguration for the SO requestDetails.
357      *
358      * @param tenantItem tenant item from A&AI named-query response
359      * @return SO cloud configuration
360      */
361     private SoCloudConfiguration constructCloudConfiguration(AaiNqInventoryResponseItem tenantItem) {
362         SoCloudConfiguration cloudConfiguration = new SoCloudConfiguration();
363         cloudConfiguration.setTenantId(tenantItem.getTenant().getTenantId());
364         cloudConfiguration.setLcpCloudRegionId(
365                 tenantItem.getItems().getInventoryResponseItems().get(0).getCloudRegion().getCloudRegionId());
366         return cloudConfiguration;
367     }
368
369     /**
370      * This method is needed to get the serviceInstanceId and vnfInstanceId which is used in the asyncSORestCall.
371      *
372      * @param requestId the request Id
373      * @param callback callback method
374      * @param request the request
375      * @param url SO REST URL
376      * @param user username
377      * @param password password
378      */
379     public static void sendRequest(String requestId, SoManager.SoCallback callback, Object request, String url,
380             String user, String password) {
381         SoManager soManager = new SoManager(url, user, password);
382         soManager.asyncSoRestCall(requestId, callback, lastServiceItemServiceInstanceId, lastVNFItemVnfId,
383                 lastVfModuleItemVfModuleInstanceId, (SoRequest) request);
384     }
385
386     /**
387      * Find the base or non base VF module item in an AAI response. If there is more than one item, then the <i>last</i>
388      * item is returned
389      *
390      * @param aaiResponseWrapper the AAI response containing the VF modules
391      * @param baseFlag true if we are searching for the base, false if we are searching for the non base
392      * @return the base or non base VF module item or null if the module was not found
393      */
394     private AaiNqInventoryResponseItem findVfModule(AaiNqResponseWrapper aaiResponseWrapper, boolean baseFlag) {
395         List<AaiNqInventoryResponseItem> lst = aaiResponseWrapper.getVfModuleItems(baseFlag);
396         return (lst.isEmpty() ? null : lst.get(lst.size() - 1));
397     }
398
399     /**
400      * Builds the request parameters from the policy payload.
401      *
402      * @param policy the policy
403      * @param request request into which to stick the request parameters
404      */
405     private void buildRequestParameters(Policy policy, SoRequestDetails request) {
406         // assume null until proven otherwise
407         request.setRequestParameters(null);
408
409         if (policy.getPayload() == null) {
410             return;
411         }
412
413         String json = policy.getPayload().get(REQ_PARAM_NM);
414         if (json == null) {
415             return;
416         }
417
418         request.setRequestParameters(Serialization.gsonPretty.fromJson(json, SoRequestParameters.class));
419     }
420
421     /**
422      * Builds the configuration parameters from the policy payload.
423      *
424      * @param policy the policy
425      * @param request request into which to stick the configuration parameters
426      */
427     private void buildConfigurationParameters(Policy policy, SoRequestDetails request) {
428         // assume null until proven otherwise
429         request.setConfigurationParameters(null);
430
431         if (policy.getPayload() == null) {
432             return;
433         }
434
435         String json = policy.getPayload().get(CONFIG_PARAM_NM);
436         if (json == null) {
437             return;
438         }
439
440         request.setConfigurationParameters(Serialization.gsonPretty.fromJson(json, CONFIG_TYPE));
441     }
442
443     /**
444      * This method is called to remember the last service instance ID, VNF Item VNF ID and vf module ID. Note these
445      * fields are static, beware for multithreaded deployments
446      *
447      * @param vnfInstanceId update the last VNF instance ID to this value
448      * @param serviceInstanceId update the last service instance ID to this value
449      * @param vfModuleId update the vfModule instance ID to this value
450      */
451     private static void preserveInstanceIds(final String vnfInstanceId, final String serviceInstanceId,
452             final String vfModuleId) {
453         lastVNFItemVnfId = vnfInstanceId;
454         lastServiceItemServiceInstanceId = serviceInstanceId;
455         lastVfModuleItemVfModuleInstanceId = vfModuleId;
456     }
457
458     /**
459      * Constructs a SO request conforming to the lcm API. The actual request is constructed and then placed in a wrapper
460      * object used to send through DMAAP.
461      *
462      * @param onset the event that is reporting the alert for policy to perform an action
463      * @param operation the control loop operation specifying the actor, operation, target, etc.
464      * @param policy the policy the was specified from the yaml generated by CLAMP or through the Policy GUI/API
465      * @param aaiCqResponse response from A&AI custom query
466      * @return a SO request conforming to the lcm API using the DMAAP wrapper
467      */
468     public SoRequest constructRequestCq(VirtualControlLoopEvent onset, ControlLoopOperation operation, Policy policy,
469             AaiCqResponse aaiCqResponse) {
470         if (!SO_ACTOR.equals(policy.getActor()) || !recipes().contains(policy.getRecipe())) {
471             return null;
472         }
473
474         // A&AI named query should have been performed by now. If not, return null
475         if (aaiCqResponse == null) {
476             return null;
477         }
478
479         SoModelInfo soModelInfo = prepareSoModelInfo(policy);
480
481         // Report the error vf module is not found
482         if (soModelInfo == null) {
483             logger.error("vf module is not found.");
484             return null;
485         }
486
487         GenericVnf vnfItem;
488         ServiceInstance vnfServiceItem;
489         Tenant tenantItem;
490         CloudRegion cloudRegionItem;
491
492         // Extract the items we're interested in from the response
493         try {
494             vnfItem = aaiCqResponse.getGenericVnfByVfModuleModelInvariantId(soModelInfo.getModelInvariantId());
495             //Report VNF not found
496             if (vnfItem == null) {
497                 logger.error("Generic Vnf is not found.");
498                 return null;
499             }
500         } catch (Exception e) {
501             logger.error("VNF Item not found in AAI response {}", Serialization.gsonPretty.toJson(aaiCqResponse), e);
502             return null;
503         }
504
505         try {
506             vnfServiceItem = aaiCqResponse.getServiceInstance();
507         } catch (Exception e) {
508             logger.error("VNF Service Item not found in AAI response {}",
509                     Serialization.gsonPretty.toJson(aaiCqResponse), e);
510             return null;
511         }
512
513         try {
514             tenantItem = aaiCqResponse.getDefaultTenant();
515         } catch (Exception e) {
516             logger.error("Tenant Item not found in AAI response {}", Serialization.gsonPretty.toJson(aaiCqResponse), e);
517             return null;
518         }
519
520         try {
521             cloudRegionItem = aaiCqResponse.getDefaultCloudRegion();
522         } catch (Exception e) {
523             logger.error("Tenant Item not found in AAI response {}", Serialization.gsonPretty.toJson(aaiCqResponse), e);
524             return null;
525         }
526
527
528
529         // Construct SO Request for a policy's recipe
530         if (RECIPE_VF_MODULE_CREATE.equals(policy.getRecipe())) {
531             return constructCreateRequestCq(aaiCqResponse, policy, tenantItem, vnfItem, vnfServiceItem, soModelInfo,
532                     cloudRegionItem);
533         } else if (RECIPE_VF_MODULE_DELETE.equals(policy.getRecipe())) {
534             return constructDeleteRequestCq(tenantItem, vnfItem, vnfServiceItem, soModelInfo, policy, cloudRegionItem);
535         } else {
536             return null;
537         }
538     }
539
540     /**
541      * Construct the So request, based on Custom Query response from A&AI.
542      *
543      * @param aaiCqResponse Custom query response from A&AI
544      * @param policy policy information
545      * @param tenantItem Tenant from CQ response
546      * @param vnfItem Generic VNF from CQ response
547      * @param vnfServiceItem Service Instance from CQ response
548      * @param vfModuleItem VF Module from CustomQuery response
549      * @param cloudRegionItem Cloud Region from Custom query response
550      * @return SoRequest well formed So Request
551      */
552     private SoRequest constructCreateRequestCq(AaiCqResponse aaiCqResponse, Policy policy, Tenant tenantItem,
553             GenericVnf vnfItem, ServiceInstance vnfServiceItem, SoModelInfo vfModuleItem, CloudRegion cloudRegionItem) {
554         SoRequest request = new SoRequest();
555         request.setOperationType(SoOperationType.SCALE_OUT);
556         //
557         //
558         // Do NOT send So the requestId, they do not support this field
559         //
560         request.setRequestDetails(new SoRequestDetails());
561         request.getRequestDetails().setRequestParameters(new SoRequestParameters());
562         request.getRequestDetails().getRequestParameters().setUserParams(null);
563
564         // cloudConfiguration
565         request.getRequestDetails().setCloudConfiguration(constructCloudConfigurationCq(tenantItem, cloudRegionItem));
566         // modelInfo
567         request.getRequestDetails().setModelInfo(vfModuleItem);
568
569
570         // requestInfo
571         request.getRequestDetails().setRequestInfo(constructRequestInfo());
572         request.getRequestDetails().getRequestInfo().setInstanceName("vfModuleName");
573
574         // relatedInstanceList
575         SoRelatedInstanceListElement relatedInstanceListElement1 = new SoRelatedInstanceListElement();
576         SoRelatedInstanceListElement relatedInstanceListElement2 = new SoRelatedInstanceListElement();
577         relatedInstanceListElement1.setRelatedInstance(new SoRelatedInstance());
578         relatedInstanceListElement2.setRelatedInstance(new SoRelatedInstance());
579
580         // Service Item
581         relatedInstanceListElement1.getRelatedInstance().setInstanceId(vnfServiceItem.getServiceInstanceId());
582         relatedInstanceListElement1.getRelatedInstance().setModelInfo(new SoModelInfo());
583         relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelType("service");
584         relatedInstanceListElement1.getRelatedInstance().getModelInfo()
585                 .setModelInvariantId(vnfServiceItem.getModelInvariantId());
586         relatedInstanceListElement1.getRelatedInstance().getModelInfo()
587                 .setModelVersionId(vnfServiceItem.getModelVersionId());
588         relatedInstanceListElement1.getRelatedInstance().getModelInfo()
589                 .setModelName(aaiCqResponse.getModelVerByVersionId(vnfServiceItem.getModelVersionId()).getModelName());
590         relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelVersion(
591                 aaiCqResponse.getModelVerByVersionId(vnfServiceItem.getModelVersionId()).getModelVersion());
592
593
594         // VNF Item
595         relatedInstanceListElement2.getRelatedInstance().setInstanceId(vnfItem.getVnfId());
596         relatedInstanceListElement2.getRelatedInstance().setModelInfo(new SoModelInfo());
597         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelType("vnf");
598         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
599                 .setModelInvariantId(vnfItem.getModelInvariantId());
600         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersionId(vnfItem.getModelVersionId());
601
602         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
603                 .setModelName(aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelName());
604         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersion(
605                 aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelVersion());
606
607
608         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
609                 .setModelCustomizationId(vnfItem.getModelCustomizationId());
610
611
612         // Insert the Service Item and VNF Item
613         request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement1);
614         request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2);
615
616         // Request Parameters
617         buildRequestParameters(policy, request.getRequestDetails());
618
619         // Configuration Parameters
620         buildConfigurationParameters(policy, request.getRequestDetails());
621         // Save the instance IDs for the VNF and service to static fields
622         // vfModuleId is not required for the create vf-module
623         preserveInstanceIds(vnfItem.getVnfId(), vnfServiceItem.getServiceInstanceId(), null);
624         if (logger.isDebugEnabled()) {
625             logger.debug("Constructed SO request: {}", Serialization.gsonPretty.toJson(request));
626         }
627         return request;
628     }
629
630     /**
631      * constructs delete request for So.
632      *
633      * @param tenantItem Tenant from A&AI CQ request
634      * @param vnfItem Generic VNF from A&AI CQ request
635      * @param vnfServiceItem ServiceInstance from A&AI CQ request
636      * @param vfModuleItem VFModule from A&AI CQ request
637      * @param policy policy information
638      * @param cloudRegionItem CloudRegion from A&AI CQ request
639      * @return SoRequest deleted
640      */
641     private SoRequest constructDeleteRequestCq(Tenant tenantItem, GenericVnf vnfItem, ServiceInstance vnfServiceItem,
642             SoModelInfo vfModuleItem, Policy policy, CloudRegion cloudRegionItem) {
643         SoRequest request = new SoRequest();
644         request.setOperationType(SoOperationType.DELETE_VF_MODULE);
645         request.setRequestDetails(new SoRequestDetails());
646         request.getRequestDetails().setRelatedInstanceList(null);
647         request.getRequestDetails().setConfigurationParameters(null);
648
649         // cloudConfiguration
650         request.getRequestDetails().setCloudConfiguration(constructCloudConfigurationCq(tenantItem, cloudRegionItem));
651         // modelInfo
652         request.getRequestDetails().setModelInfo(prepareSoModelInfo(policy));
653         // requestInfo
654         request.getRequestDetails().setRequestInfo(constructRequestInfo());
655         // Save the instance IDs for the VNF, service and vfModule to static fields
656         preserveInstanceIds(vnfItem.getVnfId(), vnfServiceItem.getServiceInstanceId(), null);
657
658         if (logger.isDebugEnabled()) {
659             logger.debug("Constructed SO request: {}", Serialization.gsonPretty.toJson(request));
660         }
661         return request;
662     }
663
664
665     /**
666      * Construct cloudConfiguration for the SO requestDetails. Overridden for custom query.
667      *
668      * @param tenantItem tenant item from A&AI named-query response
669      * @return SO cloud configuration
670      */
671     private SoCloudConfiguration constructCloudConfigurationCq(Tenant tenantItem, CloudRegion cloudRegionItem) {
672         SoCloudConfiguration cloudConfiguration = new SoCloudConfiguration();
673         cloudConfiguration.setTenantId(tenantItem.getTenantId());
674         cloudConfiguration.setLcpCloudRegionId(cloudRegionItem.getCloudRegionId());
675         return cloudConfiguration;
676     }
677
678 }