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';
23 import {DataTypeModel, DataTypesMap, PropertyFEModel, DerivedFEProperty, PropertyBEModel} from "app/models";
24 import { DataTypesService } from "app/services/data-types-service";
25 import { PROPERTY_DATA } from "app/utils";
26 import {DerivedFEAttribute} from "../../models/attributes-outputs/derived-fe-attribute";
27 import {ISdcConfig} from "../config/sdc-config.config.factory";
28 import {SdcConfigToken} from "../config/sdc-config.config";
29 import {HttpBackend, HttpClient, HttpHeaders} from "@angular/common/http";
30 import {Observable} from "rxjs/Observable";
31 import {AuthenticationService} from "./authentication.service";
33 /** This is a new service for NG2, to eventually replace app/services/data-types-service.ts
35 * This service is a singleton that holds a map of all DataTypes, recieved from server on load.
36 * It also contains convenience methods to check if a string is a valid dataType, and to retrieve a dataType's properties recursively
40 export class DataTypeService {
41 public dataTypes: DataTypesMap;
42 private readonly baseUrl: string;
43 private readonly dataTypeUrl: string;
44 private readonly dataTypeUploadUrl: string;
47 constructor(private dataTypeService: DataTypesService, private authService: AuthenticationService, private handler: HttpBackend, private httpClient: HttpClient, @Inject(SdcConfigToken) sdcConfig: ISdcConfig) {
48 this.dataTypes = dataTypeService.getAllDataTypes(); //This should eventually be replaced by an NG2 call to the backend instead of utilizing Angular1 downgraded component.
49 this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
50 this.dataTypeUrl = `${this.baseUrl}data-types`;
51 this.dataTypeUploadUrl = `${this.baseUrl}uploadType`;
52 this.httpClient = new HttpClient(handler);
55 public getDataTypeByModelAndTypeName(modelName: string, typeName: string): DataTypeModel {
56 this.dataTypes = this.dataTypeService.getAllDataTypesFromModel(modelName);
57 let dataTypeFound = this.dataTypes[typeName];
59 console.log("MISSING Datatype for model " + modelName + " and type: " + typeName);
64 public getDataTypeByTypeName(typeName: string): DataTypeModel {
66 this.dataTypes = this.dataTypeService.getAllDataTypes();
68 if (!this.dataTypes[typeName]) console.log("MISSING Datatype: " + typeName);
69 return this.dataTypes[typeName];
72 public getDataTypeByModel(modelName: string): DataTypesMap {
73 return this.dataTypeService.getAllDataTypesFromModel(modelName);
76 public findAllDataTypesByModel(modelName: string): Promise<Map<string, DataTypeModel>> {
77 return this.dataTypeService.findAllDataTypesByModel(modelName);
80 public findById(id: string): Observable<DataTypeModel> {
81 const url = `${this.dataTypeUrl}/${id}`
82 return this.httpClient.get<DataTypeModel>(url);
85 public findAllProperties(id: string): Observable<Array<PropertyBEModel>> {
86 const url = `${this.dataTypeUrl}/${id}/properties`
87 return this.httpClient.get<Array<PropertyBEModel>>(url);
90 public createProperty(id: string, property: PropertyBEModel): Observable<PropertyBEModel> {
91 const url = `${this.dataTypeUrl}/${id}/properties`;
92 return this.httpClient.post<PropertyBEModel>(url, property);
95 public updateProperty(id: string, property: PropertyBEModel): Observable<PropertyBEModel> {
96 const url = `${this.dataTypeUrl}/${id}/properties`;
97 return this.httpClient.put<PropertyBEModel>(url, property);
100 public createImportedType(model: string, importingFile: File): Observable<any> {
101 const url = `${this.dataTypeUploadUrl}/datatypesyaml`;
102 const formData = new FormData();
103 formData.append('dataTypesYaml', importingFile);
104 formData.append('model', model != 'SDC AID' ? model : "")
105 formData.append('includeToModelImport', "true");
106 let headers = new HttpHeaders({'USER_ID': this.authService.getLoggedinUser().userId});
107 let options = {headers: headers};
109 return this.httpClient.post<any>(url, formData, options);
112 public getConstraintsByParentTypeAndUniqueID(rootPropertyType, propertyName){
113 // const property = this.dataTypes[rootPropertyType].properties.filter(property =>
114 // property.name == propertyName);
115 // return property[0] && property[0].constraints ? property[0].constraints[0].validValues : null;
119 public getDerivedDataTypeProperties(dataTypeObj: DataTypeModel, propertiesArray: Array<DerivedFEProperty>, parentName: string) {
120 //push all child properties to array
121 if (!dataTypeObj) return;
122 if (dataTypeObj.properties) {
123 dataTypeObj.properties.forEach((derivedProperty) => {
124 if(dataTypeObj.name !== PROPERTY_DATA.OPENECOMP_ROOT || derivedProperty.name !== PROPERTY_DATA.SUPPLEMENTAL_DATA){//The requirement is to not display the property supplemental_data
125 propertiesArray.push(new DerivedFEProperty(derivedProperty, parentName));
127 let derivedDataTypeObj: DataTypeModel = this.getDataTypeByTypeName(derivedProperty.type);
128 this.getDerivedDataTypeProperties(derivedDataTypeObj, propertiesArray, parentName + "#" + derivedProperty.name);
131 //recurse parent (derivedFrom), in case one of parents contains properties
132 if (dataTypeObj.derivedFrom && PROPERTY_DATA.ROOT_DATA_TYPE !== dataTypeObj.derivedFrom.name) {
133 this.getDerivedDataTypeProperties(dataTypeObj.derivedFrom, propertiesArray, parentName);
137 public getDerivedDataTypeAttributes(dataTypeObj: DataTypeModel, attributesArray: Array<DerivedFEAttribute>, parentName: string) {
138 //push all child properties to array
139 if (!dataTypeObj) return;
140 if (dataTypeObj.attributes) {
141 dataTypeObj.attributes.forEach((derivedAttribute) => {
142 if(dataTypeObj.name !== PROPERTY_DATA.OPENECOMP_ROOT || derivedAttribute.name !== PROPERTY_DATA.SUPPLEMENTAL_DATA){//The requirement is to not display the property supplemental_data
143 attributesArray.push(new DerivedFEAttribute(derivedAttribute, parentName));
145 let derivedDataTypeObj: DataTypeModel = this.getDataTypeByTypeName(derivedAttribute.type);
146 this.getDerivedDataTypeAttributes(derivedDataTypeObj, attributesArray, parentName + "#" + derivedAttribute.name);
149 //recurse parent (derivedFrom), in case one of parents contains properties
150 if (dataTypeObj.derivedFrom && PROPERTY_DATA.ROOT_DATA_TYPE !== dataTypeObj.derivedFrom.name) {
151 this.getDerivedDataTypeAttributes(dataTypeObj.derivedFrom, attributesArray, parentName);
156 * Checks for custom behavior for a given data type by checking if a function exists within data-type.service with that name
157 * Additional custom behavior can be added by adding a function with the given dataType name
159 public checkForCustomBehavior = (property:PropertyFEModel) => {
160 let shortTypeName:string = property.type.split('.').pop();
161 if (this[shortTypeName]) {
162 this[shortTypeName](property); //execute function for given type, pass property as param
166 public Naming = (property: PropertyFEModel) => {
167 let generatedNamingVal: boolean = _.get(property.valueObj, 'ecomp_generated_naming', true);
168 property.flattenedChildren.forEach((prop) => {
169 if (prop.name == 'naming_policy') prop.hidden = !generatedNamingVal;
170 if (prop.name == 'instance_name') prop.hidden = generatedNamingVal;