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 { Response, URLSearchParams } from '@angular/http';
26 import {Service} from "app/models";
27 import { downgradeInjectable } from '@angular/upgrade/static';
28 import { HttpService } from '../http.service';
30 import {SdcConfigToken, ISdcConfig} from "../../config/sdc-config.config";
31 import {ForwardingPath} from "app/models/forwarding-path";
32 import {ComponentMetadata} from "app/models/component-metadata";
33 import {ComponentType} from "app/utils";
34 import {Component} from "app/models/components/component";
35 import {ComponentGenericResponse} from "app/ng2/services/responses/component-generic-response";
36 import {COMPONENT_FIELDS, SERVICE_FIELDS} from "app/utils/constants";
37 import {ComponentServiceNg2} from "./component.service";
38 import {ServiceGenericResponse} from "app/ng2/services/responses/service-generic-response";
39 import {ServicePathMapItem} from "app/models/graph/nodes-and-links-map";
43 export class ServiceServiceNg2 extends ComponentServiceNg2 {
45 protected baseUrl = "";
47 constructor(protected http: HttpService, @Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
48 super(http, sdcConfig);
49 this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
52 validateConformanceLevel(service: Service): Observable<boolean> {
54 return this.http.get(this.baseUrl + service.getTypeUrl() + service.uuid + '/conformanceLevelValidation')
55 .map((res: Response) => {
60 getNodesAndLinksMap(service: Service):Observable<Array<ServicePathMapItem>> {
61 return this.http.get(this.baseUrl + service.getTypeUrl() + service.uniqueId + '/linksMap').map(res => {
62 return <Array<ServicePathMapItem>>res.json();
66 getServicePath(service: Service, id: string):Observable<any> {
67 return this.http.get(this.baseUrl + service.getTypeUrl() + service.uniqueId + '/paths/' + id)
73 getServicePaths(service: Service):Observable<any> {
74 return this.http.get(this.baseUrl + service.getTypeUrl() + service.uniqueId + '/paths')
80 createOrUpdateServicePath(service: Service, inputsToCreate: ForwardingPath):Observable<ForwardingPath> {
81 if (inputsToCreate.uniqueId) {
82 return this.updateServicePath(service, inputsToCreate);
84 return this.createServicePath(service, inputsToCreate);
88 createServicePath(service: Service, inputsToCreate: ForwardingPath):Observable<ForwardingPath> {
89 let input = new ServicePathRequestData(inputsToCreate);
91 return this.http.post(this.baseUrl + service.getTypeUrl() + service.uniqueId + '/paths', input)
93 return this.parseServicePathResponse(res);
97 deleteServicePath(service: Service, id: string):Observable<any> {
98 return this.http.delete(this.baseUrl + service.getTypeUrl() + service.uniqueId + '/paths/' + id )
104 updateServicePath(service: Service, inputsToUpdate:ForwardingPath):Observable<ForwardingPath> {
105 let input = new ServicePathRequestData(inputsToUpdate);
107 return this.http.put(this.baseUrl + service.getTypeUrl() + service.uniqueId + '/paths', input)
109 return this.parseServicePathResponse(res);
113 checkComponentInstanceVersionChange(service: Service, newVersionId: string):Observable<Array<string>> {
114 let instanceId = service.selectedInstance.uniqueId;
115 let queries = {componentInstanceId: instanceId, newComponentInstanceId: newVersionId};
117 let params:URLSearchParams = new URLSearchParams();
118 _.map(_.keys(queries), (key:string):void => {
119 params.append(key, queries[key]);
122 let url = this.baseUrl + service.getTypeUrl() + service.uniqueId + '/paths-to-delete';
123 return this.http.get(url, {search: params}).map((res: Response) => {
124 return res.json().forwardingPathToDelete;
128 getComponentCompositionData(component:Component):Observable<ComponentGenericResponse> {
129 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]);
132 protected analyzeComponentDataResponse(res: Response):ComponentGenericResponse {
133 return new ServiceGenericResponse().deserialize(res.json());
136 private parseServicePathResponse(res: Response):ForwardingPath {
137 let resJSON = res.json();
138 let pathId = Object.keys(resJSON.forwardingPaths)[0];
139 let forwardingPath = resJSON.forwardingPaths[pathId];
140 let path:ForwardingPath = new ForwardingPath();
141 path.deserialize(forwardingPath);
142 path.uniqueId = pathId;
147 class ServicePathRequestData {
148 forwardingPaths: { [key:string]:ForwardingPath } = {};
149 componentMetadataDefinition: ComponentMetadata;
150 toscaType: string = "topology_template";
152 constructor(fp? : ForwardingPath) {
153 this.componentMetadataDefinition = new ComponentMetadata();
154 this.componentMetadataDefinition.ecompGeneratedNaming = true;
155 this.componentMetadataDefinition.componentType = ComponentType.SERVICE;
157 let id = fp.uniqueId ? fp.uniqueId : "NEW";
158 this.forwardingPaths[fp.uniqueId] = fp;