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