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, OperationModel, CreateOperationResponse} from "app/models";
28 import {downgradeInjectable} from '@angular/upgrade/static';
29 import {COMPONENT_FIELDS} from "app/utils";
30 import {ComponentGenericResponse} from "../responses/component-generic-response";
31 import {InstanceBePropertiesMap} from "../../../models/properties-inputs/property-fe-map";
32 import {API_QUERY_PARAMS} from "app/utils";
33 import { ComponentType, ServerTypeUrl } from "../../../utils/constants";
34 import { HttpService } from '../http.service';
35 import {SdcConfigToken, ISdcConfig} from "../../config/sdc-config.config";
36 import {IDependenciesServerResponse} from "../responses/dependencies-server-response";
37 import {AutomatedUpgradeGenericResponse} from "../responses/automated-upgrade-response";
38 import {IAutomatedUpgradeRequestObj} from "../../pages/automated-upgrade/automated-upgrade.service";
40 declare var angular:angular.IAngularStatic;
43 export class ComponentServiceNg2 {
47 constructor(protected http:HttpService, @Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
48 this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
51 protected getComponentDataByFieldsName(componentType:string, componentId: string, fields:Array<string>):Observable<ComponentGenericResponse> {
53 let params:URLSearchParams = new URLSearchParams();
54 _.forEach(fields, (field:string):void => {
55 params.append(API_QUERY_PARAMS.INCLUDE, field);
58 return this.http.get(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/filteredDataByParams', {search: params})
59 .map((res:Response) => {
60 return this.analyzeComponentDataResponse(res);
64 protected analyzeComponentDataResponse(res: Response):ComponentGenericResponse {
65 return new ComponentGenericResponse().deserialize(res.json());
68 private getServerTypeUrl = (componentType:string):string => {
69 switch (componentType) {
70 case ComponentType.SERVICE:
71 return ServerTypeUrl.SERVICES;
73 return ServerTypeUrl.RESOURCES;
77 getComponentMetadata(component:Component):Observable<ComponentGenericResponse> {
78 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_METADATA]);
81 getComponentInstanceAttributesAndProperties(component:Component):Observable<ComponentGenericResponse> {
82 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES, COMPONENT_FIELDS.COMPONENT_INSTANCES_ATTRIBUTES]);
85 getComponentAttributes(component:Component):Observable<ComponentGenericResponse> {
86 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_ATTRIBUTES]);
89 getComponentCompositionData(component:Component):Observable<ComponentGenericResponse> {
90 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_POLICIES, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_GROUPS]);
93 getComponentResourcePropertiesData(component:Component):Observable<ComponentGenericResponse> {
94 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_POLICIES, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_GROUPS]);
97 getComponentResourceInstances(component:Component):Observable<ComponentGenericResponse> {
98 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES]);
101 getComponentInputs(component:Component):Observable<ComponentGenericResponse> {
102 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INPUTS]);
105 getComponentDeploymentArtifacts(component:Component):Observable<ComponentGenericResponse> {
106 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_DEPLOYMENT_ARTIFACTS]);
109 getComponentInformationalArtifacts(component:Component):Observable<ComponentGenericResponse> {
110 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS]);
113 getComponentInformationalArtifactsAndInstances(component:Component):Observable<ComponentGenericResponse> {
114 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS, COMPONENT_FIELDS.COMPONENT_INSTANCES]);
117 getComponentToscaArtifacts(component:Component):Observable<ComponentGenericResponse> {
118 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_TOSCA_ARTIFACTS]);
121 getComponentProperties(component:Component):Observable<ComponentGenericResponse> {
122 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
125 getInterfaceOperations(component:Component):Observable<ComponentGenericResponse> {
126 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INTERFACE_OPERATIONS]);
129 getInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
130 return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations/' + operation.uniqueId)
131 .map((res:Response) => {
136 createInterfaceOperation(component:Component, operation:OperationModel):Observable<CreateOperationResponse> {
137 const operationList = {
138 'interfaceOperations': {
139 [operation.operationType]: operation
142 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
143 .map((res:Response) => {
148 updateInterfaceOperation(component:Component, operation:OperationModel):Observable<CreateOperationResponse> {
149 const operationList = {
150 'interfaceOperations': {
151 [operation.operationType]: operation
154 return this.http.put(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
155 .map((res:Response) => {
160 deleteInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
161 return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations/' + operation.uniqueId)
162 .map((res:Response) => {
167 getCapabilitiesAndRequirements(componentType: string, componentId:string):Observable<ComponentGenericResponse> {
168 return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_REQUIREMENTS, COMPONENT_FIELDS.COMPONENT_CAPABILITIES]);
171 getDeploymentGraphData(component:Component):Observable<ComponentGenericResponse> {
172 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_GROUPS]);
175 createInput(component:Component, inputsToCreate:InstancePropertiesAPIMap):Observable<any> {
176 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/inputs', inputsToCreate)
182 restoreComponent(componentType:string, componentId:string){
183 return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/restore', {})
186 archiveComponent(componentType:string, componentId:string){
187 return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/archive', {})
191 deleteInput(component:Component, input:InputBEModel):Observable<InputBEModel> {
193 return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/delete/' + input.uniqueId + '/input')
194 .map((res:Response) => {
195 return new InputBEModel(res.json());
199 updateComponentInputs(component:Component, inputs:InputBEModel[]):Observable<InputBEModel[]> {
201 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/update/inputs', inputs)
202 .map((res:Response) => {
203 return res.json().map((input) => new InputBEModel(input));
207 filterComponentInstanceProperties(component: Component, filterData:FilterPropertiesAssignmentData): Observable<InstanceBePropertiesMap> {//instance-property-be-map
208 let params: URLSearchParams = new URLSearchParams();
209 _.forEach(filterData.selectedTypes, (type:string) => {
210 params.append('resourceType', type);
213 return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/filteredproperties/' + filterData.propertyName, {search: params})
214 .map((res: Response) => {
219 getDependencies(componentType:string, componentId: string):Observable<Array<IDependenciesServerResponse>> {
220 return this.http.get(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/dependencies')
221 .map((res:Response) => {
226 automatedUpgrade(componentType:string, componentId: string, componentsIdsToUpgrade:Array<IAutomatedUpgradeRequestObj>):Observable<AutomatedUpgradeGenericResponse> {
227 return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/automatedupgrade', componentsIdsToUpgrade)
228 .map((res:Response) => {