[VID-6] Initial rebase push
[vid.git] / vid-app-common / src / main / webapp / app / vid / scripts / services / componentService.js
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * VID\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 "use strict";\r
22 \r
23 var ComponentService = function($log, COMPONENT, UtilityService) {\r
24 \r
25     var _this = this;\r
26 \r
27     var componentList = [ {\r
28         id : COMPONENT.NETWORK,\r
29         displayName : "Network"\r
30     }, {\r
31         id : COMPONENT.SERVICE,\r
32         displayName : "Service Instance"\r
33     }, {\r
34         id : COMPONENT.VNF,\r
35         displayName : "Virtual Network Function"\r
36     }, {\r
37         id : COMPONENT.VF_MODULE,\r
38         displayName : "VF Module"\r
39     }, {\r
40         id : COMPONENT.VOLUME_GROUP,\r
41         displayName : "Volume Group"\r
42     } ];\r
43 \r
44     var getInventoryInfo = function(suffix, inventoryItem) {\r
45         var pattern = new RegExp(suffix + "-");\r
46         for ( var key in inventoryItem) {\r
47             if (pattern.exec(key)) {\r
48                 return inventoryItem[key];\r
49             }\r
50         }\r
51     };\r
52 \r
53     /*\r
54      * Converts 'id' to a user friendly version.\r
55      * \r
56      * The algorithm used is:\r
57      * \r
58      * 1) If "id" found in COMPONENT.FULL_NAME_MAP, return the name found in the\r
59      * map.\r
60      * \r
61      * 2) Otherwise, if camel case, add "-" between camel case words.\r
62      * \r
63      * 3) Split id into multiple "partial names" assuming "-" is the delimiter.\r
64      * \r
65      * 4) Map any partial names found in COMPONENT.PARTIAL_NAME_MAP to the name\r
66      * found in the map.\r
67      * \r
68      * 5) Use partial names whenever not found in map.\r
69      * \r
70      * 5) Return name by combining all partial names with " " delimiter.\r
71      */\r
72     var getDisplayName = function(id) {\r
73         var tmp = COMPONENT.FULL_NAME_MAP[id.toLowerCase()];\r
74         if (UtilityService.hasContents(tmp)) {\r
75             return tmp;\r
76         }\r
77         /*\r
78          * Add "-" if camel case found.\r
79          */\r
80         var id = id.replace(/([a-z](?=[A-Z]))/g, '$1-');\r
81         var name = "";\r
82         var arg = id.split("-");\r
83         for (var i = 0; i < arg.length; i++) {\r
84             if (i > 0) {\r
85                 name += " ";\r
86             }\r
87             var tmp = COMPONENT.PARTIAL_NAME_MAP[arg[i].toLowerCase()];\r
88             if (UtilityService.hasContents(tmp)) {\r
89                 name += tmp;\r
90             } else {\r
91                 name += arg[i].slice(0, 1).toUpperCase() + arg[i].slice(1);\r
92             }\r
93         }\r
94         return name;\r
95     };\r
96 \r
97     return {\r
98         initialize : function(componentId) {\r
99             for (var i = 0; i < componentList.length; i++) {\r
100                 if (componentList[i].id === componentId) {\r
101                     _this.componentId = componentList[i].id;\r
102                     return componentId;\r
103                 }\r
104             }\r
105             throw "ComponentService:initializeComponent: componentId not found: "\r
106                     + componentId;\r
107         },\r
108         getComponentDisplayName : function() {\r
109             for (var i = 0; i < componentList.length; i++) {\r
110                 if (componentList[i].id === _this.componentId) {\r
111                     return componentList[i].displayName;\r
112                 }\r
113             }\r
114         },\r
115         getInventoryInfo : getInventoryInfo,\r
116         getInventoryParameterList : function(suffix, inventoryItem, isPartial) {\r
117                 console.log ("getInventoryParameterList" ); console.log ( JSON.stringify ( inventoryItem, null, 4));\r
118             var parameterList = new Array();\r
119             var pattern = new RegExp("network-id|network-name");\r
120             var pattern1 = new RegExp("neutron");\r
121         \r
122             if ( (suffix === COMPONENT.NETWORK) && (isPartial) ) {\r
123                 for ( var id in inventoryItem.object) {\r
124                                 if (pattern.exec(id) && (!(pattern1.exec(id))) ) {\r
125                                     parameterList.push({\r
126                                         id : id,\r
127                                         value : inventoryItem.object[id]\r
128                                     });\r
129                                 }\r
130                         }\r
131             }\r
132             else if (suffix === COMPONENT.NETWORK) {\r
133                 for ( var id in inventoryItem.object) {\r
134                         parameterList.push({\r
135                                 id : id,\r
136                                 value : inventoryItem.object[id]\r
137                         });\r
138                         }\r
139                 for ( var index in inventoryItem.subnets) {\r
140                         for (var fieldId in inventoryItem.subnets[index]) {\r
141                                 parameterList.push({\r
142                                         id : fieldId,\r
143                                         value : inventoryItem.subnets[index][fieldId]\r
144                                 });\r
145                         }\r
146                         }\r
147             }\r
148             else {\r
149                     for ( var id in inventoryItem) {\r
150                         //if (pattern.exec(id)) {\r
151                             parameterList.push({\r
152                                 id : id,\r
153                                 value : inventoryItem[id]\r
154                             });\r
155                         //}\r
156                     }\r
157             }\r
158             return parameterList;\r
159         },\r
160         getDisplayNames : function(inputList) {\r
161             var outputList = new Array();\r
162             for (var i = 0; i < inputList.length; i++) {\r
163                 var entry = angular.copy(inputList[i]);\r
164                 if (!UtilityService.hasContents(entry.name)) {\r
165                     entry.name = getDisplayName(entry.id);\r
166                 }\r
167                 outputList.push(entry);\r
168             }\r
169             return outputList;\r
170         },\r
171         getFieldDisplayName : function(name) {\r
172             return getDisplayName(name);\r
173         }\r
174     }\r
175 }\r
176 \r
177 appDS2.factory("ComponentService", [ "$log", "COMPONENT", "UtilityService",\r
178         ComponentService ]);\r