actually adding the files to the initial commit
[vid.git] / vid / src / main / webapp / app / vid / scripts / services / componentService.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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 "use strict";
22
23 var ComponentService = function($log, COMPONENT, UtilityService) {
24
25     var _this = this;
26
27     var componentList = [ {
28         id : COMPONENT.NETWORK,
29         displayName : "Network"
30     }, {
31         id : COMPONENT.SERVICE,
32         displayName : "Service Instance"
33     }, {
34         id : COMPONENT.VNF,
35         displayName : "Virtual Network Function"
36     }, {
37         id : COMPONENT.VF_MODULE,
38         displayName : "VF Module"
39     }, {
40         id : COMPONENT.VOLUME_GROUP,
41         displayName : "Volume Group"
42     } ];
43
44     var getInventoryInfo = function(suffix, inventoryItem) {
45         var pattern = new RegExp(suffix + "-");
46         for ( var key in inventoryItem) {
47             if (pattern.exec(key)) {
48                 return inventoryItem[key];
49             }
50         }
51     };
52
53     /*
54      * Converts 'id' to a user friendly version.
55      * 
56      * The algorithm used is:
57      * 
58      * 1) If "id" found in COMPONENT.FULL_NAME_MAP, return the name found in the
59      * map.
60      * 
61      * 2) Otherwise, if camel case, add "-" between camel case words.
62      * 
63      * 3) Split id into multiple "partial names" assuming "-" is the delimiter.
64      * 
65      * 4) Map any partial names found in COMPONENT.PARTIAL_NAME_MAP to the name
66      * found in the map.
67      * 
68      * 5) Use partial names whenever not found in map.
69      * 
70      * 5) Return name by combining all partial names with " " delimiter.
71      */
72     var getDisplayName = function(id) {
73         var tmp = COMPONENT.FULL_NAME_MAP[id.toLowerCase()];
74         if (UtilityService.hasContents(tmp)) {
75             return tmp;
76         }
77         /*
78          * Add "-" if camel case found.
79          */
80         var id = id.replace(/([a-z](?=[A-Z]))/g, '$1-');
81         var name = "";
82         var arg = id.split("-");
83         for (var i = 0; i < arg.length; i++) {
84             if (i > 0) {
85                 name += " ";
86             }
87             var tmp = COMPONENT.PARTIAL_NAME_MAP[arg[i].toLowerCase()];
88             if (UtilityService.hasContents(tmp)) {
89                 name += tmp;
90             } else {
91                 name += arg[i].slice(0, 1).toUpperCase() + arg[i].slice(1);
92             }
93         }
94         return name;
95     };
96
97     return {
98         initialize : function(componentId) {
99             for (var i = 0; i < componentList.length; i++) {
100                 if (componentList[i].id === componentId) {
101                     _this.componentId = componentList[i].id;
102                     return componentId;
103                 }
104             }
105             throw "ComponentService:initializeComponent: componentId not found: "
106                     + componentId;
107         },
108         getComponentDisplayName : function() {
109             for (var i = 0; i < componentList.length; i++) {
110                 if (componentList[i].id === _this.componentId) {
111                     return componentList[i].displayName;
112                 }
113             }
114         },
115         getInventoryInfo : getInventoryInfo,
116         getInventoryParameterList : function(suffix, inventoryItem) {
117             var parameterList = new Array();
118         //    var pattern = new RegExp("-[intv][a-z]*$");
119         //    var inventoryInfo = getInventoryInfo(suffix, inventoryItem);
120             for ( var id in inventoryItem) {
121                 //if (pattern.exec(id)) {
122                     parameterList.push({
123                         id : id,
124                         value : inventoryItem[id]
125                     });
126                 //}
127             }
128             return parameterList;
129         },
130         getDisplayNames : function(inputList) {
131             var outputList = new Array();
132             for (var i = 0; i < inputList.length; i++) {
133                 var entry = angular.copy(inputList[i]);
134                 if (!UtilityService.hasContents(entry.name)) {
135                     entry.name = getDisplayName(entry.id);
136                 }
137                 outputList.push(entry);
138             }
139             return outputList;
140         },
141         getFieldDisplayName : function(name) {
142             return getDisplayName(name);
143         }
144     }
145 }
146
147 app.factory("ComponentService", [ "$log", "COMPONENT", "UtilityService",
148         ComponentService ]);