Custom Query Code
[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
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         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelCustomizationName(vnfItem
285                 .getGenericVnf().getVnfType().substring(vnfItem.getGenericVnf().getVnfType().lastIndexOf('/') + 1));
286         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
287                 .setModelCustomizationId(vnfItem.getGenericVnf().getModelCustomizationId());
288
289         // Insert the Service Item and VNF Item
290         request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement1);
291         request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2);
292
293         // Request Parameters
294         buildRequestParameters(policy, request.getRequestDetails());
295
296         // Configuration Parameters
297         buildConfigurationParameters(policy, request.getRequestDetails());
298         // Save the instance IDs for the VNF and service to static fields
299         // vfModuleId is not required for the create vf-module
300         preserveInstanceIds(vnfItem.getGenericVnf().getVnfId(),
301                 vnfServiceItem.getServiceInstance().getServiceInstanceId(), null);
302         if (logger.isDebugEnabled()) {
303             logger.debug("Constructed SO request: {}", Serialization.gsonPretty.toJson(request));
304         }
305         return request;
306     }
307
308     /**
309      * Construct SO request to delete vf-module.
310      *
311      * @param tenantItem tenant item from A&AI named-query response
312      * @param vnfItem vnf item from A&AI named-query response
313      * @param vnfServiceItem vnf service item from A&AI named-query response
314      * @param vfModuleItem vf module item from A&AI named-query response
315      * @return SO delete vf-module request
316      */
317     private SoRequest constructDeleteRequest(AaiNqInventoryResponseItem tenantItem, AaiNqInventoryResponseItem vnfItem,
318             AaiNqInventoryResponseItem vnfServiceItem, SoModelInfo vfModuleItem, Policy policy) {
319         SoRequest request = new SoRequest();
320         request.setOperationType(SoOperationType.DELETE_VF_MODULE);
321         request.setRequestDetails(new SoRequestDetails());
322         request.getRequestDetails().setRelatedInstanceList(null);
323         request.getRequestDetails().setConfigurationParameters(null);
324
325         // cloudConfiguration
326         request.getRequestDetails().setCloudConfiguration(constructCloudConfiguration(tenantItem));
327         // modelInfo
328         request.getRequestDetails().setModelInfo(prepareSoModelInfo(policy));
329         // requestInfo
330         request.getRequestDetails().setRequestInfo(constructRequestInfo());
331         // Save the instance IDs for the VNF, service and vfModule to static fields
332         preserveInstanceIds(vnfItem.getGenericVnf().getVnfId(),
333                 vnfServiceItem.getServiceInstance().getServiceInstanceId(), null);
334
335         if (logger.isDebugEnabled()) {
336             logger.debug("Constructed SO request: {}", Serialization.gsonPretty.toJson(request));
337         }
338         return request;
339     }
340
341     /**
342      * Construct requestInfo for the SO requestDetails.
343      *
344      * @return SO request information
345      */
346     private SoRequestInfo constructRequestInfo() {
347         SoRequestInfo soRequestInfo = new SoRequestInfo();
348         soRequestInfo.setSource("POLICY");
349         soRequestInfo.setSuppressRollback(false);
350         soRequestInfo.setRequestorId("policy");
351         return soRequestInfo;
352     }
353
354     /**
355      * Construct cloudConfiguration for the SO requestDetails.
356      *
357      * @param tenantItem tenant item from A&AI named-query response
358      * @return SO cloud configuration
359      */
360     private SoCloudConfiguration constructCloudConfiguration(AaiNqInventoryResponseItem tenantItem) {
361         SoCloudConfiguration cloudConfiguration = new SoCloudConfiguration();
362         cloudConfiguration.setTenantId(tenantItem.getTenant().getTenantId());
363         cloudConfiguration.setLcpCloudRegionId(
364                 tenantItem.getItems().getInventoryResponseItems().get(0).getCloudRegion().getCloudRegionId());
365         return cloudConfiguration;
366     }
367
368     /**
369      * This method is needed to get the serviceInstanceId and vnfInstanceId which is used in the asyncSORestCall.
370      *
371      * @param requestId the request Id
372      * @param callback callback method
373      * @param request the request
374      * @param url SO REST URL
375      * @param user username
376      * @param password password
377      */
378     public static void sendRequest(String requestId, SoManager.SoCallback callback, Object request, String url,
379             String user, String password) {
380         SoManager soManager = new SoManager(url, user, password);
381         soManager.asyncSoRestCall(requestId, callback, lastServiceItemServiceInstanceId, lastVNFItemVnfId,
382                 lastVfModuleItemVfModuleInstanceId, (SoRequest) request);
383     }
384
385     /**
386      * 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>
387      * item is returned
388      *
389      * @param aaiResponseWrapper the AAI response containing the VF modules
390      * @param baseFlag true if we are searching for the base, false if we are searching for the non base
391      * @return the base or non base VF module item or null if the module was not found
392      */
393     private AaiNqInventoryResponseItem findVfModule(AaiNqResponseWrapper aaiResponseWrapper, boolean baseFlag) {
394         List<AaiNqInventoryResponseItem> lst = aaiResponseWrapper.getVfModuleItems(baseFlag);
395         return (lst.isEmpty() ? null : lst.get(lst.size() - 1));
396     }
397
398     /**
399      * Builds the request parameters from the policy payload.
400      *
401      * @param policy the policy
402      * @param request request into which to stick the request parameters
403      */
404     private void buildRequestParameters(Policy policy, SoRequestDetails request) {
405         // assume null until proven otherwise
406         request.setRequestParameters(null);
407
408         if (policy.getPayload() == null) {
409             return;
410         }
411
412         String json = policy.getPayload().get(REQ_PARAM_NM);
413         if (json == null) {
414             return;
415         }
416
417         request.setRequestParameters(Serialization.gsonPretty.fromJson(json, SoRequestParameters.class));
418     }
419
420     /**
421      * Builds the configuration parameters from the policy payload.
422      *
423      * @param policy the policy
424      * @param request request into which to stick the configuration parameters
425      */
426     private void buildConfigurationParameters(Policy policy, SoRequestDetails request) {
427         // assume null until proven otherwise
428         request.setConfigurationParameters(null);
429
430         if (policy.getPayload() == null) {
431             return;
432         }
433
434         String json = policy.getPayload().get(CONFIG_PARAM_NM);
435         if (json == null) {
436             return;
437         }
438
439         request.setConfigurationParameters(Serialization.gsonPretty.fromJson(json, CONFIG_TYPE));
440     }
441
442     /**
443      * This method is called to remember the last service instance ID, VNF Item VNF ID and vf module ID. Note these
444      * fields are static, beware for multithreaded deployments
445      *
446      * @param vnfInstanceId update the last VNF instance ID to this value
447      * @param serviceInstanceId update the last service instance ID to this value
448      * @param vfModuleId update the vfModule instance ID to this value
449      */
450     private static void preserveInstanceIds(final String vnfInstanceId, final String serviceInstanceId,
451             final String vfModuleId) {
452         lastVNFItemVnfId = vnfInstanceId;
453         lastServiceItemServiceInstanceId = serviceInstanceId;
454         lastVfModuleItemVfModuleInstanceId = vfModuleId;
455     }
456
457     /**
458      * Constructs a SO request conforming to the lcm API. The actual request is constructed and then placed in a wrapper
459      * object used to send through DMAAP.
460      *
461      * @param onset the event that is reporting the alert for policy to perform an action
462      * @param operation the control loop operation specifying the actor, operation, target, etc.
463      * @param policy the policy the was specified from the yaml generated by CLAMP or through the Policy GUI/API
464      * @param aaiCqResponse response from A&AI custom query
465      * @return a SO request conforming to the lcm API using the DMAAP wrapper
466      */
467     public SoRequest constructRequestCq(VirtualControlLoopEvent onset, ControlLoopOperation operation, Policy policy,
468             AaiCqResponse aaiCqResponse) {
469         if (!SO_ACTOR.equals(policy.getActor()) || !recipes().contains(policy.getRecipe())) {
470             return null;
471         }
472
473         // A&AI named query should have been performed by now. If not, return null
474         if (aaiCqResponse == null) {
475             return null;
476         }
477
478         GenericVnf vnfItem;
479         ServiceInstance vnfServiceItem;
480         Tenant tenantItem;
481         CloudRegion cloudRegionItem;
482
483         // Extract the items we're interested in from the response
484         try {
485             vnfItem = aaiCqResponse.getDefaultGenericVnf();
486         } catch (Exception e) {
487             logger.error("VNF Item not found in AAI response {}", Serialization.gsonPretty.toJson(aaiCqResponse), e);
488             return null;
489         }
490
491         try {
492             vnfServiceItem = aaiCqResponse.getServiceInstance();
493         } catch (Exception e) {
494             logger.error("VNF Service Item not found in AAI response {}",
495                     Serialization.gsonPretty.toJson(aaiCqResponse), e);
496             return null;
497         }
498
499         try {
500             tenantItem = aaiCqResponse.getDefaultTenant();
501         } catch (Exception e) {
502             logger.error("Tenant Item not found in AAI response {}", Serialization.gsonPretty.toJson(aaiCqResponse), e);
503             return null;
504         }
505
506         try {
507             cloudRegionItem = aaiCqResponse.getDefaultCloudRegion();
508         } catch (Exception e) {
509             logger.error("Tenant Item not found in AAI response {}", Serialization.gsonPretty.toJson(aaiCqResponse), e);
510             return null;
511         }
512
513         SoModelInfo soModelInfo = prepareSoModelInfo(policy);
514
515         // Report the error vf module is not found
516         if (soModelInfo == null) {
517             logger.error("vf module is not found.");
518             return null;
519         }
520
521         // Construct SO Request for a policy's recipe
522         if (RECIPE_VF_MODULE_CREATE.equals(policy.getRecipe())) {
523             return constructCreateRequestCq(aaiCqResponse, policy, tenantItem, vnfItem, vnfServiceItem, soModelInfo,
524                     cloudRegionItem);
525         } else if (RECIPE_VF_MODULE_DELETE.equals(policy.getRecipe())) {
526             return constructDeleteRequestCq(tenantItem, vnfItem, vnfServiceItem, soModelInfo, policy, cloudRegionItem);
527         } else {
528             return null;
529         }
530     }
531
532     /**
533      * Construct the So request, based on Custom Query response from A&AI.
534      *
535      * @param aaiCqResponse Custom query response from A&AI
536      * @param policy policy information
537      * @param tenantItem Tenant from CQ response
538      * @param vnfItem Generic VNF from CQ response
539      * @param vnfServiceItem Service Instance from CQ response
540      * @param vfModuleItem VF Module from CustomQuery response
541      * @param cloudRegionItem Cloud Region from Custom query response
542      * @return SoRequest well formed So Request
543      */
544     private SoRequest constructCreateRequestCq(AaiCqResponse aaiCqResponse, Policy policy, Tenant tenantItem,
545             GenericVnf vnfItem, ServiceInstance vnfServiceItem, SoModelInfo vfModuleItem, CloudRegion cloudRegionItem) {
546         SoRequest request = new SoRequest();
547         request.setOperationType(SoOperationType.SCALE_OUT);
548         //
549         //
550         // Do NOT send So the requestId, they do not support this field
551         //
552         request.setRequestDetails(new SoRequestDetails());
553         request.getRequestDetails().setRequestParameters(new SoRequestParameters());
554         request.getRequestDetails().getRequestParameters().setUserParams(null);
555
556         // cloudConfiguration
557         request.getRequestDetails().setCloudConfiguration(constructCloudConfigurationCq(tenantItem, cloudRegionItem));
558         // modelInfo
559         request.getRequestDetails().setModelInfo(vfModuleItem);
560
561         // requestInfo
562         request.getRequestDetails().setRequestInfo(constructRequestInfo());
563         String vfModuleName = aaiCqResponse.getDefaultVfModule().getVfModuleName();
564         request.getRequestDetails().getRequestInfo().setInstanceName(vfModuleName);
565
566         // relatedInstanceList
567         SoRelatedInstanceListElement relatedInstanceListElement1 = new SoRelatedInstanceListElement();
568         SoRelatedInstanceListElement relatedInstanceListElement2 = new SoRelatedInstanceListElement();
569         relatedInstanceListElement1.setRelatedInstance(new SoRelatedInstance());
570         relatedInstanceListElement2.setRelatedInstance(new SoRelatedInstance());
571
572         // Service Item (Note that Model Name and Model Version are not available in A&AI schema for ServiceInstance)
573         relatedInstanceListElement1.getRelatedInstance().setInstanceId(vnfServiceItem.getServiceInstanceId());
574         relatedInstanceListElement1.getRelatedInstance().setModelInfo(new SoModelInfo());
575         relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelType("service");
576         relatedInstanceListElement1.getRelatedInstance().getModelInfo()
577                 .setModelInvariantId(vnfServiceItem.getModelInvariantId());
578         relatedInstanceListElement1.getRelatedInstance().getModelInfo()
579                 .setModelVersionId(vnfServiceItem.getModelVersionId());
580
581
582         // VNF Item (Note that Model Name, Model Version, and Model Customization Name are not available in A&AI schema
583         // for Generic VNF)
584         relatedInstanceListElement2.getRelatedInstance().setInstanceId(vnfItem.getVnfId());
585         relatedInstanceListElement2.getRelatedInstance().setModelInfo(new SoModelInfo());
586         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelType("vnf");
587         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
588                 .setModelInvariantId(vnfItem.getModelInvariantId());
589         relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersionId(vnfItem.getModelVersionId());
590         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
591                 .setModelCustomizationId(vnfItem.getModelCustomizationId());
592
593         // Insert the Service Item and VNF Item
594         request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement1);
595         request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2);
596
597         // Request Parameters
598         buildRequestParameters(policy, request.getRequestDetails());
599
600         // Configuration Parameters
601         buildConfigurationParameters(policy, request.getRequestDetails());
602         // Save the instance IDs for the VNF and service to static fields
603         // vfModuleId is not required for the create vf-module
604         preserveInstanceIds(vnfItem.getVnfId(), vnfServiceItem.getServiceInstanceId(), null);
605         if (logger.isDebugEnabled()) {
606             logger.debug("Constructed SO request: {}", Serialization.gsonPretty.toJson(request));
607         }
608         return request;
609     }
610
611     /**
612      * constructs delete request for So.
613      *
614      * @param tenantItem Tenant from A&AI CQ request
615      * @param vnfItem Generic VNF from A&AI CQ request
616      * @param vnfServiceItem ServiceInstance from A&AI CQ request
617      * @param vfModuleItem VFModule from A&AI CQ request
618      * @param policy policy information
619      * @param cloudRegionItem CloudRegion from A&AI CQ request
620      * @return SoRequest deleted
621      */
622     private SoRequest constructDeleteRequestCq(Tenant tenantItem, GenericVnf vnfItem, ServiceInstance vnfServiceItem,
623             SoModelInfo vfModuleItem, Policy policy, CloudRegion cloudRegionItem) {
624         SoRequest request = new SoRequest();
625         request.setOperationType(SoOperationType.DELETE_VF_MODULE);
626         request.setRequestDetails(new SoRequestDetails());
627         request.getRequestDetails().setRelatedInstanceList(null);
628         request.getRequestDetails().setConfigurationParameters(null);
629
630         // cloudConfiguration
631         request.getRequestDetails().setCloudConfiguration(constructCloudConfigurationCq(tenantItem, cloudRegionItem));
632         // modelInfo
633         request.getRequestDetails().setModelInfo(prepareSoModelInfo(policy));
634         // requestInfo
635         request.getRequestDetails().setRequestInfo(constructRequestInfo());
636         // Save the instance IDs for the VNF, service and vfModule to static fields
637         preserveInstanceIds(vnfItem.getVnfId(), vnfServiceItem.getServiceInstanceId(), null);
638
639         if (logger.isDebugEnabled()) {
640             logger.debug("Constructed SO request: {}", Serialization.gsonPretty.toJson(request));
641         }
642         return request;
643     }
644
645
646     /**
647      * Construct cloudConfiguration for the SO requestDetails. Overridden for custom query.
648      *
649      * @param tenantItem tenant item from A&AI named-query response
650      * @return SO cloud configuration
651      */
652     private SoCloudConfiguration constructCloudConfigurationCq(Tenant tenantItem, CloudRegion cloudRegionItem) {
653         SoCloudConfiguration cloudConfiguration = new SoCloudConfiguration();
654         cloudConfiguration.setTenantId(tenantItem.getTenantId());
655         cloudConfiguration.setLcpCloudRegionId(cloudRegionItem.getCloudRegionId());
656         return cloudConfiguration;
657     }
658
659 }