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 {Injectable, Inject} from '@angular/core';
22 import { Observable } from 'rxjs/Observable';
23 import {PropertyFEModel, PropertyBEModel} from "app/models";
24 import {CommonUtils, ComponentType, ServerTypeUrl, ComponentInstanceFactory} from "app/utils";
25 import {Component, ComponentInstance, Capability, PropertyModel, ArtifactGroupModel, ArtifactModel, AttributeModel, IFileDownload} from "app/models";
26 import {SdcConfigToken, ISdcConfig} from "../../config/sdc-config.config";
27 import { HttpClient, HttpHeaders } from '@angular/common/http';
28 import { InputBEModel } from '../../../models/properties-inputs/input-be-model';
29 import { HttpHelperService } from '../http-hepler.service';
32 export class ComponentInstanceServiceNg2 {
36 constructor(private http: HttpClient, @Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
37 this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
40 private getServerTypeUrl = (componentType:string):string => {
41 switch (componentType) {
42 case ComponentType.SERVICE:
43 return ServerTypeUrl.SERVICES;
45 return ServerTypeUrl.RESOURCES;
48 getComponentInstanceProperties(component: Component, componentInstanceId: string): Observable<Array<PropertyBEModel>> {
49 return this.http.get<Array<PropertyBEModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/componentInstances/' + componentInstanceId + '/properties')
51 return CommonUtils.initBeProperties(res);
55 getComponentInstanceInputs(component: Component, componentInstance: ComponentInstance): Observable<Array<PropertyBEModel>> {
56 return this.http.get<Array<InputBEModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/componentInstances/' + componentInstance.uniqueId + '/' + componentInstance.componentUid + '/inputs')
58 return CommonUtils.initInputs(res);
62 getComponentInstanceArtifactsByGroupType = (componentType:string, componentId:string, componentInstanceId:string, artifactGroupType:string):Observable<ArtifactGroupModel> => {
64 return this.http.get<ArtifactGroupModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/resourceInstances/" + componentInstanceId + "/artifactsByType/" + artifactGroupType)
66 return new ArtifactGroupModel(res);
70 getArtifactByGroupType = (componentType:string, componentId:string, artifactGroupType:string):Observable<ArtifactGroupModel> => {
72 return this.http.get<ArtifactGroupModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/artifactsByType/" + artifactGroupType)
73 .map(response => new ArtifactGroupModel(response));
76 changeResourceInstanceVersion = (componentType:string, componentId:string, componentInstanceId:string, componentUid:string):Observable<ComponentInstance> => {
77 return this.http.post<ComponentInstance>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + componentInstanceId + '/changeVersion', {'componentUid': componentUid})
79 return ComponentInstanceFactory.createComponentInstance(res);
83 updateComponentInstance = (componentType:string, componentId:string, componentInstance:ComponentInstance):Observable<ComponentInstance> => {
84 return this.http.post<ComponentInstance>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + componentInstance.uniqueId, componentInstance.toJSON())
86 return ComponentInstanceFactory.createComponentInstance(response);
90 updateInstanceProperties(componentType:string, componentId:string, componentInstanceId: string, properties: PropertyBEModel[]) {
92 return this.http.post<Array<PropertyModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + componentInstanceId + '/properties', properties)
94 return res.map((resProperty) => {
95 let newProp = new PropertyModel(resProperty);
96 newProp.resourceInstanceUniqueId = componentInstanceId
102 getInstanceCapabilityProperties(componentType: string, componentId: string, componentInstanceId: string, capability: Capability): Observable<Array<PropertyModel>> {
104 return this.http.get<Array<PropertyModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/componentInstances/' + componentInstanceId + '/capability/' + capability.type +
105 '/capabilityName/' + capability.name + '/ownerId/' + capability.ownerId + '/properties')
107 capability.properties = res.map((capProp) => new PropertyModel(capProp)); // update capability properties
108 return capability.properties;
112 updateInstanceCapabilityProperties(component: Component, componentInstanceId: string, capability: Capability, properties: PropertyBEModel[]): Observable<Array<PropertyModel>> {
114 return this.http.put<Array<PropertyModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/componentInstances/' + componentInstanceId + '/capability/' + capability.type +
115 '/capabilityName/' + capability.name + '/ownerId/' + capability.ownerId + '/properties', properties)
117 const savedProperties: PropertyModel[] = res.map((resProperty) => new PropertyModel(resProperty));
118 savedProperties.forEach((savedProperty) => {
119 const propIdx = capability.properties.findIndex((p) => p.uniqueId === savedProperty.uniqueId);
120 if (propIdx !== -1) {
121 capability.properties.splice(propIdx, 1, savedProperty);
124 return savedProperties;
128 updateInstanceInputs(component: Component, componentInstanceId: string, inputs: PropertyBEModel[]): Observable<PropertyBEModel[]> {
130 return this.http.post<Array<PropertyModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstance/' + componentInstanceId + '/inputs', inputs)
132 return res.map((resInput) => new PropertyBEModel(resInput));
136 getComponentGroupInstanceProperties(component: Component, groupInstanceId: string): Observable<Array<PropertyBEModel>> {
137 return this.http.get<Array<PropertyBEModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/groups/' + groupInstanceId + '/properties')
139 return CommonUtils.initBeProperties(res);
143 updateComponentGroupInstanceProperties(componentType:string, componentId:string, groupInstanceId: string, properties: PropertyBEModel[]): Observable<Array<PropertyBEModel>> {
144 return this.http.put<Array<PropertyBEModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/groups/' + groupInstanceId + '/properties', properties)
146 return res.map((resProperty) => new PropertyBEModel(resProperty));
150 getComponentPolicyInstanceProperties(componentType:string, componentId:string, policyInstanceId: string): Observable<Array<PropertyBEModel>> {
151 return this.http.get<Array<PropertyBEModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/policies/' + policyInstanceId + '/properties')
153 return CommonUtils.initBeProperties(res);
157 updateComponentPolicyInstanceProperties(componentType:string, componentId:string, policyInstanceId: string, properties: PropertyBEModel[]): Observable<Array<PropertyBEModel>> {
158 return this.http.put<Array<PropertyBEModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/policies/' + policyInstanceId + '/properties', properties)
160 return res.map((resProperty) => new PropertyBEModel(resProperty));
164 addInstanceArtifact = (componentType:string, componentId:string, instanceId:string, artifact:ArtifactModel):Observable<ArtifactModel> => {
165 // let deferred = this.$q.defer<ArtifactModel>();
166 let headerObj = new HttpHeaders();
167 if (artifact.payloadData) {
168 headerObj = headerObj.set('Content-MD5', HttpHelperService.getHeaderMd5(artifact));
170 return this.http.post<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + instanceId + '/artifacts', JSON.stringify(artifact), {headers: headerObj})
172 return new ArtifactModel(res);
176 updateInstanceArtifact = (componentType:string, componentId:string, instanceId:string, artifact:ArtifactModel):Observable<ArtifactModel> => {
177 return this.http.post<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + instanceId + '/artifacts/' + artifact.uniqueId, artifact).map((res) => {
178 return new ArtifactModel(res);
182 deleteInstanceArtifact = (componentId:string, componentType:string, instanceId:string, artifactId:string, artifactLabel:string):Observable<ArtifactModel> => {
183 return this.http.delete<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/resourceInstance/" + instanceId + '/artifacts/' + artifactId + '?operation=' + artifactLabel)
185 return new ArtifactModel(res);
189 downloadInstanceArtifact = (componentType:string, componentId:string, instanceId:string, artifactId:string):Observable<IFileDownload> => {
190 return this.http.get<IFileDownload>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/resourceInstances/" + instanceId + "/artifacts/" + artifactId);
193 uploadInstanceEnvFile = (componentType:string, componentId:string, instanceId:string, artifact:ArtifactModel):Observable<ArtifactModel> => {
194 let headerObj = new HttpHeaders();
195 if (artifact.payloadData) {
196 headerObj = headerObj.set('Content-MD5', HttpHelperService.getHeaderMd5(artifact));
198 return this.http.post<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + instanceId + '/artifacts/' + artifact.uniqueId, JSON.stringify(artifact), {headers: headerObj});
202 updateInstanceAttribute = (componentType:string, componentId:string, attribute:AttributeModel):Observable<AttributeModel> => {
203 let instanceId = attribute.resourceInstanceUniqueId;
204 return this.http.post<AttributeModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/resourceInstance/" + instanceId + "/attribute", attribute)
206 let newAttribute = new AttributeModel(response);
207 newAttribute.readonly = true;
208 newAttribute.resourceInstanceUniqueId = instanceId;