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, PolicyInstance} from "app/models";
29 import {COMPONENT_FIELDS, CommonUtils, SERVICE_FIELDS} from "app/utils";
30 import {downgradeInjectable} from '@angular/upgrade/static';
31 import {ComponentGenericResponse} from "../responses/component-generic-response";
32 import {InstanceBePropertiesMap} from "../../../models/properties-inputs/property-fe-map";
33 import {API_QUERY_PARAMS} from "app/utils";
34 import { ComponentType, ServerTypeUrl } from "../../../utils/constants";
35 import { HttpService } from '../http.service';
36 import {SdcConfigToken, ISdcConfig} from "../../config/sdc-config.config";
37 import {ConstraintObject} from 'app/ng2/components/logic/service-dependencies/service-dependencies.component';
38 import {IDependenciesServerResponse} from "../responses/dependencies-server-response";
39 import {AutomatedUpgradeGenericResponse} from "../responses/automated-upgrade-response";
40 import {IAutomatedUpgradeRequestObj} from "../../pages/automated-upgrade/automated-upgrade.service";
42 declare var angular:angular.IAngularStatic;
45 export class ComponentServiceNg2 {
49 constructor(protected http:HttpService, @Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
50 this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
53 protected getComponentDataByFieldsName(componentType:string, componentId: string, fields:Array<string>):Observable<ComponentGenericResponse> {
55 let params:URLSearchParams = new URLSearchParams();
56 _.forEach(fields, (field:string):void => {
57 params.append(API_QUERY_PARAMS.INCLUDE, field);
60 return this.http.get(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/filteredDataByParams', {search: params})
61 .map((res:Response) => {
62 return this.analyzeComponentDataResponse(res);
66 protected analyzeComponentDataResponse(res: Response):ComponentGenericResponse {
67 return new ComponentGenericResponse().deserialize(res.json());
70 private getServerTypeUrl = (componentType:string):string => {
71 switch (componentType) {
72 case ComponentType.SERVICE:
73 return ServerTypeUrl.SERVICES;
75 return ServerTypeUrl.RESOURCES;
79 getComponentMetadata(component:Component):Observable<ComponentGenericResponse> {
80 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_METADATA]);
83 getComponentInstanceAttributesAndProperties(component:Component):Observable<ComponentGenericResponse> {
84 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES, COMPONENT_FIELDS.COMPONENT_INSTANCES_ATTRIBUTES]);
87 getComponentInstanceProperties(component:Component):Observable<ComponentGenericResponse> {
88 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES]);
91 getComponentAttributes(component:Component):Observable<ComponentGenericResponse> {
92 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_ATTRIBUTES]);
95 getComponentCompositionData(component:Component):Observable<ComponentGenericResponse> {
96 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]);
99 getComponentResourcePropertiesData(component:Component):Observable<ComponentGenericResponse> {
100 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_POLICIES, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_GROUPS]);
103 getComponentResourceInstances(component:Component):Observable<ComponentGenericResponse> {
104 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES]);
107 getComponentInputs(component:Component):Observable<ComponentGenericResponse> {
108 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INPUTS]);
111 getComponentInputsWithProperties(component:Component):Observable<ComponentGenericResponse> {
112 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INPUTS, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES, COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
115 getComponentDeploymentArtifacts(component:Component):Observable<ComponentGenericResponse> {
116 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_DEPLOYMENT_ARTIFACTS]);
119 getComponentInformationalArtifacts(component:Component):Observable<ComponentGenericResponse> {
120 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS]);
123 getComponentInformationalArtifactsAndInstances(component:Component):Observable<ComponentGenericResponse> {
124 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS, COMPONENT_FIELDS.COMPONENT_INSTANCES]);
127 getComponentToscaArtifacts(component:Component):Observable<ComponentGenericResponse> {
128 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_TOSCA_ARTIFACTS]);
131 getComponentProperties(component:Component):Observable<ComponentGenericResponse> {
132 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
135 getInterfaces(component:Component):Observable<ComponentGenericResponse> {
136 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INTERFACE_OPERATIONS]);
139 getInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
140 return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaces/' + operation.interfaceId + '/operations/' + operation.uniqueId)
141 .map((res:Response) => {
146 createInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
147 const operationList = {
149 [operation.interfaceType]: {
150 'type': operation.interfaceType,
152 [operation.name]: new BEOperationModel(operation)
157 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
158 .map((res:Response) => {
159 const interf = _.find(res.json().interfaces, (interf: any) => interf.type === operation.interfaceType);
160 const newOperation = _.find(interf.operations, (op:OperationModel) => op.name === operation.name);
161 return new OperationModel({
163 interfaceType: interf.type,
164 interfaceId: interf.uniqueId
169 updateInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
170 const operationList = {
172 [operation.interfaceType]: {
173 'type': operation.interfaceType,
175 [operation.name]: new BEOperationModel(operation)
180 return this.http.put(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
181 .map((res:Response) => {
182 const interf = _.find(res.json().interfaces, (interf: any) => interf.type === operation.interfaceType);
183 const newOperation = _.find(interf.operations, (op:OperationModel) => op.name === operation.name);
184 return new OperationModel({
186 interfaceType: interf.type,
187 interfaceId: interf.uniqueId
192 deleteInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
193 return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaces/' + operation.interfaceId + '/operations/' + operation.uniqueId)
194 .map((res:Response) => {
199 getInterfaceTypes(component:Component):Observable<{[id:string]: Array<string>}> {
200 return this.http.get(this.baseUrl + 'interfaceLifecycleTypes')
201 .map((res:Response) => {
202 const interfaceMap = {};
203 _.forEach(res.json(), (interf:any) => {
204 interfaceMap[interf.toscaPresentation.type] = _.keys(interf.toscaPresentation.operations);
210 getCapabilitiesAndRequirements(componentType: string, componentId:string):Observable<ComponentGenericResponse> {
211 return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_REQUIREMENTS, COMPONENT_FIELDS.COMPONENT_CAPABILITIES]);
214 createCapability(component: Component, capabilityData: Capability): Observable<Array<Capability>> {
217 [capabilityData.type]: [capabilityData]
220 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/capabilities', capBEObj)
221 .map((res: Response) => {
226 updateCapability(component: Component, capabilityData: Capability): Observable<Array<Capability>> {
229 [capabilityData.type]: [capabilityData]
232 return this.http.put(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/capabilities', capBEObj)
233 .map((res: Response) => {
238 deleteCapability(component: Component, capId: string): Observable<Capability> {
239 return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/capabilities/' + capId)
240 .map((res: Response) => {
245 createRequirement(component: Component, requirementData: Requirement): Observable<any> {
248 [requirementData.capability]: [requirementData]
251 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/requirements', reqBEObj)
252 .map((res: Response) => {
257 updateRequirement(component: Component, requirementData: Requirement): Observable<any> {
260 [requirementData.capability]: [requirementData]
263 return this.http.put(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/requirements', reqBEObj)
264 .map((res: Response) => {
269 deleteRequirement(component: Component, reqId: string): Observable<Requirement> {
270 return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/requirements/' + reqId)
271 .map((res: Response) => {
276 getDeploymentGraphData(component:Component):Observable<ComponentGenericResponse> {
277 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_GROUPS]);
280 createInput(component:Component, inputsToCreate:InstancePropertiesAPIMap, isSelf:boolean):Observable<any> {
281 let inputs = isSelf ? { serviceProperties: inputsToCreate.componentInstanceProperties } : inputsToCreate;
282 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/inputs', inputs)
288 createPolicy(component:Component, policiesToCreate:InstancePropertiesAPIMap, isSelf:boolean):Observable<any> {
291 {'componentPropertiesToPolicies': {
292 ...policiesToCreate.componentInstanceProperties
295 {'componentInstancePropertiesToPolicies': {
296 ...policiesToCreate.componentInstanceProperties
299 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/policies', policiesList)
305 deletePolicy(component:Component, policy: PolicyInstance) {
306 return this.http.put(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/policies/' + policy.uniqueId + '/undeclare', policy)
312 restoreComponent(componentType:string, componentId:string){
313 return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/restore', {})
316 archiveComponent(componentType:string, componentId:string){
317 return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/archive', {})
321 deleteInput(component:Component, input:InputBEModel):Observable<InputBEModel> {
322 return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/delete/' + input.uniqueId + '/input')
323 .map((res:Response) => {
324 return new InputBEModel(res.json());
328 updateComponentInputs(component:Component, inputs:InputBEModel[]):Observable<InputBEModel[]> {
329 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/update/inputs', inputs)
330 .map((res:Response) => {
331 return res.json().map((input) => new InputBEModel(input));
335 filterComponentInstanceProperties(component: Component, filterData:FilterPropertiesAssignmentData): Observable<InstanceBePropertiesMap> {//instance-property-be-map
336 let params: URLSearchParams = new URLSearchParams();
337 _.forEach(filterData.selectedTypes, (type:string) => {
338 params.append('resourceType', type);
341 return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/filteredproperties/' + filterData.propertyName, {search: params})
342 .map((res: Response) => {
347 createServiceProperty(component: Component, propertyModel:PropertyBEModel): Observable<PropertyBEModel> {
348 let serverObject = {};
349 serverObject[propertyModel.name] = propertyModel;
350 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties', serverObject)
352 let property:PropertyBEModel = new PropertyBEModel(res.json());
357 getServiceProperties(component: Component): Observable<Array<PropertyBEModel>> {
358 return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties')
359 .map((res: Response) => {
361 return new Array<PropertyBEModel>();
363 return CommonUtils.initBeProperties(res.json());
367 updateServiceProperties(component: Component, properties: PropertyBEModel[]) {
368 return this.http.put( this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties', properties)
369 .map((res: Response) => {
370 const resJson = res.json();
371 return _.map(resJson,
372 (resValue:PropertyBEModel) => new PropertyBEModel(resValue));
376 deleteServiceProperty(component:Component, property:PropertyBEModel):Observable<string> {
377 return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties/' + property.uniqueId )
378 .map((res:Response) => {
379 return property.uniqueId;
383 getDependencies(componentType:string, componentId: string):Observable<Array<IDependenciesServerResponse>> {
384 return this.http.get(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/dependencies')
385 .map((res:Response) => {
390 automatedUpgrade(componentType:string, componentId: string, componentsIdsToUpgrade:Array<IAutomatedUpgradeRequestObj>):Observable<AutomatedUpgradeGenericResponse> {
391 return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/automatedupgrade', componentsIdsToUpgrade)
392 .map((res:Response) => {
397 updateComponentInstance(component:Component, componentInstance:ComponentInstance):Observable<ComponentInstance> {
398 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstance/' + componentInstance.uniqueId, componentInstance)
399 .map((res:Response) => {
404 getServiceFilterConstraints(component:Component):Observable<ComponentGenericResponse> {
405 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [SERVICE_FIELDS.NODE_FILTER]);
408 createServiceFilterConstraints(component:Component, componentInstance:ComponentInstance, constraint:ConstraintObject):Observable<any> {
409 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter', constraint)
410 .map((res:Response) => {
415 updateServiceFilterConstraints(component:Component, componentInstance:ComponentInstance, constraints:Array<ConstraintObject>):Observable<any> {
416 return this.http.put(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter/', constraints)
417 .map((res:Response) => {
422 deleteServiceFilterConstraints(component:Component, componentInstance:ComponentInstance, constraintIndex:number) {
423 return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter/' + constraintIndex)
424 .map((res:Response) => {