Sync Integ to Master
[sdc.git] / catalog-ui / src / app / view-models / workspace / tabs / network-call-flow / network-call-flow-view-model.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 'use strict';
22 import * as _ from "lodash";
23 import {IWorkspaceViewModelScope} from "app/view-models/workspace/workspace-view-model";
24 import {VendorModel} from "app/view-models/workspace/tabs/management-workflow/management-workflow-view-model";
25 import {ResourceType, ArtifactType} from "app/utils";
26 import {ComponentInstance} from "app/models";
27 import {ComponentGenericResponse} from "../../../../ng2/services/responses/component-generic-response";
28 import {ComponentServiceNg2} from "../../../../ng2/services/component-services/component.service";
29 declare var PunchOutRegistry;
30
31 export interface INetworkCallFlowViewModelScope extends IWorkspaceViewModelScope {
32     vendorMessageModel:VendorModel;
33     isLoading: boolean;
34 }
35
36 export class participant {
37     name:string;
38     id:string;
39
40     constructor(instance:ComponentInstance) {
41         this.name = instance.name;
42         this.id = instance.uniqueId;
43     }
44 }
45
46
47 export class NetworkCallFlowViewModel {
48
49     static '$inject' = [
50         '$scope',
51         'uuid4',
52         'ComponentServiceNg2'
53     ];
54
55     constructor(private $scope:INetworkCallFlowViewModelScope,
56                 private uuid4:any,
57                 private ComponentServiceNg2: ComponentServiceNg2) {
58
59         this.$scope.isLoading = true;
60
61         PunchOutRegistry.loadOnBoarding(()=> {
62             this.$scope.isLoading = false;
63             this.initComponentInstancesAndInformationalArtifacts();
64         });
65     }
66
67     private getVFParticipantsFromInstances(instances:Array<ComponentInstance>):Array<participant> {
68         let participants = [];
69         _.forEach(instances, (instance)=> {
70             if (ResourceType.VF == instance.originType) {
71                 participants.push(new participant(instance));
72             }
73         });
74         return participants;
75     }
76
77     private initComponentInstancesAndInformationalArtifacts = ():void => {
78         if(!this.$scope.component.artifacts || !this.$scope.component.componentInstances) {
79             this.$scope.isLoading = true;
80             this.ComponentServiceNg2.getComponentInformationalArtifactsAndInstances(this.$scope.component).subscribe((response:ComponentGenericResponse) => {
81                 this.$scope.component.artifacts = response.artifacts;
82                 this.$scope.component.componentInstances = response.componentInstances;
83                 this.initScope();
84                 this.$scope.isLoading = false;
85             });
86         } else {
87             this.initScope();
88         }
89     }
90
91     private initScope():void {
92         this.$scope.vendorMessageModel = new VendorModel(
93             this.$scope.component.artifacts.filteredByType(ArtifactType.THIRD_PARTY_RESERVED_TYPES.NETWORK_CALL_FLOW),
94             this.$scope.component.uniqueId,
95             this.$scope.isViewMode(),
96             this.$scope.user.userId,
97             this.uuid4.generate(),
98             ArtifactType.THIRD_PARTY_RESERVED_TYPES.NETWORK_CALL_FLOW,
99             this.getVFParticipantsFromInstances(this.$scope.component.componentInstances)
100         );
101
102         this.$scope.thirdParty = true;
103         this.$scope.setValidState(true);
104     }
105
106 }