Consider component model when retrieving capability types
[sdc.git] / catalog-ui / src / app / ng2 / pages / workspace / req-and-capabilities / req-and-capabilities.service.ts
1 import { Injectable } from "@angular/core";
2 import { TopologyTemplateService } from "../../../services/component-services/topology-template.service";
3 import { Store } from "@ngxs/store";
4 import { SdcUiServices } from "onap-ui-angular";
5 import { CapabilityTypeModel } from "../../../../models/capability-types";
6 import { RelationshipTypeModel } from "../../../../models/relationship-types";
7 import { NodeTypeModel } from "../../../../models/node-types";
8 import { WorkspaceService } from "../workspace.service";
9 import { ToscaTypesServiceNg2 } from "../../../services/tosca-types.service";
10
11
12
13 @Injectable()
14 export class ReqAndCapabilitiesService {
15
16     private capabilityTypesList: CapabilityTypeModel[];
17     private relationshipTypesList: RelationshipTypeModel[];
18     private nodeTypesList: NodeTypeModel[];
19     private capabilitiesListUpdated: boolean = false;
20     private requirementsListUpdated: boolean = false;
21     private nodeTypeListUpdated: boolean = false;
22
23     readonly INPUTS_FOR_REQUIREMENTS: string = 'INPUTS_FOR_REQUIREMENTS';
24     readonly INPUTS_FOR_CAPABILITIES: string = 'INPUTS_FOR_CAPABILITIES';
25
26     constructor(
27         private workspaceService: WorkspaceService,
28         private modalService: SdcUiServices.ModalService,
29         private loaderService: SdcUiServices.LoaderService,
30         private topologyTemplateService: TopologyTemplateService,
31         private store: Store,
32         private toscaTypesServiceNg2: ToscaTypesServiceNg2){}
33
34     public isViewOnly = (): boolean => {
35         return this.store.selectSnapshot((state) => state.workspace.isViewOnly);
36     }
37
38     public isDesigner = (): boolean => {
39         return this.store.selectSnapshot((state) => state.workspace.isDesigner);
40     }
41
42     public async initInputs(initInputsFor: string) {
43
44         if (!this.capabilitiesListUpdated){
45             // -- COMMON for both --
46             this.capabilityTypesList = [];
47             let capabilityTypesResult = await this.toscaTypesServiceNg2.fetchCapabilityTypes(this.workspaceService.metadata.model);
48             Object.keys(capabilityTypesResult).forEach(key => {this.capabilityTypesList.push(capabilityTypesResult[key])})
49             this.capabilitiesListUpdated = true;
50         }
51
52         if (initInputsFor === 'INPUTS_FOR_REQUIREMENTS') {
53             if (!this.requirementsListUpdated){
54                 this.relationshipTypesList = [];
55                 let relationshipTypesResult = await this.toscaTypesServiceNg2.fetchRelationshipTypes(this.workspaceService.metadata.model);
56                 Object.keys(relationshipTypesResult).forEach(key => {this.relationshipTypesList.push(relationshipTypesResult[key])});
57                 this.requirementsListUpdated = true;
58             }
59
60             if (!this.nodeTypeListUpdated){
61                 this.nodeTypesList = [];
62                 let nodeTypesResult = await this.toscaTypesServiceNg2.fetchNodeTypes();
63                 Object.keys(nodeTypesResult).forEach(key => {this.nodeTypesList.push(nodeTypesResult[key])})
64                 this.nodeTypeListUpdated = true;
65             }
66         }
67     }
68
69     getCapabilityTypesList() {
70         return this.capabilityTypesList;
71     }
72
73     getRelationsShipeTypeList() {
74         return this.relationshipTypesList;
75     }
76
77     getNodeTypesList() {
78         return this.nodeTypesList;
79     }
80 }