blob: d2f76e4f647daabf2982b741cb1427184f9cd390 [file] [log] [blame]
Ittay Stern6f900cc2018-08-29 17:01:32 +03001import {ActivatedRouteSnapshot, Resolve} from "@angular/router";
2import {Injectable} from "@angular/core";
Einat Vinouzee1f79742019-08-27 16:01:01 +03003import {from, Observable} from "rxjs";
Ittay Stern6f900cc2018-08-29 17:01:32 +03004import {AaiService} from "../../services/aaiService/aai.service";
5import {forkJoin} from "rxjs/observable/forkJoin";
6import {AppState} from "../../store/reducers";
7import {NgRedux} from "@angular-redux/store";
8import {createServiceInstance} from "../../storeUtil/utils/service/service.actions";
Einat Vinouzee1f79742019-08-27 16:01:01 +03009import {ServiceInstance} from "../../models/serviceInstance";
10import * as _ from "lodash";
11import {ModelInfo} from "../../models/modelInfo";
Ittay Stern6f900cc2018-08-29 17:01:32 +030012
13@Injectable()
14export class ViewEditResolver implements Resolve<Observable<boolean>> {
15
16 constructor(private _aaiService: AaiService, private _store: NgRedux<AppState>) {
17 }
18
19 resolve(route: ActivatedRouteSnapshot): Observable<boolean> {
Einat Vinouzee1f79742019-08-27 16:01:01 +030020 const serviceModelId: string = route.queryParamMap.get("serviceModelId");
Ittay Stern6f900cc2018-08-29 17:01:32 +030021 const serviceInstanceId: string = route.queryParamMap.get("serviceInstanceId");
22 const subscriberId: string = route.queryParamMap.get("subscriberId");
23 const serviceType: string = route.queryParamMap.get("serviceType");
Einat Vinouzee1f79742019-08-27 16:01:01 +030024 let serviceModelApi = this._aaiService.getServiceModelById(serviceModelId);
25 let serviceInstanceApi = this._aaiService.retrieveAndStoreServiceInstanceTopology(serviceInstanceId, subscriberId, serviceType, serviceModelId);
26 let streams: Observable<any>[] = [serviceModelApi, serviceInstanceApi];
27 streams = streams.filter( stream => stream !== undefined);
28 return forkJoin(streams).switchMap(([serviceModel, serviceInstance]) => {
29 return from(this.retrieveLatestVersionAndSetServiceInstance(serviceInstance.modelInfo.modelInvariantId).then((response)=>{
30 this.setServiceLatestAvailableVersion(serviceInstance, response);
31 this.applyRequestsResponsesToStateAndInitServiceInstance(serviceModelId, serviceInstance, serviceModel);
32 return true;
33 }));
34
35 });
Ittay Stern6f900cc2018-08-29 17:01:32 +030036 }
37
Einat Vinouzee1f79742019-08-27 16:01:01 +030038 private retrieveLatestVersionAndSetServiceInstance(modelInvariantId: string) :Promise<ModelInfo>{
39 return this._aaiService.retrieveServiceLatestUpdateableVersion(modelInvariantId).toPromise();
40 }
41
42 applyRequestsResponsesToStateAndInitServiceInstance(serviceModelId: string,
43 serviceInstance, serviceModel) {
44 this.setIsALaCarte(serviceInstance, serviceModel.service.vidNotions.instantiationType);
45 this.setTestApi(serviceInstance);
46 this._store.dispatch(createServiceInstance(serviceInstance, serviceModelId));
47 }
48
49 setServiceLatestAvailableVersion(serviceInstance :ServiceInstance, modelInfoObject: ModelInfo) :void{
50 if(!_.isNil(modelInfoObject) && !_.isNil(modelInfoObject.modelVersion)){
51 serviceInstance.latestAvailableVersion = Number(modelInfoObject.modelVersion);
52 }
53 else {
54 serviceInstance.latestAvailableVersion = 0;
55 }
56 }
57
58 setTestApi(service: any) :void{
Ittay Stern6f900cc2018-08-29 17:01:32 +030059 if (this._store.getState().global.flags['FLAG_ADD_MSO_TESTAPI_FIELD'] && service.isALaCarte) {
60 service.testApi = sessionStorage.getItem("msoRequestParametersTestApiValue");
61 }
62 };
Einat Vinouzee1f79742019-08-27 16:01:01 +030063
64 setIsALaCarte(service: any, instantiationType) :void{
Ittay Sternf7926712019-07-07 19:23:03 +030065 service.isALaCarte = instantiationType === 'ALaCarte';
Ittay Stern6f900cc2018-08-29 17:01:32 +030066 };
67
68}