dd9d9fb292434fdb7003855d3242248174722a49
[vid.git] / vid-webpack-master / src / app / services / aaiService / aai.service.ts
1 import {Injectable} from '@angular/core';
2 import {HttpClient, HttpHeaders} from '@angular/common/http';
3 import { Constants } from '../../shared/utils/constants';
4 import { ServiceType } from "../../shared/models/serviceType";
5 import {GetSubDetailsResponse} from "./responseInterfaces/getSubDetailsResponseInterface";
6 import {Observable} from "rxjs/Observable";
7 import * as _ from 'lodash';
8 import {CategoryParams} from "../../shared/models/categoryParams";
9 import {GetCategoryParamsResponseInterface} from "./responseInterfaces/getCategoryParamsResponseInterface";
10 import {Project} from "../../shared/models/project";
11 import {OwningEntity} from "../../shared/models/owningEntity";
12 import {GetServicesResponseInterface} from "./responseInterfaces/getServicesResponseInterface";
13 import {Subscriber} from "../../shared/models/subscriber";
14 import {GetSubscribersResponse} from "./responseInterfaces/getSubscribersResponseInterface";
15 import {AicZone} from "../../shared/models/aicZone";
16 import {GetAicZonesResponse} from "./responseInterfaces/getAicZonesResponseInterface";
17 import {LcpRegionsAndTenants} from "../../shared/models/lcpRegionsAndTenants";
18 import {LcpRegion} from "../../shared/models/lcpRegion";
19 import {Tenant} from "../../shared/models/tenant";
20 import {ProductFamily} from "../../shared/models/productFamily"
21 import {
22   updateAicZones, updateCategoryParameters, updateLcpRegionsAndTenants, updateModel, updateProductFamilies,
23   updateServiceTypes, updateSubscribers, updateUserId
24 } from '../../service.actions';
25 import {SelectOption} from '../../shared/models/selectOption';
26 import {NgRedux} from "@angular-redux/store";
27 import {AppState} from "../../store/reducers";
28 import {ResponseContentType, ResponseType} from "@angular/http";
29 import 'rxjs/add/operator/do';
30 import 'rxjs/add/observable/of';
31 import 'rxjs/add/operator/catch';
32
33 @Injectable()
34 export class AaiService {
35
36   constructor (private http: HttpClient, private store: NgRedux<AppState>) {}
37
38   public getServiceModelById(serviceModelId: string): Observable<any> {
39     if (_.has(this.store.getState().service.serviceHierarchy,serviceModelId)){
40       return Observable.of(<any> JSON.parse(JSON.stringify(this.store.getState().service.serviceHierarchy[serviceModelId])));
41     }
42     let pathQuery: string = Constants.Path.SERVICES_PATH + serviceModelId;
43     return this.http.get(pathQuery).map(res => res )
44       .do((res) => {
45         this.store.dispatch(updateModel(res));
46       });
47   }
48
49   public getUserId() : Observable<any>{
50     return this.http.get("../../getuserID",{responseType: 'text'}).do((res)=>this.store.dispatch(updateUserId(res)));
51   }
52
53
54   public getCRAccordingToNetworkFunctionId(networkCollectionFunction,cloudOwner,cloudRegionId){
55     return this.http.get('../../aai_get_instance_groups_by_cloudregion/'+cloudOwner+'/'+cloudRegionId+'/' + networkCollectionFunction)
56     .map(res=>res).do((res)=>console.log(res));
57   }
58
59   public getCategoryParameters(familyName): Observable<CategoryParams> {
60     familyName = familyName || Constants.Path.PARAMETER_STANDARDIZATION_FAMILY;
61     let pathQuery: string = Constants.Path.GET_CATEGORY_PARAMETERS +"?familyName=" + familyName+ "&r=" + Math.random();
62
63     return this.http.get<GetCategoryParamsResponseInterface>(pathQuery)
64       .map(this.categoryParametersResponseToProductAndOwningEntity)
65       .do(res => {
66         this.store.dispatch(updateCategoryParameters(res))
67       });
68   }
69
70
71
72     categoryParametersResponseToProductAndOwningEntity(res: GetCategoryParamsResponseInterface): CategoryParams  {
73     if (res && res.categoryParameters) {
74       const owningEntityList = res.categoryParameters.owningEntity.map(owningEntity => new OwningEntity(owningEntity));
75       const projectList = res.categoryParameters.project.map(project => new Project(project));
76       const lineOfBusinessList = res.categoryParameters.lineOfBusiness.map(owningEntity => new SelectOption(owningEntity));
77       const platformList = res.categoryParameters.platform.map(platform => new SelectOption(platform));
78
79       return new CategoryParams(owningEntityList, projectList, lineOfBusinessList, platformList);
80     } else {
81       return new CategoryParams();
82     }
83   }
84
85   public getProductFamilies(): Observable<ProductFamily[]> {
86     return this.getServices().map(res => res.service.map(service => new ProductFamily(service)));
87   }
88
89   public getServices(): Observable<GetServicesResponseInterface> {
90     let pathQuery: string = Constants.Path.AAI_GET_SERVICES + Constants.Path.ASSIGN + Math.random();
91
92     return this.http.get<GetServicesResponseInterface>(pathQuery);
93   }
94
95   public getSubscribers(): Observable<Subscriber[]> {
96     if (this.store.getState().service.subscribers){
97       return Observable.of(<any> JSON.parse(JSON.stringify(this.store.getState().service.subscribers)));
98     }
99
100     let pathQuery: string = Constants.Path.AAI_GET_SUBSCRIBERS + Constants.Path.ASSIGN + Math.random();
101
102     return this.http.get<GetSubscribersResponse>(pathQuery).map(res =>
103        res.customer.map( subscriber => new Subscriber(subscriber))).do((res) => {
104       this.store.dispatch(updateSubscribers(res));
105     });
106   }
107
108   public getAicZones(): Observable<AicZone[]> {
109     if (this.store.getState().service.aicZones){
110       return Observable.of(<any> JSON.parse(JSON.stringify(this.store.getState().service.aicZones)));
111     }
112
113     let pathQuery: string = Constants.Path.AAI_GET_AIC_ZONES + Constants.Path.ASSIGN + Math.random();
114
115     return this.http.get<GetAicZonesResponse>(pathQuery).map(res =>
116        res.zone.map(aicZone => new AicZone(aicZone))).do((res) => {
117       this.store.dispatch(updateAicZones(res));
118     });
119   }
120
121   public getLcpRegionsAndTenants(globalCustomerId, serviceType): Observable<LcpRegionsAndTenants> {
122     if (this.store.getState().service.lcpRegionsAndTenants.lcpRegionList.length !== 0){
123       return Observable.of(<any> JSON.parse(JSON.stringify(this.store.getState().service.lcpRegionsAndTenants)));
124     }
125     let pathQuery: string = Constants.Path.AAI_GET_TENANTS
126       + globalCustomerId + Constants.Path.FORWARD_SLASH + serviceType + Constants.Path.ASSIGN + Math.random();
127
128     console.log("AaiService:getSubscriptionServiceTypeList: globalCustomerId: "
129       + globalCustomerId);
130     if (globalCustomerId != null) {
131       return this.http.get(pathQuery)
132         .map(this.tenantResponseToLcpRegionsAndTenants).do((res) => {
133           this.store.dispatch(updateLcpRegionsAndTenants(res));
134         });
135     }
136   }
137
138   tenantResponseToLcpRegionsAndTenants(cloudRegionTenantList): LcpRegionsAndTenants {
139
140     const lcpRegionsTenantsMap = {};
141
142     const lcpRegionList = _.uniqBy(cloudRegionTenantList, 'cloudRegionID').map((cloudRegionTenant) => {
143            return new LcpRegion(cloudRegionTenant)
144          });
145
146     lcpRegionList.forEach(region => {
147       lcpRegionsTenantsMap[region.id] = _.filter(cloudRegionTenantList, {'cloudRegionID' : region.id})
148                                                         .map((cloudRegionTenant) => {
149                                                             return new Tenant(cloudRegionTenant)
150                                                         });
151       const reducer = (accumulator, currentValue) => {
152           accumulator.isPermitted = accumulator.isPermitted || currentValue.isPermitted;
153
154          return accumulator;
155       };
156       region.isPermitted = lcpRegionsTenantsMap[region.id].reduce(reducer).isPermitted;
157     });
158
159     return new LcpRegionsAndTenants(lcpRegionList, lcpRegionsTenantsMap);
160   }
161
162   public getServiceTypes(subscriberId): Observable<ServiceType[]> {
163     console.log("AaiService:getSubscriptionServiceTypeList: globalCustomerId: " + subscriberId);
164     if (_.has(this.store.getState().service.serviceTypes, subscriberId)){
165       return Observable.of(<any> JSON.parse(JSON.stringify(this.store.getState().service.serviceTypes[subscriberId])));
166     }
167
168     return this.getSubscriberDetails(subscriberId)
169       .map(this.subDetailsResponseToServiceTypes)
170       .do((res) => {this.store.dispatch(updateServiceTypes(res, subscriberId));});
171   }
172
173   public getSubscriberDetails(subscriberId): Observable<GetSubDetailsResponse> {
174     let pathQuery: string = Constants.Path.AAI_SUB_DETAILS_PATH + subscriberId + Constants.Path.ASSIGN + Math.random();
175
176     if (subscriberId != null) {
177       return this.http.get<GetSubDetailsResponse>(pathQuery);
178     }
179   }
180
181   subDetailsResponseToServiceTypes(res: GetSubDetailsResponse): ServiceType[] {
182     if (res && res['service-subscriptions']) {
183       const serviceSubscriptions = res['service-subscriptions']['service-subscription'];
184       return serviceSubscriptions.map((subscription, index) => new ServiceType(String(index), subscription))
185     } else {
186       return [];
187     }
188   }
189 }