b4e184ab44be52be87e5e96ab5bc81feb3023e69
[sdc.git] / catalog-ui / src / app / utils / common-utils.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 import * as _ from "lodash";
22 import {Module, AttributeModel, ResourceInstance, PropertyModel, InputFEModel, OperationModel} from "../models";
23 import {ComponentInstanceFactory} from "./component-instance-factory";
24 import {InputBEModel, PropertyBEModel, RelationshipModel} from "app/models";
25 import { PolicyInstance } from "app/models/graph/zones/policy-instance";
26 import { GroupInstance } from "../models/graph/zones/group-instance";
27 import { InterfaceModel } from "../models/operation";
28 import {AttributeBEModel} from "../models/attributes-outputs/attribute-be-model";
29 import {OutputBEModel} from "../models/attributes-outputs/output-be-model";
30
31 export class CommonUtils {
32
33     static initProperties(propertiesObj:Array<PropertyModel>, uniqueId?:string):Array<PropertyModel> {
34
35         let properties = new Array<PropertyModel>();
36         if (propertiesObj) {
37             propertiesObj.forEach((property:PropertyModel):void => {
38                 if (uniqueId) {
39                     property.readonly = property.parentUniqueId != uniqueId;
40                 }
41                 properties.push(new PropertyModel(property));
42             });
43         }
44         return properties;
45     };
46
47     static initAttributes(attributesObj:Array<AttributeModel>, uniqueId?:string):Array<AttributeModel> {
48
49         let attributes = new Array<AttributeModel>();
50         if (attributesObj) {
51             attributesObj.forEach((attribute:AttributeModel):void => {
52                 if (uniqueId) {
53                     attribute.readonly = attribute.parentUniqueId != uniqueId;
54                 }
55                 attributes.push(new AttributeModel(attribute));
56             });
57         }
58         return attributes;
59     };
60
61     static initComponentInstances(componentInstanceObj:Array<ResourceInstance>):Array<ResourceInstance> {
62
63         let componentInstances = new Array<ResourceInstance>();
64         if (componentInstanceObj) {
65             componentInstanceObj.forEach((instance:ResourceInstance):void => {
66                 componentInstances.push(ComponentInstanceFactory.createComponentInstance(instance));
67             });
68         }
69         return componentInstances;
70     };
71
72     static initModules(moduleArrayObj:Array<Module>):Array<Module> {
73
74         let modules = new Array<Module>();
75
76         if (moduleArrayObj) {
77             moduleArrayObj.forEach((module:Module):void => {
78                 if (module.type === "org.openecomp.groups.VfModule") {
79                     modules.push(new Module(module));
80                 }
81             });
82         }
83         return modules;
84     };
85
86     static initInputs(inputsObj: Array<InputBEModel>): Array<InputBEModel> {
87
88         let inputs = new Array<InputBEModel>();
89
90         if(inputsObj) {
91             inputsObj.forEach((input: InputBEModel):void => {
92                 inputs.push(new InputBEModel(input));
93             })
94         }
95
96         return inputs;
97     }
98
99     static initOutputs(outputsObj: Array<OutputBEModel>): Array<OutputBEModel> {
100
101         let outputs = new Array<OutputBEModel>();
102
103         if(outputsObj) {
104             outputsObj.forEach((output: OutputBEModel):void => {
105                 outputs.push(new OutputBEModel(output));
106             })
107         }
108
109         return outputs;
110     }
111
112     static initBeProperties(propertiesObj: Array<PropertyBEModel>): Array<PropertyBEModel> {
113
114         let properties = new Array<PropertyBEModel>();
115
116         if (propertiesObj) {
117             propertiesObj.forEach((property: PropertyBEModel): void => {
118                 properties.push(new PropertyBEModel(property));
119             })
120         }
121
122         return properties;
123     }
124
125     static initBeAttributes(attributesObj: Array<AttributeBEModel>): Array<AttributeBEModel> {
126
127         let attributes = new Array<AttributeBEModel>();
128
129         if (attributesObj) {
130             attributesObj.forEach((attribute: AttributeBEModel): void => {
131                 attributes.push(new AttributeBEModel(attribute));
132             })
133         }
134
135         return attributes;
136     }
137
138     static initComponentInstanceRelations = (componentInstanceRelationsObj:Array<RelationshipModel>):Array<RelationshipModel> => {
139         if (componentInstanceRelationsObj) {
140              let componentInstancesRelations: Array<RelationshipModel> = [];
141             componentInstanceRelationsObj.forEach((instanceRelation:RelationshipModel):void => {
142                 componentInstancesRelations.push(new RelationshipModel(instanceRelation));
143             });
144             return componentInstancesRelations;
145         }
146     };
147
148     static initPolicies = (policiesObj: Array<PolicyInstance>):Array<PolicyInstance> => {
149         let policies = new Array<PolicyInstance>();
150
151         if (policiesObj) {
152             policiesObj.forEach((policy: PolicyInstance): void => {
153                 policies.push(new PolicyInstance(policy));
154             })
155         }
156
157         return policies;
158     }
159     static initGroups = (groupsObj: Array<GroupInstance>):Array<GroupInstance> => {
160         let groups = new Array<GroupInstance>();
161
162         if(groupsObj) {
163             groupsObj.forEach((group: GroupInstance):void => {
164                 groups.push(new GroupInstance(group));
165             });
166         }
167
168         return groups;
169     }
170
171     static initInterfaces(interfaces: Array<InterfaceModel>): Array<InterfaceModel> {
172
173         return _.map(interfaces, (interf: InterfaceModel) => {
174
175             return new InterfaceModel({
176                 type: interf.type,
177                 uniqueId: interf.uniqueId,
178                 operations: _.map(interf.operations,
179                     (operation: OperationModel) => {
180                         const newOperation = new OperationModel(operation);
181                         newOperation.interfaceType = interf.type;
182                         newOperation.interfaceId = interf.uniqueId;
183
184                         const {inputs, outputs} = operation;
185                         if (inputs) {
186                             newOperation.createInputsList(inputs.listToscaDataDefinition);
187                         }
188                         if (outputs) {
189                             newOperation.createOutputsList(outputs.listToscaDataDefinition);
190                         }
191
192                         return newOperation;
193                     }
194                 )
195             });
196
197         });
198     }
199     
200     static initInterfaceOperations(interfaces: Array<InterfaceModel>): Array<OperationModel> {
201
202         return _.reduce(interfaces, (acc, interf: InterfaceModel) => {
203
204             return acc.concat(
205                 _.map(interf.operations,
206                     (operation: OperationModel) => {
207                         const newOperation = new OperationModel(operation);
208                         newOperation.interfaceType = interf.type;
209                         newOperation.interfaceId = interf.uniqueId;
210
211                         const {inputs, outputs} = operation;
212                         if (inputs) {
213                             newOperation.createInputsList(inputs.listToscaDataDefinition);
214                         }
215                         if (outputs) {
216                             newOperation.createOutputsList(outputs.listToscaDataDefinition);
217                         }
218
219                         return newOperation;
220                     }
221                 )
222             );
223
224         }, []);
225     }
226
227 }
228