2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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";
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';
41 export class ServiceServiceNg2 extends ComponentServiceNg2 {
43 protected baseUrl = "";
45 constructor(protected http: HttpClient, @Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
46 super(http, sdcConfig);
47 this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
50 validateConformanceLevel(service: Service): Observable<boolean> {
52 return this.http.get<boolean>(this.baseUrl + service.getTypeUrl() + service.uuid + '/conformanceLevelValidation');
55 getNodesAndLinksMap(serviceId: string):Observable<Array<ServicePathMapItem>> {
56 return this.http.get<Array<ServicePathMapItem>>(this.baseUrl + 'services/' + serviceId + '/linksMap');
59 createOrUpdateServicePath(serviceId: string, inputsToCreate: ForwardingPath):Observable<ForwardingPath> {
60 if (inputsToCreate.uniqueId) {
61 return this.updateServicePath(serviceId, inputsToCreate);
63 return this.createServicePath(serviceId, inputsToCreate);
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);
74 deleteServicePath(serviceId: string, id: string):Observable<any> {
75 return this.http.delete<any>(this.baseUrl + 'services/' + serviceId + '/paths/' + id);
78 updateServicePath(serviceId: string, inputsToUpdate:ForwardingPath):Observable<ForwardingPath> {
79 let input = new ServicePathRequestData(inputsToUpdate);
81 return this.http.put<{[key:string]:ForwardingPath}>(this.baseUrl + 'services/' + serviceId + '/paths', input)
83 return this.parseServicePathResponse(res);
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_INSTANCES_OUTPUTS,
93 COMPONENT_FIELDS.COMPONENT_INPUTS,
94 COMPONENT_FIELDS.COMPONENT_OUTPUTS,
95 COMPONENT_FIELDS.COMPONENT_INSTANCES,
96 COMPONENT_FIELDS.COMPONENT_CAPABILITIES
100 getServiceConsumptionInputs(service: Service, serviceInstanceId: String, interfaceId: string, operation: OperationModel): Observable<any> {
101 return this.http.get<any>(this.baseUrl + service.getTypeUrl() + service.uniqueId + '/consumption/' + serviceInstanceId + '/interfaces/' + interfaceId +
102 '/operations/' + operation.uniqueId + '/inputs');
105 createOrUpdateServiceConsumptionInputs(service: Service, serviceInstanceId: String, consumptionInputsList: Array<{[id: string]: Array<ConsumptionInput>}>): Observable<any> {
106 return this.http.post(this.baseUrl + service.getTypeUrl() + service.uniqueId + '/consumption/' + serviceInstanceId, consumptionInputsList);
109 checkComponentInstanceVersionChange(componentType:string, componentId:string, instanceId:string, newInstanceId:string):Observable<Array<string>> {
110 let queries = {componentInstanceId: instanceId, newComponentInstanceId: newInstanceId};
112 let params:HttpParams = new HttpParams();
113 _.map(_.keys(queries), (key:string):void => {
114 params = params.append(key, queries[key]);
117 let url = this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/paths-to-delete';
118 return this.http.get<any>(url, {params: params}).map((res) => {
119 return res.forwardingPathToDelete;
123 getComponentCompositionData(component:Component):Observable<ComponentGenericResponse> {
124 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]);
127 private parseServicePathResponse(res: { [key:string]:ForwardingPath }):ForwardingPath {
129 let pathId = Object.keys(resJSON.forwardingPaths)[0];
130 let forwardingPath = resJSON.forwardingPaths[pathId];
131 let path:ForwardingPath = new ForwardingPath();
132 path.deserialize(forwardingPath);
133 path.uniqueId = pathId;
138 class ServicePathRequestData {
139 forwardingPaths: { [key:string]:ForwardingPath } = {};
140 componentMetadataDefinition: ComponentMetadata;
141 toscaType: string = "topology_template";
143 constructor(fp? : ForwardingPath) {
144 this.componentMetadataDefinition = new ComponentMetadata();
145 this.componentMetadataDefinition.ecompGeneratedNaming = true;
146 this.componentMetadataDefinition.componentType = ComponentType.SERVICE;
148 let id = fp.uniqueId ? fp.uniqueId : "NEW";
149 this.forwardingPaths[fp.uniqueId] = fp;