aboutsummaryrefslogtreecommitdiffstats
path: root/ncomp-sirius-manager-console/src/main/groovy/org/openecomp/ncomp/sirius/manager/console/Console.groovy
blob: 4f7f03894ea78cf6abbbfafc75b190719d8d6b47 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*-
 * ============LICENSE_START==========================================
 * OPENECOMP - DCAE
 * ===================================================================
 * 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============================================
 */
	
package org.openecomp.ncomp.sirius.manager.console

import java.util.Properties;
import java.util.TimeZone;

import org.openecomp.ncomp.core.logs.LogMessage;
import org.openecomp.ncomp.sirius.manager.AbstractClient;

import org.json.JSONObject
import org.json.JSONArray

import org.openecomp.ncomp.utils.PropertyUtil

import org.eclipse.emf.ecore.EObject;

import static org.openecomp.ncomp.sirius.manager.console.Utils.*
import static org.openecomp.ncomp.webservice.utils.DateUtils.dateFromString

class Console {
	protected AbstractClient client
	private String language
	private String namespace
	public Console(String filename, String name) {
		// ALWAYS USE GMT.
		Properties props = PropertyUtil.getPropertiesFromClasspath(filename);
		language = props.getProperty("${name}.language", "somf");
		namespace = props.getProperty("${name}.namespace", null);
		TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
	}
	public Console() {
		language = "somf"
		TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
	}
	public create(String path, o) {
		def json = null
		switch (o) {
			case String: json = new JSONObject(o); break
			case JSONObject: json = o; break
			case EObject:
					case Map: json = object2json(o); break
			default: throw new RuntimeException("Unable to determine JSON: $o")
		}
		//		println json.toString(2)
		return json2object(client.create(fixPath(path),json));
	}
	public update(String path, o) {
		def json = null
		//		println "o=$o"
		switch (o) {
			case String: json = new JSONObject(o); break
			case JSONObject: json = o; break
			case EObject:
					case Map: json = object2json(o); break
			default: throw new RuntimeException("Unable to determine JSON: $o")
		}
		//		println json.toString(2)
		return json2object(client.update(fixPath(path),json));
	}
	public delete(String path) {
		return json2object(client.delete(fixPath(path)))
	}
	public def list(String path, int levels = 1) {
		def x = client.list(fixPath(path),levels)
		return json2object(x)
	}
	public def listAll(String path) {
		JSONObject x = client.listAll(fixPath(path))
		return json2object(x.get("list"))
	}
	public def listReferences(String path, boolean recursive = true) {
		JSONObject x = client.listReferences(fixPath(path),recursive)
		return json2object(x.get("list"))
	}
	public def operation(String path, String name, o = "{}", Long timeout = null) {
		def json = null
		switch (o) {
			case String: json = new JSONObject(o); break
			case JSONObject: json = o; break
			case EObject:
					case Map: json = object2json(o); break
		}
		switch (language) {
			case "restconf":
				return json2object(client.operationOdl("/restconf/operations/$namespace:$name",timeout,json))
		}
		return json2object(client.operation(fixPath(path),name,timeout,json))
	}
	def String fixPath(String path) {
		if (path.startsWith("/gui")) return path
		if (path.startsWith("/south")) return path
		if (!path.startsWith("/")) path = "/$path"
		if (!path.startsWith("/resources")) path = "/resources$path"
		return path
	}
	public void printMetrics(String path, start, end) {
		switch (start) { case String: start = dateFromString(start); break }
		switch (end) { case String: end = dateFromString(end); break }
		def values = client.operationPath("/resources",ModelPackage.eINSTANCE.abstractManagementServer,"getValues",
				null, null, path, start.time, end.time, null, false)
		def table = [ header : ["Time", "Metric", "Value"], rows : []]
		values.each {
			def row = []
			row += new Date(it.time)
			row += it.metricName
			row += it.value
			table.rows += [row]
		}
		pTable(table)
	}
	public void printMetricsAll(String path, List<String> metrics, start, end, options = null) {
		switch (start) { case String: start = dateFromString(start); break }
		switch (end) { case String: end = dateFromString(end); break }
		def values = client.operationPath("/resources",ModelPackage.eINSTANCE.abstractManagementServer,"getValuesAll",
				null, path, metrics, start.time, end.time, fixOptions(options))
		def table = [ header : [
				"Path",
				"Time",
				"Metric",
				"Value"
			], rows : []]
		values.each {
			def row = []
			row += it.resourceName
			row += new Date(it.time)
			row += it.metricName
			row += it.value
			table.rows += [row]
		}
		pTable(table)
	}
	public void printMessages(String path, start, end) {
		if (start == null) start = '-1day'
		if (end == null) end = 'now'
		switch (start) { case String: start = dateFromString(start); break }
		switch (end) { case String: end = dateFromString(end); break }
		def values = client.operationPath("/resources",ModelPackage.eINSTANCE.abstractManagementServer,"getMessages",
				null, path, start.time, end.time)
		def table = [ header : [
				"Time",
				"Level",
				"Path",
				"Message"
			], rows : []]
		values.each { LogMessage msg ->
			def row = []
			row += new Date(msg.time)
			row += msg.level
			row += msg.resourceName
			row += msg.message
			table.rows += [row]
		}
		pTable(table)
	}

	public void saveTableCsv(t,filename) {
		def f = new File(filename)
		new File(filename).withWriter { out ->
			def row = []
			t.columns.each { row += it.colName }
			out.writeLine row.join(",")
			t.rows.each { r ->
				row = []
				r.cells.each { row += it.value }
				out.writeLine row.join(",")
			}
		}
	}
	String rootDirectory = null
	public create(String path) {
		if (rootDirectory == null) 
			throw new RuntimeException("rootDirectory is NULL")
		File file = new File("$rootDirectory/${fixPath(path)}.json")
		if (! file.exists()) 
			throw new RuntimeException("file does not exist: $file")
		def json = null
		try { json = new JSONObject(file.text) } catch (e) {
			throw new RuntimeException("JSON error: $file : $e")
		}
		create(path,json)
	}
	public update(String path) {
		if (rootDirectory == null) 
			throw new RuntimeException("rootDirectory is NULL")
		File file = new File("$rootDirectory/${fixPath(path)}.json")
		if (! file.exists()) 
			throw new RuntimeException("file does not exist: $file")
		def json = null
		try { json = new JSONObject(file.text) } catch (e) {
			throw new RuntimeException("JSON error: $file : $e")
		}
		update(path,json)
	}

}