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 * as _ from "lodash";
22 import {Injectable, Inject} from '@angular/core';
23 import {Observable} from 'rxjs/Observable';
24 import 'rxjs/add/operator/map';
25 import 'rxjs/add/operator/toPromise';
26 import {Response, URLSearchParams} from '@angular/http';
27 import { Component, InputBEModel, InstancePropertiesAPIMap, FilterPropertiesAssignmentData} from "app/models";
28 import {COMPONENT_FIELDS} from "app/utils";
29 import {ComponentGenericResponse} from "../responses/component-generic-response";
30 import {InstanceBePropertiesMap} from "../../../models/properties-inputs/property-fe-map";
31 import {API_QUERY_PARAMS} from "app/utils";
32 import { ComponentType, ServerTypeUrl } from "../../../utils/constants";
33 import { HttpService } from '../http.service';
34 import {SdcConfigToken, ISdcConfig} from "../../config/sdc-config.config";
36 declare var angular:angular.IAngularStatic;
39 export class ComponentServiceNg2 {
43 constructor(protected http:HttpService, @Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
44 this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
47 protected getComponentDataByFieldsName(componentType:string, componentId: string, fields:Array<string>):Observable<ComponentGenericResponse> {
49 let params:URLSearchParams = new URLSearchParams();
50 _.forEach(fields, (field:string):void => {
51 params.append(API_QUERY_PARAMS.INCLUDE, field);
54 return this.http.get(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/filteredDataByParams', {search: params})
55 .map((res:Response) => {
56 return this.analyzeComponentDataResponse(res);
60 protected analyzeComponentDataResponse(res: Response):ComponentGenericResponse {
61 return new ComponentGenericResponse().deserialize(res.json());
64 private getServerTypeUrl = (componentType:string):string => {
65 switch (componentType) {
66 case ComponentType.SERVICE:
67 return ServerTypeUrl.SERVICES;
69 return ServerTypeUrl.RESOURCES;
73 getComponentMetadata(component:Component):Observable<ComponentGenericResponse> {
74 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_METADATA]);
77 getComponentInstanceAttributesAndProperties(component:Component):Observable<ComponentGenericResponse> {
78 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES, COMPONENT_FIELDS.COMPONENT_INSTANCES_ATTRIBUTES]);
81 getComponentAttributes(component:Component):Observable<ComponentGenericResponse> {
82 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_ATTRIBUTES]);
85 getComponentCompositionData(component:Component):Observable<ComponentGenericResponse> {
86 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_POLICIES, COMPONENT_FIELDS.COMPONENT_GROUPS]);
89 getComponentResourceInstances(component:Component):Observable<ComponentGenericResponse> {
90 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES]);
93 getComponentInputs(component:Component):Observable<ComponentGenericResponse> {
94 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INPUTS]);
97 getComponentDeploymentArtifacts(component:Component):Observable<ComponentGenericResponse> {
98 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_DEPLOYMENT_ARTIFACTS]);
101 getComponentInformationalArtifacts(component:Component):Observable<ComponentGenericResponse> {
102 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS]);
105 getComponentInformationalArtifactsAndInstances(component:Component):Observable<ComponentGenericResponse> {
106 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS, COMPONENT_FIELDS.COMPONENT_INSTANCES]);
109 getComponentToscaArtifacts(component:Component):Observable<ComponentGenericResponse> {
110 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_TOSCA_ARTIFACTS]);
113 getComponentProperties(component:Component):Observable<ComponentGenericResponse> {
114 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
117 getCapabilitiesAndRequirements(componentType: string, componentId:string):Observable<ComponentGenericResponse> {
118 return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_REQUIREMENTS, COMPONENT_FIELDS.COMPONENT_CAPABILITIES]);
121 getDeploymentGraphData(component:Component):Observable<ComponentGenericResponse> {
122 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_GROUPS]);
125 createInput(component:Component, inputsToCreate:InstancePropertiesAPIMap):Observable<any> {
126 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/inputs', inputsToCreate)
132 deleteInput(component:Component, input:InputBEModel):Observable<InputBEModel> {
134 return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/delete/' + input.uniqueId + '/input')
135 .map((res:Response) => {
136 return new InputBEModel(res.json());
140 updateComponentInputs(component:Component, inputs:InputBEModel[]):Observable<InputBEModel[]> {
142 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/update/inputs', inputs)
143 .map((res:Response) => {
144 return res.json().map((input) => new InputBEModel(input));
148 filterComponentInstanceProperties(component: Component, filterData:FilterPropertiesAssignmentData): Observable<InstanceBePropertiesMap> {//instance-property-be-map
149 let params: URLSearchParams = new URLSearchParams();
150 _.forEach(filterData.selectedTypes, (type:string) => {
151 params.append('resourceType', type);
154 return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/filteredproperties/' + filterData.propertyName, {search: params})
155 .map((res: Response) => {