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