d595c2b8f6d17683caca8e330e3dcabfc174724e
[sdc.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 import {Component, Inject, OnInit} from '@angular/core';
21 import {IStepComponent} from "../../../../../../models/wizard-step";
22 import {ConnectionWizardService} from "../connection-wizard.service";
23 import {Component as IComponent} from "../../../../../../models/components/component";
24 import {ComponentServiceNg2} from "../../../../../services/component-services/component.service";
25 import {Observable} from "rxjs";
26 import {Operation} from "../create-interface-operation/model/operation";
27
28 @Component({
29   selector: 'app-relationship-operations-step',
30   templateUrl: './relationship-operations-step.component.html',
31   styleUrls: ['./relationship-operations-step.component.less']
32 })
33 export class RelationshipOperationsStepComponent implements OnInit, IStepComponent {
34
35   private connectionWizardService: ConnectionWizardService;
36   private componentService: ComponentServiceNg2;
37   interfaceTypeMap: Map<string, Array<string>>;
38   component: IComponent;
39   operationList: Array<Operation>;
40   operationList$: Observable<Array<Operation>>;
41   enableAddOperation: boolean;
42
43   constructor(@Inject('$stateParams') private stateParams,
44               connectionWizardService: ConnectionWizardService,
45               componentService: ComponentServiceNg2) {
46     this.component = stateParams.component;
47     this.componentService = componentService;
48     this.connectionWizardService = connectionWizardService;
49     this.interfaceTypeMap = new Map<string, Array<string>>();
50   }
51
52   ngOnInit() {
53     this.loadOperationList();
54     this.loadInterfaceTypeMap();
55   }
56
57   private loadOperationList(): void {
58     if (this.connectionWizardService.selectedMatch.operations) {
59       this.operationList = this.connectionWizardService.selectedMatch.operations.slice();
60     } else {
61       this.operationList = new Array<Operation>();
62     }
63     this.operationList$ = Observable.of(this.operationList);
64   }
65
66   private loadInterfaceTypeMap(): void {
67     this.componentService.getInterfaceTypes(null).subscribe(response => {
68       for (const interfaceType in response) {
69         let operationList = response[interfaceType];
70         //ignore interfaceTypes that doesn't contain operations
71         if (operationList && operationList.length > 0) {
72           //remove operations already on the list
73           const existingOperations =
74               this.operationList.filter(operation => operation.interfaceType === interfaceType);
75           operationList = operationList
76           .filter(operationType => !existingOperations.find(operation => operation.operationType === operationType));
77           if (operationList && operationList.length > 0) {
78             operationList.sort();
79             this.interfaceTypeMap.set(interfaceType, operationList);
80           }
81         }
82       }
83     });
84   }
85
86   preventBack(): boolean {
87     return false;
88   }
89
90   preventNext(): boolean {
91     return false;
92   }
93
94   addOperation() {
95     this.enableAddOperation = !this.enableAddOperation;
96   }
97
98   operationAdded(operation: Operation) {
99     this.enableAddOperation = false;
100     if (operation) {
101       const foundOperation = this.operationList
102       .find(operation1 => operation1.interfaceType === operation.interfaceType
103           && operation1.operationType === operation.operationType);
104       if (foundOperation) {
105         return;
106       }
107       this.operationList.push(operation);
108       this.operationList = this.operationList.slice();
109       this.connectionWizardService.selectedMatch.addToOperations(operation);
110       this.removeFromInterfaceMap(operation);
111     }
112   }
113
114   onRemoveOperation(operation: Operation) {
115     if (!this.operationList) {
116       return;
117     }
118     const index = this.operationList.indexOf(operation);
119     if (index > -1) {
120       this.operationList.splice(index, 1);
121       this.operationList = this.operationList.slice();
122       this.connectionWizardService.selectedMatch.removeFromOperations(operation);
123       this.addToInterfaceMap(operation);
124     }
125   }
126
127   private removeFromInterfaceMap(operation: Operation) {
128     if (!this.interfaceTypeMap.has(operation.interfaceType)) {
129       return;
130     }
131     const operationList = this.interfaceTypeMap.get(operation.interfaceType);
132     if (!operationList) {
133       return;
134     }
135
136     const index = operationList.indexOf(operation.operationType);
137     if (index > -1) {
138       operationList.splice(index, 1);
139     }
140     if (operationList.length == 0) {
141       this.interfaceTypeMap.delete(operation.interfaceType);
142     } else {
143       this.interfaceTypeMap.set(operation.interfaceType, operationList);
144     }
145   }
146
147   private addToInterfaceMap(operation: Operation) {
148     if (!this.interfaceTypeMap.has(operation.interfaceType)) {
149       this.interfaceTypeMap.set(operation.interfaceType, new Array<string>(operation.operationType));
150       return;
151     }
152
153     const operationList = this.interfaceTypeMap.get(operation.interfaceType);
154     if (!operationList) {
155       this.interfaceTypeMap.set(operation.interfaceType, new Array<string>(operation.operationType));
156       return;
157     }
158     operationList.push(operation.operationType);
159     operationList.sort();
160     this.interfaceTypeMap.set(operation.interfaceType, operationList);
161   }
162
163 }