Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / services / component-services / service.service.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 import { Injectable, Inject } from '@angular/core';
22 import { Observable } from 'rxjs/Observable';
23 import 'rxjs/add/operator/map';
24 import 'rxjs/add/operator/toPromise';
25 import {Service} from "app/models";
26
27 import {SdcConfigToken, ISdcConfig} from "../../config/sdc-config.config";
28 import {ForwardingPath} from "app/models/forwarding-path";
29 import {ComponentMetadata} from "app/models/component-metadata";
30 import {ComponentType} from "app/utils";
31 import {Component} from "app/models/components/component";
32 import {ComponentGenericResponse} from "app/ng2/services/responses/component-generic-response";
33 import {COMPONENT_FIELDS, SERVICE_FIELDS} from "app/utils/constants";
34 import {ComponentServiceNg2} from "./component.service";
35 import {ServicePathMapItem} from "app/models/graph/nodes-and-links-map";
36 import { HttpClient, HttpParams } from '@angular/common/http';
37 import { OperationModel } from '../../../models/operation';
38 import { ConsumptionInput } from '../../components/logic/service-consumption/service-consumption.component';
39
40 @Injectable()
41 export class ServiceServiceNg2 extends ComponentServiceNg2 {
42
43     protected baseUrl = "";
44
45     constructor(protected http: HttpClient, @Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
46         super(http, sdcConfig);
47         this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
48     }
49
50     validateConformanceLevel(service: Service): Observable<boolean> {
51
52         return this.http.get<boolean>(this.baseUrl + service.getTypeUrl() + service.uuid + '/conformanceLevelValidation');
53     }
54
55     getNodesAndLinksMap(serviceId: string):Observable<Array<ServicePathMapItem>> {
56         return this.http.get<Array<ServicePathMapItem>>(this.baseUrl + 'services/' + serviceId + '/linksMap');
57     }
58
59     createOrUpdateServicePath(serviceId: string, inputsToCreate: ForwardingPath):Observable<ForwardingPath> {
60         if (inputsToCreate.uniqueId) {
61             return this.updateServicePath(serviceId, inputsToCreate);
62         } else {
63             return this.createServicePath(serviceId, inputsToCreate);
64         }
65     }
66
67     createServicePath(serviceId: string, inputsToCreate: ForwardingPath):Observable<ForwardingPath> {
68         let input = new ServicePathRequestData(inputsToCreate);
69         return this.http.post<ForwardingPath>(this.baseUrl  + 'services/' + serviceId +   '/paths', input).map((res:any) => {
70             return this.parseServicePathResponse(res);
71         });
72     }
73
74     deleteServicePath(serviceId: string, id: string):Observable<any> {
75         return this.http.delete<any>(this.baseUrl  + 'services/' + serviceId + '/paths/' + id);
76     }
77
78     updateServicePath(serviceId: string, inputsToUpdate:ForwardingPath):Observable<ForwardingPath> {
79         let input = new ServicePathRequestData(inputsToUpdate);
80
81         return this.http.put<{[key:string]:ForwardingPath}>(this.baseUrl  + 'services/' + serviceId + '/paths', input)
82             .map((res) => {
83                 return this.parseServicePathResponse(res);
84             });
85     }
86
87     getServiceConsumptionData(service: Service):Observable<ComponentGenericResponse> {
88         return this.getComponentDataByFieldsName(service.componentType, service.uniqueId, [
89             COMPONENT_FIELDS.COMPONENT_INSTANCES_INTERFACES,
90             COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES,
91             COMPONENT_FIELDS.COMPONENT_INSTANCES_INPUTS,
92             COMPONENT_FIELDS.COMPONENT_INPUTS,
93             COMPONENT_FIELDS.COMPONENT_INSTANCES,
94             COMPONENT_FIELDS.COMPONENT_CAPABILITIES
95         ]);
96     }
97
98     getServiceConsumptionInputs(service: Service, serviceInstanceId: String, interfaceId: string, operation: OperationModel): Observable<any> {
99         return this.http.get<any>(this.baseUrl + service.getTypeUrl() + service.uniqueId + '/consumption/' + serviceInstanceId + '/interfaces/' + interfaceId +
100             '/operations/' + operation.uniqueId + '/inputs');
101     }
102
103     createOrUpdateServiceConsumptionInputs(service: Service, serviceInstanceId: String, consumptionInputsList: Array<{[id: string]: Array<ConsumptionInput>}>): Observable<any> {
104         return this.http.post(this.baseUrl + service.getTypeUrl() + service.uniqueId + '/consumption/' + serviceInstanceId, consumptionInputsList);
105     }
106
107     checkComponentInstanceVersionChange(componentType:string, componentId:string, instanceId:string, newInstanceId:string):Observable<Array<string>> {
108         let queries = {componentInstanceId: instanceId, newComponentInstanceId: newInstanceId};
109
110         let params:HttpParams = new HttpParams();
111         _.map(_.keys(queries), (key:string):void => {
112             params = params.append(key, queries[key]);
113         });
114
115         let url = this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/paths-to-delete';
116         return this.http.get<any>(url, {params: params}).map((res) => {
117             return res.forwardingPathToDelete;
118         });
119     }
120
121     getComponentCompositionData(component:Component):Observable<ComponentGenericResponse> {
122         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, SERVICE_FIELDS.FORWARDING_PATHS, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_POLICIES, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_GROUPS]);
123     }
124
125     private parseServicePathResponse(res: { [key:string]:ForwardingPath }):ForwardingPath {
126         let resJSON = res;
127         let pathId = Object.keys(resJSON.forwardingPaths)[0];
128         let forwardingPath = resJSON.forwardingPaths[pathId];
129         let path:ForwardingPath = new ForwardingPath();
130         path.deserialize(forwardingPath);
131         path.uniqueId = pathId;
132         return path;
133     }
134 }
135
136 class ServicePathRequestData {
137     forwardingPaths: { [key:string]:ForwardingPath } = {};
138     componentMetadataDefinition: ComponentMetadata;
139     toscaType: string = "topology_template";
140
141     constructor(fp? : ForwardingPath) {
142         this.componentMetadataDefinition = new ComponentMetadata();
143         this.componentMetadataDefinition.ecompGeneratedNaming = true;
144         this.componentMetadataDefinition.componentType = ComponentType.SERVICE;
145         if (fp) {
146             let id = fp.uniqueId ? fp.uniqueId : "NEW";
147             this.forwardingPaths[fp.uniqueId] = fp;
148         }
149     }
150 }
151