Add Semicolon at the end
[vid.git] / vid-webpack-master / src / app / drawingBoard / service-planning / objectsToTree / models / vrf / vrfModal / networkStep / network.step.service.ts
1 import {Injectable} from "@angular/core";
2 import {NgRedux} from "@angular-redux/store";
3 import {AppState} from "../../../../../../../shared/store/reducers";
4 import {AaiService} from "../../../../../../../shared/services/aaiService/aai.service";
5 import {
6   updateGenericModalCriteria,
7   updateGenericModalhelper
8 } from "../../../../../../../shared/storeUtil/utils/global/global.actions";
9 import {ElementsTableService} from "../../../../../../../shared/components/searchMembersModal/members-table/elements-table.service";
10 import {
11   ICriteria, ISearchField, ITableContent,
12   ModalInformation
13 } from "../../../../../../../shared/components/searchMembersModal/members-table/element-table-row.model";
14 import {Observable, of} from "rxjs";
15 import * as _ from "lodash";
16 import {CustomTableColumnDefinition} from "../../../../../../../shared/components/searchMembersModal/members-table/elements-table.component";
17 import {NetworkModalRow} from "./network.step.model";
18
19 @Injectable()
20 export class NetworkStepService {
21   constructor(private _store: NgRedux<AppState>, private _aaiService: AaiService) {}
22
23   uniqObjectField: string = "instanceId";
24   type: string = "Network";
25
26   getNetworkStep = (serviceInstance, serviceModelId, ...otherSteps): ModalInformation => {
27     return {
28       type: this.type,
29       serviceModelId: serviceModelId,
30       title: 'Associate network to VRF Entry',
31       description: 'Select a network to associate to the VRF Entry',
32       noElementsMsg: 'No network were found.',
33       maxSelectRow: 1,
34       uniqObjectField: this.uniqObjectField,
35       backAction: (searchElementsModalComponent) => {
36         searchElementsModalComponent.closeDialog();
37       },
38       topButton: {
39         text: 'Next',
40         action: (searchElementsModalComponent) => {
41           let tmpMembers = searchElementsModalComponent._membersTableService.allElementsStatusMap;
42           let vpnStep = otherSteps[0];
43           for (let key in tmpMembers) {
44             if (tmpMembers[key].isSelected) {
45               this._store.dispatch(updateGenericModalhelper(`selected${this.type}`, tmpMembers[key], this.uniqObjectField));
46             }
47           }
48           ElementsTableService.changeModalInformationDataTrigger.next({
49             modalInformation: vpnStep.getVpnStep(serviceInstance, searchElementsModalComponent.modalInformation.serviceModelId, ...otherSteps),
50             selectedRowsIds: []
51           })
52         }
53       },
54       searchButton: {
55         text: 'Search',
56         action: (searchElementsModalComponent) => {
57           this.getNetworks(serviceInstance).subscribe((networks) => {
58             const currentRole = searchElementsModalComponent.modalInformation.criteria.find((criteria: ICriteria) => {
59               criteria.label === 'Roles';
60             });
61             ElementsTableService.changeFnTableDataTrigger.next(
62               networks.filter((network) => {
63                 if (_.isNil(currentRole)) return network;
64                 return network.role === currentRole.currentValue;
65               }));
66           });
67         }
68       },
69       getElements: (...args) => this.getNetworks(serviceInstance),
70       criteria: this.getNetworkStepCriteria(serviceInstance),
71       searchFields: this.getsNetworkStepSearchFields(serviceInstance),
72       tableHeaders: this.getNetworkStepHeaders(),
73       tableContent: this.getNetworkTableContent()
74     };
75   };
76
77   getNetworks = (serviceInstance): Observable<any[]> => {
78     let cloudRegion = serviceInstance.lcpCloudRegionId;
79     let tenantId = serviceInstance.tenantName;
80     return this._aaiService.retrieveActiveNetwork(cloudRegion, tenantId).map((networks: NetworkModalRow[]) => {
81       this.generateRolesOptions(networks);
82       return this.sortElementsResultByField(networks, this.uniqObjectField);
83     });
84   };
85
86   getNetworkStepCriteria(serviceInstance): ICriteria[] {
87     return [{
88       label: "Roles",
89       defaultValue: "",
90       type: 'DROPDOWN',
91       onInit: (): Observable<any> => of(this._store.getState().global.genericModalCriteria.roles),
92       onChange: (that, selectedOption): void => {
93         that.currentValue = selectedOption;
94       },
95       dataTestId: 'roles-criteria',
96       isRequired: false
97     }]
98   }
99
100   getsNetworkStepSearchFields = (serviceInstance): ISearchField[] => {
101     return [
102       {
103         title: 'Region',
104         dataTestId: 'Region',
105         value: serviceInstance["lcpCloudRegionId"],
106         type: "LABEL"
107       }, {
108         title: 'Tenant',
109         dataTestId: 'Tenant',
110         value: serviceInstance["tenantName"],
111         type: "LABEL"
112       },
113       {
114         title: 'Orch. status',
115         dataTestId: 'Orch_status',
116         value: "Active",
117         type: "LABEL"
118       },
119       {
120         title: 'Related to',
121         dataTestId: 'Related_to',
122         value: "VPN binding",
123         type: "LABEL"
124       }
125     ]
126   };
127
128   generateRolesOptions = (networks): void => {
129     let roles: string[] = (_.uniq(_.flatten(_.map(networks, 'role').filter((role) => !_.isNil(role))))).sort();
130     const SELECTED_AN_OPTION: string = "-- select an option --";
131     const NOT_ASSIGNED: string = "Not assigned";
132
133     roles = _.remove([SELECTED_AN_OPTION, this.shouldAddNotAssigned(networks) ? NOT_ASSIGNED : null, ...roles], null);
134     this._store.dispatch(updateGenericModalCriteria("roles", roles));
135   };
136
137   getNetworkStepHeaders = (): CustomTableColumnDefinition[] => {
138     return [
139       {displayName: `Name`, key: ['instanceName']},
140       {displayName: `Type`, key: ['instanceType']},
141       {displayName: `Role`, key: ['role']},
142       {displayName: 'Orch. Status', key: ['orchStatus']},
143       {displayName: 'Physical name', key: ['physicalName']},
144       {displayName: 'Instance ID', key: ['instanceId']},
145       {displayName: 'Model UUID', key: ['modelInfo', 'modelVersionId']},
146       {displayName: 'Service name', key: ['serviceName']},
147       {displayName: 'Service UUID', key: ['serviceUUID']},
148       {displayName: 'Tenant', key: ['tenantName']},
149       {displayName: 'Region', key: ['lcpCloudRegionId']}
150     ];
151   };
152
153   getNetworkTableContent = (): ITableContent[] => {
154     return [
155       {
156         id: 'instanceName',
157         contents: [{
158           id: ['name'],
159           value: ["instanceName"]
160         }]
161       },
162       {
163         id: 'instanceType',
164         contents: [{
165           id: ['type'],
166           value: ['instanceType']
167         }]
168       },
169       {
170         id: 'role',
171         contents: [{
172           id: ['role'],
173           value: ['role']
174         }]
175       },
176       {
177         id: 'network-orch-status',
178         contents: [{
179           id: ['orchStatus'],
180           value: ['orchStatus']
181         }]
182       },
183       {
184         id: 'network-physical-name',
185         contents: [{
186           id: ['physicalName'],
187           value: ['physicalName']
188         }]
189       },
190       {
191         id: 'network-instance-id',
192         contents: [{
193           id: ['instanceID'],
194           value: ['instanceId']
195         }]
196       },
197       {
198         id: 'network-model-uuid',
199         contents: [{
200           id: ['modelUUID'],
201           value: ['modelInfo', 'modelVersionId']
202         }]
203       },
204       {
205         id: 'network-serviceName',
206         contents: [{
207           id: ['serviceName'],
208           value: ['serviceName']
209         }]
210       },
211       {
212         id: 'network-service-id',
213         contents: [{
214           id: ['serviceUUID'],
215           value: ['serviceUUID']
216         }]
217       },
218       {
219         id: 'network-tenantName',
220         contents: [{
221           id: ['tenant'],
222           value: ['tenantName']
223         }]
224       },
225       {
226         id: 'network-region',
227         contents: [{
228           id: ['region'],
229           value: ['lcpCloudRegionId']
230         }]
231       }
232     ];
233   };
234
235   sortElementsResultByField = (elements, fieldName): any[] => {
236     return _.sortBy(elements, o => o[fieldName]);
237   };
238
239   shouldAddNotAssigned = (networks): boolean => {
240     return _.values(networks).some(network => _.isNil(network.role) || network.role === "");
241   };
242 }