Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 1 | import {Component, ViewChild} from '@angular/core'; |
| 2 | import {ContextMenuComponent, ContextMenuService} from 'ngx-contextmenu'; |
| 3 | import {DialogService} from 'ng2-bootstrap-modal'; |
| 4 | import {MsoService} from '../../../shared/services/msoService/mso.service' |
| 5 | import * as _ from 'lodash'; |
| 6 | import {ActivatedRoute} from '@angular/router'; |
| 7 | import {ServiceInstance} from "../../../shared/models/serviceInstance"; |
| 8 | import {OwningEntity} from "../../../shared/models/owningEntity"; |
| 9 | import {MessageBoxData} from "../../../shared/components/messageBox/messageBox.data"; |
| 10 | import {NgRedux} from "@angular-redux/store"; |
| 11 | import {AppState} from "../../../shared/store/reducers"; |
| 12 | import {IframeService} from "../../../shared/utils/iframe.service"; |
| 13 | import { |
| 14 | GenericFormPopupComponent, |
| 15 | PopupType |
| 16 | } from "../../../shared/components/genericFormPopup/generic-form-popup.component"; |
| 17 | import {ServicePopupService} from "../../../shared/components/genericFormPopup/genericFormServices/service/service.popup.service"; |
| 18 | import {SdcUiCommon} from "onap-ui-angular"; |
| 19 | import {DrawingBoardModes} from "../drawing-board.modes"; |
| 20 | import {DrawingBoardHeaderService} from "./drawing-board-header.service"; |
| 21 | import {ServiceInstanceActions} from "../../../shared/models/serviceInstanceActions"; |
| 22 | import {DrawingBoardPermissions} from "../../guards/servicePlanningGuard/drawingBoardGuard"; |
| 23 | import {MessageBoxService} from "../../../shared/components/messageBox/messageBox.service"; |
| 24 | |
| 25 | @Component({ |
| 26 | selector: 'drawing-board-header', |
| 27 | providers: [MsoService], |
| 28 | templateUrl: './drawing-board-header.component.html', |
| 29 | styleUrls: ['./drawing-board-header.component.scss'] |
| 30 | }) |
| 31 | |
| 32 | export class DrawingBoardHeader { |
| 33 | serviceName: string; |
| 34 | numServicesToDeploy: number; |
| 35 | status: string = 'Designing a new service'; |
| 36 | serviceModelId: string; |
| 37 | jobId: string; |
| 38 | parentElementClassName = 'content'; |
| 39 | mode : DrawingBoardModes = DrawingBoardModes.CREATE; |
| 40 | serviceOrchStatus: string; |
| 41 | isDeleted: boolean = false; |
| 42 | store : NgRedux<AppState>; |
| 43 | drawingBoardPermissions : DrawingBoardPermissions; |
| 44 | drawingBoardHeaderService : DrawingBoardHeaderService; |
| 45 | isServiceFailed: boolean; |
| 46 | serviceStatusMessage: string; |
| 47 | constructor(private _contextMenuService: ContextMenuService, private dialogService: DialogService, |
| 48 | private _iframeService : IframeService, |
| 49 | private route: ActivatedRoute, private msoService: MsoService, |
| 50 | private _servicePopupService : ServicePopupService, |
| 51 | private _drawingBoardHeaderService : DrawingBoardHeaderService, |
| 52 | private _store: NgRedux<AppState>, |
| 53 | private _drawingBoardPermissions : DrawingBoardPermissions) { |
| 54 | this.store = _store; |
| 55 | this.drawingBoardPermissions = _drawingBoardPermissions; |
| 56 | this.drawingBoardHeaderService = _drawingBoardHeaderService; |
| 57 | this.mode = (!_.isNil(this.route.routeConfig.path) && this.route.routeConfig.path !== "") ? this.route.routeConfig.path as DrawingBoardModes : DrawingBoardModes.CREATE; |
| 58 | this.route |
| 59 | .queryParams |
| 60 | .subscribe((params) => { |
| 61 | this.serviceModelId = params['serviceModelId']; |
| 62 | this.jobId = params['jobId']; |
| 63 | if (_.has(this.store.getState().service.serviceHierarchy, this.serviceModelId)) { |
| 64 | this.setValuesFromStore(); |
| 65 | this.store.subscribe(() => { |
| 66 | this.setValuesFromStore(); |
| 67 | }); |
| 68 | } |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | @ViewChild(ContextMenuComponent) public contextMenu: ContextMenuComponent; |
| 74 | |
| 75 | editViewEdit(): void { |
| 76 | window.parent.location.assign(this._drawingBoardHeaderService.generateOldViewEditPath()); |
| 77 | } |
| 78 | |
| 79 | isPermitted() : boolean { |
| 80 | return this.drawingBoardPermissions.isEditPermitted; |
| 81 | } |
| 82 | |
| 83 | getModeName():string { |
| 84 | switch (this.mode) { |
| 85 | case DrawingBoardModes.CREATE: |
| 86 | return 'IN DESIGN'; |
| 87 | case DrawingBoardModes.VIEW: |
| 88 | case DrawingBoardModes.RETRY: |
| 89 | return 'VIEW ONLY'; |
| 90 | case DrawingBoardModes.EDIT: |
| 91 | case DrawingBoardModes.RETRY_EDIT: |
| 92 | return 'IN EDITING'; |
| 93 | default: |
| 94 | return 'IN DESIGN'; |
| 95 | } |
| 96 | |
| 97 | } |
| 98 | public onContextMenu($event: MouseEvent, item: any): void { |
| 99 | this._contextMenuService.show.next({ |
| 100 | contextMenu: this.contextMenu, |
| 101 | event: $event, |
| 102 | item: item, |
| 103 | }); |
| 104 | $event.preventDefault(); |
| 105 | $event.stopPropagation(); |
| 106 | } |
| 107 | |
| 108 | private setValuesFromStore() { |
| 109 | if(!_.isNil(this.store.getState().service.serviceInstance) && !_.isNil(this.store.getState().service.serviceInstance[this.serviceModelId])){ |
| 110 | const serviceInstance = this.store.getState().service.serviceInstance[this.serviceModelId]; |
| 111 | this.numServicesToDeploy = serviceInstance.bulkSize; |
| 112 | this.serviceName = serviceInstance.instanceName || '<Automatically Assigned>'; |
| 113 | this.serviceOrchStatus = serviceInstance.orchStatus || ""; |
| 114 | this.isServiceFailed = serviceInstance.isFailed; |
| 115 | this.serviceStatusMessage = serviceInstance.statusMessage; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | public editService(): void { |
| 120 | this._iframeService.addClassOpenModal(this.parentElementClassName); |
| 121 | this.dialogService.addDialog(GenericFormPopupComponent, { |
| 122 | type: PopupType.SERVICE, |
| 123 | uuidData: <any>{ |
| 124 | type : PopupType.SERVICE, |
| 125 | isMacro : this.store.getState().service.serviceHierarchy[this.serviceModelId].service.instantiationType === 'Macro', |
| 126 | serviceId: this.serviceModelId, |
| 127 | popupService: this._servicePopupService |
| 128 | }, |
| 129 | isUpdateMode: true |
| 130 | }); |
| 131 | } |
| 132 | |
| 133 | extractOwningEntityNameAccordingtoId(id:String): string { |
| 134 | let owningEntityName; |
| 135 | _.forEach(this.store.getState().service.categoryParameters.owningEntityList,(owningEntity: OwningEntity) => { |
| 136 | if (owningEntity.id === id) { |
| 137 | owningEntityName = owningEntity.name; |
| 138 | |
| 139 | }}); |
| 140 | return owningEntityName; |
| 141 | } |
| 142 | |
| 143 | extractServiceFields(): any { |
| 144 | let instanceFields : ServiceInstance; |
| 145 | instanceFields = this.store.getState().service.serviceInstance[this.serviceModelId]; |
| 146 | if (instanceFields.action === ServiceInstanceActions.Create) { |
| 147 | instanceFields.subscriberName = this.store.getState().service.subscribers.find(sub => sub.id === instanceFields.globalSubscriberId).name; |
| 148 | instanceFields.owningEntityName = this.extractOwningEntityNameAccordingtoId(instanceFields.owningEntityId); |
| 149 | } |
| 150 | return _.omit(instanceFields,'optionalGroupMembersMap'); |
| 151 | } |
| 152 | |
| 153 | |
| 154 | public deployService(): void { |
| 155 | let instanceFields = this.extractServiceFields(); |
| 156 | if (this.mode !== DrawingBoardModes.RETRY_EDIT) { |
| 157 | instanceFields.rollbackOnFailure = instanceFields.rollbackOnFailure === 'true'; |
| 158 | this.msoService.submitMsoTask(instanceFields).subscribe((result) => { |
| 159 | window.parent.postMessage("navigateToInstantiationStatus", '*'); |
| 160 | }); |
| 161 | } else { |
| 162 | this.msoService.retryBulkMsoTask(this.jobId, instanceFields).subscribe((result) => { |
| 163 | window.parent.postMessage("navigateToInstantiationStatus", '*'); |
| 164 | }); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | closePage() { |
| 169 | let messageBoxData : MessageBoxData = new MessageBoxData( |
| 170 | "Delete Instantiation", // modal title |
| 171 | "You are about to stop the instantiation process of this service. \nAll data will be lost. Are you sure you want to stop?", |
| 172 | SdcUiCommon.ModalType.warning, |
| 173 | SdcUiCommon.ModalSize.medium, |
| 174 | [ |
| 175 | {text:"Stop Instantiation", size:"large", callback: this.navigate.bind(this), closeModal:true}, |
| 176 | {text:"Cancel", size:"medium", closeModal:true} |
| 177 | ]); |
| 178 | |
| 179 | MessageBoxService.openModal.next(messageBoxData); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | |
| 184 | navigate(){ |
| 185 | window.parent.postMessage("navigateTo", "*"); |
| 186 | } |
| 187 | } |