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 {Inject, Injectable} from '@angular/core';
30 import {DataTypesService} from "app/services/data-types-service";
31 import {PROPERTY_DATA} from "app/utils";
32 import {DerivedFEAttribute} from "../../models/attributes-outputs/derived-fe-attribute";
33 import {ISdcConfig} from "../config/sdc-config.config.factory";
34 import {SdcConfigToken} from "../config/sdc-config.config";
35 import {HttpBackend, HttpClient, HttpHeaders} from "@angular/common/http";
36 import {Observable} from "rxjs/Observable";
37 import {AuthenticationService} from "./authentication.service";
39 /** This is a new service for NG2, to eventually replace app/services/data-types-service.ts
41 * This service is a singleton that holds a map of all DataTypes, recieved from server on load.
42 * It also contains convenience methods to check if a string is a valid dataType, and to retrieve a dataType's properties recursively
46 export class DataTypeService {
47 public dataTypes: DataTypesMap;
48 private readonly baseUrl: string;
49 private readonly dataTypeUrl: string;
50 private readonly dataTypeUploadUrl: string;
53 constructor(private dataTypeService: DataTypesService, private authService: AuthenticationService, private handler: HttpBackend, private httpClient: HttpClient, @Inject(SdcConfigToken) sdcConfig: ISdcConfig) {
54 this.dataTypes = dataTypeService.getAllDataTypes(); //This should eventually be replaced by an NG2 call to the backend instead of utilizing Angular1 downgraded component.
55 this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
56 this.dataTypeUrl = `${this.baseUrl}data-types`;
57 this.dataTypeUploadUrl = `${this.baseUrl}uploadType`;
58 this.httpClient = new HttpClient(handler);
61 public getDataTypeByModelAndTypeName(modelName: string, typeName: string): DataTypeModel {
62 this.dataTypes = this.dataTypeService.getAllDataTypesFromModel(modelName);
63 let dataTypeFound = this.dataTypes[typeName];
65 console.log("MISSING Datatype for model " + modelName + " and type: " + typeName);
70 public getDataTypeByTypeName(typeName: string): DataTypeModel {
71 if (!this.dataTypes) {
72 this.dataTypes = this.dataTypeService.getAllDataTypes();
74 if (!this.dataTypes[typeName]) console.log("MISSING Datatype: " + typeName);
75 return this.dataTypes[typeName];
78 public getDataTypeByModel(modelName: string): DataTypesMap {
79 return this.dataTypeService.getAllDataTypesFromModel(modelName);
82 public findAllDataTypesByModel(modelName: string): Promise<Map<string, DataTypeModel>> {
83 return this.dataTypeService.findAllDataTypesByModel(modelName);
86 public findById(id: string): Observable<DataTypeModel> {
87 const url = `${this.dataTypeUrl}/${id}`
88 return this.httpClient.get<DataTypeModel>(url);
91 public findAllProperties(id: string): Observable<Array<PropertyBEModel>> {
92 const url = `${this.dataTypeUrl}/${id}/properties`
93 return this.httpClient.get<Array<PropertyBEModel>>(url);
96 public createProperty(id: string, property: PropertyBEModel): Observable<PropertyBEModel> {
97 const url = `${this.dataTypeUrl}/${id}/properties`;
98 return this.httpClient.post<PropertyBEModel>(url, property);
101 public updateProperty(id: string, property: PropertyBEModel): Observable<PropertyBEModel> {
102 const url = `${this.dataTypeUrl}/${id}/properties`;
103 return this.httpClient.put<PropertyBEModel>(url, property);
106 public deleteProperty(dataTypeId: string, propertyId: string): Observable<Object> {
107 const url = `${this.dataTypeUrl}/${dataTypeId}/${propertyId}`;
108 let headers = new HttpHeaders({'USER_ID': this.authService.getLoggedinUser().userId});
109 let options = {headers: headers};
110 return this.httpClient.delete(url, options).map((res: Response) => {
115 public createImportedType(model: string, importingFile: File): Observable<any> {
116 const url = `${this.dataTypeUploadUrl}/datatypesyaml`;
117 const formData = new FormData();
118 formData.append('dataTypesYaml', importingFile);
119 formData.append('model', model != 'SDC AID' ? model : "")
120 formData.append('includeToModelImport', "true");
121 let headers = new HttpHeaders({'USER_ID': this.authService.getLoggedinUser().userId});
122 let options = {headers: headers};
124 return this.httpClient.post<any>(url, formData, options);
127 public getConstraintsByParentTypeAndUniqueID(rootPropertyType, propertyName) {
128 // const property = this.dataTypes[rootPropertyType].properties.filter(property =>
129 // property.name == propertyName);
130 // return property[0] && property[0].constraints ? property[0].constraints[0].validValues : null;
134 public getDerivedDataTypeProperties(dataTypeObj: DataTypeModel, propertiesArray: Array<DerivedFEProperty>, parentName: string) {
135 //push all child properties to array
136 if (!dataTypeObj) return;
137 if (dataTypeObj.properties) {
138 dataTypeObj.properties.forEach((derivedProperty) => {
139 if (dataTypeObj.name !== PROPERTY_DATA.OPENECOMP_ROOT || derivedProperty.name !== PROPERTY_DATA.SUPPLEMENTAL_DATA) {//The requirement is to not display the property supplemental_data
140 propertiesArray.push(new DerivedFEProperty(derivedProperty, parentName));
142 let derivedDataTypeObj: DataTypeModel = this.getDataTypeByTypeName(derivedProperty.type);
143 this.getDerivedDataTypeProperties(derivedDataTypeObj, propertiesArray, parentName + "#" + derivedProperty.name);
146 //recurse parent (derivedFrom), in case one of parents contains properties
147 if (dataTypeObj.derivedFrom && PROPERTY_DATA.ROOT_DATA_TYPE !== dataTypeObj.derivedFrom.name) {
148 this.getDerivedDataTypeProperties(dataTypeObj.derivedFrom, propertiesArray, parentName);
152 public getDerivedDataTypeAttributes(dataTypeObj: DataTypeModel, attributesArray: Array<DerivedFEAttribute>, parentName: string) {
153 //push all child properties to array
154 if (!dataTypeObj) return;
155 if (dataTypeObj.attributes) {
156 dataTypeObj.attributes.forEach((derivedAttribute) => {
157 if (dataTypeObj.name !== PROPERTY_DATA.OPENECOMP_ROOT || derivedAttribute.name !== PROPERTY_DATA.SUPPLEMENTAL_DATA) {//The requirement is to not display the property supplemental_data
158 attributesArray.push(new DerivedFEAttribute(derivedAttribute, parentName));
160 let derivedDataTypeObj: DataTypeModel = this.getDataTypeByTypeName(derivedAttribute.type);
161 this.getDerivedDataTypeAttributes(derivedDataTypeObj, attributesArray, parentName + "#" + derivedAttribute.name);
164 //recurse parent (derivedFrom), in case one of parents contains properties
165 if (dataTypeObj.derivedFrom && PROPERTY_DATA.ROOT_DATA_TYPE !== dataTypeObj.derivedFrom.name) {
166 this.getDerivedDataTypeAttributes(dataTypeObj.derivedFrom, attributesArray, parentName);
171 * Checks for custom behavior for a given data type by checking if a function exists within data-type.service with that name
172 * Additional custom behavior can be added by adding a function with the given dataType name
174 public checkForCustomBehavior = (property: PropertyFEModel) => {
175 let shortTypeName: string = property.type.split('.').pop();
176 if (this[shortTypeName]) {
177 this[shortTypeName](property); //execute function for given type, pass property as param
181 public Naming = (property: PropertyFEModel) => {
182 let generatedNamingVal: boolean = _.get(property.valueObj, 'ecomp_generated_naming', true);
183 property.flattenedChildren.forEach((prop) => {
184 if (prop.name == 'naming_policy') prop.hidden = !generatedNamingVal;
185 if (prop.name == 'instance_name') prop.hidden = generatedNamingVal;