Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / pages / composition / graph / service-path-creator / service-path-creator.component.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 { Component, ElementRef, forwardRef, Inject } from '@angular/core';
23 import {Link} from './link-row/link.model';
24 import {ForwardingPath} from 'app/models/forwarding-path';
25 import {ServiceServiceNg2} from "app/ng2/services/component-services/service.service";
26 import {ForwardingPathLink} from "app/models/forwarding-path-link";
27 import {ServicePathMapItem} from "app/models/graph/nodes-and-links-map";
28 import {CompositionService} from "app/ng2/pages/composition/composition.service";
29
30 @Component({
31     selector: 'service-path-creator',
32     templateUrl: './service-path-creator.component.html',
33     styleUrls:['./service-path-creator.component.less'],
34     providers: [ServiceServiceNg2]
35 })
36
37 export class ServicePathCreatorComponent {
38
39     linksMap:Array<ServicePathMapItem>;
40     links:Array<Link> = [];
41     input:any;
42     headers: Array<string> = [];
43     removeRow: Function;
44     forwardingPath:ForwardingPath;
45     //isExtendAllowed:boolean = false;
46
47     constructor(private serviceService: ServiceServiceNg2,
48                 private compositionService: CompositionService) {
49         this.forwardingPath = new ForwardingPath();
50         this.links = [new Link(new ForwardingPathLink('', '', '', '', '', ''), true, false, true)];
51         this.headers = ['Source', 'Source Connection Point', 'Target', 'Target Connection Point', ' '];
52         this.removeRow = () => {
53             if (this.links.length === 1) {
54                 return;
55             }
56             this.links.splice(this.links.length-1, 1);
57             this.enableCurrentRow();
58         };
59     }
60
61     ngOnInit() {
62         this.serviceService.getNodesAndLinksMap(this.input.serviceId).subscribe((res:any) => {
63             this.linksMap = res;
64         });
65         this.processExistingPath();
66
67     }
68
69     private processExistingPath() {
70         if (this.input.pathId) {
71             let forwardingPath = <ForwardingPath>{...this.compositionService.forwardingPaths[this.input.pathId]};
72             this.forwardingPath.name = forwardingPath.name;
73             this.forwardingPath.destinationPortNumber = forwardingPath.destinationPortNumber;
74             this.forwardingPath.protocol = forwardingPath.protocol;
75             this.forwardingPath.uniqueId = forwardingPath.uniqueId;
76             this.links = [];
77             _.forEach(forwardingPath.pathElements.listToscaDataDefinition, (link:ForwardingPathLink) => {
78                 this.links[this.links.length] = new Link(link, false, false, false);
79             });
80             this.links[this.links.length - 1].canEdit = true;
81             this.links[this.links.length - 1].canRemove = true;
82             this.links[0].isFirst = true;
83         }
84     }
85
86     isExtendAllowed():boolean {
87         if (this.links[this.links.length-1].toCP) {
88             return true;
89         }
90         return false;
91     }
92
93     enableCurrentRow() {
94         this.links[this.links.length-1].canEdit = true;
95         if (this.links.length !== 1) {
96             this.links[this.links.length-1].canRemove = true;
97         }
98     }
99
100     addRow() {
101         this.disableRows();
102         this.links[this.links.length] = new Link(
103             new ForwardingPathLink(this.links[this.links.length-1].toNode,
104                 this.links[this.links.length-1].toCP,
105                 '',
106                 '',
107                 this.links[this.links.length-1].toCPOriginId,
108                 ''
109             ),
110             true,
111             true,
112             false
113         );
114     }
115
116     disableRows() {
117         for (let i = 0 ; i < this.links.length ; i++) {
118             this.links[i].canEdit = false;
119             this.links[i].canRemove = false;
120         }
121     }
122
123     createPathLinksObject() {
124         for (let i = 0 ; i < this.links.length ; i++) {
125             let link = this.links[i];
126             this.forwardingPath.addPathLink(link.fromNode, link.fromCP, link.toNode, link.toCP, link.fromCPOriginId, link.toCPOriginId);
127         }
128     }
129
130     createServicePathData() {
131         this.createPathLinksObject();
132         return this.forwardingPath;
133     }
134
135     checkFormValidForSubmit():boolean {
136         if (this.forwardingPath.name && this.isPathValid() ) {
137             return true;
138         }
139         return false;
140     }
141
142     isPathValid():boolean {
143         let lastLink = this.links[this.links.length -1] ;
144         if (lastLink.toNode && lastLink.toCP && lastLink.fromNode && lastLink.fromCP) {
145             return true;
146         }
147         return false;
148     }
149 }