Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / pages / composition / graph / utils / composition-graph-service-path-utils.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 import * as _ from "lodash";
22 import {CompositionGraphGeneralUtils} from "./composition-graph-general-utils";
23 import {ServiceServiceNg2} from 'app/ng2/services/component-services/service.service';
24 import {Service} from "app/models/components/service";
25 import {ForwardingPath} from "app/models/forwarding-path";
26 import {ForwardingPathLink} from "app/models/forwarding-path-link";
27 import {ComponentRef, Injectable} from "@angular/core";
28 import {CompositionCiServicePathLink} from "app/models/graph/graph-links/composition-graph-links/composition-ci-service-path-link";
29 import {SdcUiServices} from "onap-ui-angular";
30 import {QueueServiceUtils} from "app/ng2/utils/queue-service-utils";
31 import {ServicePathsListComponent} from "app/ng2/pages/composition/graph/service-paths-list/service-paths-list.component";
32 import {ButtonModel, ModalModel} from "app/models";
33 import {ServicePathCreatorComponent} from "app/ng2/pages/composition/graph/service-path-creator/service-path-creator.component";
34 import {ModalService} from "app/ng2/services/modal.service";
35 import {ModalComponent} from "app/ng2/components/ui/modal/modal.component";
36 import {Select, Store} from "@ngxs/store";
37 import {WorkspaceState} from "app/ng2/store/states/workspace.state";
38 import {WorkspaceService} from "app/ng2/pages/workspace/workspace.service";
39 import {CompositionService} from "../../composition.service";
40 import {CommonGraphUtils} from "../common/common-graph-utils";
41 import {GRAPH_EVENTS} from "app/utils/constants";
42 import {EventListenerService} from "app/services/event-listener-service";
43
44 @Injectable()
45 export class ServicePathGraphUtils {
46
47     constructor(
48         private generalGraphUtils: CompositionGraphGeneralUtils,
49         private serviceService: ServiceServiceNg2,
50         private commonGraphUtils: CommonGraphUtils,
51         private loaderService: SdcUiServices.LoaderService,
52         private queueServiceUtils: QueueServiceUtils,
53         private modalService: ModalService,
54         private workspaceService: WorkspaceService,
55         private compositionService: CompositionService,
56         private store:Store,
57         private eventListenerService: EventListenerService
58     ) {
59     }
60
61     private isViewOnly = (): boolean => {
62         return this.store.selectSnapshot(state => state.workspace.isViewOnly);
63     }
64     private modalInstance: ComponentRef<ModalComponent>;
65
66     public deletePathsFromGraph(cy: Cy.Instance) {
67         cy.remove(`[type="${CompositionCiServicePathLink.LINK_TYPE}"]`);
68     }
69
70     public drawPath(cy: Cy.Instance, forwardingPath: ForwardingPath) {
71         let pathElements = forwardingPath.pathElements.listToscaDataDefinition;
72
73         _.forEach(pathElements, (link: ForwardingPathLink) => {
74             let data: CompositionCiServicePathLink = new CompositionCiServicePathLink(link);
75             data.source = _.find(
76                 this.compositionService.componentInstances,
77                 instance => instance.name === data.forwardingPathLink.fromNode
78             ).uniqueId;
79             data.target = _.find(
80                 this.compositionService.componentInstances,
81                 instance => instance.name === data.forwardingPathLink.toNode
82             ).uniqueId;
83             data.pathId = forwardingPath.uniqueId;
84             data.pathName = forwardingPath.name;
85             this.commonGraphUtils.insertServicePathLinkToGraph(cy, data);
86         });
87     }
88
89     public createOrUpdateServicePath = (path: any): void => {
90         this.loaderService.activate();
91
92         let onSuccess: (response: ForwardingPath) => void = (response: ForwardingPath) => {
93             this.loaderService.deactivate();
94             this.compositionService.forwardingPaths[response.uniqueId] = response;
95             this.eventListenerService.notifyObservers(GRAPH_EVENTS.ON_SERVICE_PATH_CREATED, response.uniqueId)
96         };
97
98         this.queueServiceUtils.addBlockingUIAction(
99             () => this.serviceService.createOrUpdateServicePath(this.workspaceService.metadata.uniqueId, path).subscribe(onSuccess
100             , (error) => {this.loaderService.deactivate()})
101         );
102     };
103
104     public onCreateServicePath = (): void => {
105         // this.showServicePathMenu = false;
106         let cancelButton: ButtonModel = new ButtonModel('Cancel', 'outline white', this.modalService.closeCurrentModal);
107         let saveButton: ButtonModel = new ButtonModel('Create', 'blue', this.createPath, this.getDisabled);
108         let modalModel: ModalModel = new ModalModel('l', 'Create Service Flow', '', [saveButton, cancelButton], 'standard', true);
109         this.modalInstance = this.modalService.createCustomModal(modalModel);
110         this.modalService.addDynamicContentToModal(this.modalInstance, ServicePathCreatorComponent, {serviceId: this.workspaceService.metadata.uniqueId});
111         this.modalInstance.instance.open();
112     };
113
114     public onListServicePath = (): void => {
115         // this.showServicePathMenu = false;
116         let cancelButton: ButtonModel = new ButtonModel('Close', 'outline white', this.modalService.closeCurrentModal);
117         let modalModel: ModalModel = new ModalModel('md', 'Service Flows List', '', [cancelButton], 'standard', true);
118         this.modalInstance = this.modalService.createCustomModal(modalModel);
119         this.modalService.addDynamicContentToModal(this.modalInstance, ServicePathsListComponent, {
120             serviceId: this.workspaceService.metadata.uniqueId,
121             onCreateServicePath: this.onCreateServicePath,
122             onEditServicePath: this.onEditServicePath,
123             isViewOnly: this.isViewOnly()
124         });
125         this.modalInstance.instance.open();
126     };
127
128     public onEditServicePath = (id: string): void => {
129         let cancelButton: ButtonModel = new ButtonModel('Cancel', 'outline white', this.modalService.closeCurrentModal);
130         let saveButton: ButtonModel = new ButtonModel('Save', 'blue', this.createPath, this.getDisabled);
131         let modalModel: ModalModel = new ModalModel('l', 'Edit Path', '', [saveButton, cancelButton], 'standard', true);
132         this.modalInstance = this.modalService.createCustomModal(modalModel);
133         this.modalService.addDynamicContentToModal(this.modalInstance, ServicePathCreatorComponent, {
134             serviceId: this.workspaceService.metadata.uniqueId,
135             pathId: id
136         });
137         this.modalInstance.instance.open();
138     };
139
140     public getDisabled = (): boolean => {
141         return this.isViewOnly() || !this.modalInstance.instance.dynamicContent.instance.checkFormValidForSubmit();
142     };
143
144     public createPath = (): void => {
145         this.createOrUpdateServicePath(this.modalInstance.instance.dynamicContent.instance.createServicePathData());
146         this.modalService.closeCurrentModal();
147     };
148 }