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.getComponentInstanceInputsByIdAndType(component.uniqueId, component.componentType, componentInstance);
79 getComponentInstanceInputsByIdAndType(componentId: string, componentType:string, componentInstance: ComponentInstance): Observable<Array<PropertyBEModel>> {
80 return this.http.get<Array<InputBEModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/componentInstances/' + componentInstance.uniqueId + '/' + componentInstance.componentUid + '/inputs')
82 return CommonUtils.initInputs(res);
86 getComponentInstanceOutputs(component: Component, componentInstance: ComponentInstance): Observable<Array<AttributeBEModel>> {
87 return this.http.get<Array<OutputBEModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/componentInstances/' + componentInstance.uniqueId + '/' + componentInstance.componentUid + '/outputs')
89 return CommonUtils.initOutputs(res);
93 getComponentInstanceArtifactsByGroupType = (componentType:string, componentId:string, componentInstanceId:string, artifactGroupType:string):Observable<ArtifactGroupModel> => {
95 return this.http.get<ArtifactGroupModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/resourceInstances/" + componentInstanceId + "/artifactsByType/" + artifactGroupType)
97 return new ArtifactGroupModel(res);
101 getArtifactByGroupType = (componentType:string, componentId:string, artifactGroupType:string):Observable<ArtifactGroupModel> => {
103 return this.http.get<ArtifactGroupModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/artifactsByType/" + artifactGroupType)
104 .map(response => new ArtifactGroupModel(response));
107 changeResourceInstanceVersion = (componentType:string, componentId:string, componentInstanceId:string, componentUid:string):Observable<ComponentInstance> => {
108 return this.http.post<ComponentInstance>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + componentInstanceId + '/changeVersion', {'componentUid': componentUid})
110 return ComponentInstanceFactory.createComponentInstance(res);
114 updateComponentInstance = (componentType:string, componentId:string, componentInstance:ComponentInstance):Observable<ComponentInstance> => {
115 return this.http.post<ComponentInstance>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + componentInstance.uniqueId, componentInstance.toJSON())
117 return ComponentInstanceFactory.createComponentInstance(response);
121 updateInstanceProperties(componentType:string, componentId:string, componentInstanceId: string, properties: PropertyBEModel[]) {
123 return this.http.post<Array<PropertyModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + componentInstanceId + '/properties', properties)
125 return res.map((resProperty) => {
126 let newProp = new PropertyModel(resProperty);
127 newProp.resourceInstanceUniqueId = componentInstanceId;
133 updateInstanceAttributes(componentType:string, componentId:string, componentInstanceId: string, attributes: AttributeBEModel[]) {
135 return this.http.post<Array<AttributeModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + componentInstanceId + '/attributes', attributes)
137 return res.map((resAttribute) => {
138 let newAttrib = new AttributeModel(resAttribute);
139 newAttrib.resourceInstanceUniqueId = componentInstanceId;
145 getInstanceCapabilityProperties(componentType: string, componentId: string, componentInstanceId: string, capability: Capability): Observable<Array<PropertyModel>> {
147 return this.http.get<Array<PropertyModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/componentInstances/' + componentInstanceId + '/capability/' + capability.type +
148 '/capabilityName/' + capability.name + '/ownerId/' + capability.ownerId + '/properties')
150 capability.properties = res.map((capProp) => new PropertyModel(capProp)); // update capability properties
151 return capability.properties;
155 updateInstanceCapabilityProperties(component: Component, componentInstanceId: string, capability: Capability, properties: PropertyBEModel[]): Observable<Array<PropertyModel>> {
157 return this.http.put<Array<PropertyModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/componentInstances/' + componentInstanceId + '/capability/' + capability.type +
158 '/capabilityName/' + capability.name + '/ownerId/' + capability.ownerId + '/properties', properties)
160 const savedProperties: PropertyModel[] = res.map((resProperty) => new PropertyModel(resProperty));
161 savedProperties.forEach((savedProperty) => {
162 const propIdx = capability.properties.findIndex((p) => p.uniqueId === savedProperty.uniqueId);
163 if (propIdx !== -1) {
164 capability.properties.splice(propIdx, 1, savedProperty);
167 return savedProperties;
171 updateInstanceRequirement(componentType: string, componentId: string, componentInstanceId: string, requirement: Requirement): Observable<Requirement> {
172 return this.http.put<Requirement>(this.baseUrl + componentType + componentId + '/componentInstances/' + componentInstanceId + '/requirement/' + requirement.capability +
173 '/requirementName/' + requirement.name, requirement);
176 updateInstanceCapability(componentTypeUrl: string, componentId: string, componentInstanceId: string, capability: Capability): Observable<Capability> {
177 const url = `${this.baseUrl}${componentTypeUrl}${componentId}/componentInstances/${componentInstanceId}/capability/`;
178 return this.http.put<Capability>(url, capability);
181 updateInstanceInputs(component: Component, componentInstanceId: string, inputs: PropertyBEModel[]): Observable<PropertyBEModel[]> {
183 return this.http.post<Array<PropertyModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstance/' + componentInstanceId + '/inputs', inputs)
185 return res.map((resInput) => new PropertyBEModel(resInput));
189 updateInstanceOutputs(component: Component, componentInstanceId: string, outputs: AttributeBEModel[]): Observable<AttributeBEModel[]> {
191 return this.http.post<Array<AttributeModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstance/' + componentInstanceId + '/outputs', outputs)
193 return res.map((resOutput) => new AttributeBEModel(resOutput));
197 getComponentGroupInstanceProperties(component: Component, groupInstanceId: string): Observable<Array<PropertyBEModel>> {
198 return this.http.get<Array<PropertyBEModel>>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/groups/' + groupInstanceId + '/properties')
200 return CommonUtils.initBeProperties(res);
204 updateComponentGroupInstanceProperties(componentType:string, componentId:string, groupInstanceId: string, properties: PropertyBEModel[]): Observable<Array<PropertyBEModel>> {
205 return this.http.put<Array<PropertyBEModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/groups/' + groupInstanceId + '/properties', properties)
207 return res.map((resProperty) => new PropertyBEModel(resProperty));
211 getComponentPolicyInstanceProperties(componentType:string, componentId:string, policyInstanceId: string): Observable<Array<PropertyBEModel>> {
212 return this.http.get<Array<PropertyBEModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/policies/' + policyInstanceId + '/properties')
214 return CommonUtils.initBeProperties(res);
218 updateComponentPolicyInstanceProperties(componentType:string, componentId:string, policyInstanceId: string, properties: PropertyBEModel[]): Observable<Array<PropertyBEModel>> {
219 return this.http.put<Array<PropertyBEModel>>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/policies/' + policyInstanceId + '/properties', properties)
221 return res.map((resProperty) => new PropertyBEModel(resProperty));
225 addInstanceArtifact = (componentType:string, componentId:string, instanceId:string, artifact:ArtifactModel):Observable<ArtifactModel> => {
226 let headerObj = new HttpHeaders();
227 if (artifact.payloadData) {
228 headerObj = headerObj.set('Content-MD5', HttpHelperService.getHeaderMd5(artifact));
230 return this.http.post<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + instanceId + '/artifacts', JSON.stringify(artifact), {headers: headerObj})
232 return new ArtifactModel(res);
236 updateInstanceArtifact = (componentType:string, componentId:string, instanceId:string, artifact:ArtifactModel):Observable<ArtifactModel> => {
237 return this.http.post<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + instanceId + '/artifacts/' + artifact.uniqueId, artifact).map((res) => {
238 return new ArtifactModel(res);
242 deleteInstanceArtifact = (componentId:string, componentType:string, instanceId:string, artifactId:string, artifactLabel:string):Observable<ArtifactModel> => {
243 return this.http.delete<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/resourceInstance/" + instanceId + '/artifacts/' + artifactId + '?operation=' + artifactLabel)
245 return new ArtifactModel(res);
249 downloadInstanceArtifact = (componentType:string, componentId:string, instanceId:string, artifactId:string):Observable<IFileDownload> => {
250 return this.http.get<IFileDownload>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/resourceInstances/" + instanceId + "/artifacts/" + artifactId);
253 uploadInstanceEnvFile = (componentType:string, componentId:string, instanceId:string, artifact:ArtifactModel):Observable<ArtifactModel> => {
254 let headerObj = new HttpHeaders();
255 if (artifact.payloadData) {
256 headerObj = headerObj.set('Content-MD5', HttpHelperService.getHeaderMd5(artifact));
258 return this.http.post<ArtifactModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/resourceInstance/' + instanceId + '/artifacts/' + artifact.uniqueId, JSON.stringify(artifact), {headers: headerObj});
262 updateInstanceAttribute = (componentType:string, componentId:string, attribute:AttributeModel):Observable<AttributeModel> => {
263 let instanceId = attribute.resourceInstanceUniqueId;
264 return this.http.post<AttributeModel>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "/resourceInstance/" + instanceId + "/attribute", attribute)
266 let newAttribute = new AttributeModel(response);
267 newAttribute.readonly = true;
268 newAttribute.resourceInstanceUniqueId = instanceId;