CSIT Fix for SDC-2585
[sdc.git] / catalog-ui / src / app / directives / graphs-v2 / 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 { LoaderService } from "app/services";
23 import { CompositionGraphGeneralUtils } from "./composition-graph-general-utils";
24 import { ICompositionGraphScope } from "../composition-graph.directive";
25 import { ServiceServiceNg2 } from 'app/ng2/services/component-services/service.service';
26 import { Service } from "../../../../models/components/service";
27 import { ForwardingPath } from "app/models/forwarding-path";
28 import { ForwardingPathLink } from "app/models/forwarding-path-link";
29 import { CompositionCiServicePathLink } from "../../../../models/graph/graph-links/composition-graph-links/composition-ci-service-path-link";
30 import { CommonGraphUtils } from "app/directives/graphs-v2/common/common-graph-utils";
31
32 export class ServicePathGraphUtils {
33
34     constructor(
35         private loaderService: LoaderService,
36         private generalGraphUtils: CompositionGraphGeneralUtils,
37         private serviceService: ServiceServiceNg2,
38         private commonGraphUtils: CommonGraphUtils
39     ) { }
40
41     public deletePathsFromGraph(cy: Cy.Instance, service: Service) {
42         cy.remove(`[type="${CompositionCiServicePathLink.LINK_TYPE}"]`);
43     }
44
45     public drawPath(cy: Cy.Instance, forwardingPath: ForwardingPath, service: Service) {
46         let pathElements = forwardingPath.pathElements.listToscaDataDefinition;
47
48         _.forEach(pathElements, (link: ForwardingPathLink) => {
49             let data: CompositionCiServicePathLink = new CompositionCiServicePathLink(link);
50             data.source = _.find(
51                 service.componentInstances,
52                 instance => instance.name === data.forwardingPathLink.fromNode
53             ).uniqueId;
54             data.target = _.find(
55                 service.componentInstances,
56                 instance => instance.name === data.forwardingPathLink.toNode
57             ).uniqueId;
58             data.pathId = forwardingPath.uniqueId;
59             data.pathName = forwardingPath.name;
60             this.commonGraphUtils.insertServicePathLinkToGraph(cy, data);
61         });
62     }
63
64     public createOrUpdateServicePath = (scope: ICompositionGraphScope, path: any): void => {
65         let service = <Service>scope.component;
66         this.loaderService.showLoader('composition-graph');
67
68         let onSuccess: (response: ForwardingPath) => void = (response: ForwardingPath) => {
69
70             service.forwardingPaths[response.uniqueId] = response;
71             scope.selectedPathId = response.uniqueId;
72         };
73
74         this.generalGraphUtils.getGraphUtilsServerUpdateQueue().addBlockingUIActionWithReleaseCallback(
75             () => this.serviceService.createOrUpdateServicePath(service, path).subscribe(onSuccess),
76             () => this.loaderService.hideLoader('composition-graph')
77         );
78     };
79 }
80
81 ServicePathGraphUtils.$inject = [
82     'LoaderService',
83     'CompositionGraphGeneralUtils',
84     'ServiceServiceNg2',
85     'CommonGraphUtils'
86 ];