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, ComponentInstance, InputBEModel, InstancePropertiesAPIMap, FilterPropertiesAssignmentData,
28 PropertyBEModel, OperationModel, BEOperationModel, Capability, Requirement
30 import {downgradeInjectable} from '@angular/upgrade/static';
31 import {COMPONENT_FIELDS, CommonUtils, SERVICE_FIELDS} from "app/utils";
32 import {ComponentGenericResponse} from "../responses/component-generic-response";
33 import {InstanceBePropertiesMap} from "../../../models/properties-inputs/property-fe-map";
34 import {API_QUERY_PARAMS} from "app/utils";
35 import { ComponentType, ServerTypeUrl } from "../../../utils/constants";
36 import { HttpService } from '../http.service';
37 import {SdcConfigToken, ISdcConfig} from "../../config/sdc-config.config";
38 import {ConstraintObject} from 'app/ng2/components/logic/service-dependencies/service-dependencies.component';
39 import {IDependenciesServerResponse} from "../responses/dependencies-server-response";
40 import {AutomatedUpgradeGenericResponse} from "../responses/automated-upgrade-response";
41 import {IAutomatedUpgradeRequestObj} from "../../pages/automated-upgrade/automated-upgrade.service";
43 declare var angular:angular.IAngularStatic;
46 export class ComponentServiceNg2 {
50 constructor(protected http:HttpService, @Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
51 this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
54 protected getComponentDataByFieldsName(componentType:string, componentId: string, fields:Array<string>):Observable<ComponentGenericResponse> {
56 let params:URLSearchParams = new URLSearchParams();
57 _.forEach(fields, (field:string):void => {
58 params.append(API_QUERY_PARAMS.INCLUDE, field);
61 return this.http.get(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/filteredDataByParams', {search: params})
62 .map((res:Response) => {
63 return this.analyzeComponentDataResponse(res);
67 protected analyzeComponentDataResponse(res: Response):ComponentGenericResponse {
68 return new ComponentGenericResponse().deserialize(res.json());
71 private getServerTypeUrl = (componentType:string):string => {
72 switch (componentType) {
73 case ComponentType.SERVICE:
74 return ServerTypeUrl.SERVICES;
76 return ServerTypeUrl.RESOURCES;
80 getComponentMetadata(component:Component):Observable<ComponentGenericResponse> {
81 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_METADATA]);
84 getComponentInstanceAttributesAndProperties(component:Component):Observable<ComponentGenericResponse> {
85 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES, COMPONENT_FIELDS.COMPONENT_INSTANCES_ATTRIBUTES]);
88 getComponentInstanceProperties(component:Component):Observable<ComponentGenericResponse> {
89 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES]);
92 getComponentAttributes(component:Component):Observable<ComponentGenericResponse> {
93 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_ATTRIBUTES]);
96 getComponentCompositionData(component:Component):Observable<ComponentGenericResponse> {
97 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]);
100 getComponentResourcePropertiesData(component:Component):Observable<ComponentGenericResponse> {
101 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_POLICIES, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_GROUPS]);
104 getComponentResourceInstances(component:Component):Observable<ComponentGenericResponse> {
105 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES]);
108 getComponentInputs(component:Component):Observable<ComponentGenericResponse> {
109 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INPUTS]);
112 getComponentDeploymentArtifacts(component:Component):Observable<ComponentGenericResponse> {
113 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_DEPLOYMENT_ARTIFACTS]);
116 getComponentInformationalArtifacts(component:Component):Observable<ComponentGenericResponse> {
117 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS]);
120 getComponentInformationalArtifactsAndInstances(component:Component):Observable<ComponentGenericResponse> {
121 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS, COMPONENT_FIELDS.COMPONENT_INSTANCES]);
124 getComponentToscaArtifacts(component:Component):Observable<ComponentGenericResponse> {
125 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_TOSCA_ARTIFACTS]);
128 getComponentProperties(component:Component):Observable<ComponentGenericResponse> {
129 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
132 getInterfaces(component:Component):Observable<ComponentGenericResponse> {
133 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INTERFACE_OPERATIONS]);
136 getInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
137 return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaces/' + operation.interfaceId + '/operations/' + operation.uniqueId)
138 .map((res:Response) => {
143 createInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
144 const operationList = {
146 [operation.interfaceType]: {
147 'type': operation.interfaceType,
149 [operation.name]: new BEOperationModel(operation)
154 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
155 .map((res:Response) => {
156 const interf = _.find(res.json().interfaces, (interf: any) => interf.type === operation.interfaceType);
157 const newOperation = _.find(interf.operations, (op:OperationModel) => op.name === operation.name);
158 return new OperationModel({
160 interfaceType: interf.type,
161 interfaceId: interf.uniqueId
166 updateInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
167 const operationList = {
169 [operation.interfaceType]: {
170 'type': operation.interfaceType,
172 [operation.name]: new BEOperationModel(operation)
177 return this.http.put(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
178 .map((res:Response) => {
179 const interf = _.find(res.json().interfaces, (interf: any) => interf.type === operation.interfaceType);
180 const newOperation = _.find(interf.operations, (op:OperationModel) => op.name === operation.name);
181 return new OperationModel({
183 interfaceType: interf.type,
184 interfaceId: interf.uniqueId
189 deleteInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
190 return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaces/' + operation.interfaceId + '/operations/' + operation.uniqueId)
191 .map((res:Response) => {
196 getInterfaceTypes(component:Component):Observable<{[id:string]: Array<string>}> {
197 return this.http.get(this.baseUrl + 'interfaceLifecycleTypes')
198 .map((res:Response) => {
199 const interfaceMap = {};
200 _.forEach(res.json(), (interf:any) => {
201 interfaceMap[interf.toscaPresentation.type] = _.keys(interf.toscaPresentation.operations);
207 getCapabilitiesAndRequirements(componentType: string, componentId:string):Observable<ComponentGenericResponse> {
208 return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_REQUIREMENTS, COMPONENT_FIELDS.COMPONENT_CAPABILITIES]);
211 createCapability(component: Component, capabilityData: Capability): Observable<Array<Capability>> {
214 [capabilityData.type]: [capabilityData]
217 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/capabilities', capBEObj)
218 .map((res: Response) => {
223 updateCapability(component: Component, capabilityData: Capability): Observable<Array<Capability>> {
226 [capabilityData.type]: [capabilityData]
229 return this.http.put(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/capabilities', capBEObj)
230 .map((res: Response) => {
235 deleteCapability(component: Component, capId: string): Observable<Capability> {
236 return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/capabilities/' + capId)
237 .map((res: Response) => {
242 createRequirement(component: Component, requirementData: Requirement): Observable<any> {
245 [requirementData.capability]: [requirementData]
248 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/requirements', reqBEObj)
249 .map((res: Response) => {
254 updateRequirement(component: Component, requirementData: Requirement): Observable<any> {
257 [requirementData.capability]: [requirementData]
260 return this.http.put(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/requirements', reqBEObj)
261 .map((res: Response) => {
266 deleteRequirement(component: Component, reqId: string): Observable<Requirement> {
267 return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/requirements/' + reqId)
268 .map((res: Response) => {
273 getDeploymentGraphData(component:Component):Observable<ComponentGenericResponse> {
274 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_GROUPS]);
277 createInput(component:Component, inputsToCreate:InstancePropertiesAPIMap, isSelf:boolean):Observable<any> {
278 let inputs = isSelf ? { serviceProperties: inputsToCreate.componentInstanceProperties } : inputsToCreate;
279 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/inputs', inputs)
285 restoreComponent(componentType:string, componentId:string){
286 return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/restore', {})
289 archiveComponent(componentType:string, componentId:string){
290 return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/archive', {})
294 deleteInput(component:Component, input:InputBEModel):Observable<InputBEModel> {
295 return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/delete/' + input.uniqueId + '/input')
296 .map((res:Response) => {
297 return new InputBEModel(res.json());
301 updateComponentInputs(component:Component, inputs:InputBEModel[]):Observable<InputBEModel[]> {
302 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/update/inputs', inputs)
303 .map((res:Response) => {
304 return res.json().map((input) => new InputBEModel(input));
308 filterComponentInstanceProperties(component: Component, filterData:FilterPropertiesAssignmentData): Observable<InstanceBePropertiesMap> {//instance-property-be-map
309 let params: URLSearchParams = new URLSearchParams();
310 _.forEach(filterData.selectedTypes, (type:string) => {
311 params.append('resourceType', type);
314 return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/filteredproperties/' + filterData.propertyName, {search: params})
315 .map((res: Response) => {
320 createServiceProperty(component: Component, propertyModel:PropertyBEModel): Observable<PropertyBEModel> {
321 let serverObject = {};
322 serverObject[propertyModel.name] = propertyModel;
323 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties', serverObject)
325 let property:PropertyBEModel = new PropertyBEModel(res.json());
330 getServiceProperties(component: Component): Observable<Array<PropertyBEModel>> {
331 return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties')
332 .map((res: Response) => {
334 return new Array<PropertyBEModel>();
336 return CommonUtils.initBeProperties(res.json());
340 updateServiceProperties(component: Component, properties: PropertyBEModel[]) {
341 return this.http.put( this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties', properties)
342 .map((res: Response) => {
343 const resJson = res.json();
344 return _.map(resJson,
345 (resValue:PropertyBEModel) => new PropertyBEModel(resValue));
349 deleteServiceProperty(component:Component, property:PropertyBEModel):Observable<string> {
350 return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties/' + property.uniqueId )
351 .map((res:Response) => {
352 return property.uniqueId;
356 getDependencies(componentType:string, componentId: string):Observable<Array<IDependenciesServerResponse>> {
357 return this.http.get(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/dependencies')
358 .map((res:Response) => {
363 automatedUpgrade(componentType:string, componentId: string, componentsIdsToUpgrade:Array<IAutomatedUpgradeRequestObj>):Observable<AutomatedUpgradeGenericResponse> {
364 return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/automatedupgrade', componentsIdsToUpgrade)
365 .map((res:Response) => {
370 updateComponentInstance(component:Component, componentInstance:ComponentInstance):Observable<ComponentInstance> {
371 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstance/' + componentInstance.uniqueId, componentInstance)
372 .map((res:Response) => {
377 getServiceFilterConstraints(component:Component):Observable<ComponentGenericResponse> {
378 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [SERVICE_FIELDS.NODE_FILTER]);
381 createServiceFilterConstraints(component:Component, componentInstance:ComponentInstance, constraint:ConstraintObject):Observable<any> {
382 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter', constraint)
383 .map((res:Response) => {
388 updateServiceFilterConstraints(component:Component, componentInstance:ComponentInstance, constraints:Array<ConstraintObject>):Observable<any> {
389 return this.http.put(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter/', constraints)
390 .map((res:Response) => {
395 deleteServiceFilterConstraints(component:Component, componentInstance:ComponentInstance, constraintIndex:number) {
396 return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter/' + constraintIndex)
397 .map((res:Response) => {