Extend Modern UI for pnf usecase
[vid.git] / vid-webpack-master / src / app / shared / components / genericFormPopup / genericFormServices / pnf / pnf.popup.service.ts
1 import {Injectable} from '@angular/core';
2 import {GenericPopupInterface} from "../generic-popup.interface";
3 import {ITreeNode} from "angular-tree-component/dist/defs/api";
4 import {FormPopupDetails, PopupType} from "../../../../models/formControlModels/formPopupDetails.model";
5 import {FormGroup} from "@angular/forms";
6 import {ModelInformationItem} from "../../../model-information/model-information.component";
7 import {ServiceModel} from "../../../../models/serviceModel";
8 import {Subject} from "rxjs/Subject";
9 import {ControlGeneratorUtil} from "../../../genericForm/formControlsServices/control.generator.util.service";
10 import {PnfControlGenerator} from "../../../genericForm/formControlsServices/pnfGenerator/pnf.control.generator";
11 import {IframeService} from "../../../../utils/iframe.service";
12 import {DefaultDataGeneratorService} from "../../../../services/defaultDataServiceGenerator/default.data.generator.service";
13 import {AaiService} from "../../../../services/aaiService/aai.service";
14 import {BasicPopupService} from "../basic.popup.service";
15 import {NgRedux} from "@angular-redux/store";
16 import {AppState} from "../../../../store/reducers";
17 import {Subscriber} from "../../../../models/subscriber";
18 import {Constants} from "../../../../utils/constants";
19 import {PnfInstance} from "../../../../models/pnfInstance";
20 import {ModelInfo} from "../../../../models/modelInfo";
21 import {changeInstanceCounter} from "../../../../storeUtil/utils/general/general.actions";
22 import {createPNFInstance, updatePNFInstance} from "../../../../storeUtil/utils/pnf/pnf.actions";
23 import * as _ from 'lodash';
24
25 @Injectable()
26 export class PnfPopupService implements GenericPopupInterface {
27   dynamicInputs: any;
28   instance: any;
29   model: any;
30   serviceModel: ServiceModel;
31   modelInformations: ModelInformationItem[] = [];
32   uuidData: Object;
33   closeDialogEvent: Subject<any> = new Subject<any>();
34   isUpdateMode: boolean;
35
36   constructor(
37     private _basicControlGenerator: ControlGeneratorUtil,
38     private _pnfControlGenerator: PnfControlGenerator,
39     private _iframeService: IframeService,
40     private _defaultDataGeneratorService: DefaultDataGeneratorService,
41     private _aaiService: AaiService,
42     private _basicPopupService: BasicPopupService,
43     private _store: NgRedux<AppState>) {
44   }
45
46   getGenericFormPopupDetails(serviceId: string, modelName: string, pnfStoreKey: string, node: ITreeNode, uuidData: Object, isUpdateMode: boolean): FormPopupDetails {
47     this.uuidData = uuidData;
48     this.isUpdateMode = isUpdateMode;
49     this.instance = this.getInstance(serviceId, modelName, pnfStoreKey);
50     this.getModelInformation(serviceId, modelName);
51
52     return new FormPopupDetails(this,
53       PopupType.PNF_MACRO,
54       uuidData,
55       this.getTitle(isUpdateMode),
56       this.getSubLeftTitle(),
57       this.getSubRightTitle(),
58       this.getControls(serviceId, modelName, pnfStoreKey),
59       this._basicPopupService.getDynamicInputs(serviceId, modelName, pnfStoreKey, 'pnfs'),
60       this.modelInformations,
61       (that, form: FormGroup) => {
62         that.onSubmit(that, form);
63       },
64       (that: any, form: FormGroup) => {
65         that.onCancel(that, form);
66       }
67     )
68   }
69
70   getControls(serviceId: string, modelName: string, pnfStoreKey: string) {
71     if (this._store.getState().service.serviceHierarchy[serviceId].service.vidNotions.instantiationType === 'Macro') {
72       return this._pnfControlGenerator.getMacroFormControls(serviceId, pnfStoreKey, modelName);
73     } else {
74       return this._pnfControlGenerator.getAlaCarteFormControls(serviceId, pnfStoreKey, modelName);
75     }
76   }
77
78   getInstance(serviceId: string, modelName: string, pnfStoreKey: string): any {
79     if (_.isNil(pnfStoreKey)) {
80       return new PnfInstance();
81     }
82     return this._store.getState().service.serviceInstance[serviceId].pnfs[pnfStoreKey];
83   }
84
85   getModelInformation(serviceId: string, modelName: string): void {
86     this._aaiService.getServiceModelById(serviceId).subscribe((result: any) => {
87       this.serviceModel = new ServiceModel(result);
88       this.model = this._basicPopupService.getModelFromResponse(result, 'pnfs', modelName);
89       const serviceInstance = this._store.getState().service.serviceInstance[serviceId];
90       this.modelInformations = [
91         new ModelInformationItem("Subscriber Name", "subscriberName", [this.extractSubscriberNameBySubscriberId(serviceInstance.globalSubscriberId, this._store)], "", true),
92         new ModelInformationItem("Service Name", "serviceModelName", [this.serviceModel.name], "", true),
93         new ModelInformationItem("Service Instance Name", "serviceName", [serviceInstance.instanceName], "", false),
94         new ModelInformationItem("Model Name", "modelName", [this.model.name], "", true),
95         new ModelInformationItem("Model version", "modelVersion", [this.model.version], "", true),
96         new ModelInformationItem("Description", "description", [this.model.description]),
97         new ModelInformationItem("Category", "category", [this.model.category]),
98         new ModelInformationItem("Sub Category", "subCategory", [this.model.subCategory]),
99         new ModelInformationItem("UUID", "uuid", [this.model.uuid], Constants.ServicePopup.TOOLTIP_UUID, true),
100         new ModelInformationItem("Invariant UUID", "invariantUuid", [this.model.invariantUuid], Constants.ServicePopup.TOOLTIP_INVARIANT_UUID, true),
101         new ModelInformationItem("Service type", "serviceType", [this.serviceModel.serviceType]),
102         new ModelInformationItem("Service role", "serviceRole", [this.serviceModel.serviceRole]),
103         new ModelInformationItem("Minimum to instantiate", "min", [!_.isNil(this.model.min) ? this.model.min.toString() : '0'], "", false),
104         this._basicPopupService.createMaximumToInstantiateModelInformationItem(this.model)
105       ];
106     })
107   }
108
109   getSubLeftTitle(): string {
110     return "PNF MODEL: " + this._store.getState().service.serviceHierarchy[this.uuidData['serviceId']].pnfs[this.uuidData['modelName']].name;
111   }
112
113   getSubRightTitle(): string {
114     return "PNF Instance Details";
115   }
116
117   storePNF = (that, formValues: any): void => {
118     formValues.modelInfo = new ModelInfo(that.model);
119     formValues.uuid = formValues.modelInfo.uuid;
120     formValues.isMissingData = false;
121     if (!that.isUpdateMode) {
122       that._store.dispatch(changeInstanceCounter(formValues.modelInfo.modelUniqueId, that.uuidData.serviceId, 1, <any>{data: {type: 'PNF'}}));
123       this._store.dispatch(createPNFInstance(formValues, that.uuidData['modelName'], that.uuidData['serviceId'], that.uuidData['modelName']));
124     } else {
125       that._store.dispatch(updatePNFInstance(formValues, that.uuidData.modelName, that.uuidData.serviceId, that.uuidData.pnfStoreKey))
126     }
127   };
128
129   getTitle(isUpdateMode: boolean): string {
130     return isUpdateMode ? "Edit PNF instance" : "Set a new PNF";
131   }
132
133   onCancel(that, form): void {
134     form.reset();
135     that._iframeService.removeClassCloseModal('content');
136     this.closeDialogEvent.next(that);
137   }
138
139   onSubmit(that, form: FormGroup, ...args): void {
140     form.value['instanceParams'] = form.value['instanceParams'] && [form.value['instanceParams']];
141     that.storePNF(that, form.value);
142     window.parent.postMessage({
143       eventId: 'submitIframe',
144       data: {
145         serviceModelId: that.uuidData.serviceId
146       }
147     }, "*");
148     that.onCancel(that, form);
149   }
150
151   extractSubscriberNameBySubscriberId(subscriberId: string, store: NgRedux<AppState>) {
152     let result: string = null;
153     let filteredArray: any = _.filter(store.getState().service.subscribers, function (o: Subscriber) {
154       return o.id === subscriberId
155     });
156     if (filteredArray.length > 0) {
157       result = filteredArray[0].name;
158     }
159     return result;
160   }
161
162 }