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 { Component, InputBEModel, InstancePropertiesAPIMap, FilterPropertiesAssignmentData, OperationModel, CreateOperationResponse, ArtifactModel} from "app/models";
27 import {COMPONENT_FIELDS} from "app/utils";
28 import {ComponentGenericResponse} from "../responses/component-generic-response";
29 import {InstanceBePropertiesMap} from "../../../models/properties-inputs/property-fe-map";
30 import {API_QUERY_PARAMS} from "app/utils";
31 import {ComponentType, ServerTypeUrl, SERVICE_FIELDS} from "../../../utils/constants";
32 import {SdcConfigToken, ISdcConfig} from "../../config/sdc-config.config";
33 import {IDependenciesServerResponse} from "../responses/dependencies-server-response";
34 import {AutomatedUpgradeGenericResponse} from "../responses/automated-upgrade-response";
35 import {IAutomatedUpgradeRequestObj} from "../../pages/automated-upgrade/automated-upgrade.service";
36 import {ComponentInstance} from "../../../models/componentsInstances/componentInstance";
37 import {CommonUtils} from "../../../utils/common-utils";
38 import {RelationshipModel} from "../../../models/graph/relationship";
39 import { HttpClient, HttpParams, HttpHeaders } from "@angular/common/http";
40 import { BEOperationModel, InterfaceModel } from "../../../models/operation";
41 import { PropertyBEModel } from "../../../models/properties-inputs/property-be-model";
42 import { PolicyInstance } from "../../../models/graph/zones/policy-instance";
43 import { ConstraintObject } from "../../components/logic/service-dependencies/service-dependencies.component";
44 import { Requirement } from "../../../models/requirement";
45 import { Capability } from "../../../models/capability";
48 PLEASE DO NOT USE THIS SERVICE IN ANGULAR2! Use the topology-template.service instead
51 export class ComponentServiceNg2 {
55 constructor(protected http: HttpClient, @Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
56 this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
59 protected getComponentDataByFieldsName(componentType:string, componentId:string, fields:Array<string>):Observable<ComponentGenericResponse> {
61 let params: HttpParams = new HttpParams();
62 _.forEach(fields, (field:string):void => {
63 params = params.append(API_QUERY_PARAMS.INCLUDE, field);
66 return this.http.get<ComponentGenericResponse>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/filteredDataByParams', {params: params})
68 return new ComponentGenericResponse().deserialize(res);
71 protected getServerTypeUrl = (componentType:string):string => {
72 switch (componentType) {
73 case ComponentType.SERVICE:
74 return ServerTypeUrl.SERVICES;
76 return ServerTypeUrl.RESOURCES;
80 getFullComponent(uniqueId:string):Observable<ComponentGenericResponse> {
81 return this.http.get<ComponentGenericResponse>(this.baseUrl + uniqueId)
83 return new ComponentGenericResponse().deserialize(res);
87 getComponentMetadata(uniqueId:string, type:string):Observable<ComponentGenericResponse> {
88 return this.getComponentDataByFieldsName(type, uniqueId, [COMPONENT_FIELDS.COMPONENT_METADATA]);
92 getComponentInstanceAttributesAndProperties(component:Component):Observable<ComponentGenericResponse> {
93 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES, COMPONENT_FIELDS.COMPONENT_INSTANCES_ATTRIBUTES]);
96 getComponentInstanceProperties(component:Component): Observable<ComponentGenericResponse> {
97 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES]);
100 getComponentAttributes(component:Component):Observable<ComponentGenericResponse> {
101 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_ATTRIBUTES]);
104 getComponentCompositionData(component:Component):Observable<ComponentGenericResponse> {
105 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]);
108 getComponentResourcePropertiesData(component:Component):Observable<ComponentGenericResponse> {
109 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_POLICIES, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_GROUPS]);
112 getComponentResourceInstances(component:Component):Observable<ComponentGenericResponse> {
113 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES]);
116 getComponentInputs(component:Component):Observable<ComponentGenericResponse> {
117 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INPUTS]);
120 getComponentInputsWithProperties(component:Component):Observable<ComponentGenericResponse> {
121 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INPUTS, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES, COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
124 getComponentDeploymentArtifacts(component:Component):Observable<ComponentGenericResponse> {
125 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_DEPLOYMENT_ARTIFACTS]);
128 getComponentInformationalArtifacts(component:Component):Observable<ComponentGenericResponse> {
129 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS]);
132 getComponentInformationalArtifactsAndInstances(component:Component):Observable<ComponentGenericResponse> {
133 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS, COMPONENT_FIELDS.COMPONENT_INSTANCES]);
136 getComponentToscaArtifacts(component:Component):Observable<ComponentGenericResponse> {
137 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_TOSCA_ARTIFACTS]);
140 getComponentProperties(component:Component):Observable<ComponentGenericResponse> {
141 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
144 getInterfaceOperations(component:Component):Observable<ComponentGenericResponse> {
145 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INTERFACE_OPERATIONS]);
148 getInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
149 return this.http.get<OperationModel>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations/' + operation.uniqueId);
152 // tslint:disable-next-line:member-ordering
153 createInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
154 const operationList = {
156 [operation.interfaceType]: {
157 'type': operation.interfaceType,
159 [operation.name]: new BEOperationModel(operation)
164 return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
166 const interf:InterfaceModel = _.find(res.interfaces, interf => interf.type === operation.interfaceType);
167 const newOperation:OperationModel = _.find(interf.operations, op => op.name === operation.name);
168 return new OperationModel({
170 interfaceType: interf.type,
171 interfaceId: interf.uniqueId,
172 artifactFileName: operation.artifactFileName
177 // tslint:disable-next-line:member-ordering
178 updateInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
179 const operationList = {
181 [operation.interfaceType]: {
182 type: operation.interfaceType,
184 [operation.name]: new BEOperationModel(operation)
189 return this.http.put<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
191 const interf: InterfaceModel = _.find(res.interfaces, interf => interf.type === operation.interfaceType);
192 const newOperation: OperationModel = _.find(interf.operations, op => op.name === operation.name);
193 return new OperationModel({
195 interfaceType: interf.type,
196 interfaceId: interf.uniqueId,
197 artifactFileName: operation.artifactFileName
203 deleteInterfaceOperation(component: Component, operation:OperationModel):Observable<OperationModel> {
204 return this.http.delete<OperationModel>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaces/' + operation.interfaceId + '/operations/' + operation.uniqueId);
207 getInterfaceTypes(component:Component):Observable<{[id:string]: Array<string>}> {
208 return this.http.get<any>(this.baseUrl + 'interfaceLifecycleTypes')
210 const interfaceMap = {};
211 _.forEach(res, (interf: any) => {
212 interfaceMap[interf.toscaPresentation.type] = _.keys(interf.toscaPresentation.operations);
218 uploadInterfaceOperationArtifact(component:Component, newOperation:OperationModel, oldOperation:OperationModel) {
220 artifactType: "WORKFLOW",
221 artifactName: oldOperation.artifactFileName,
222 description: "Workflow Artifact Description",
223 payloadData: oldOperation.artifactData
226 const headers = new HttpHeaders();
227 JSON.stringify(payload);
228 const payloadString = JSON.stringify(payload, null, ' ');
229 const md5Result = md5(payloadString).toLowerCase();
230 headers.append('Content-MD5', btoa(md5Result));
232 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uuid + '/interfaces/' + newOperation.interfaceId + '/operations/' + newOperation.uniqueId + '/artifacts/' + newOperation.implementation.artifactUUID,
234 ).map((res: any) => {
235 const fileName = res.artifactDisplayName || res.artifactName;
236 newOperation.artifactFileName = fileName;
241 getCapabilitiesAndRequirements(componentType: string, componentId:string):Observable<ComponentGenericResponse> {
242 return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_REQUIREMENTS, COMPONENT_FIELDS.COMPONENT_CAPABILITIES]);
249 getDeploymentGraphData(component:Component):Observable<ComponentGenericResponse> {
250 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_GROUPS]);
253 createInput(component:Component, inputsToCreate:InstancePropertiesAPIMap, isSelf:boolean):Observable<any> {
254 const inputs = isSelf ? { serviceProperties: inputsToCreate.componentInstanceProperties } : inputsToCreate;
255 return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/inputs', inputs);
258 createListInput(component:Component, input:any, isSelf:boolean):Observable<any> {
261 // change componentInstanceProperties -> serviceProperties
263 componentInstInputsMap: {
264 serviceProperties: input.componentInstInputsMap.componentInstanceProperties
266 listInput: input.listInput
271 return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/listInput', inputs);
274 createPolicy(component:Component, policiesToCreate:InstancePropertiesAPIMap, isSelf:boolean):Observable<any> {
277 {'componentPropertiesToPolicies': {
278 ...policiesToCreate.componentInstanceProperties
281 {'componentInstancePropertiesToPolicies': {
282 ...policiesToCreate.componentInstanceProperties
285 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/policies', policiesList);
288 deletePolicy(component:Component, policy: PolicyInstance) {
289 return this.http.put<PolicyInstance>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/policies/' + policy.uniqueId + '/undeclare', policy);
292 restoreComponent(componentType:string, componentId:string) {
293 return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/restore', {})
296 archiveComponent(componentType:string, componentId:string) {
297 return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/archive', {})
301 deleteInput(component:Component, input:InputBEModel):Observable<InputBEModel> {
303 return this.http.delete<InputBEModel>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/delete/' + input.uniqueId + '/input')
305 return new InputBEModel(res);
309 updateComponentInputs(component:Component, inputs:InputBEModel[]):Observable<InputBEModel[]> {
311 return this.http.post<InputBEModel[]>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/update/inputs', inputs)
313 return res.map((input) => new InputBEModel(input));
317 filterComponentInstanceProperties(component:Component, filterData:FilterPropertiesAssignmentData):Observable<InstanceBePropertiesMap> {//instance-property-be-map
318 let params: HttpParams = new HttpParams();
319 _.forEach(filterData.selectedTypes, (type:string) => {
320 params = params.append('resourceType', type);
323 return this.http.get<InstanceBePropertiesMap>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/filteredproperties/' + filterData.propertyName, {params: params});
326 createServiceProperty(component: Component, propertyModel:PropertyBEModel): Observable<PropertyBEModel> {
327 let serverObject = {};
328 serverObject[propertyModel.name] = propertyModel;
329 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties', serverObject)
330 .map((res: PropertyBEModel) => {
331 const property: PropertyBEModel = new PropertyBEModel(res);
336 getServiceProperties(component: Component): Observable<PropertyBEModel[]> {
337 return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties')
338 .map((res: PropertyBEModel[]) => {
340 return new Array<PropertyBEModel>();
342 return CommonUtils.initBeProperties(res);
346 updateServiceProperties(component: Component, properties: PropertyBEModel[]) {
347 return this.http.put<PropertyBEModel[]>( this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties', properties)
350 return _.map(resJson,
351 (resValue:PropertyBEModel) => new PropertyBEModel(resValue));
355 deleteServiceProperty(component:Component, property:PropertyBEModel):Observable<string> {
356 return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties/' + property.uniqueId )
357 .map((res:Response) => {
358 return property.uniqueId;
362 getDependencies(componentType:string, componentId: string):Observable<IDependenciesServerResponse[]> {
363 return this.http.get<IDependenciesServerResponse[]>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/dependencies');
366 automatedUpgrade(componentType:string, componentId:string, componentsIdsToUpgrade:IAutomatedUpgradeRequestObj[]):Observable<AutomatedUpgradeGenericResponse> {
367 return this.http.post<AutomatedUpgradeGenericResponse>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/automatedupgrade', componentsIdsToUpgrade);
370 updateMultipleComponentInstances(componentId:string, instances:ComponentInstance[]):Observable<ComponentInstance[]> {
371 return this.http.post<ComponentInstance[]>(this.baseUrl + componentId + '/resourceInstance/multipleComponentInstance', instances);
374 getServiceFilterConstraints(component:Component):Observable<ComponentGenericResponse> {
375 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [SERVICE_FIELDS.NODE_FILTER]);
378 createServiceFilterConstraints(component:Component, componentInstance:ComponentInstance, constraint:ConstraintObject):Observable<any> {
379 return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter', constraint);
382 updateServiceFilterConstraints(component:Component, componentInstance:ComponentInstance, constraints:ConstraintObject[]):Observable<any> {
383 return this.http.put<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter/', constraints);
386 deleteServiceFilterConstraints(component:Component, componentInstance:ComponentInstance, constraintIndex:number) {
387 return this.http.delete<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter/' + constraintIndex);
390 protected analyzeComponentDataResponse(res: Response):ComponentGenericResponse {
391 return new ComponentGenericResponse().deserialize(res);