summaryrefslogtreecommitdiffstats
path: root/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/appc/ApplicationControllerClient.java
blob: 7a157de0f206b8e055652cbfcf7934ad4bf48ae5 (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
package org.openecomp.mso.client.appc;

import java.beans.BeanInfo;

import java.util.Map;

import org.openecomp.mso.bpmn.core.PropertyConfiguration;

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.Instant;
import java.util.Properties;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;

import org.openecomp.appc.client.lcm.api.AppcClientServiceFactoryProvider;
import org.openecomp.appc.client.lcm.api.AppcLifeCycleManagerServiceFactory;
import org.openecomp.appc.client.lcm.api.ApplicationContext;
import org.openecomp.appc.client.lcm.api.LifeCycleManagerStateful;
import org.openecomp.appc.client.lcm.api.ResponseHandler;
import org.openecomp.appc.client.lcm.exceptions.AppcClientException;
import org.openecomp.appc.client.lcm.model.Action;
import org.openecomp.appc.client.lcm.model.ActionIdentifiers;
import org.openecomp.appc.client.lcm.model.AuditOutput;
import org.openecomp.appc.client.lcm.model.CommonHeader;
import org.openecomp.appc.client.lcm.model.Flags;
import org.openecomp.appc.client.lcm.model.Flags.Force;
import org.openecomp.appc.client.lcm.model.Flags.Mode;
import org.openecomp.appc.client.lcm.model.Payload;
import org.openecomp.appc.client.lcm.model.Status;
import org.openecomp.appc.client.lcm.model.ZULU;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.openecomp.mso.logger.MsoLogger;

public class ApplicationControllerClient {

    private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
    
	private static final int PARTIAL_SERIES = 500;

	private final String apiVer = "2.00";
	private final String originatorId = "MSO";
	private final int flagsTTL = 65000;
	private final static String clientName = "MSO";

	@Autowired
	public ApplicationControllerSupport appCSupport;

	private LifeCycleManagerStateful client;

	public Status runCommand(Action action, ActionIdentifiers identifier, Flags flags, Payload payload,
			String requestID) throws Exception {
		Object requestObject = createRequest(action, identifier, flags, payload, requestID);
		client = getAppCClient();
		Method lcmMethod = appCSupport.getAPIMethod(action.name(), client, false);
		appCSupport.logLCMMessage(requestObject);
		Object response = lcmMethod.invoke(client, requestObject);
		return appCSupport.getStatusFromGenericResponse(response);
	}

	public void shutdownclient() {
		AppcClientServiceFactoryProvider.getFactory(AppcLifeCycleManagerServiceFactory.class)
				.shutdownLifeCycleManager(false);
	}

	public LifeCycleManagerStateful getAppCClient() throws AppcClientException {
		if (client == null)
			client = AppcClientServiceFactoryProvider.getFactory(AppcLifeCycleManagerServiceFactory.class)
					.createLifeCycleManagerStateful(new ApplicationContext(), getLCMProperties());
		return client;
	}

	private Properties getLCMProperties() {
		return getLCMPropertiesHelper();
	}

	protected Properties getLCMPropertiesHelper() {
		Properties properties = new Properties();
		Map<String, String> globalProperties = PropertyConfiguration.getInstance()
				.getProperties("mso.bpmn.urn.properties");

		properties.put("topic.read", globalProperties.get("appc.topic.read"));
		properties.put("topic.read.timeout", globalProperties.get("appc.topic.read.timeout"));
		properties.put("client.response.timeout", globalProperties.get("appc.client.response.timeout"));
		properties.put("topic.write", globalProperties.get("appc.topic.write"));
		properties.put("poolMembers", globalProperties.get("appc.pool.members"));
		properties.put("client.key", globalProperties.get("appc.client.key"));
		properties.put("client.secret", globalProperties.get("appc.client.secret"));
		properties.put("client.name", clientName);
		return properties;
	}

	public Object createRequest(Action action, ActionIdentifiers identifier, Flags flags, Payload payload,
			String requestId) throws Exception {
		Object requestObject = appCSupport.getInput(action.name());
		try {
			org.openecomp.appc.client.lcm.model.CommonHeader commonHeader = buildCommonHeader(requestId);
			requestObject.getClass().getDeclaredMethod("setCommonHeader", CommonHeader.class).invoke(requestObject,
					commonHeader);
			requestObject.getClass().getDeclaredMethod("setAction", Action.class).invoke(requestObject, action);
			requestObject.getClass().getDeclaredMethod("setActionIdentifiers", ActionIdentifiers.class)
					.invoke(requestObject, identifier);
		} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
		    LOGGER.debug("Exception:", e);
			throw new Exception("Error Building AppC Request: " + e.getMessage());
		}
		return requestObject;
	}

	private org.openecomp.appc.client.lcm.model.CommonHeader buildCommonHeader(String requestId) {
		org.openecomp.appc.client.lcm.model.CommonHeader commonHeader = new org.openecomp.appc.client.lcm.model.CommonHeader();
		commonHeader.setApiVer(apiVer);
		commonHeader.setOriginatorId(originatorId);
		commonHeader.setRequestId(requestId == null ? UUID.randomUUID().toString() : requestId);
		commonHeader.setSubRequestId(requestId);
		org.openecomp.appc.client.lcm.model.Flags flags = new org.openecomp.appc.client.lcm.model.Flags();
		String flagsMode = "NORMAL";
		Mode mode = Mode.valueOf(flagsMode);
		flags.setMode(mode);
		String flagsForce = "FALSE";
		Force force = Force.valueOf(flagsForce);
		flags.setForce(force);
		flags.setTtl(flagsTTL);
		commonHeader.setFlags(flags);
		Instant timestamp = Instant.now();
		ZULU zulu = new ZULU(timestamp.toString());
		commonHeader.setTimestamp(zulu);
		return commonHeader;
	}

	public Flags createRequestFlags() {
		Flags flags = new Flags();
		flags.setTtl(6000);
		return flags;
	}
}