1a1b45ac8ecbba575a07547b09567f8d2c46442a
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / onap / so / apihandlerinfra / MsoRequest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
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.so.apihandlerinfra;
23
24 import java.io.IOException;
25 import java.io.StringWriter;
26 import java.sql.Timestamp;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Map.Entry;
32 import java.util.StringTokenizer;
33
34 import javax.ws.rs.core.MultivaluedMap;
35 import javax.ws.rs.core.Response;
36 import javax.xml.XMLConstants;
37 import javax.xml.transform.OutputKeys;
38 import javax.xml.transform.Transformer;
39 import javax.xml.transform.TransformerFactory;
40 import javax.xml.transform.dom.DOMSource;
41 import javax.xml.transform.stream.StreamResult;
42
43 import org.onap.so.apihandler.common.ResponseBuilder;
44 import org.onap.so.apihandlerinfra.tasksbeans.TasksRequest;
45 import org.onap.so.apihandlerinfra.validation.ApplyUpdatedConfigValidation;
46 import org.onap.so.apihandlerinfra.validation.CloudConfigurationValidation;
47 import org.onap.so.apihandlerinfra.validation.ConfigurationParametersValidation;
48 import org.onap.so.apihandlerinfra.validation.InPlaceSoftwareUpdateValidation;
49 import org.onap.so.apihandlerinfra.validation.InstanceIdMapValidation;
50 import org.onap.so.apihandlerinfra.validation.ModelInfoValidation;
51 import org.onap.so.apihandlerinfra.validation.PlatformLOBValidation;
52 import org.onap.so.apihandlerinfra.validation.ProjectOwningEntityValidation;
53 import org.onap.so.apihandlerinfra.validation.RelatedInstancesValidation;
54 import org.onap.so.apihandlerinfra.validation.RequestInfoValidation;
55 import org.onap.so.apihandlerinfra.validation.RequestParametersValidation;
56 import org.onap.so.apihandlerinfra.validation.RequestScopeValidation;
57 import org.onap.so.apihandlerinfra.validation.SubscriberInfoValidation;
58 import org.onap.so.apihandlerinfra.validation.UserParamsValidation;
59 import org.onap.so.apihandlerinfra.validation.ValidationInformation;
60 import org.onap.so.apihandlerinfra.validation.ValidationRule;
61 import org.onap.so.apihandlerinfra.vnfbeans.RequestStatusType;
62 import org.onap.so.apihandlerinfra.vnfbeans.VnfInputs;
63 import org.onap.so.apihandlerinfra.vnfbeans.VnfRequest;
64 import org.onap.so.db.request.beans.InfraActiveRequests;
65 import org.onap.so.exceptions.ValidationException;
66 import org.onap.so.logger.MessageEnum;
67 import org.onap.so.logger.MsoLogger;
68 import org.onap.so.requestsdb.client.RequestsDbClient;
69 import org.onap.so.serviceinstancebeans.CloudConfiguration;
70 import org.onap.so.serviceinstancebeans.InstanceDirection;
71 import org.onap.so.serviceinstancebeans.ModelInfo;
72 import org.onap.so.serviceinstancebeans.ModelType;
73 import org.onap.so.serviceinstancebeans.PolicyException;
74 import org.onap.so.serviceinstancebeans.RelatedInstance;
75 import org.onap.so.serviceinstancebeans.RelatedInstanceList;
76 import org.onap.so.serviceinstancebeans.RequestError;
77 import org.onap.so.serviceinstancebeans.RequestInfo;
78 import org.onap.so.serviceinstancebeans.RequestParameters;
79 import org.onap.so.serviceinstancebeans.Service;
80 import org.onap.so.serviceinstancebeans.ServiceException;
81 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
82 import org.springframework.beans.factory.annotation.Autowired;
83 import org.springframework.stereotype.Component;
84 import org.w3c.dom.Document;
85 import org.w3c.dom.Element;
86 import org.w3c.dom.Node;
87 import org.w3c.dom.NodeList;
88
89 import com.fasterxml.jackson.annotation.JsonInclude.Include;
90 import com.fasterxml.jackson.core.JsonGenerationException;
91 import com.fasterxml.jackson.databind.JsonMappingException;
92 import com.fasterxml.jackson.databind.ObjectMapper;
93
94
95 @Component
96 public class MsoRequest {
97       
98         @Autowired
99         private RequestsDbClient requestsDbClient;
100         
101         @Autowired
102         private ResponseBuilder builder;
103     
104     private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH,MsoRequest.class);
105     
106     public Response buildServiceErrorResponse (int httpResponseCode, MsoException exceptionType, 
107                 String errorText, String messageId, List<String> variables, String version) {
108         
109         if(errorText.length() > 1999){
110                 errorText = errorText.substring(0, 1999);
111         }
112
113         RequestError re = new RequestError();
114
115         if("PolicyException".equals(exceptionType.name())){
116
117                 PolicyException pe = new PolicyException();
118                 pe.setMessageId(messageId);
119                 pe.setText(errorText);
120                 if(variables != null){
121                         for(String variable: variables){
122                                 pe.getVariables().add(variable);
123                         }
124                 }
125                 re.setPolicyException(pe);
126
127         } else {
128
129                 ServiceException se = new ServiceException();
130                 se.setMessageId(messageId);
131                 se.setText(errorText);
132                 if(variables != null){
133                                 for(String variable: variables){
134                                         se.getVariables().add(variable);
135                                 }
136                         }
137                 re.setServiceException(se);
138         }
139
140         String requestErrorStr = null;
141
142         try{
143                 ObjectMapper mapper = new ObjectMapper();
144                 mapper.setSerializationInclusion(Include.NON_DEFAULT);
145                 requestErrorStr = mapper.writeValueAsString(re);
146         }catch(Exception e){
147                 msoLogger.error (MessageEnum.APIH_VALIDATION_ERROR, "", "", MsoLogger.ErrorCode.DataError, "Exception in buildServiceErrorResponse writing exceptionType to string ", e);
148         }
149
150         return builder.buildResponse(httpResponseCode, null, requestErrorStr, version);
151     }
152
153    
154
155     // Parse request JSON
156     public void parse (ServiceInstancesRequest sir, HashMap<String,String> instanceIdMap, Actions action, String version,
157                 String originalRequestJSON, int reqVersion, Boolean aLaCarteFlag) throws ValidationException, IOException {
158         
159         msoLogger.debug ("Validating the Service Instance request");       
160         List<ValidationRule> rules = new ArrayList<>();
161         msoLogger.debug ("Incoming version is: " + version + " coverting to int: " + reqVersion);
162             RequestParameters requestParameters = sir.getRequestDetails().getRequestParameters();
163         ValidationInformation info = new ValidationInformation(sir, instanceIdMap, action,
164                         reqVersion, aLaCarteFlag, requestParameters);
165         
166         rules.add(new InstanceIdMapValidation());
167         
168         if(reqVersion >= 6 && action == Action.inPlaceSoftwareUpdate){
169                 rules.add(new InPlaceSoftwareUpdateValidation());
170         }else if(reqVersion >= 6 && action == Action.applyUpdatedConfig){
171                 rules.add(new ApplyUpdatedConfigValidation());
172         }else{
173                 rules.add(new RequestScopeValidation());
174                 rules.add(new RequestParametersValidation());
175                 rules.add(new RequestInfoValidation());
176                 rules.add(new ModelInfoValidation());
177                 rules.add(new CloudConfigurationValidation());
178                 rules.add(new SubscriberInfoValidation());
179                 rules.add(new PlatformLOBValidation());
180                 rules.add(new ProjectOwningEntityValidation());
181                 rules.add(new RelatedInstancesValidation());
182                 rules.add(new ConfigurationParametersValidation());
183         } 
184             if(reqVersion >= 7 && requestParameters != null && requestParameters.getUserParams() != null){
185                 for(Map<String, Object> params : requestParameters.getUserParams()){
186                         if(params.containsKey("service")){
187                                 ObjectMapper obj = new ObjectMapper();
188                                         String input = obj.writeValueAsString(params.get("service"));
189                                         Service validate = obj.readValue(input, Service.class);
190                                         info.setUserParams(validate);
191                                         rules.add(new UserParamsValidation());
192                                         break;
193                         }
194                 }
195             }
196             for(ValidationRule rule : rules){
197                 rule.validate(info);
198         }
199     }
200     void parseOrchestration (ServiceInstancesRequest sir) throws ValidationException {
201         RequestInfo requestInfo = sir.getRequestDetails().getRequestInfo();
202
203         if (requestInfo == null) {
204             throw new ValidationException ("requestInfo");
205         }
206
207         if (empty (requestInfo.getSource ())) {
208                 throw new ValidationException ("source");
209         }
210         if (empty (requestInfo.getRequestorId ())) {
211                 throw new ValidationException ("requestorId");
212         }
213     }
214     public Map<String, List<String>> getOrchestrationFilters (MultivaluedMap<String, String> queryParams) throws ValidationException {
215
216         String queryParam = null;
217         Map<String, List<String>> orchestrationFilterParams = new HashMap<>();
218
219
220         for (Entry<String,List<String>> entry : queryParams.entrySet()) {
221             queryParam = entry.getKey();
222
223             try{
224                   if("filter".equalsIgnoreCase(queryParam)){
225                           for(String value : entry.getValue()) {
226                                   StringTokenizer st = new StringTokenizer(value, ":");
227         
228                                   int counter=0;
229                                   String mapKey=null;
230                                   List<String> orchestrationList = new ArrayList<>();
231                                   while (st.hasMoreElements()) {
232                                           if(counter == 0){
233                                                   mapKey = st.nextElement() + "";
234                                           } else{
235                                                   orchestrationList.add(st.nextElement() + "");
236                                           }
237                                          counter++;
238                                   }
239                                   orchestrationFilterParams.put(mapKey, orchestrationList);
240                           }
241                   }
242
243             }catch(Exception e){
244                 //msoLogger.error (MessageEnum.APIH_VALIDATION_ERROR, e);
245                 throw new ValidationException ("QueryParam ServiceInfo", e);
246
247                 }
248
249         }
250
251
252         return orchestrationFilterParams;
253   }
254
255     public InfraActiveRequests createRequestObject (ServiceInstancesRequest servInsReq, Actions action, String requestId,
256                  Status status, String originalRequestJSON, String requestScope) {
257         InfraActiveRequests aq = new InfraActiveRequests ();
258         try {
259             if (null == servInsReq) {
260                 servInsReq = new ServiceInstancesRequest ();
261             }
262            
263             String networkType = "";
264             String vnfType = "";
265             aq.setRequestId (requestId);
266             aq.setRequestAction(action.toString());
267             aq.setAction(action.toString());
268
269             Timestamp startTimeStamp = new Timestamp (System.currentTimeMillis());
270
271             aq.setStartTime (startTimeStamp);
272             RequestInfo requestInfo =servInsReq.getRequestDetails().getRequestInfo();
273             if (requestInfo != null) {
274                 
275                 if(requestInfo.getSource() != null){
276                         aq.setSource(requestInfo.getSource());
277                 }
278                 if(requestInfo.getCallbackUrl() != null){
279                         aq.setCallBackUrl(requestInfo.getCallbackUrl());
280                 }
281                 if(requestInfo.getCorrelator() != null){
282                         aq.setCorrelator(requestInfo.getCorrelator());
283                 }
284
285                 if(requestInfo.getRequestorId() != null) {
286                         aq.setRequestorId(requestInfo.getRequestorId());
287                 }
288             }
289
290             if (servInsReq.getRequestDetails().getModelInfo() != null  ||  (action == Action.inPlaceSoftwareUpdate || action == Action.applyUpdatedConfig)) {
291                 aq.setRequestScope(requestScope);
292             }
293
294             if (servInsReq.getRequestDetails().getCloudConfiguration() != null) {
295                 CloudConfiguration cloudConfiguration = servInsReq.getRequestDetails().getCloudConfiguration();
296                 if(cloudConfiguration.getLcpCloudRegionId() != null) {
297                         aq.setAicCloudRegion(cloudConfiguration.getLcpCloudRegionId());
298                 }
299
300                 if(cloudConfiguration.getTenantId() != null) {
301                         aq.setTenantId(cloudConfiguration.getTenantId());
302                 }
303
304             }
305
306             if(servInsReq.getServiceInstanceId() != null){
307                 aq.setServiceInstanceId(servInsReq.getServiceInstanceId());
308             }
309
310             if(servInsReq.getVnfInstanceId() != null){
311                 aq.setVnfId(servInsReq.getVnfInstanceId());
312             }
313
314             if(ModelType.service.name().equalsIgnoreCase(requestScope)){
315                 if(servInsReq.getRequestDetails().getRequestInfo().getInstanceName() != null){
316                         aq.setServiceInstanceName(requestInfo.getInstanceName());
317                 }
318             }
319
320             if(ModelType.network.name().equalsIgnoreCase(requestScope)){
321                 aq.setNetworkName(servInsReq.getRequestDetails().getRequestInfo().getInstanceName());
322                 aq.setNetworkType(networkType);
323                 aq.setNetworkId(servInsReq.getNetworkInstanceId());
324             }
325
326             if(ModelType.volumeGroup.name().equalsIgnoreCase(requestScope)){
327                 aq.setVolumeGroupId(servInsReq.getVolumeGroupInstanceId());
328                 aq.setVolumeGroupName(servInsReq.getRequestDetails().getRequestInfo().getInstanceName());
329                 aq.setVnfType(vnfType);
330
331             }
332
333             if(ModelType.vfModule.name().equalsIgnoreCase(requestScope)){
334                 aq.setVfModuleName(requestInfo.getInstanceName());
335                 aq.setVfModuleModelName(servInsReq.getRequestDetails().getModelInfo().getModelName());
336                 aq.setVfModuleId(servInsReq.getVfModuleInstanceId());
337                 aq.setVolumeGroupId(servInsReq.getVolumeGroupInstanceId());
338                 aq.setVnfType(vnfType);
339
340             }
341             
342             if(ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
343                 aq.setConfigurationId(servInsReq.getConfigurationId());
344                 aq.setConfigurationName(requestInfo.getInstanceName());
345             }
346
347             if(ModelType.vnf.name().equalsIgnoreCase(requestScope)){
348                 aq.setVnfName(requestInfo.getInstanceName());
349                                 if (null != servInsReq.getRequestDetails()) {
350                                         RelatedInstanceList[] instanceList = servInsReq.getRequestDetails().getRelatedInstanceList();
351
352                                         if (instanceList != null) {
353
354                                                 for(RelatedInstanceList relatedInstanceList : instanceList){
355
356                                                         RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();
357                                                         if(relatedInstance.getModelInfo().getModelType().equals(ModelType.service)){
358                                                                 aq.setVnfType(vnfType);
359                                                         }
360                                                 }
361                                         }
362                                 }
363             }
364
365             aq.setRequestBody (originalRequestJSON);
366
367             aq.setRequestStatus (status.toString ());
368             aq.setLastModifiedBy (Constants.MODIFIED_BY_APIHANDLER);           
369         } catch (Exception e) {
370                 msoLogger.error (MessageEnum.APIH_DB_INSERT_EXC, "", "", MsoLogger.ErrorCode.DataError, "Exception when creation record request", e);
371         
372             if (!status.equals (Status.FAILED)) {
373                 throw e;
374             }
375         }
376         return aq;
377     }
378     
379     public InfraActiveRequests createRequestObject (TasksRequest taskRequest, Action action, String requestId,
380                  Status status, String originalRequestJSON) {
381         InfraActiveRequests aq = new InfraActiveRequests ();
382        try {
383         
384            org.onap.so.apihandlerinfra.tasksbeans.RequestInfo requestInfo = taskRequest.getRequestDetails().getRequestInfo();
385            aq.setRequestId (requestId);
386            aq.setRequestAction(action.name());
387            aq.setAction(action.name());
388
389            Timestamp startTimeStamp = new Timestamp (System.currentTimeMillis());
390
391            aq.setStartTime (startTimeStamp);           
392            if (requestInfo != null) {
393                 
394                 if(requestInfo.getSource() != null){
395                         aq.setSource(requestInfo.getSource());
396                 }           
397
398                 if(requestInfo.getRequestorId() != null) {
399                         aq.setRequestorId(requestInfo.getRequestorId());
400                 }
401            }  
402
403            aq.setRequestBody (originalRequestJSON);
404            aq.setRequestStatus (status.toString ());
405            aq.setLastModifiedBy (Constants.MODIFIED_BY_APIHANDLER);
406                   
407        } catch (Exception e) {
408         msoLogger.error (MessageEnum.APIH_DB_INSERT_EXC, "", "", MsoLogger.ErrorCode.DataError, "Exception when creation record request", e);
409        
410            if (!status.equals (Status.FAILED)) {
411                throw e;
412            }
413        }
414        return aq;
415    }
416     
417     public void createErrorRequestRecord (Status status, String requestId, String errorMessage, Actions action, String requestScope, String requestJSON) {
418         try {
419             InfraActiveRequests request = new InfraActiveRequests(requestId);
420             Timestamp startTimeStamp = new Timestamp (System.currentTimeMillis());
421             request.setStartTime (startTimeStamp);
422             request.setRequestStatus(status.toString());
423             request.setStatusMessage(errorMessage);
424             request.setProgress((long) 100);
425             request.setLastModifiedBy(Constants.MODIFIED_BY_APIHANDLER);
426             request.setRequestAction(action.toString());
427             request.setRequestScope(requestScope);
428             request.setRequestBody(requestJSON);
429             Timestamp endTimeStamp = new Timestamp(System.currentTimeMillis());
430             request.setEndTime(endTimeStamp);
431                         requestsDbClient.save(request);
432         } catch (Exception e) {
433                 msoLogger.error(MessageEnum.APIH_DB_UPDATE_EXC, e.getMessage(), "", "", MsoLogger.ErrorCode.DataError, "Exception when updating record in DB");
434             msoLogger.debug ("Exception: ", e);
435         }
436     }
437     
438     
439
440
441     public Response buildResponse (int httpResponseCode, String errorCode, InfraActiveRequests inProgress) {
442         return buildResponseWithError (httpResponseCode, errorCode, inProgress, null);
443     }
444
445     public Response buildResponseWithError (int httpResponseCode,
446                                             String errorCode,
447                                             InfraActiveRequests inProgress,
448                                             String errorString) {
449
450
451
452         // Log the failed request into the MSO Requests database
453
454         return Response.status (httpResponseCode).entity (null).build ();
455
456     }
457
458     public Response buildResponseFailedValidation (int httpResponseCode, String exceptionMessage) {
459
460         return Response.status (httpResponseCode).entity (null).build ();
461     }
462     
463   
464
465     public String getServiceType (VnfInputs vnfInputs) {
466         if (vnfInputs.getServiceType () != null)
467                 return vnfInputs.getServiceType ();
468         if (vnfInputs.getServiceId () != null)
469                 return vnfInputs.getServiceId ();
470         return null;
471     }
472
473     public long translateStatus (RequestStatusType status) {        
474         switch (status) {
475         case FAILED:
476         case COMPLETE:
477                 return Constants.PROGRESS_REQUEST_COMPLETED;            
478         case IN_PROGRESS:
479                 return Constants.PROGRESS_REQUEST_IN_PROGRESS;
480                 default:
481                         return 0;               
482         }
483     }
484
485     public static String domToStr (Document doc) {
486         if (doc == null) {
487             return null;
488         }
489
490         try {
491             StringWriter sw = new StringWriter ();
492             StreamResult sr = new StreamResult (sw);
493             TransformerFactory tf = TransformerFactory.newInstance ();
494                         tf.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
495                         tf.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET,"");
496             Transformer t = tf.newTransformer ();
497             t.setOutputProperty (OutputKeys.STANDALONE, "yes");
498             NodeList nl = doc.getDocumentElement ().getChildNodes ();
499             DOMSource source = null;
500             for (int x = 0; x < nl.getLength (); x++) {
501                 Node e = nl.item (x);
502                 if (e instanceof Element) {
503                     source = new DOMSource (e);
504                     break;
505                 }
506             }
507             if (source != null) {
508                 t.transform (source, sr);
509
510                 String s = sw.toString ();
511                 return s;
512             }
513
514             return null;
515
516         } catch (Exception e) {
517             msoLogger.error (MessageEnum.APIH_DOM2STR_ERROR, "", "", MsoLogger.ErrorCode.DataError, "Exception in domToStr", e);
518         }
519         return null;
520     }
521
522     public void addBPMNSpecificInputs(VnfRequest vnfReq, VnfInputs vnfInputs, String personaModelId, String personaModelVersion, Boolean isBaseVfModule,
523                         String vnfPersonaModelId, String vnfPersonaModelVersion) {
524         vnfInputs.setPersonaModelId(personaModelId);
525         vnfInputs.setPersonaModelVersion(personaModelVersion);
526         vnfInputs.setIsBaseVfModule(isBaseVfModule);
527         vnfInputs.setVnfPersonaModelId(vnfPersonaModelId);
528         vnfInputs.setVnfPersonaModelVersion(vnfPersonaModelVersion);
529
530         vnfReq.setVnfInputs(vnfInputs);
531       
532     }
533
534     private static boolean empty(String s) {
535           return (s == null || s.trim().isEmpty());
536     }
537
538     public String getRequestJSON(ServiceInstancesRequest sir) throws JsonGenerationException, JsonMappingException, IOException {
539         ObjectMapper mapper = new ObjectMapper();
540         mapper.setSerializationInclusion(Include.NON_NULL);
541         //mapper.configure(Feature.WRAP_ROOT_VALUE, true);
542         msoLogger.debug ("building sir from object " + sir);
543         String requestJSON = mapper.writeValueAsString(sir);
544         
545         // Perform mapping from VID-style modelInfo fields to ASDC-style modelInfo fields
546         
547         msoLogger.debug("REQUEST JSON before mapping: " + requestJSON);
548         // modelUuid = modelVersionId
549         requestJSON = requestJSON.replaceAll("\"modelVersionId\":","\"modelUuid\":");
550         // modelCustomizationUuid = modelCustomizationId
551         requestJSON = requestJSON.replaceAll("\"modelCustomizationId\":","\"modelCustomizationUuid\":");
552         // modelInstanceName = modelCustomizationName
553         requestJSON = requestJSON.replaceAll("\"modelCustomizationName\":","\"modelInstanceName\":");
554         // modelInvariantUuid = modelInvariantId 
555         requestJSON = requestJSON.replaceAll("\"modelInvariantId\":","\"modelInvariantUuid\":");        
556         msoLogger.debug("REQUEST JSON after mapping: " + requestJSON);
557         
558         return requestJSON;
559     }
560
561
562         public boolean getAlacarteFlag(ServiceInstancesRequest sir) {
563                 if(sir.getRequestDetails().getRequestParameters() != null &&
564                                 sir.getRequestDetails().getRequestParameters().getALaCarte() != null)
565                         return sir.getRequestDetails().getRequestParameters().getALaCarte();
566                 
567                 return false;
568         }
569
570
571         public String getNetworkType(ServiceInstancesRequest sir, String requestScope) {
572                   if(requestScope.equalsIgnoreCase(ModelType.network.name()))
573                         return sir.getRequestDetails().getModelInfo().getModelName();   
574                   else return null;
575         }
576
577
578         public String getServiceInstanceType(ServiceInstancesRequest sir, String requestScope) {
579                  if(requestScope.equalsIgnoreCase(ModelType.network.name()))
580                         return sir.getRequestDetails().getModelInfo().getModelName();   
581                   else return null;             
582         }
583
584
585         public String getSDCServiceModelVersion(ServiceInstancesRequest sir) {
586                 String sdcServiceModelVersion = null;
587                 if(sir.getRequestDetails().getRelatedInstanceList() != null)
588                         for(RelatedInstanceList relatedInstanceList : sir.getRequestDetails().getRelatedInstanceList()){
589                                 RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();
590                                 ModelInfo relatedInstanceModelInfo = relatedInstance.getModelInfo ();   
591                                 if(relatedInstanceModelInfo.getModelType().equals(ModelType.service))                                           
592                                         sdcServiceModelVersion = relatedInstanceModelInfo.getModelVersion ();
593                         }
594         return sdcServiceModelVersion;
595         }
596
597
598         public String getVfModuleType(ServiceInstancesRequest sir, String requestScope, Actions action, int reqVersion) {       
599         
600         String serviceInstanceType = null;
601         String networkType = null;
602         String vnfType = null;
603         String vfModuleType = null;
604         String vfModuleModelName = null;
605                 ModelInfo modelInfo = sir.getRequestDetails().getModelInfo();
606                 RelatedInstanceList[] instanceList = sir.getRequestDetails().getRelatedInstanceList();
607                 String serviceModelName = null;
608         String vnfModelName = null;
609         String asdcServiceModelVersion = null;
610         String volumeGroupId = null;
611         boolean isRelatedServiceInstancePresent = false;
612         boolean isRelatedVnfInstancePresent = false;
613         boolean isSourceVnfPresent = false;
614         boolean isDestinationVnfPresent = false;
615         boolean isConnectionPointPresent = false;       
616
617             if (instanceList != null) {
618                 for(RelatedInstanceList relatedInstanceList : instanceList){
619                         RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();
620                         ModelInfo relatedInstanceModelInfo = relatedInstance.getModelInfo ();   
621
622                         if (action != Action.deleteInstance) {
623                                 
624                                 if(ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
625                                         if(InstanceDirection.source.equals(relatedInstance.getInstanceDirection()) && relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)) {
626                                                 isSourceVnfPresent = true;
627                                         } else if(InstanceDirection.destination.equals(relatedInstance.getInstanceDirection()) && 
628                                                         (relatedInstanceModelInfo.getModelType().equals(ModelType.vnf) || (relatedInstanceModelInfo.getModelType().equals(ModelType.pnf) && reqVersion == 6))) {
629                                                 isDestinationVnfPresent = true;
630                                         }
631                                 }
632                                 
633                                 if(ModelType.connectionPoint.equals(relatedInstanceModelInfo.getModelType()) && ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
634                                         isConnectionPointPresent = true;
635                                 }
636                         }
637                         
638
639                         if(relatedInstanceModelInfo.getModelType().equals(ModelType.service)) {
640                                 isRelatedServiceInstancePresent = true;                         
641                                 serviceModelName = relatedInstanceModelInfo.getModelName ();
642                                 asdcServiceModelVersion = relatedInstanceModelInfo.getModelVersion ();
643                         } else if(relatedInstanceModelInfo.getModelType().equals(ModelType.vnf) && !(ModelType.configuration.name().equalsIgnoreCase(requestScope))) {
644                                 isRelatedVnfInstancePresent = true;                             
645                                 vnfModelName = relatedInstanceModelInfo.getModelCustomizationName();
646                         } else if(relatedInstanceModelInfo.getModelType().equals(ModelType.volumeGroup)) {                              
647                                 volumeGroupId = relatedInstance.getInstanceId ();
648                         }
649                 }
650                 
651                 if(requestScope.equalsIgnoreCase (ModelType.volumeGroup.name ())) {                     
652                         serviceInstanceType = serviceModelName;
653                         vnfType = serviceModelName + "/" + vnfModelName;          
654                 }
655                 else if(requestScope.equalsIgnoreCase(ModelType.vfModule.name ())) {         
656                         vfModuleModelName = modelInfo.getModelName ();
657                         serviceInstanceType = serviceModelName;
658                         vnfType = serviceModelName + "/" + vnfModelName;
659                         vfModuleType = vnfType + "::" + vfModuleModelName;
660                         sir.setVolumeGroupInstanceId (volumeGroupId);            
661                 }
662                 else if (requestScope.equalsIgnoreCase (ModelType.vnf.name ()))
663                         vnfType = serviceModelName + "/" + sir.getRequestDetails().getModelInfo().getModelCustomizationName();                  
664                
665         }     
666         
667                 return vfModuleType;
668
669         }
670         
671         public String getVnfType(ServiceInstancesRequest sir, String requestScope, Actions action, int reqVersion) {    
672                 
673         String serviceInstanceType = null;
674         String networkType = null;
675         String vnfType = null;
676         String vfModuleType = null;
677         String vfModuleModelName = null;
678                 ModelInfo modelInfo = sir.getRequestDetails().getModelInfo();
679             MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH, MsoRequest.class);
680                 RelatedInstanceList[] instanceList = sir.getRequestDetails().getRelatedInstanceList();
681                 String serviceModelName = null;
682         String vnfModelName = null;
683         String asdcServiceModelVersion = null;
684         String volumeGroupId = null;
685         boolean isRelatedServiceInstancePresent = false;
686         boolean isRelatedVnfInstancePresent = false;
687         boolean isSourceVnfPresent = false;
688         boolean isDestinationVnfPresent = false;
689         boolean isConnectionPointPresent = false;       
690
691             if (instanceList != null) {
692                 for(RelatedInstanceList relatedInstanceList : instanceList){
693                         RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();
694                         ModelInfo relatedInstanceModelInfo = relatedInstance.getModelInfo ();   
695
696                         if (action != Action.deleteInstance) {
697                                 
698                                 if(ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
699                                         if(InstanceDirection.source.equals(relatedInstance.getInstanceDirection()) && relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)) {
700                                                 isSourceVnfPresent = true;
701                                         } else if(InstanceDirection.destination.equals(relatedInstance.getInstanceDirection()) && 
702                                                         (relatedInstanceModelInfo.getModelType().equals(ModelType.vnf) || (relatedInstanceModelInfo.getModelType().equals(ModelType.pnf) && reqVersion == 6))) {
703                                                 isDestinationVnfPresent = true;
704                                         }
705                                 }
706                                 
707                                 if(ModelType.connectionPoint.equals(relatedInstanceModelInfo.getModelType()) && ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
708                                         isConnectionPointPresent = true;
709                                 }
710                         }
711                         
712
713                         if(relatedInstanceModelInfo.getModelType().equals(ModelType.service)) {
714                                 isRelatedServiceInstancePresent = true;                         
715                                 serviceModelName = relatedInstanceModelInfo.getModelName ();
716                                 asdcServiceModelVersion = relatedInstanceModelInfo.getModelVersion ();
717                         } else if(relatedInstanceModelInfo.getModelType().equals(ModelType.vnf) && !(ModelType.configuration.name().equalsIgnoreCase(requestScope))) {
718                                 isRelatedVnfInstancePresent = true;                             
719                                 vnfModelName = relatedInstanceModelInfo.getModelCustomizationName();
720                         } else if(relatedInstanceModelInfo.getModelType().equals(ModelType.volumeGroup)) {                              
721                                 volumeGroupId = relatedInstance.getInstanceId ();
722                         }
723                 }
724                 
725                 if(requestScope.equalsIgnoreCase (ModelType.volumeGroup.name ())) {                     
726                         serviceInstanceType = serviceModelName;
727                         vnfType = serviceModelName + "/" + vnfModelName;          
728                 }
729                 else if(requestScope.equalsIgnoreCase(ModelType.vfModule.name ())) {         
730                         vfModuleModelName = modelInfo.getModelName ();
731                         serviceInstanceType = serviceModelName;
732                         vnfType = serviceModelName + "/" + vnfModelName;
733                         vfModuleType = vnfType + "::" + vfModuleModelName;
734                         sir.setVolumeGroupInstanceId (volumeGroupId);            
735                 }
736                 else if (requestScope.equalsIgnoreCase (ModelType.vnf.name ()))
737                         vnfType = serviceModelName + "/" + sir.getRequestDetails().getModelInfo().getModelCustomizationName();                  
738                
739         }     
740         
741                 return vnfType;
742
743         }
744 }