615f25ec28f67c0af4e249b666fbef94d610d3c8
[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.OLDVERSION,\r
35                 displayName : "Previous Version"\r
36         }, {\r
37                 id : COMPONENT.VNF,\r
38                 displayName : "Virtual Network Function"\r
39     }, {\r
40                 id : COMPONENT.VF_MODULE,\r
41                 displayName : "VF Module"\r
42     }, {\r
43         id: COMPONENT.VOLUME_GROUP,\r
44         displayName: "Volume Group"\r
45     }, {\r
46         id : COMPONENT.CONFIGURATION,\r
47         displayName : "Port Mirroring Configuration"\r
48     } ];\r
49 \r
50     var getInventoryInfo = function(suffix, inventoryItem) {\r
51         var pattern = new RegExp(suffix + "-");\r
52         for ( var key in inventoryItem) {\r
53             if (pattern.exec(key)) {\r
54                 return inventoryItem[key];\r
55             }\r
56         }\r
57     };\r
58 \r
59     /*\r
60      * Converts 'id' to a user friendly version.\r
61      * \r
62      * The algorithm used is:\r
63      * \r
64      * 1) If "id" found in COMPONENT.FULL_NAME_MAP, return the name found in the\r
65      * map.\r
66      * \r
67      * 2) Otherwise, if camel case, add "-" between camel case words.\r
68      * \r
69      * 3) Split id into multiple "partial names" assuming "-" is the delimiter.\r
70      * \r
71      * 4) Map any partial names found in COMPONENT.PARTIAL_NAME_MAP to the name\r
72      * found in the map.\r
73      * \r
74      * 5) Use partial names whenever not found in map.\r
75      * \r
76      * 5) Return name by combining all partial names with " " delimiter.\r
77      */\r
78     var getDisplayName = function(id) {\r
79         var tmp = COMPONENT.FULL_NAME_MAP[id.toLowerCase()];\r
80         if (UtilityService.hasContents(tmp)) {\r
81             return tmp;\r
82         }\r
83         /*\r
84          * Add "-" if camel case found.\r
85          */\r
86         var id = id.replace(/([a-z](?=[A-Z]))/g, '$1-');\r
87         var name = "";\r
88         var arg = id.split("-");\r
89         for (var i = 0; i < arg.length; i++) {\r
90             if (i > 0) {\r
91                 name += " ";\r
92             }\r
93             var tmp = COMPONENT.PARTIAL_NAME_MAP[arg[i].toLowerCase()];\r
94             if (UtilityService.hasContents(tmp)) {\r
95                 name += tmp;\r
96             } else {\r
97                 name += arg[i].slice(0, 1).toUpperCase() + arg[i].slice(1);\r
98             }\r
99         }\r
100         return name;\r
101     };\r
102 \r
103     return {\r
104         initialize : function(componentId) {\r
105             for (var i = 0; i < componentList.length; i++) {\r
106                 if (componentList[i].id === componentId) {\r
107                     _this.componentId = componentList[i].id;\r
108                     return componentId;\r
109                 }\r
110             }\r
111             throw "ComponentService:initializeComponent: componentId not found: "\r
112                     + componentId;\r
113         },\r
114         getComponentDisplayName : function() {\r
115             for (var i = 0; i < componentList.length; i++) {\r
116                 if (componentList[i].id === _this.componentId) {\r
117                     return componentList[i].displayName;\r
118                 }\r
119             }\r
120         },\r
121         getInventoryInfo : getInventoryInfo,\r
122         getInventoryParameterList : function(suffix, inventoryItem, isPartial) {\r
123                 console.log ("getInventoryParameterList" ); console.log ( JSON.stringify ( inventoryItem, null, 4));\r
124             var parameterList = new Array();\r
125             var pattern = new RegExp("network-id|network-name");\r
126             var pattern1 = new RegExp("neutron");\r
127         \r
128             if ( (suffix === COMPONENT.NETWORK) && (isPartial) ) {\r
129                 for ( var id in inventoryItem.object) {\r
130                                 if (pattern.exec(id) && (!(pattern1.exec(id))) ) {\r
131                                     parameterList.push({\r
132                                         id : id,\r
133                                         value : inventoryItem.object[id]\r
134                                     });\r
135                                 }\r
136                         }\r
137             }\r
138             else if (suffix === COMPONENT.NETWORK) {\r
139                 for ( var id in inventoryItem.object) {\r
140                         parameterList.push({\r
141                                 id : id,\r
142                                 value : inventoryItem.object[id]\r
143                         });\r
144                         }\r
145                 for ( var index in inventoryItem.subnets) {\r
146                         for (var fieldId in inventoryItem.subnets[index]) {\r
147                                 parameterList.push({\r
148                                         id : fieldId,\r
149                                         value : inventoryItem.subnets[index][fieldId]\r
150                                 });\r
151                         }\r
152                         }\r
153             }\r
154             else {\r
155                     for ( var id in inventoryItem) {\r
156                         //if (pattern.exec(id)) {\r
157                             parameterList.push({\r
158                                 id : id,\r
159                                 value : inventoryItem[id]\r
160                             });\r
161                         //}\r
162                     }\r
163             }\r
164             return parameterList;\r
165         },\r
166         getDisplayNames : function(inputList) {\r
167             var outputList = new Array();\r
168             for (var i = 0; i < inputList.length; i++) {\r
169                 var entry = angular.copy(inputList[i]);\r
170                 if (!UtilityService.hasContents(entry.name)) {\r
171                     entry.name = getDisplayName(entry.id);\r
172                 }\r
173                 outputList.push(entry);\r
174             }\r
175             return outputList;\r
176         },\r
177         getFieldDisplayName : function(name) {\r
178             return getDisplayName(name);\r
179         }\r
180     }\r
181 }\r
182 \r
183 appDS2.factory("ComponentService", [ "$log", "COMPONENT", "UtilityService",\r
184         ComponentService ]);\r