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 {Inject, Injectable} from '@angular/core';
22 import {Observable} from 'rxjs/Observable';
35 import {CommonUtils, ComponentInstanceFactory, ComponentType, ServerTypeUrl} from "app/utils";
36 import {ISdcConfig, SdcConfigToken} from "../../config/sdc-config.config";
37 import {HttpClient, HttpHeaders} from '@angular/common/http';
38 import {InputBEModel} from '../../../models/properties-inputs/input-be-model';
39 import {HttpHelperService} from '../http-hepler.service';
40 import {AttributeBEModel} from "../../../models/attributes-outputs/attribute-be-model";
41 import {OutputBEModel} from "../../../models/attributes-outputs/output-be-model";
44 export class ComponentInstanceServiceNg2 {
48 constructor(private http: HttpClient, @Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
49 this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
52 private getServerTypeUrl = (componentType:string):string => {
53 switch (componentType) {
54 case ComponentType.SERVICE:
55 return ServerTypeUrl.SERVICES;
57 return ServerTypeUrl.RESOURCES;
60 getComponentInstanceProperties(component: Component, componentInstanceId: string): Observable<Array<PropertyBEModel>> {
61 return this.http.get<Array<PropertyBEModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/componentInstances/' + componentInstanceId + '/properties')
63 return CommonUtils.initBeProperties(res);
67 getComponentInstanceAttributes(component: Component, componentInstanceId: string): Observable<Array<AttributeBEModel>> {
68 return this.http.get<Array<AttributeBEModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/componentInstances/' + componentInstanceId + '/attributes')
70 return CommonUtils.initBeAttributes(res);
74 getComponentInstanceInputs(component: Component, componentInstance: ComponentInstance): Observable<Array<PropertyBEModel>> {
75 return this.http.get<Array<InputBEModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/componentInstances/' + componentInstance.uniqueId + '/' + componentInstance.componentUid + '/inputs')
77 return CommonUtils.initInputs(res);
81 getComponentInstanceOutputs(component: Component, componentInstance: ComponentInstance): Observable<Array<AttributeBEModel>> {
82 return this.http.get<Array<OutputBEModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/componentInstances/' + componentInstance.uniqueId + '/' + componentInstance.componentUid + '/outputs')
84 return CommonUtils.initOutputs(res);
88 getComponentInstanceArtifactsByGroupType = (componentType:string, componentId:string, componentInstanceId:string, artifactGroupType:string):Observable<ArtifactGroupModel> => {
90 return this.http.get<ArtifactGroupModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/resourceInstances/" + componentInstanceId + "/artifactsByType/" + artifactGroupType)
92 return new ArtifactGroupModel(res);
96 getArtifactByGroupType = (componentType:string, componentId:string, artifactGroupType:string):Observable<ArtifactGroupModel> => {
98 return this.http.get<ArtifactGroupModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/artifactsByType/" + artifactGroupType)
99 .map(response => new ArtifactGroupModel(response));
102 changeResourceInstanceVersion = (componentType:string, componentId:string, componentInstanceId:string, componentUid:string):Observable<ComponentInstance> => {
103 return this.http.post<ComponentInstance>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + componentInstanceId + '/changeVersion', {'componentUid': componentUid})
105 return ComponentInstanceFactory.createComponentInstance(res);
109 updateComponentInstance = (componentType:string, componentId:string, componentInstance:ComponentInstance):Observable<ComponentInstance> => {
110 return this.http.post<ComponentInstance>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + componentInstance.uniqueId, componentInstance.toJSON())
112 return ComponentInstanceFactory.createComponentInstance(response);
116 updateInstanceProperties(componentType:string, componentId:string, componentInstanceId: string, properties: PropertyBEModel[]) {
118 return this.http.post<Array<PropertyModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + componentInstanceId + '/properties', properties)
120 return res.map((resProperty) => {
121 let newProp = new PropertyModel(resProperty);
122 newProp.resourceInstanceUniqueId = componentInstanceId;
128 updateInstanceAttributes(componentType:string, componentId:string, componentInstanceId: string, attributes: AttributeBEModel[]) {
130 return this.http.post<Array<AttributeModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + componentInstanceId + '/attributes', attributes)
132 return res.map((resAttribute) => {
133 let newAttrib = new AttributeModel(resAttribute);
134 newAttrib.resourceInstanceUniqueId = componentInstanceId;
140 getInstanceCapabilityProperties(componentType: string, componentId: string, componentInstanceId: string, capability: Capability): Observable<Array<PropertyModel>> {
142 return this.http.get<Array<PropertyModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/componentInstances/' + componentInstanceId + '/capability/' + capability.type +
143 '/capabilityName/' + capability.name + '/ownerId/' + capability.ownerId + '/properties')
145 capability.properties = res.map((capProp) => new PropertyModel(capProp)); // update capability properties
146 return capability.properties;
150 updateInstanceCapabilityProperties(component: Component, componentInstanceId: string, capability: Capability, properties: PropertyBEModel[]): Observable<Array<PropertyModel>> {
152 return this.http.put<Array<PropertyModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/componentInstances/' + componentInstanceId + '/capability/' + capability.type +
153 '/capabilityName/' + capability.name + '/ownerId/' + capability.ownerId + '/properties', properties)
155 const savedProperties: PropertyModel[] = res.map((resProperty) => new PropertyModel(resProperty));
156 savedProperties.forEach((savedProperty) => {
157 const propIdx = capability.properties.findIndex((p) => p.uniqueId === savedProperty.uniqueId);
158 if (propIdx !== -1) {
159 capability.properties.splice(propIdx, 1, savedProperty);
162 return savedProperties;
166 updateInstanceRequirement(componentType: string, componentId: string, componentInstanceId: string, requirement: Requirement): Observable<Requirement> {
167 return this.http.put<Requirement>(this.baseUrl + componentType + componentId + '/componentInstances/' + componentInstanceId + '/requirement/' + requirement.capability +
168 '/requirementName/' + requirement.name, requirement);
171 updateInstanceCapability(componentTypeUrl: string, componentId: string, componentInstanceId: string, capability: Capability): Observable<Capability> {
172 const url = `${this.baseUrl}${componentTypeUrl}${componentId}/componentInstances/${componentInstanceId}/capability/`;
173 return this.http.put<Capability>(url, capability);
176 updateInstanceInputs(component: Component, componentInstanceId: string, inputs: PropertyBEModel[]): Observable<PropertyBEModel[]> {
178 return this.http.post<Array<PropertyModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstance/' + componentInstanceId + '/inputs', inputs)
180 return res.map((resInput) => new PropertyBEModel(resInput));
184 updateInstanceOutputs(component: Component, componentInstanceId: string, outputs: AttributeBEModel[]): Observable<AttributeBEModel[]> {
186 return this.http.post<Array<AttributeModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstance/' + componentInstanceId + '/outputs', outputs)
188 return res.map((resOutput) => new AttributeBEModel(resOutput));
192 getComponentGroupInstanceProperties(component: Component, groupInstanceId: string): Observable<Array<PropertyBEModel>> {
193 return this.http.get<Array<PropertyBEModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/groups/' + groupInstanceId + '/properties')
195 return CommonUtils.initBeProperties(res);
199 updateComponentGroupInstanceProperties(componentType:string, componentId:string, groupInstanceId: string, properties: PropertyBEModel[]): Observable<Array<PropertyBEModel>> {
200 return this.http.put<Array<PropertyBEModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/groups/' + groupInstanceId + '/properties', properties)
202 return res.map((resProperty) => new PropertyBEModel(resProperty));
206 getComponentPolicyInstanceProperties(componentType:string, componentId:string, policyInstanceId: string): Observable<Array<PropertyBEModel>> {
207 return this.http.get<Array<PropertyBEModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/policies/' + policyInstanceId + '/properties')
209 return CommonUtils.initBeProperties(res);
213 updateComponentPolicyInstanceProperties(componentType:string, componentId:string, policyInstanceId: string, properties: PropertyBEModel[]): Observable<Array<PropertyBEModel>> {
214 return this.http.put<Array<PropertyBEModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/policies/' + policyInstanceId + '/properties', properties)
216 return res.map((resProperty) => new PropertyBEModel(resProperty));
220 addInstanceArtifact = (componentType:string, componentId:string, instanceId:string, artifact:ArtifactModel):Observable<ArtifactModel> => {
221 let headerObj = new HttpHeaders();
222 if (artifact.payloadData) {
223 headerObj = headerObj.set('Content-MD5', HttpHelperService.getHeaderMd5(artifact));
225 return this.http.post<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + instanceId + '/artifacts', JSON.stringify(artifact), {headers: headerObj})
227 return new ArtifactModel(res);
231 updateInstanceArtifact = (componentType:string, componentId:string, instanceId:string, artifact:ArtifactModel):Observable<ArtifactModel> => {
232 return this.http.post<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + instanceId + '/artifacts/' + artifact.uniqueId, artifact).map((res) => {
233 return new ArtifactModel(res);
237 deleteInstanceArtifact = (componentId:string, componentType:string, instanceId:string, artifactId:string, artifactLabel:string):Observable<ArtifactModel> => {
238 return this.http.delete<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/resourceInstance/" + instanceId + '/artifacts/' + artifactId + '?operation=' + artifactLabel)
240 return new ArtifactModel(res);
244 downloadInstanceArtifact = (componentType:string, componentId:string, instanceId:string, artifactId:string):Observable<IFileDownload> => {
245 return this.http.get<IFileDownload>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/resourceInstances/" + instanceId + "/artifacts/" + artifactId);
248 uploadInstanceEnvFile = (componentType:string, componentId:string, instanceId:string, artifact:ArtifactModel):Observable<ArtifactModel> => {
249 let headerObj = new HttpHeaders();
250 if (artifact.payloadData) {
251 headerObj = headerObj.set('Content-MD5', HttpHelperService.getHeaderMd5(artifact));
253 return this.http.post<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + instanceId + '/artifacts/' + artifact.uniqueId, JSON.stringify(artifact), {headers: headerObj});
257 updateInstanceAttribute = (componentType:string, componentId:string, attribute:AttributeModel):Observable<AttributeModel> => {
258 let instanceId = attribute.resourceInstanceUniqueId;
259 return this.http.post<AttributeModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/resourceInstance/" + instanceId + "/attribute", attribute)
261 let newAttribute = new AttributeModel(response);
262 newAttribute.readonly = true;
263 newAttribute.resourceInstanceUniqueId = instanceId;