aboutsummaryrefslogtreecommitdiffstats
path: root/mso-api-handlers/mso-api-handler-infra/src/main/java/org/openecomp/mso/apihandlerinfra/MsoRequest.java
blob: 64bdc3f2695d4653f7ac577c76dae7e953229b06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
/*-
 * ============LICENSE_START=======================================================
 * OPENECOMP - MSO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.mso.apihandlerinfra;

import java.io.StringReader;
import java.io.StringWriter;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.StringTokenizer;

import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig.Feature;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.hibernate.Session;
import org.jboss.resteasy.specimpl.MultivaluedMapImpl;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import org.openecomp.mso.apihandler.common.ErrorNumbers;
import org.openecomp.mso.apihandler.common.ValidationException;
import org.openecomp.mso.apihandlerinfra.serviceinstancebeans.CloudConfiguration;
import org.openecomp.mso.apihandlerinfra.serviceinstancebeans.ModelInfo;
import org.openecomp.mso.apihandlerinfra.serviceinstancebeans.PolicyException;
import org.openecomp.mso.apihandlerinfra.serviceinstancebeans.RelatedInstance;
import org.openecomp.mso.apihandlerinfra.serviceinstancebeans.RelatedInstanceList;
import org.openecomp.mso.apihandlerinfra.serviceinstancebeans.RequestError;
import org.openecomp.mso.apihandlerinfra.serviceinstancebeans.ServiceException;
import org.openecomp.mso.apihandlerinfra.serviceinstancebeans.ServiceInstancesRequest;
import org.openecomp.mso.apihandlerinfra.vnfbeans.ActionType;
import org.openecomp.mso.apihandlerinfra.vnfbeans.ObjectFactory;
import org.openecomp.mso.apihandlerinfra.serviceinstancebeans.RequestInfo;
import org.openecomp.mso.apihandlerinfra.vnfbeans.RequestStatusType;
import org.openecomp.mso.apihandlerinfra.vnfbeans.VnfInputs;
import org.openecomp.mso.apihandlerinfra.vnfbeans.VnfRequest;
import org.openecomp.mso.logger.MsoLogger;
import org.openecomp.mso.logger.MessageEnum;
import org.openecomp.mso.properties.MsoJavaProperties;
import org.openecomp.mso.requestsdb.HibernateUtil;
import org.openecomp.mso.requestsdb.InfraActiveRequests;
import org.openecomp.mso.requestsdb.RequestsDatabase;
import org.openecomp.mso.utils.UUIDChecker;

public class MsoRequest {

    private String requestId;
    private String requestXML;
    private String requestJSON;
    private String requestUri;
    private VnfRequest vnfReq;
    private RequestInfo requestInfo;
    private ModelInfo modelInfo;
    private CloudConfiguration cloudConfiguration ;
    private VnfInputs vnfInputs;
    private String vnfParams;
    private Action action;
    private String errorMessage;
    private String errorCode;
    private String httpResponse;
    private String responseBody;
    private RequestStatusType status;
    private ServiceInstancesRequest sir;
    private long startTime;
    private long progress = Constants.PROGRESS_REQUEST_RECEIVED;
    private String serviceInstanceType;
    private String vnfType;
    private String vfModuleType;
    private String vfModuleModelName;
    private String networkType;
    private String asdcServiceModelVersion; 
    private String requestScope;

    private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH);
    private static final String NOT_PROVIDED = "not provided";

    MsoRequest (String requestId) {
        this.requestId = requestId;
        this.startTime = System.currentTimeMillis();
        MsoLogger.setLogContext (requestId, null);

    } 
    
    MsoRequest () {

        this.startTime = System.currentTimeMillis();
        MsoLogger.setLogContext (requestId, null);

    }
    
    
    public Response buildServiceErrorResponse (int httpResponseCode,
            MsoException exceptionType,
            String text,
            String messageId,
            List<String> variables) {

        this.errorCode = messageId;

        if (text != null) {
            this.errorMessage = text;
        }
        else {
            this.errorMessage = "";
        }
        this.httpResponse = Integer.toString(httpResponseCode);
        if(errorMessage.length() > 1999){
            errorMessage = errorMessage.substring(0, 1999);
        }

        RequestError re = new RequestError();

        if(exceptionType.name().equals("PolicyException")){

            PolicyException pe = new PolicyException();
            pe.setMessageId(messageId);
            pe.setText(text);
            if(variables != null){
                for(String variable: variables){
                    pe.getVariables().add(variable);
                }
            }
            re.setPolicyException(pe);

        } else {

            ServiceException se = new ServiceException();
            se.setMessageId(messageId);
            se.setText(text);
            if(variables != null){
                if(variables != null){
                    for(String variable: variables){
                        se.getVariables().add(variable);
                    }
                }
            }
            re.setServiceException(se);
         }

        String requestErrorStr = null;

        try{
            ObjectMapper mapper = new ObjectMapper();
            mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_DEFAULT);
            requestErrorStr = mapper.writeValueAsString(re);
        }catch(Exception e){
            msoLogger.error (MessageEnum.APIH_VALIDATION_ERROR, "", "", MsoLogger.ErrorCode.DataError, "Exception in buildServiceErrorResponse writing exceptionType to string ", e);
        }

       
        return Response.status (httpResponseCode).entity(requestErrorStr).build ();
              
    }


    // Parse request JSON
    void parse (ServiceInstancesRequest sir, HashMap<String,String> instanceIdMap, Action action) throws ValidationException {

        msoLogger.debug ("Validating the Service Instance request");
        
        this.sir = sir;
        this.action = action;
        
        try{
            ObjectMapper mapper = new ObjectMapper();
            //mapper.configure(Feature.WRAP_ROOT_VALUE, true);
            requestJSON = mapper.writeValueAsString(sir.getRequestDetails());

        } catch(Exception e){
            throw new ValidationException ("Parse ServiceInstanceRequest to JSON string");
        }       
        
        if(instanceIdMap != null){
            if(instanceIdMap.get("serviceInstanceId") != null){
                if (!UUIDChecker.isValidUUID (instanceIdMap.get ("serviceInstanceId"))) {
                    throw new ValidationException ("serviceInstanceId");
                }
                this.sir.setServiceInstanceId(instanceIdMap.get("serviceInstanceId"));
            }
        
            if(instanceIdMap.get("vnfInstanceId") != null){
                if (!UUIDChecker.isValidUUID (instanceIdMap.get ("vnfInstanceId"))) {
                    throw new ValidationException ("vnfInstanceId");
                }
                this.sir.setVnfInstanceId(instanceIdMap.get("vnfInstanceId"));
            }
        
            if(instanceIdMap.get("vfModuleInstanceId") != null){
                if (!UUIDChecker.isValidUUID (instanceIdMap.get ("vfModuleInstanceId"))) {
                    throw new ValidationException ("vfModuleInstanceId");
                }
                this.sir.setVfModuleInstanceId(instanceIdMap.get("vfModuleInstanceId"));
            }
        
            if(instanceIdMap.get("volumeGroupInstanceId") != null){
                if (!UUIDChecker.isValidUUID (instanceIdMap.get ("volumeGroupInstanceId"))) {
                    throw new ValidationException ("volumeGroupInstanceId");
                }
                this.sir.setVolumeGroupInstanceId(instanceIdMap.get("volumeGroupInstanceId"));
            }
        
            if(instanceIdMap.get("networkInstanceId") != null){
                if (!UUIDChecker.isValidUUID (instanceIdMap.get ("networkInstanceId"))) {
                    throw new ValidationException ("networkInstanceId");
                }
                this.sir.setNetworkInstanceId(instanceIdMap.get("networkInstanceId"));
            }
        }
              
        this.modelInfo = sir.getRequestDetails().getModelInfo();
        
        if (this.modelInfo == null) {
            throw new ValidationException ("model-info");
        }
         
        this.requestInfo = sir.getRequestDetails().getRequestInfo();
        
        if (this.requestInfo == null) {
            throw new ValidationException ("requestInfo");
        }
        
        if (modelInfo.getModelType () == null) {
            throw new ValidationException ("modelType");
        }        
        
        this.requestScope = modelInfo.getModelType().name();
        
        if (empty (modelInfo.getModelInvariantId ()) && !(requestScope.equalsIgnoreCase (ModelType.network.name ()) && 
                (action == Action.createInstance || action == Action.updateInstance))) {
            throw new ValidationException ("modelInvariantId");
        }
        
        if (!empty (modelInfo.getModelInvariantId ()) && !UUIDChecker.isValidUUID (modelInfo.getModelInvariantId ())) {
            throw new ValidationException ("modelInvariantId format");
        }        
        
        if (empty (modelInfo.getModelName ()) && (action == Action.createInstance || action == Action.updateInstance || (action == Action.deleteInstance && 
                (requestScope.equalsIgnoreCase (ModelType.network.name ()) || requestScope.equalsIgnoreCase (ModelType.vfModule.name ()))))) {
            throw new ValidationException ("modelName");
        }
        
        if (empty (modelInfo.getModelVersion ()) && !(requestScope.equalsIgnoreCase (ModelType.network.name ()) && 
                (action == Action.createInstance || action == Action.updateInstance))) {
            throw new ValidationException ("modelVersion");
        }
        
        if(requestScope.equalsIgnoreCase(ModelType.vnf.name()) && action != Action.deleteInstance && empty (modelInfo.getModelCustomizationName ())) {
            throw new ValidationException ("modelCustomizationName");
        }
               
        this.cloudConfiguration = sir.getRequestDetails ().getCloudConfiguration ();
        if (!requestScope.equalsIgnoreCase (ModelType.service.name ()) && cloudConfiguration == null) {
            throw new ValidationException ("cloudConfiguration");
        }
        
        if (cloudConfiguration != null) {
            if (empty (cloudConfiguration.getLcpCloudRegionId ())) {
                throw new ValidationException ("lcpCloudRegionId");
            }
            if (empty (cloudConfiguration.getTenantId ())) {
                throw new ValidationException ("tenantId");
            }
        }
        
        if (requestScope.equalsIgnoreCase (ModelType.service.name ()) && action == Action.createInstance) {
            if (sir.getRequestDetails ().getRequestParameters () == null) {
                throw new ValidationException ("requestParameters");
            }
            if (empty (sir.getRequestDetails ().getRequestParameters ().getSubscriptionServiceType ())) {
                throw new ValidationException ("subscriptionServiceType");
            }
        }        
        
        if(requestScope.equalsIgnoreCase(ModelType.service.name())){
            this.serviceInstanceType = modelInfo.getModelName();
        }
        
        if(requestScope.equalsIgnoreCase(ModelType.network.name())){
            this.networkType = modelInfo.getModelName();
        }        
        
        // Verify instanceName existence and format 
        if (empty (requestInfo.getInstanceName ()) && action == Action.createInstance) {
            throw new ValidationException ("instanceName");
        }
        
        if (!empty (requestInfo.getInstanceName ()) && !requestInfo.getInstanceName ().matches (Constants.VALID_INSTANCE_NAME_FORMAT)) {
                throw new ValidationException ("instanceName format");
        }
        
        if (empty (requestInfo.getProductFamilyId ()) && ((requestScope.equalsIgnoreCase (ModelType.vnf.name ()) && action == Action.createInstance) ||
                (requestScope.equalsIgnoreCase (ModelType.network.name ()) && (action == Action.createInstance || action == Action.updateInstance)))) {
            throw new ValidationException ("productFamilyId");
        }        
       
        if (empty (requestInfo.getSource ())) {
            throw new ValidationException ("source");
        }
        

        RelatedInstanceList[] instanceList = sir.getRequestDetails().getRelatedInstanceList();

        String serviceModelName = null;
        String vnfModelName = null;
        String asdcServiceModelVersion = null;
        String volumeGroupId = null;
        boolean isRelatedServiceInstancePresent = false;
        boolean isRelatedVnfInstancePresent = false;         

        if (instanceList != null) {
            for(RelatedInstanceList relatedInstanceList : instanceList){
                RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();

                if (!empty (relatedInstance.getInstanceName ()) && !relatedInstance.getInstanceName ().matches (Constants.VALID_INSTANCE_NAME_FORMAT)) {
                    throw new ValidationException ("instanceName format in relatedInstance");
                }

                if (empty (relatedInstance.getInstanceId ())) {
                    throw new ValidationException ("instanceId in relatedInstance");
                }

                if (!UUIDChecker.isValidUUID (relatedInstance.getInstanceId ())) {
                    throw new ValidationException ("instanceId format in relatedInstance");
                }

                if (relatedInstance.getModelInfo () == null) {
                    throw new ValidationException ("modelInfo in relatedInstance");
                }

                if (relatedInstance.getModelInfo ().getModelType () == null) {
                    throw new ValidationException ("modelType in relatedInstance");
                }

                if (action != Action.deleteInstance) {

                    if (empty (relatedInstance.getModelInfo ().getModelInvariantId ()) &&
                              !(requestScope.equalsIgnoreCase (ModelType.vfModule.name ()) && action == Action.createInstance &&
                                  relatedInstance.getModelInfo ().getModelType ().equals(ModelType.volumeGroup))) {
                        throw new ValidationException ("modelInvariantId in relatedInstance");
                    }

                    if (!empty (relatedInstance.getModelInfo ().getModelInvariantId ()) &&
                              !UUIDChecker.isValidUUID (relatedInstance.getModelInfo ().getModelInvariantId ())) {
                        throw new ValidationException ("modelInvariantId format in relatedInstance");
                    }

                    if (empty(relatedInstance.getModelInfo ().getModelName ()) &&
                              !(requestScope.equalsIgnoreCase (ModelType.vfModule.name ()) && action == Action.createInstance &&
                              relatedInstance.getModelInfo ().getModelType ().equals (ModelType.volumeGroup))) {
                        throw new ValidationException ("modelName in relatedInstance");
                    }

                    if (empty (relatedInstance.getModelInfo ().getModelVersion ())  &&
                              !(requestScope.equalsIgnoreCase (ModelType.vfModule.name ()) && action == Action.createInstance &&
                              relatedInstance.getModelInfo ().getModelType ().equals (ModelType.volumeGroup))) {
                        throw new ValidationException ("modelVersion in relatedInstance");
                    }
                }

                if (empty (relatedInstance.getModelInfo ().getModelCustomizationName ()) &&
                          relatedInstance.getModelInfo ().getModelType ().equals (ModelType.vnf)) {
                    throw new ValidationException ("modelCustomizationName in relatedInstance");
                }

                if(relatedInstance.getModelInfo().getModelType().equals(ModelType.service)) {
                      isRelatedServiceInstancePresent = true;
                    if (!relatedInstance.getInstanceId ().equals (this.sir.getServiceInstanceId ())) {
                          throw new ValidationException ("serviceInstanceId matching the serviceInstanceId in request URI");
                    }
                    serviceModelName = relatedInstance.getModelInfo ().getModelName ();
                    asdcServiceModelVersion = relatedInstance.getModelInfo().getModelVersion ();
                }
                else if(relatedInstance.getModelInfo().getModelType().equals(ModelType.vnf)) {
                    isRelatedVnfInstancePresent = true;
                    if (!relatedInstance.getInstanceId ().equals (this.sir.getVnfInstanceId ())) {
                        throw new ValidationException ("vnfInstanceId matching the vnfInstanceId in request URI");
                    }
                    vnfModelName = relatedInstance.getModelInfo().getModelCustomizationName();
                }
                else if(relatedInstance.getModelInfo().getModelType().equals(ModelType.volumeGroup)) {
                    volumeGroupId = relatedInstance.getInstanceId ();
                }
            }
         
        
            if(requestScope.equalsIgnoreCase (ModelType.volumeGroup.name ())) {
                if (!isRelatedServiceInstancePresent) {
                    throw new ValidationException ("related service instance for volumeGroup request");
                }
                if (!isRelatedVnfInstancePresent) {
                    throw new ValidationException ("related vnf instance for volumeGroup request");
                }
                this.serviceInstanceType = serviceModelName;
                this.vnfType = serviceModelName + "/" + vnfModelName;
                this.asdcServiceModelVersion = asdcServiceModelVersion;
            }
            else if(requestScope.equalsIgnoreCase(ModelType.vfModule.name ())) {
                if (!isRelatedServiceInstancePresent) {
                    throw new ValidationException ("related service instance for vfModule request");
                }
                if (!isRelatedVnfInstancePresent) {
                    throw new ValidationException ("related vnf instance for vfModule request");
                }
                String vfModuleModelName = modelInfo.getModelName ();
                this.vfModuleModelName = vfModuleModelName;
                this.serviceInstanceType = serviceModelName;
                this.vnfType = serviceModelName + "/" + vnfModelName;
                this.asdcServiceModelVersion = asdcServiceModelVersion;
                this.vfModuleType = vnfType + "::" + vfModuleModelName;
                this.sir.setVolumeGroupInstanceId (volumeGroupId);
            }
            else if (requestScope.equalsIgnoreCase (ModelType.vnf.name ())) {
                if (!isRelatedServiceInstancePresent) {
                    throw new ValidationException ("related service instance for vnf request");
                }
                this.vnfType = serviceModelName + "/" + sir.getRequestDetails().getModelInfo().getModelCustomizationName();
           }
        }
        else if (action != Action.deleteInstance && !requestScope.equalsIgnoreCase(ModelType.service.name ()) && 
                !requestScope.equalsIgnoreCase(ModelType.network.name ())) {
            throw new ValidationException ("related instances");
        }
        
    }
    
    public Map<String, List<String>> getOrchestrationFilters (MultivaluedMap<String, String> queryParams) throws ValidationException {

        String queryParam = null; 
        Map<String, List<String>> orchestrationFilterParams = new HashMap<String, List<String>>();
        
        
        for (Entry<String,List<String>> entry : queryParams.entrySet()) {
            queryParam = entry.getKey();
             
            try{
                if(queryParam.equalsIgnoreCase("filter")){

                    StringTokenizer st = new StringTokenizer(entry.getValue().get(0), ":");

                    int counter=0;
                    String mapKey=null;
                    List<String> orchestrationList = new ArrayList<String>();
                    while (st.hasMoreElements()) {
                        if(counter == 0){
                            mapKey = st.nextElement() + "";
                        } else{
                            orchestrationList.add(st.nextElement() + "");
                        }
                        counter++;
                    }
                    orchestrationFilterParams.put(mapKey, orchestrationList);
                }

            }catch(Exception e){
                //msoLogger.error (MessageEnum.APIH_VALIDATION_ERROR, e);
                throw new ValidationException ("QueryParam ServiceInfo", e);

            }
            
        }

        return orchestrationFilterParams;
    }

    public void createRequestRecord (Status status, Action action) {

        Session session = null;
        try {

            session = HibernateUtil.getSessionFactory ().openSession ();
            session.beginTransaction ();

            if (null == sir) {
                sir = new ServiceInstancesRequest ();
            }

            InfraActiveRequests aq = new InfraActiveRequests ();
            aq.setRequestId (requestId);
            
            aq.setRequestAction(action.name());
            aq.setAction(action.name());
            
            Timestamp startTimeStamp = new Timestamp (System.currentTimeMillis());
            
            aq.setStartTime (startTimeStamp);
                      
            if (requestInfo != null) {  

                if(requestInfo.getSource() != null){
                    aq.setSource(requestInfo.getSource());
                }
                if(requestInfo.getCallbackUrl() != null){
                    aq.setCallBackUrl(requestInfo.getCallbackUrl());
                }
                if(requestInfo.getCorrelator() != null){
                    aq.setCorrelator(requestInfo.getCorrelator());
                }
  
            }
                       
            if (modelInfo != null) { 
                aq.setRequestScope(requestScope);
            }
            
            if (cloudConfiguration != null) {
                if(cloudConfiguration.getLcpCloudRegionId() != null) {
                    aq.setAicCloudRegion(cloudConfiguration.getLcpCloudRegionId());
                }

                if(cloudConfiguration.getTenantId() != null) {
                    aq.setTenantId(cloudConfiguration.getTenantId());
                }

            }
            
            if(sir.getServiceInstanceId() != null){
                aq.setServiceInstanceId(sir.getServiceInstanceId());
            }
            
            if(sir.getVnfInstanceId() != null){
                aq.setVnfId(sir.getVnfInstanceId());
            }

            if (null != requestScope) {
                if (requestScope.equalsIgnoreCase(ModelType.service.name())) {
                    if (requestInfo.getInstanceName() != null) {
                        aq.setServiceInstanceName(requestInfo.getInstanceName());
                    }
                }

                if (requestScope.equalsIgnoreCase(ModelType.network.name())) {
                    aq.setNetworkName(requestInfo.getInstanceName());
                    aq.setNetworkType(networkType);
                    aq.setNetworkId(sir.getNetworkInstanceId());

                }

                if (requestScope.equalsIgnoreCase(ModelType.volumeGroup.name())) {
                    aq.setVolumeGroupId(sir.getVolumeGroupInstanceId());
                    aq.setVolumeGroupName(requestInfo.getInstanceName());
                    aq.setVnfType(vnfType);

                }

                if (requestScope.equalsIgnoreCase(ModelType.vfModule.name())) {
                    aq.setVfModuleName(requestInfo.getInstanceName());
                    aq.setVfModuleModelName(modelInfo.getModelName());
                    aq.setVfModuleId(sir.getVfModuleInstanceId());
                    aq.setVolumeGroupId(sir.getVolumeGroupInstanceId());
                    aq.setVnfType(vnfType);

                }

                if (requestScope.equalsIgnoreCase(ModelType.vnf.name())) {
                    aq.setVnfName(requestInfo.getInstanceName());
                    if (null != sir.getRequestDetails()) {
                        RelatedInstanceList[] instanceList = sir.getRequestDetails().getRelatedInstanceList();

                        if (instanceList != null) {

                            for (RelatedInstanceList relatedInstanceList : instanceList) {

                                RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();
                                if (relatedInstance.getModelInfo().getModelType().equals(ModelType.service)) {
                                    aq.setVnfType(vnfType);
                                }
                            }
                        }
                    }
                    //aq.setVnfType(sir.getRequestDetails().getRelatedInstanceList());

                }
            }

            aq.setRequestBody (this.requestJSON);
            
                       
            aq.setRequestStatus (status.toString ());
            aq.setLastModifiedBy (Constants.MODIFIED_BY_APIHANDLER);
            
            if ((status == Status.FAILED) || (status == Status.COMPLETE)) {
                aq.setStatusMessage (this.errorMessage);
                aq.setResponseBody (this.responseBody);
                aq.setProgress(new Long(100));

                Timestamp endTimeStamp = new Timestamp (System.currentTimeMillis());
                aq.setEndTime (endTimeStamp);
            }
 
            msoLogger.debug ("About to insert a record");

            session.save (aq);
            session.getTransaction ().commit ();
            session.close ();
        } catch (Exception e) {
            msoLogger.error (MessageEnum.APIH_DB_INSERT_EXC, "", "", MsoLogger.ErrorCode.DataError, "Exception when creation record request", e);
            if (session != null) {
                session.close ();
            }
            if (!status.equals (Status.FAILED)) {
                throw e;
            }
        }
    }

    public void updateFinalStatus (Status status) {
        int result = 0;
        try {
            result = RequestsDatabase.updateInfraFinalStatus (requestId,
                                                              status.toString (),
                                                              this.errorMessage,
                                                              this.progress,
                                                              this.responseBody,
                                                              Constants.MODIFIED_BY_APIHANDLER);
        } catch (Exception e) {
            msoLogger.error(MessageEnum.APIH_DB_UPDATE_EXC, e.getMessage(), "", "", MsoLogger.ErrorCode.DataError, "Exception when updating record in DB");
            msoLogger.debug ("Exception: ", e);
        }
    }

    public Response buildResponse (int httpResponseCode, String errorCode, InfraActiveRequests inProgress) {
        return buildResponseWithError (httpResponseCode, errorCode, inProgress, null);
    }

    public Response buildResponseWithError (int httpResponseCode,
                                            String errorCode,
                                            InfraActiveRequests inProgress,
                                            String errorString) {

      
        
        // Log the failed request into the MSO Requests database

        return Response.status (httpResponseCode).entity (null).build ();

    }

    public Response buildResponseFailedValidation (int httpResponseCode, String exceptionMessage) {

 

        return Response.status (httpResponseCode).entity (null).build ();
    }

    public String getRequestUri () {
        return requestUri;
    }

    public void setRequestUri (String requestUri) {
        this.requestUri = requestUri;
    }

    public VnfInputs getVnfInputs () {
        return vnfInputs;
    }

    public RequestInfo getRequestInfo () {
        return requestInfo;
    }

    public String getResponseBody () {
        return responseBody;
    }

    public void setResponseBody (String responseBody) {
        this.responseBody = responseBody;
    }

    public String getHttpResponse () {
        return httpResponse;
    }

    public void setHttpResponse (String httpResponse) {
        this.httpResponse = httpResponse;
    }

    public String getRequestId () {
        return requestId;
    }

    public String getRequestXML () {
        return requestXML;
    }

    public void setRequestXML (String requestXML) {
        this.requestXML = requestXML;
    }

    public RequestStatusType getStatus () {
        return status;
    }
    
    public String getServiceType () {
        if (this.vnfInputs.getServiceType () != null)
            return this.vnfInputs.getServiceType ();
        if (this.vnfInputs.getServiceId () != null)
            return this.vnfInputs.getServiceId ();
        return null;
    }

    public void setStatus (RequestStatusType status) {
        this.status = status;
        switch (status) {
        case FAILED:
        case COMPLETE:
            this.progress = Constants.PROGRESS_REQUEST_COMPLETED;
            break;
        case IN_PROGRESS:
            this.progress = Constants.PROGRESS_REQUEST_IN_PROGRESS;
            break;
        }
    }
    
    public ModelInfo getModelInfo() {
        return modelInfo;
    }
    
    public ServiceInstancesRequest getServiceInstancesRequest() {
        return sir;
    }
    
    public String getServiceInstanceType () {
        return serviceInstanceType;
    }    
    
    public String getNetworkType () {
        return networkType;
    }
    
    public String getVnfType () {
        return vnfType;
    }
    
    public String getVfModuleModelName () {
        return vfModuleModelName;
    }
    
    public String getVfModuleType () {
        return vfModuleType;
    }
    
    public String getAsdcServiceModelVersion () {
        return asdcServiceModelVersion;
    }
    
    public static String domToStr (Document doc) {
        if (doc == null) {
            return null;
        }

        try {
            StringWriter sw = new StringWriter ();
            StreamResult sr = new StreamResult (sw);
            TransformerFactory tf = TransformerFactory.newInstance ();
            Transformer t = tf.newTransformer ();
            t.setOutputProperty (OutputKeys.STANDALONE, "yes");
            NodeList nl = doc.getDocumentElement ().getChildNodes ();
            DOMSource source = null;
            for (int x = 0; x < nl.getLength (); x++) {
                Node e = nl.item (x);
                if (e instanceof Element) {
                    source = new DOMSource (e);
                    break;
                }
            }
            if (source != null) {
                t.transform (source, sr);

                String s = sw.toString ();
                return s;
            }

            return null;

        } catch (Exception e) {
            msoLogger.error (MessageEnum.APIH_DOM2STR_ERROR, "", "", MsoLogger.ErrorCode.DataError, "Exception in domToStr", e);
        }
        return null;
    }
    
    public void addBPMNSpecificInputs(String personaModelId, String personaModelVersion, Boolean isBaseVfModule,
                String vnfPersonaModelId, String vnfPersonaModelVersion) {
        vnfInputs.setPersonaModelId(personaModelId);
        vnfInputs.setPersonaModelVersion(personaModelVersion);
        vnfInputs.setIsBaseVfModule(isBaseVfModule);
        vnfInputs.setVnfPersonaModelId(vnfPersonaModelId);
        vnfInputs.setVnfPersonaModelVersion(vnfPersonaModelVersion);

        this.vnfReq.setVnfInputs(vnfInputs);

          StringWriter stringWriter = new StringWriter ();
          try {
              JAXBContext jaxbContext = JAXBContext.newInstance (VnfRequest.class);
              Marshaller jaxbMarshaller = jaxbContext.createMarshaller ();

              // output pretty printed
              jaxbMarshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, true);

              jaxbMarshaller.marshal (this.vnfReq, stringWriter);

          } catch (JAXBException e) {
              msoLogger.debug ("Exception: ", e);
          }

          this.requestXML = stringWriter.toString ();
          msoLogger.debug("REQUEST XML to BPEL: " + this.requestXML);


    }
    
    private static boolean empty(String s) {
        return (s == null || s.trim().isEmpty());
    }
}