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