aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOInfrastructureBPMN/src/main/java/org/openecomp/mso/bpmn/infrastructure/workflow/service/ServicePluginFactory.java
blob: 96768bace868df5e0ae3b17811a64c11633dcbb1 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2018 Huawei 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.mso.bpmn.infrastructure.workflow.service;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.camunda.bpm.engine.runtime.Execution;
import org.openecomp.mso.bpmn.core.PropertyConfiguration;
import org.openecomp.mso.bpmn.core.domain.ServiceDecomposition;
import org.openecomp.mso.bpmn.core.json.JsonUtils;
import org.openecomp.mso.logger.MessageEnum;
import org.openecomp.mso.logger.MsoLogger;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class ServicePluginFactory {

	// SOTN calculate route
	public static final String OOF_Default_EndPoint = "http://192.168.1.223:8443/oof/sotncalc";

	public static final String Third_SP_Default_EndPoint = "http://192.168.1.223:8443/sp/resourcemgr/querytps";

	private static final int DEFAULT_TIME_OUT = 60000;

	static JsonUtils jsonUtil = new JsonUtils();

	private static MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);

	private static ServicePluginFactory instance;
	

	public static synchronized ServicePluginFactory getInstance() {
		if (null == instance) {
			instance = new ServicePluginFactory();
		}
		return instance;
	}

	private ServicePluginFactory() {

	}
	
	public String test()
	{
		return "";
	}
	
	private String getThirdSPEndPoint(){
	    Map<String, String> properties = PropertyConfiguration.getInstance().getProperties("topology.properties");
	    if (properties != null) {
	        String thirdSPEndPoint = properties.get("third-sp-endpoint");
	        if(null != thirdSPEndPoint && !thirdSPEndPoint.isEmpty()){
	        	return thirdSPEndPoint;
	        }
	    }
		return Third_SP_Default_EndPoint;
	}

	private String getOOFCalcEndPoint(){
	    Map<String, String> properties = PropertyConfiguration.getInstance().getProperties("topology.properties");
	    if (properties != null) {
	        String oofCalcEndPoint = properties.get("oof-calc-endpoint");
	        if(null != oofCalcEndPoint && !oofCalcEndPoint.isEmpty()){
	        	return oofCalcEndPoint;
	        }
	    }
		return OOF_Default_EndPoint;
	}
	

	public String preProcessService(ServiceDecomposition serviceDecomposition, String uuiRequest) {

		// now only for sotn
		if (isSOTN(serviceDecomposition, uuiRequest)) {
			// We Need to query the terminalpoint of the VPN by site location
			// info
			return preProcessSOTNService(serviceDecomposition, uuiRequest);
		}
		return uuiRequest;
	}

	public String doServiceHoming(ServiceDecomposition serviceDecomposition, String uuiRequest) {
		// now only for sotn
		if (isSOTN(serviceDecomposition, uuiRequest)) {
			return doSOTNServiceHoming(serviceDecomposition, uuiRequest);
		}
		return uuiRequest;
	}

	private boolean isSOTN(ServiceDecomposition serviceDecomposition, String uuiRequest) {
		// there should be a register platform , we check it very simple here.
		return uuiRequest.contains("clientSignal") && uuiRequest.contains("vpnType");
	}

	private String preProcessSOTNService(ServiceDecomposition serviceDecomposition, String uuiRequest) {
		Map<String, Object> uuiObject = getJsonObject(uuiRequest, Map.class);
		Map<String, Object> serviceObject = (Map<String, Object>) uuiObject.get("service");
		Map<String, Object> serviceParametersObject = (Map<String, Object>) serviceObject.get("parameters");
		Map<String, Object> serviceRequestInputs = (Map<String, Object>) serviceParametersObject.get("requestInputs");
		List<Object> resources = (List<Object>) serviceParametersObject.get("resources");
		// This is a logic for demo , it could not be finalized to community.
		String srcLocation = "";
		String dstLocation = "";
		String srcClientSignal = "";
		String dstClientSignal = "";
		// support R2 uuiReq and R1 uuiReq
		// logic for R2 uuiRequest params in service level
		for (Entry<String, Object> entry : serviceRequestInputs.entrySet()) {
			if (entry.getKey().toLowerCase().contains("location")) {
				if ("".equals(srcLocation)) {
					srcLocation = (String) entry.getValue();
				} else if ("".equals(dstLocation)) {
					dstLocation = (String) entry.getValue();
				}
			}
			if (entry.getKey().toLowerCase().contains("clientsignal")) {
				if ("".equals(srcClientSignal)) {
					srcClientSignal = (String) entry.getValue();
				} else if ("".equals(dstClientSignal)) {
					dstClientSignal = (String) entry.getValue();
				}
			}
		}

		// logic for R1 uuiRequest, params in resource level
		for (Object resource : resources) {
			Map<String, Object> resourceObject = (Map<String, Object>) resource;
			Map<String, Object> resourceParametersObject = (Map<String, Object>) resourceObject.get("parameters");
			Map<String, Object> resourceRequestInputs = (Map<String, Object>) resourceParametersObject
					.get("requestInputs");
			for (Entry<String, Object> entry : resourceRequestInputs.entrySet()) {
				if (entry.getKey().toLowerCase().contains("location")) {
					if ("".equals(srcLocation)) {
						srcLocation = (String) entry.getValue();
					} else if ("".equals(dstLocation)) {
						dstLocation = (String) entry.getValue();
					}
				}
				if (entry.getKey().toLowerCase().contains("clientsignal")) {
					if ("".equals(srcClientSignal)) {
						srcClientSignal = (String) entry.getValue();
					} else if ("".equals(dstClientSignal)) {
						dstClientSignal = (String) entry.getValue();
					}
				}
			}
		}

		Map<String, Object> vpnRequestInputs = getVPNResourceRequestInputs(resources);
		// here we put client signal to vpn resource inputs
		vpnRequestInputs.put("src-client-signal", srcClientSignal);
		vpnRequestInputs.put("dst-client-signal", dstClientSignal);

		// Now we need to query terminal points from SP resourcemgr system.
		List<Object> locationTerminalPointList = queryTerminalPointsFromServiceProviderSystem(srcLocation, dstLocation);
		Map<String, Object> tpInfoMap = (Map<String, Object>) locationTerminalPointList.get(0);

		serviceRequestInputs.put("inner-src-access-provider-id", tpInfoMap.get("access-provider-id"));
		serviceRequestInputs.put("inner-src-access-client-id", tpInfoMap.get("access-client-id"));
		serviceRequestInputs.put("inner-src-access-topology-id", tpInfoMap.get("access-topology-id"));
		serviceRequestInputs.put("inner-src-access-node-id", tpInfoMap.get("access-node-id"));
		serviceRequestInputs.put("inner-src-access-ltp-id", tpInfoMap.get("access-ltp-id"));
		tpInfoMap = (Map<String, Object>) locationTerminalPointList.get(1);

		serviceRequestInputs.put("inner-dst-access-provider-id", tpInfoMap.get("access-provider-id"));
		serviceRequestInputs.put("inner-dst-access-client-id", tpInfoMap.get("access-client-id"));
		serviceRequestInputs.put("inner-dst-access-topology-id", tpInfoMap.get("access-topology-id"));
		serviceRequestInputs.put("inner-dst-access-node-id", tpInfoMap.get("access-node-id"));
		serviceRequestInputs.put("inner-dst-access-ltp-id", tpInfoMap.get("access-ltp-id"));

		String newRequest = getJsonString(uuiObject);
		return newRequest;
	}

	private List<Object> queryTerminalPointsFromServiceProviderSystem(String srcLocation, String dstLocation) {
		Map<String, String> locationSrc = new HashMap<>();
		locationSrc.put("location", srcLocation);
		Map<String, String> locationDst = new HashMap<>();
		locationDst.put("location", dstLocation);
		List<Map<String, String>> locations = new ArrayList<>();
		locations.add(locationSrc);
		locations.add(locationDst);
		List<Object> returnList = new ArrayList<>();
		String reqContent = getJsonString(locations);
		String url = getThirdSPEndPoint();
		String responseContent = sendRequest(url, "POST", reqContent);
		if (null != responseContent) {
			returnList = getJsonObject(responseContent, List.class);
		}
		return returnList;
	}

	private Map<String, Object> getVPNResourceRequestInputs(List<Object> resources) {
		for (Object resource : resources) {
			Map<String, Object> resourceObject = (Map<String, Object>) resource;
			Map<String, Object> resourceParametersObject = (Map<String, Object>) resourceObject.get("parameters");
			Map<String, Object> resourceRequestInputs = (Map<String, Object>) resourceParametersObject
					.get("requestInputs");
			for (Entry<String, Object> entry : resourceRequestInputs.entrySet()) {
				if (entry.getKey().toLowerCase().contains("vpntype")) {
					return resourceRequestInputs;
				}
			}
		}
		return null;
	}
	
	public static void main(String args[]){
		String str = "restconf/config/GENERIC-RESOURCE-API:services/service/eca7e542-12ba-48de-8544-fac59303b14e/service-data/networks/network/aec07806-1671-4af2-b722-53c8e320a633/network-data/";
		
		int index1 = str.indexOf("/network/");
		int index2 = str.indexOf("/network-data");
		
		String str1 = str.substring(index1 + "/network/".length(), index2);
		System.out.println(str1);
		
	}

	private String doSOTNServiceHoming(ServiceDecomposition serviceDecomposition, String uuiRequest) {
		// query the route for the service.
		Map<String, Object> uuiObject = getJsonObject(uuiRequest, Map.class);
		Map<String, Object> serviceObject = (Map<String, Object>) uuiObject.get("service");
		Map<String, Object> serviceParametersObject = (Map<String, Object>) serviceObject.get("parameters");
		Map<String, Object> serviceRequestInputs = (Map<String, Object>) serviceParametersObject.get("requestInputs");
		Map<String, Object> oofQueryObject = new HashMap<>();
		List<Object> resources = (List<Object>) serviceParametersObject.get("resources");
		oofQueryObject.put("src-access-provider-id", serviceRequestInputs.get("inner-src-access-provider-id"));
		oofQueryObject.put("src-access-client-id", serviceRequestInputs.get("inner-src-access-client-id"));
		oofQueryObject.put("src-access-topology-id", serviceRequestInputs.get("inner-src-access-topology-id"));
		oofQueryObject.put("src-access-node-id", serviceRequestInputs.get("inner-src-access-node-id"));
		oofQueryObject.put("src-access-ltp-id", serviceRequestInputs.get("inner-src-access-ltp-id"));
		oofQueryObject.put("dst-access-provider-id", serviceRequestInputs.get("inner-dst-access-provider-id"));
		oofQueryObject.put("dst-access-client-id", serviceRequestInputs.get("inner-dst-access-client-id"));
		oofQueryObject.put("dst-access-topology-id", serviceRequestInputs.get("inner-dst-access-topology-id"));
		oofQueryObject.put("dst-access-node-id", serviceRequestInputs.get("inner-dst-access-node-id"));
		oofQueryObject.put("dst-access-ltp-id", serviceRequestInputs.get("inner-dst-access-ltp-id"));
		String oofRequestReq = getJsonString(oofQueryObject);
		String url = getOOFCalcEndPoint();
		String responseContent = sendRequest(url, "POST", oofRequestReq);

		List<Object> returnList = new ArrayList<>();
		if (null != responseContent) {
			returnList = getJsonObject(responseContent, List.class);
		}
		// in demo we have only one VPN. no cross VPNs, so get first item.
		Map<String, Object> returnRoute = getReturnRoute(returnList);
		Map<String, Object> vpnRequestInputs = getVPNResourceRequestInputs(resources);
		vpnRequestInputs.putAll(returnRoute);
		String newRequest = getJsonString(uuiObject);
		return newRequest;
	}
	
	private Map<String, Object> getReturnRoute(List<Object> returnList){
		Map<String, Object> returnRoute = new HashMap<>();
		for(Object returnVpn :returnList){
			Map<String, Object> returnVpnInfo = (Map<String, Object>) returnVpn;
		    String accessTopoId = (String)returnVpnInfo.get("access-topology-id");
			if("100".equals(accessTopoId)){
				returnRoute.putAll(returnVpnInfo);
			}
			else if("101".equals(accessTopoId)){
				for(String key : returnVpnInfo.keySet()){
					returnRoute.put("domain1-" + key, returnVpnInfo.get(key));
				}
			}
			else if("102".equals(accessTopoId)){
				for(String key : returnVpnInfo.keySet()){
					returnRoute.put("domain2-" + key, returnVpnInfo.get(key));
				}
			}
			else{
				for(String key : returnVpnInfo.keySet()){
					returnRoute.put("domain" + accessTopoId +"-" + key, returnVpnInfo.get(key));
				}
			}
		}
		return returnRoute;
	}

	private Map<String, Object> getResourceParams(Execution execution, String resourceCustomizationUuid,
			String serviceParameters) {
		List<String> resourceList = jsonUtil.StringArrayToList(execution,
				(String) JsonUtils.getJsonValue(serviceParameters, "resources"));
		// Get the right location str for resource. default is an empty array.
		String resourceInputsFromUui = "";
		for (String resource : resourceList) {
			String resCusUuid = (String) JsonUtils.getJsonValue(resource, "resourceCustomizationUuid");
			if (resourceCustomizationUuid.equals(resCusUuid)) {
				String resourceParameters = JsonUtils.getJsonValue(resource, "parameters");
				resourceInputsFromUui = JsonUtils.getJsonValue(resourceParameters, "requestInputs");
			}
		}
		Map<String, Object> resourceInputsFromUuiMap = getJsonObject(resourceInputsFromUui, Map.class);
		return resourceInputsFromUuiMap;
	}

	public static <T> T getJsonObject(String jsonstr, Class<T> type) {
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
		try {
			return mapper.readValue(jsonstr, type);
		} catch (IOException e) {
			LOGGER.error(MessageEnum.RA_NS_EXC, "", "", MsoLogger.ErrorCode.BusinessProcesssError,
					"fail to unMarshal json", e);
		}
		return null;
	}

	public static String getJsonString(Object srcObj) {
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
		String jsonStr = null;
		try {
			jsonStr = mapper.writeValueAsString(srcObj);
		} catch (JsonProcessingException e) {
			LOGGER.debug("SdcToscaParserException", e);
			e.printStackTrace();
		}
		return jsonStr;
	}

	private static String sendRequest(String url, String methodType, String content) {
		
		String msbUrl = url;
		HttpRequestBase method = null;
		HttpResponse httpResponse = null;

		try {
			int timeout = DEFAULT_TIME_OUT;

			RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout)
					.setConnectionRequestTimeout(timeout).build();

			HttpClient client = HttpClientBuilder.create().build();

			if ("POST".equals(methodType.toUpperCase())) {
				HttpPost httpPost = new HttpPost(msbUrl);
				httpPost.setConfig(requestConfig);
				httpPost.setEntity(new StringEntity(content, ContentType.APPLICATION_JSON));
				method = httpPost;
			} else if ("PUT".equals(methodType.toUpperCase())) {
				HttpPut httpPut = new HttpPut(msbUrl);
				httpPut.setConfig(requestConfig);
				httpPut.setEntity(new StringEntity(content, ContentType.APPLICATION_JSON));
				method = httpPut;
			} else if ("GET".equals(methodType.toUpperCase())) {
				HttpGet httpGet = new HttpGet(msbUrl);
				httpGet.setConfig(requestConfig);
				method = httpGet;
			} else if ("DELETE".equals(methodType.toUpperCase())) {
				HttpDelete httpDelete = new HttpDelete(msbUrl);
				httpDelete.setConfig(requestConfig);
				method = httpDelete;
			}

			// now have no auth
			// String userCredentials =
			// SDNCAdapterProperties.getEncryptedProperty(Constants.SDNC_AUTH_PROP,
			// Constants.DEFAULT_SDNC_AUTH, Constants.ENCRYPTION_KEY);
			// String authorization = "Basic " +
			// DatatypeConverter.printBase64Binary(userCredentials.getBytes());
			// method.setHeader("Authorization", authorization);

			httpResponse = client.execute(method);
			String responseContent = null;
			if (null != httpResponse && httpResponse.getEntity() != null) {
				try {
					responseContent = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
				} catch (ParseException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != method) {
				method.reset();
			}
			method = null;
			return responseContent;

		} catch (SocketTimeoutException | ConnectTimeoutException e) {
			return null;

		} catch (Exception e) {
			return null;

		} finally {
			if (httpResponse != null) {
				try {
					EntityUtils.consume(httpResponse.getEntity());
				} catch (Exception e) {
				}
			}
			if (method != null) {
				try {
					method.reset();
				} catch (Exception e) {

				}
			}
		}
	}
}