aboutsummaryrefslogtreecommitdiffstats
path: root/ncomp-sirius-manager-server/src/main/java/org/openecomp/ncomp/sirius/manager/BasicManagementServerProvider.java
blob: dd466286a4584413d6331e205a1586a215b33483 (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
/*-
 * ============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;

import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.log4j.Logger;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.json.JSONObject;

import org.openecomp.ncomp.core.logs.LogMessage;
import org.openecomp.ncomp.core.metrics.Metric;
import org.openecomp.ncomp.sirius.function.FunctionUtils;
import org.openecomp.ncomp.sirius.manager.properties.AbstractProperty;
import org.openecomp.ncomp.sirius.manager.server.AbstractManagementServer;
import org.openecomp.ncomp.sirius.manager.server.LoggerInfo;
import org.openecomp.ncomp.sirius.manager.server.ManagementInfo;
import org.openecomp.ncomp.sirius.manager.server.ServerFactory;

// This is a base class providing the AbstractManagementServer functionality needed
// in an XxxProvider class

public class BasicManagementServerProvider extends BasicAdaptorProvider {
	private static final Logger logger = Logger.getLogger(BasicManagementServerProvider.class);
	private AbstractManagementServer a;

	public BasicManagementServerProvider (ISiriusServer controller, AbstractManagementServer a) {
		super(controller, a);
		this.a = a;
	}
	
	public void logs(JSONObject cx, EList<LogMessage> logs) {
		uploadInfo(null, createManagementInfoList(cx, null, logs, null));
	}
	
	public void properties(JSONObject cx, EList<AbstractProperty> l) {
		uploadInfo(null, createManagementInfoList(cx, null, null, l));
	}
	
	public void metrics(JSONObject cx, EList<Metric> metrics) {
		uploadInfo(null, createManagementInfoList(cx, metrics, null, null));
	}
	
	AtomicLong numUploads = new AtomicLong();
	public void uploadInfo(JSONObject cx, EList<ManagementInfo> info) {
		Date d = new Date();
		numUploads.incrementAndGet();
		if (cx == null) {
			for (ManagementInfo i : info) {
				if (i.getLogs().size() > 0) {
					controller.getServer().logs.updateLogs(a,i.getLogs(),a.getConfiguration().getRules(),logger);
				}
				if (i.getMetrics().size() > 0) {
					controller.getServer().metrics.updateMetrics(a,i.getMetrics());
				}
				if (i.getProps().size() > 0) {
					controller.getServer().properties.updatePropertries(a,i.getProps());
				}
			}
		}
		logger.info("uploadInfo done: " + (new Date().getTime()-d.getTime())/1000 + " numCurrentUploads= " + numUploads.decrementAndGet());
	}
	
	private static EList<ManagementInfo> createManagementInfoList(JSONObject context, EList<Metric> metrics, EList<LogMessage> logs, EList<AbstractProperty> properties) {
		ManagementInfo m = ServerFactory.eINSTANCE.createManagementInfo();
		EList<ManagementInfo> l = new BasicEList<ManagementInfo>();
		l.add(m);
		if (metrics != null) m.getMetrics().addAll(metrics);
		if (logs != null) m.getLogs().addAll(logs);
		if (properties != null) m.getProps().addAll(properties);
		m.setIp(context.getString("remoteIp"));
		logger.info("received data from: " + m.getIp() + " #m=" + m.getMetrics().size() +  " #l=" + m.getLogs().size() +  " #p=" + m.getProps().size());
		return l;
	}
		  
	public EList<org.openecomp.ncomp.core.metrics.DoubleMetric> getValues(org.json.JSONObject cx, java.lang.String path, java.lang.Long start, java.lang.Long end, org.openecomp.ncomp.core.metrics.MetricValueOption option, boolean relativeInterval) {
		return controller.getServer().metrics.getValues(path, start, end, option, relativeInterval);
	}

	public EList<org.openecomp.ncomp.core.metrics.DoubleMetric> getValuesAll(org.json.JSONObject cx, java.lang.String path, EList<java.lang.String> metrics, java.lang.Long start, java.lang.Long end, org.openecomp.ncomp.core.metrics.MetricValueOption option, boolean relativeInterval) {
		return controller.getServer().metrics.getValuesAll(path, metrics, start, end, option, relativeInterval);
	}

	public EList<org.openecomp.ncomp.core.logs.LogMessage> getMessages(org.json.JSONObject cx, java.lang.String path, java.lang.Long start, java.lang.Long end) {
		return controller.getServer().logs.getMessages(path, new Date(start), new Date(end));
	}

	public org.openecomp.ncomp.sirius.manager.server.LoggerInfo getRequestLogger(java.lang.String userName, java.lang.String action, java.lang.String resourcePath, org.json.JSONObject context) {
		LoggerInfo info = ServerFactory.eINSTANCE.createLoggerInfo();
	  	info.setName(action);
	  	return info;  		
	}

	public EList<org.openecomp.ncomp.core.function.ValuePair> evaluate(java.lang.String path, org.openecomp.ncomp.core.function.Function function) {
		return FunctionUtils.evaluate(controller.getServer().getObject(), path, function);
	}
	
	public void update(java.lang.String path, org.openecomp.ncomp.core.function.Function function) {
		// TODO IMPLEMENT
		throw new UnsupportedOperationException();
	}
	
	public static void ecoreSetup() {
		// TODO Auto-generated method stub
		
	}


}