Catalog alignment
[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
29 export class CommonUtils {
30
31     static initProperties(propertiesObj:Array<PropertyModel>, uniqueId?:string):Array<PropertyModel> {
32
33         let properties = new Array<PropertyModel>();
34         if (propertiesObj) {
35             _.forEach(propertiesObj, (property:PropertyModel):void => {
36                 if (uniqueId) {
37                     property.readonly = property.parentUniqueId != uniqueId;
38                 }
39                 properties.push(new PropertyModel(property));
40             });
41         }
42         return properties;
43     };
44
45     static initAttributes(attributesObj:Array<AttributeModel>, uniqueId?:string):Array<AttributeModel> {
46
47         let attributes = new Array<AttributeModel>();
48         if (attributesObj) {
49             _.forEach(attributesObj, (attribute:AttributeModel):void => {
50                 if (uniqueId) {
51                     attribute.readonly = attribute.parentUniqueId != uniqueId;
52                 }
53                 attributes.push(new AttributeModel(attribute));
54             });
55         }
56         return attributes;
57     };
58
59     static initComponentInstances(componentInstanceObj:Array<ResourceInstance>):Array<ResourceInstance> {
60
61         let componentInstances = new Array<ResourceInstance>();
62         if (componentInstanceObj) {
63             _.forEach(componentInstanceObj, (instance:ResourceInstance):void => {
64                 componentInstances.push(ComponentInstanceFactory.createComponentInstance(instance));
65             });
66         }
67         return componentInstances;
68     };
69
70     static initModules(moduleArrayObj:Array<Module>):Array<Module> {
71
72         let modules = new Array<Module>();
73
74         if (moduleArrayObj) {
75             _.forEach(moduleArrayObj, (module:Module):void => {
76                 if (module.type === "org.openecomp.groups.VfModule") {
77                     modules.push(new Module(module));
78                 }
79             });
80         }
81         return modules;
82     };
83
84     static initInputs(inputsObj: Array<InputBEModel>): Array<InputBEModel> {
85
86         let inputs = new Array<InputBEModel>();
87
88         if(inputsObj) {
89             _.forEach(inputsObj, (input: InputBEModel):void => {
90                 inputs.push(new InputBEModel(input));
91             })
92         }
93
94         return inputs;
95     }
96
97     static initBeProperties(propertiesObj: Array<PropertyBEModel>): Array<PropertyBEModel> {
98
99         let properties = new Array<PropertyBEModel>();
100
101         if (propertiesObj) {
102             _.forEach(propertiesObj, (property: PropertyBEModel): void => {
103                 properties.push(new PropertyBEModel(property));
104             })
105         }
106
107         return properties;
108     }
109
110     static initComponentInstanceRelations = (componentInstanceRelationsObj:Array<RelationshipModel>):Array<RelationshipModel> => {
111         if (componentInstanceRelationsObj) {
112              let componentInstancesRelations: Array<RelationshipModel> = [];
113             _.forEach(componentInstanceRelationsObj, (instanceRelation:RelationshipModel):void => {
114                 componentInstancesRelations.push(new RelationshipModel(instanceRelation));
115             });
116             return componentInstancesRelations;
117         }
118     };
119
120     static initPolicies = (policiesObj: Array<PolicyInstance>):Array<PolicyInstance> => {
121         let policies = new Array<PolicyInstance>();
122
123         if (policiesObj) {
124             _.forEach(policiesObj, (policy: PolicyInstance): void => {
125                 policies.push(new PolicyInstance(policy));
126             })
127         }
128
129         return policies;
130     }
131     static initGroups = (groupsObj: Array<GroupInstance>):Array<GroupInstance> => {
132         let groups = new Array<GroupInstance>();
133
134         if(groupsObj) {
135             _.forEach(groupsObj, (group: GroupInstance):void => {
136                 groups.push(new GroupInstance(group));
137             });
138         }
139
140         return groups;
141     }
142
143     static initInterfaces(interfaces: Array<InterfaceModel>): Array<InterfaceModel> {
144
145         return _.map(interfaces, (interf: InterfaceModel) => {
146
147             return new InterfaceModel({
148                 type: interf.type,
149                 uniqueId: interf.uniqueId,
150                 operations: _.map(interf.operations,
151                     (operation: OperationModel) => {
152                         const newOperation = new OperationModel(operation);
153                         newOperation.interfaceType = interf.type;
154                         newOperation.interfaceId = interf.uniqueId;
155
156                         const {inputs, outputs} = operation;
157                         if (inputs) {
158                             newOperation.createInputsList(inputs.listToscaDataDefinition);
159                         }
160                         if (outputs) {
161                             newOperation.createOutputsList(outputs.listToscaDataDefinition);
162                         }
163
164                         return newOperation;
165                     }
166                 )
167             });
168
169         });
170     }
171     
172     static initInterfaceOperations(interfaces: Array<InterfaceModel>): Array<OperationModel> {
173
174         return _.reduce(interfaces, (acc, interf: InterfaceModel) => {
175
176             return acc.concat(
177                 _.map(interf.operations,
178                     (operation: OperationModel) => {
179                         const newOperation = new OperationModel(operation);
180                         newOperation.interfaceType = interf.type;
181                         newOperation.interfaceId = interf.uniqueId;
182
183                         const {inputs, outputs} = operation;
184                         if (inputs) {
185                             newOperation.createInputsList(inputs.listToscaDataDefinition);
186                         }
187                         if (outputs) {
188                             newOperation.createOutputsList(outputs.listToscaDataDefinition);
189                         }
190
191                         return newOperation;
192                     }
193                 )
194             );
195
196         }, []);
197     }
198
199 }
200