aboutsummaryrefslogtreecommitdiffstats
path: root/ncomp-docker-adaptor/src/main/java/org/openecomp/ncomp/servers/docker/DockerAbstractClient.java
blob: 30a5777f100f03f9a73b61071f08974c3a4f99df (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
/*-
 * ============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.servers.docker;

import java.util.HashMap;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EOperation;
import org.json.JSONArray;
import org.json.JSONObject;

import org.openecomp.ncomp.utils.emf.EUtils;
import org.openecomp.ncomp.sirius.manager.ManagementServer;

public abstract class DockerAbstractClient {
	public static final Logger logger = Logger.getLogger(DockerAbstractClient.class);
	abstract public byte[] httpBinaryTransaction(String path, String method, HashMap<String, String> headers, JSONObject body, Long timeout);

	public Properties props;
	public String language;
	public String namespace;
	private static HashMap<EObject, DockerAbstractClient> map1 = new HashMap<EObject, DockerAbstractClient>();
	private static HashMap<EObject, String> map2 = new HashMap<EObject, String>();

	public void add(String uri, EObject o) {
		map1.put(o, this);
		map2.put(o, uri);
	}

	static DockerAbstractClient findClient(EObject o) {
		return map1.get(o);
	}

	public JSONObject operationJson(EObject o, String name, Long timeout, JSONObject json) {
		return operationPath2(map2.get(o), name, timeout, json);
	}

	public JSONObject operationPath2(String path, String name, Long timeout, JSONObject json) {
		HashMap<String, String> headers = new HashMap<String, String>();
		headers.put("action", name);
		logger.debug("operation: " + name + "\n" + json.toString(2));
		return httpJsonTransaction(path, "PUT", headers, json, timeout);
	}
	public JSONObject operationOdl(String path, Long timeout, JSONObject json) {
		JSONObject json1 = new JSONObject();
		json1.put("input", json);
		logger.debug("ODL operation: " + path + "\n" + json1.toString(2));
		return httpJsonTransaction(path, "POST", null, json1, timeout);
	}

	public Object operation2(String path, EObject o, String opName, Long timeout, Object[] params) {
		EOperation op = EUtils.name2operation(o.eClass(), opName);
		if (op == null)
			throw new RuntimeException("no such operation: " + opName + " on " + o);
		JSONObject res = operationPath2(path, opName, timeout, ManagementServer.params2json(op, params));
		return ManagementServer.json2response(op, res);
	}

	public JSONObject operation(String resourcePath, String opName, Long timeout, JSONObject json) {
		return operationPath2(resourcePath, opName, timeout, json);
	}

	public Object operation(String path, EObject o, String opName, Long timeout, Object... params) {
		return operation2(path, o, opName, timeout, params);
	}

	public Object operation(EObject o, String opName, Long timeout, Object... params) {
		return operation2(map2.get(o), o, opName, timeout, params);
	}

	public Object operationPath(String resourcePath, EClass c, String opName, Long timeout, Object... params) {
		EOperation op = EUtils.name2operation(c, opName);
		if (op == null) {
			throw new RuntimeException("Unknown operation " + opName + " on Eclass " + c.getName());
		}
		JSONObject res;
		JSONObject json1 = ManagementServer.params2json(op, params);
		if (language != null && language.equals("restconf")) { 
		    res = operationOdl("/restconf/operations/" + namespace + ":" + opName,timeout,json1);
		}
		else {
			res = operationPath2(resourcePath, op.getName(), timeout, json1 );
		}
		return ManagementServer.json2response(op, res);
	}

	// abstract public void sendToDataRouter(String feedname, String fileId,
	// JSONObject metadata, InputStream is);

	// public void sendToDataRouter(String feedname, String fileId, JSONObject
	// metadata, byte[] bytes) {
	// ByteArrayInputStream in = new ByteArrayInputStream(bytes);
	// sendToDataRouter(feedname, fileId, metadata, in);
	// }

	public void create(String resourcePath, String json) {
		create(resourcePath, new JSONObject(json));
	}

	public void create(String resourcePath, JSONObject json) {
		httpJsonTransaction(resourcePath, "POST", null, json, null);
	}

	public void update(String resourcePath, JSONObject json) {
		httpJsonTransaction(resourcePath, "PUT", null, json, null);
	}

	public void delete(String resourcePath) {
		httpJsonTransaction(resourcePath, "DELETE", null, new JSONObject(), null);
	}

	public JSONObject method(String resourcePath, String method, JSONObject json) {
		return httpJsonTransaction(resourcePath, method, null, json, null);
	}
	
	public byte[] methodAsBinary(String resourcePath, String method, JSONObject json) {
		return httpBinaryTransaction(resourcePath, method, null, json, null);
	}

	public String methodAsString(String resourcePath, String method, JSONObject json) {
		return httpStringTransaction(resourcePath, method, null, json, null);
	}

	public JSONObject list(String resourcePath) {
		if (language != null && language.equals("rest")) {
			return httpJsonTransaction(resourcePath, "GET", null, new JSONObject(), null);
		} else {
			return list(resourcePath, 1);
		}
	}
	
	public JSONObject listAll(String resourcePath) {
		return httpJsonTransaction(resourcePath + "?match=regexp", "GET", null, new JSONObject(), null);
	}

	public JSONObject listReferences(String resourcePath, boolean recursive) {
		return httpJsonTransaction(resourcePath + "?references=" + recursive, "GET", null, new JSONObject(), null);
	}

	public JSONObject list(String resourcePath, int levels) {
		return httpJsonTransaction(resourcePath + "?levels=" + levels, "GET", null, new JSONObject(), null);
	}
	
	public JSONObject httpJsonTransaction(String path, String method, HashMap<String, String> headers, JSONObject body) {
		return httpJsonTransaction(path, method, headers, body,null);
	}

	public JSONObject httpJsonTransaction(String path, String method, HashMap<String, String> headers, JSONObject body, Long timeout) {
		String s = httpStringTransaction(path, method, headers, body, timeout);
		if (s == null) return null;
		if (s.startsWith("[")) {
			JSONArray a = new JSONArray(s);
			JSONObject json = new JSONObject();
			json.put("$list", a);
			return json;
		} else {
			logger.info("json returned <- " + "'" + s + "'");
			JSONObject json = (!s.isEmpty()) ? new JSONObject(s) : new JSONObject();
			return json;
		}
	}

	public String httpStringTransaction(String path, String method, HashMap<String, String> headers, JSONObject body, Long timeout) {
		byte[] b = httpBinaryTransaction(path, method, headers, body, timeout);
		if (b == null) return null;
		else return new String(b);
	}

}