Extend Modern UI for pnf usecase
[vid.git] / vid-webpack-master / src / app / shared / services / aaiService / aai.service.ts
1 import {NgRedux} from "@angular-redux/store";
2 import {HttpClient} from '@angular/common/http';
3 import {Injectable} from '@angular/core';
4 import * as _ from 'lodash';
5 import 'rxjs/add/operator/catch';
6 import 'rxjs/add/operator/do';
7 import {Observable, of} from "rxjs";
8
9 import {AicZone} from "../../models/aicZone";
10 import {CategoryParams} from "../../models/categoryParams";
11 import {LcpRegion} from "../../models/lcpRegion";
12 import {LcpRegionsAndTenants} from "../../models/lcpRegionsAndTenants";
13 import {OwningEntity} from "../../models/owningEntity";
14 import {ProductFamily} from "../../models/productFamily";
15 import {Project} from "../../models/project";
16 import {SelectOption} from '../../models/selectOption';
17 import {ServiceType} from "../../models/serviceType";
18 import {Subscriber} from "../../models/subscriber";
19 import {Tenant} from "../../models/tenant";
20 import {Constants} from '../../utils/constants';
21 import {AppState} from "../../store/reducers";
22 import {GetAicZonesResponse} from "./responseInterfaces/getAicZonesResponseInterface";
23 import {GetCategoryParamsResponseInterface} from "./responseInterfaces/getCategoryParamsResponseInterface";
24 import {GetServicesResponseInterface} from "./responseInterfaces/getServicesResponseInterface";
25 import {GetSubDetailsResponse} from "./responseInterfaces/getSubDetailsResponseInterface";
26 import {GetSubscribersResponse} from "./responseInterfaces/getSubscribersResponseInterface";
27 import {Root} from "./model/crawledAaiService";
28 import {VnfInstance} from "../../models/vnfInstance";
29 import {VfModuleInstance} from "../../models/vfModuleInstance";
30 import {ServiceInstance} from "../../models/serviceInstance";
31 import {VfModuleMap} from "../../models/vfModulesMap";
32 import {
33   updateAicZones,
34   updateCategoryParameters,
35   updateLcpRegionsAndTenants,
36   updateServiceTypes,
37   updateSubscribers,
38   updateUserId
39 } from "../../storeUtil/utils/general/general.actions";
40 import {createServiceInstance, updateModel,} from "../../storeUtil/utils/service/service.actions";
41 import {FeatureFlagsService, Features} from "../featureFlag/feature-flags.service";
42 import {VnfMember} from "../../models/VnfMember";
43 import {setOptionalMembersVnfGroupInstance} from "../../storeUtil/utils/vnfGroup/vnfGroup.actions";
44 import {NetworkModalRow} from "../../../drawingBoard/service-planning/objectsToTree/models/vrf/vrfModal/networkStep/network.step.model";
45 import {VPNModalRow} from "../../../drawingBoard/service-planning/objectsToTree/models/vrf/vrfModal/vpnStep/vpn.step.model";
46 import {ModelInfo} from "../../models/modelInfo";
47
48 @Injectable()
49 export class AaiService {
50   constructor(private http: HttpClient, private store: NgRedux<AppState>, private featureFlagsService:FeatureFlagsService) {
51
52   }
53
54   sdncPreload(): Observable<boolean> {
55     let pathQuery: string = Constants.Path.PRE_LOAD;
56     return this.http.post<boolean>(pathQuery, {})
57   }
58
59   getServiceModelById = (serviceModelId: string): Observable<any> => {
60     if (_.has(this.store.getState().service.serviceHierarchy, serviceModelId)) {
61       this.initializePnfModelCustomizationName(this.store.getState().service.serviceHierarchy[serviceModelId]);
62       return of(<any> JSON.parse(JSON.stringify(this.store.getState().service.serviceHierarchy[serviceModelId])));
63     }
64     let pathQuery: string = Constants.Path.SERVICES_PATH + serviceModelId;
65     return this.http.get(pathQuery).map(res => res)
66       .do((res) => {
67         this.store.dispatch(updateModel(res));
68       });
69   };
70
71   retrieveServiceLatestUpdateableVersion = (modelInvariantId: string): Observable<ModelInfo> => {
72     let pathQuery: string = Constants.Path.SERVICE_LATEST_VERSION + modelInvariantId;
73     return this.http.get<ModelInfo>(pathQuery)
74   };
75
76   getUserId = (): Observable<any> => {
77     return this.http.get("../../getuserID", {responseType: 'text'}).do((res) => this.store.dispatch(updateUserId(res)));
78   };
79
80
81   resolve = (root: Root, serviceInstance: ServiceInstance) => {
82     if (root.type === 'service-instance') {
83       serviceInstance.instanceName = root.name;
84       serviceInstance.orchStatus = root.orchestrationStatus;
85       serviceInstance.modelInavariantId = root.modelInvariantId;
86       for (let i = 0; i < root.children.length; i++) {
87         let child = root.children[i];
88         if (child.type === 'generic-vnf') {
89           let vnf = new VnfInstance();
90           vnf.originalName = child.name;
91           vnf.orchStatus = child.orchestrationStatus
92           if (child.children.length > 0) {
93             let vfModuleMap = new VfModuleMap();
94             for (let j = 0; j < child.children.length; j++) {
95               let child = root.children[i];
96               if (child.type === 'vf-module') {
97                 let vfModule = new VfModuleInstance();
98                 vfModule.instanceName = child.name;
99                 vfModule.orchStatus = child.orchestrationStatus;
100                 vfModuleMap.vfModules[child.name] = vfModule;
101               }
102             }
103             vnf.vfModules = {"a": vfModuleMap};
104           }
105           serviceInstance.vnfs[child.name] = vnf;
106
107         }
108       }
109
110     }
111   };
112
113
114   getCRAccordingToNetworkFunctionId = (networkCollectionFunction, cloudOwner, cloudRegionId) => {
115     return this.http.get('../../aai_get_instance_groups_by_cloudregion/' + cloudOwner + '/' + cloudRegionId + '/' + networkCollectionFunction)
116       .map(res => res).do((res) => console.log(res));
117   };
118
119   getCategoryParameters = (familyName): Observable<CategoryParams> => {
120     familyName = familyName || Constants.Path.PARAMETER_STANDARDIZATION_FAMILY;
121     let pathQuery: string = Constants.Path.GET_CATEGORY_PARAMETERS + "?familyName=" + familyName + "&r=" + Math.random();
122
123     return this.http.get<GetCategoryParamsResponseInterface>(pathQuery)
124       .map(this.categoryParametersResponseToProductAndOwningEntity)
125       .do(res => {
126         this.store.dispatch(updateCategoryParameters(res))
127       });
128   };
129
130
131   categoryParametersResponseToProductAndOwningEntity = (res: GetCategoryParamsResponseInterface): CategoryParams => {
132     if (res && res.categoryParameters) {
133       const owningEntityList = res.categoryParameters.owningEntity.map(owningEntity => new OwningEntity(owningEntity));
134       const projectList = res.categoryParameters.project.map(project => new Project(project));
135       const lineOfBusinessList = res.categoryParameters.lineOfBusiness.map(owningEntity => new SelectOption(owningEntity));
136       const platformList = res.categoryParameters.platform.map(platform => new SelectOption(platform));
137
138       return new CategoryParams(owningEntityList, projectList, lineOfBusinessList, platformList);
139     } else {
140       return new CategoryParams();
141     }
142   };
143
144   getProductFamilies = (): Observable<ProductFamily[]> => {
145
146     let pathQuery: string = Constants.Path.AAI_GET_SERVICES + Constants.Path.ASSIGN + Math.random();
147
148     return this.http.get<GetServicesResponseInterface>(pathQuery).map(res => res.service.map(service => new ProductFamily(service)));
149   };
150
151   getServices = (): Observable<GetServicesResponseInterface> => {
152     let pathQuery: string = Constants.Path.AAI_GET_SERVICES + Constants.Path.ASSIGN + Math.random();
153
154     return this.http.get<GetServicesResponseInterface>(pathQuery);
155   };
156
157   getSubscribers = (): Observable<Subscriber[]> => {
158
159     if (this.store.getState().service.subscribers) {
160       return of(<any> JSON.parse(JSON.stringify(this.store.getState().service.subscribers)));
161     }
162
163     let pathQuery: string = Constants.Path.AAI_GET_SUBSCRIBERS + Constants.Path.ASSIGN + Math.random();
164
165     return this.http.get<GetSubscribersResponse>(pathQuery).map(res =>
166       res.customer.map(subscriber => new Subscriber(subscriber))).do((res) => {
167       this.store.dispatch(updateSubscribers(res));
168     });
169   };
170
171   getAicZones = (): Observable<AicZone[]> => {
172     if (this.store.getState().service.aicZones) {
173       return of(<any> JSON.parse(JSON.stringify(this.store.getState().service.aicZones)));
174     }
175
176     let pathQuery: string = Constants.Path.AAI_GET_AIC_ZONES + Constants.Path.ASSIGN + Math.random();
177
178     return this.http.get<GetAicZonesResponse>(pathQuery).map(res =>
179       res.zone.map(aicZone => new AicZone(aicZone))).do((res) => {
180       this.store.dispatch(updateAicZones(res));
181     });
182   };
183
184   getLcpRegionsAndTenants = (globalCustomerId, serviceType): Observable<LcpRegionsAndTenants> => {
185
186     let pathQuery: string = Constants.Path.AAI_GET_TENANTS
187       + globalCustomerId + Constants.Path.FORWARD_SLASH + serviceType + Constants.Path.ASSIGN + Math.random();
188
189     console.log("AaiService:getSubscriptionServiceTypeList: globalCustomerId: "
190       + globalCustomerId);
191     if (globalCustomerId != null) {
192       return this.http.get(pathQuery)
193         .map(this.tenantResponseToLcpRegionsAndTenants).do((res) => {
194           this.store.dispatch(updateLcpRegionsAndTenants(res));
195         });
196     }
197   };
198
199   tenantResponseToLcpRegionsAndTenants = (cloudRegionTenantList): LcpRegionsAndTenants => {
200
201     const lcpRegionsTenantsMap = {};
202
203     const lcpRegionList = _.uniqBy(cloudRegionTenantList, 'cloudRegionID').map((cloudRegionTenant) => {
204       const cloudOwner:string = cloudRegionTenant["cloudOwner"];
205       const cloudRegionId:string = cloudRegionTenant["cloudRegionID"];
206       const name:string = this.extractLcpRegionName(cloudRegionId, cloudOwner);
207       const isPermitted:boolean = cloudRegionTenant["is-permitted"];
208       return new LcpRegion(cloudRegionId, name, isPermitted, cloudOwner);
209     });
210
211     lcpRegionList.forEach(region => {
212       lcpRegionsTenantsMap[region.id] = _.filter(cloudRegionTenantList, {'cloudRegionID': region.id})
213         .map((cloudRegionTenant) => {
214           return new Tenant(cloudRegionTenant)
215         });
216       const reducer = (accumulator, currentValue) => {
217         accumulator.isPermitted = accumulator.isPermitted || currentValue.isPermitted;
218
219         return accumulator;
220       };
221       region.isPermitted = lcpRegionsTenantsMap[region.id].reduce(reducer).isPermitted;
222     });
223
224     return new LcpRegionsAndTenants(lcpRegionList, lcpRegionsTenantsMap);
225   };
226
227   public extractLcpRegionName(cloudRegionId: string, cloudOwner: string):string {
228     return this.featureFlagsService.getFlagState(Features.FLAG_1810_CR_ADD_CLOUD_OWNER_TO_MSO_REQUEST) ?
229       cloudRegionId+AaiService.formatCloudOwnerTrailer(cloudOwner) : cloudRegionId;
230   };
231
232   public static formatCloudOwnerTrailer(cloudOwner: string):string {
233     return " ("+ cloudOwner.trim().toLowerCase().replace(/^[^-]*-/, "").toUpperCase() + ")";
234   }
235
236   getServiceTypes = (subscriberId): Observable<ServiceType[]> => {
237
238     console.log("AaiService:getSubscriptionServiceTypeList: globalCustomerId: " + subscriberId);
239     if (_.has(this.store.getState().service.serviceTypes, subscriberId)) {
240       return of(<ServiceType[]> JSON.parse(JSON.stringify(this.store.getState().service.serviceTypes[subscriberId])));
241     }
242
243     return this.getSubscriberDetails(subscriberId)
244       .map(this.subDetailsResponseToServiceTypes)
245       .do((res) => {
246         this.store.dispatch(updateServiceTypes(res, subscriberId));
247       });
248   };
249
250   getSubscriberDetails = (subscriberId): Observable<GetSubDetailsResponse> => {
251     let pathQuery: string = Constants.Path.AAI_SUB_DETAILS_PATH + subscriberId + Constants.Path.ASSIGN + Math.random() + Constants.Path.AAI_OMIT_SERVICE_INSTANCES + true;
252
253     if (subscriberId != null) {
254       return this.http.get<GetSubDetailsResponse>(pathQuery);
255     }
256   };
257
258   subDetailsResponseToServiceTypes = (res: GetSubDetailsResponse): ServiceType[] => {
259     if (res && res['service-subscriptions']) {
260       const serviceSubscriptions = res['service-subscriptions']['service-subscription'];
261       return serviceSubscriptions.map((subscription, index) => new ServiceType(String(index), subscription))
262     } else {
263       return [];
264     }
265   };
266
267
268   public retrieveServiceInstanceTopology(serviceInstanceId : string, subscriberId: string, serviceType: string):Observable<ServiceInstance> {
269     let pathQuery: string = `${Constants.Path.AAI_GET_SERVICE_INSTANCE_TOPOLOGY_PATH}${subscriberId}/${serviceType}/${serviceInstanceId}`;
270     return this.http.get<ServiceInstance>(pathQuery);
271   }
272
273   public retrieveActiveNetwork(cloudRegion : string, tenantId: string) : Observable<NetworkModalRow[]>{
274     let pathQuery: string = `${Constants.Path.AAI_GET_ACTIVE_NETWORKS_PATH}?cloudRegion=${cloudRegion}&tenantId=${tenantId}`;
275     return this.http.get<NetworkModalRow[]>(pathQuery);
276   }
277
278   public retrieveActiveVPNs() : Observable<VPNModalRow[]>{
279     let pathQuery: string = `${Constants.Path.AAI_GET_VPNS_PATH}`;
280     return this.http.get<VPNModalRow[]>(pathQuery);
281   }
282
283   public retrieveAndStoreServiceInstanceTopology(serviceInstanceId: string, subscriberId: string, serviceType: string, serviceModeId: string):Observable<ServiceInstance> {
284     return this.retrieveServiceInstanceTopology(serviceInstanceId, subscriberId, serviceType).do((service:ServiceInstance) => {
285       this.store.dispatch(createServiceInstance(service, serviceModeId));
286     });
287   };
288
289
290   public retrieveServiceInstanceRetryTopology(jobId : string) :Observable<ServiceInstance> {
291     let pathQuery: string = `${Constants.Path.SERVICES_RETRY_TOPOLOGY}/${jobId}`;
292     return this.http.get<ServiceInstance>(pathQuery);
293
294   }
295
296   public retrieveAndStoreServiceInstanceRetryTopology(jobId: string, serviceModeId : string):Observable<ServiceInstance> {
297     return this.retrieveServiceInstanceRetryTopology(jobId).do((service:ServiceInstance) => {
298       this.store.dispatch(createServiceInstance(service, serviceModeId));
299     });
300   };
301
302   public getOptionalGroupMembers(serviceModelId: string, subscriberId: string, serviceType: string, serviceInvariantId: string, groupType: string, groupRole: string): Observable<VnfMember[]> {
303     let pathQuery: string = `${Constants.Path.AAI_GET_SERVICE_GROUP_MEMBERS_PATH}${subscriberId}/${serviceType}/${serviceInvariantId}/${groupType}/${groupRole}`;
304     if(_.has(this.store.getState().service.serviceInstance[serviceModelId].optionalGroupMembersMap,pathQuery)){
305       return of(<VnfMember[]> JSON.parse(JSON.stringify(this.store.getState().service.serviceInstance[serviceModelId].optionalGroupMembersMap[pathQuery])));
306     }
307     return this.http.get<VnfMember[]>(pathQuery)
308       .do((res) => {
309         this.store.dispatch(setOptionalMembersVnfGroupInstance(serviceModelId, pathQuery, res))
310       });
311   }
312
313   //TODO: make other places use this function
314   extractSubscriberNameBySubscriberId(subscriberId: string) {
315     let result: string = null;
316     let filteredArray: any = _.filter(this.store.getState().service.subscribers, function (o: Subscriber) {
317       return o.id === subscriberId
318     });
319     if (filteredArray.length > 0) {
320       result = filteredArray[0].name;
321     }
322     return result;
323   }
324
325   initializePnfModelCustomizationName(serviceHierarchy) : void {
326     let pnfs = serviceHierarchy.pnfs;
327     for (let pnf in pnfs) {
328       if (!pnfs[pnf].modelCustomizationName){
329         pnfs[pnf].modelCustomizationName = pnf;
330       }
331     }
332   }
333
334   loadMockMembers(): any {
335     return [
336       {
337         "action":"None",
338         "instanceName":"VNF1_INSTANCE_NAME",
339         "instanceId":"VNF1_INSTANCE_ID",
340         "orchStatus":null,
341         "productFamilyId":null,
342         "lcpCloudRegionId":"hvf23b",
343         "tenantId":"3e9a20a3e89e45f884e09df0cc2d2d2a",
344         "tenantName":"APPC-24595-T-IST-02C",
345         "modelInfo":{
346           "modelInvariantId":"vnf-instance-model-invariant-id",
347           "modelVersionId":"7a6ee536-f052-46fa-aa7e-2fca9d674c44",
348           "modelVersion":"2.0",
349           "modelName":"vf_vEPDG",
350           "modelType":"vnf"
351         },
352         "instanceType":"VNF1_INSTANCE_TYPE",
353         "provStatus":null,
354         "inMaint":false,
355         "uuid":"7a6ee536-f052-46fa-aa7e-2fca9d674c44",
356         "originalName":null,
357         "legacyRegion":null,
358         "lineOfBusiness":null,
359         "platformName":null,
360         "trackById":"7a6ee536-f052-46fa-aa7e-2fca9d674c44:002",
361         "serviceInstanceId":"service-instance-id1",
362         "serviceInstanceName":"service-instance-name"
363       },
364       {
365         "action":"None",
366         "instanceName":"VNF2_INSTANCE_NAME",
367         "instanceId":"VNF2_INSTANCE_ID",
368         "orchStatus":null,
369         "productFamilyId":null,
370         "lcpCloudRegionId":"hvf23b",
371         "tenantId":"3e9a20a3e89e45f884e09df0cc2d2d2a",
372         "tenantName":"APPC-24595-T-IST-02C",
373         "modelInfo":{
374           "modelInvariantId":"vnf-instance-model-invariant-id",
375           "modelVersionId":"eb5f56bf-5855-4e61-bd00-3e19a953bf02",
376           "modelVersion":"1.0",
377           "modelName":"vf_vEPDG",
378           "modelType":"vnf"
379         },
380         "instanceType":"VNF2_INSTANCE_TYPE",
381         "provStatus":null,
382         "inMaint":true,
383         "uuid":"eb5f56bf-5855-4e61-bd00-3e19a953bf02",
384         "originalName":null,
385         "legacyRegion":null,
386         "lineOfBusiness":null,
387         "platformName":null,
388         "trackById":"eb5f56bf-5855-4e61-bd00-3e19a953bf02:003",
389         "serviceInstanceId":"service-instance-id2",
390         "serviceInstanceName":"service-instance-name"
391       }
392     ]
393
394   }
395
396
397 }