aboutsummaryrefslogtreecommitdiffstats
path: root/vid/src/main/webapp/app/vid/scripts/services/componentService.js
blob: 7c1cfcb9cd3a09c7731f905ce9b0b1360fa06756 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*-
 * ============LICENSE_START=======================================================
 * VID
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

"use strict";

var ComponentService = function($log, COMPONENT, UtilityService) {

    var _this = this;

    var componentList = [ {
	id : COMPONENT.NETWORK,
	displayName : "Network"
    }, {
	id : COMPONENT.SERVICE,
	displayName : "Service Instance"
    }, {
	id : COMPONENT.VNF,
	displayName : "Virtual Network Function"
    }, {
	id : COMPONENT.VF_MODULE,
	displayName : "VF Module"
    }, {
	id : COMPONENT.VOLUME_GROUP,
	displayName : "Volume Group"
    } ];

    var getInventoryInfo = function(suffix, inventoryItem) {
	var pattern = new RegExp(suffix + "-");
	for ( var key in inventoryItem) {
	    if (pattern.exec(key)) {
		return inventoryItem[key];
	    }
	}
    };

    /*
     * Converts 'id' to a user friendly version.
     * 
     * The algorithm used is:
     * 
     * 1) If "id" found in COMPONENT.FULL_NAME_MAP, return the name found in the
     * map.
     * 
     * 2) Otherwise, if camel case, add "-" between camel case words.
     * 
     * 3) Split id into multiple "partial names" assuming "-" is the delimiter.
     * 
     * 4) Map any partial names found in COMPONENT.PARTIAL_NAME_MAP to the name
     * found in the map.
     * 
     * 5) Use partial names whenever not found in map.
     * 
     * 5) Return name by combining all partial names with " " delimiter.
     */
    var getDisplayName = function(id) {
	var tmp = COMPONENT.FULL_NAME_MAP[id.toLowerCase()];
	if (UtilityService.hasContents(tmp)) {
	    return tmp;
	}
	/*
	 * Add "-" if camel case found.
	 */
	var id = id.replace(/([a-z](?=[A-Z]))/g, '$1-');
	var name = "";
	var arg = id.split("-");
	for (var i = 0; i < arg.length; i++) {
	    if (i > 0) {
		name += " ";
	    }
	    var tmp = COMPONENT.PARTIAL_NAME_MAP[arg[i].toLowerCase()];
	    if (UtilityService.hasContents(tmp)) {
		name += tmp;
	    } else {
		name += arg[i].slice(0, 1).toUpperCase() + arg[i].slice(1);
	    }
	}
	return name;
    };

    return {
	initialize : function(componentId) {
	    for (var i = 0; i < componentList.length; i++) {
		if (componentList[i].id === componentId) {
		    _this.componentId = componentList[i].id;
		    return componentId;
		}
	    }
	    throw "ComponentService:initializeComponent: componentId not found: "
		    + componentId;
	},
	getComponentDisplayName : function() {
	    for (var i = 0; i < componentList.length; i++) {
		if (componentList[i].id === _this.componentId) {
		    return componentList[i].displayName;
		}
	    }
	},
	getInventoryInfo : getInventoryInfo,
	getInventoryParameterList : function(suffix, inventoryItem) {
	    var parameterList = new Array();
	//    var pattern = new RegExp("-[intv][a-z]*$");
	//    var inventoryInfo = getInventoryInfo(suffix, inventoryItem);
	    for ( var id in inventoryItem) {
		//if (pattern.exec(id)) {
		    parameterList.push({
			id : id,
			value : inventoryItem[id]
		    });
		//}
	    }
	    return parameterList;
	},
	getDisplayNames : function(inputList) {
	    var outputList = new Array();
	    for (var i = 0; i < inputList.length; i++) {
		var entry = angular.copy(inputList[i]);
		if (!UtilityService.hasContents(entry.name)) {
		    entry.name = getDisplayName(entry.id);
		}
		outputList.push(entry);
	    }
	    return outputList;
	},
	getFieldDisplayName : function(name) {
	    return getDisplayName(name);
	}
    }
}

app.factory("ComponentService", [ "$log", "COMPONENT", "UtilityService",
	ComponentService ]);