1710 Rebase - Second Attempt
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / openecomp / mso / client / appc / ApplicationControllerClient.java
1 package org.openecomp.mso.client.appc;
2
3 import java.beans.BeanInfo;
4
5 import java.util.Map;
6
7 import org.openecomp.mso.bpmn.core.PropertyConfiguration;
8
9 import java.beans.IntrospectionException;
10 import java.beans.Introspector;
11 import java.beans.PropertyDescriptor;
12 import java.lang.reflect.InvocationTargetException;
13 import java.lang.reflect.Method;
14 import java.time.Instant;
15 import java.util.Properties;
16 import java.util.UUID;
17
18 import org.springframework.beans.factory.annotation.Autowired;
19
20 import org.openecomp.appc.client.lcm.api.AppcClientServiceFactoryProvider;
21 import org.openecomp.appc.client.lcm.api.AppcLifeCycleManagerServiceFactory;
22 import org.openecomp.appc.client.lcm.api.ApplicationContext;
23 import org.openecomp.appc.client.lcm.api.LifeCycleManagerStateful;
24 import org.openecomp.appc.client.lcm.api.ResponseHandler;
25 import org.openecomp.appc.client.lcm.exceptions.AppcClientException;
26 import org.openecomp.appc.client.lcm.model.Action;
27 import org.openecomp.appc.client.lcm.model.ActionIdentifiers;
28 import org.openecomp.appc.client.lcm.model.AuditOutput;
29 import org.openecomp.appc.client.lcm.model.CommonHeader;
30 import org.openecomp.appc.client.lcm.model.Flags;
31 import org.openecomp.appc.client.lcm.model.Flags.Force;
32 import org.openecomp.appc.client.lcm.model.Flags.Mode;
33 import org.openecomp.appc.client.lcm.model.Payload;
34 import org.openecomp.appc.client.lcm.model.Status;
35 import org.openecomp.appc.client.lcm.model.ZULU;
36 import com.fasterxml.jackson.annotation.JsonInclude.Include;
37 import com.fasterxml.jackson.core.JsonProcessingException;
38 import com.fasterxml.jackson.databind.ObjectMapper;
39 import com.fasterxml.jackson.databind.ObjectWriter;
40
41 public class ApplicationControllerClient {
42
43         private static final int ACCEPT_SERIES = 100;
44         private static final int ERROR_SERIES = 200;
45         private static final int REJECT_SERIES = 300;
46         private static final int SUCCESS_SERIES = 400;
47         private static final int SUCCESS_STATUS = SUCCESS_SERIES + 0;
48         private static final int PARTIAL_SERIES = 500;
49         private static final int PARTIAL_SUCCESS_STATUS = PARTIAL_SERIES + 0;
50
51         private final boolean useLCMBypass = false;
52
53         private final String apiVer = "2.00";
54         private final String originatorId = "MSO";
55         private final int flagsTTL = 65000;
56         private final static String clientName = "MSO";
57
58         @Autowired
59         public ApplicationControllerSupport appCSupport;
60
61         private LifeCycleManagerStateful client;
62
63         public Status runCommand(Action action, ActionIdentifiers identifier, Flags flags, Payload payload,
64                         String requestID) throws Exception {
65                 Object requestObject = createRequest(action, identifier, flags, payload, requestID);
66                 client = getAppCClient();
67                 Method lcmMethod = appCSupport.getAPIMethod(action.name(), client, false);
68                 appCSupport.logLCMMessage(requestObject);
69                 Object response = lcmMethod.invoke(client, requestObject);
70                 return appCSupport.getStatusFromGenericResponse(response);
71         }
72
73         public void shutdownclient() {
74                 AppcClientServiceFactoryProvider.getFactory(AppcLifeCycleManagerServiceFactory.class)
75                                 .shutdownLifeCycleManager(false);
76         }
77
78         public LifeCycleManagerStateful getAppCClient() throws AppcClientException {
79                 if (client == null)
80                         client = AppcClientServiceFactoryProvider.getFactory(AppcLifeCycleManagerServiceFactory.class)
81                                         .createLifeCycleManagerStateful(new ApplicationContext(), getLCMProperties());
82                 return client;
83         }
84
85         private Properties getLCMProperties() {
86                 return getLCMPropertiesHelper();
87         }
88
89         protected Properties getLCMPropertiesHelper() {
90                 Properties properties = new Properties();
91                 Map<String, String> globalProperties = PropertyConfiguration.getInstance()
92                                 .getProperties("mso.bpmn.urn.properties");
93
94                 properties.put("topic.read", globalProperties.get("appc.topic.read"));
95                 properties.put("topic.read.timeout", globalProperties.get("appc.topic.read.timeout"));
96                 properties.put("client.response.timeout", globalProperties.get("appc.client.response.timeout"));
97                 properties.put("topic.write", globalProperties.get("appc.topic.write"));
98                 properties.put("poolMembers", globalProperties.get("appc.pool.members"));
99                 properties.put("client.key", globalProperties.get("appc.client.key"));
100                 properties.put("client.secret", globalProperties.get("appc.client.secret"));
101                 properties.put("client.name", clientName);
102                 return properties;
103         }
104
105         public Object createRequest(Action action, ActionIdentifiers identifier, Flags flags, Payload payload,
106                         String requestId) throws Exception {
107                 Object requestObject = appCSupport.getInput(action.name());
108                 try {
109                         org.openecomp.appc.client.lcm.model.CommonHeader commonHeader = buildCommonHeader(requestId);
110                         requestObject.getClass().getDeclaredMethod("setCommonHeader", CommonHeader.class).invoke(requestObject,
111                                         commonHeader);
112                         requestObject.getClass().getDeclaredMethod("setAction", Action.class).invoke(requestObject, action);
113                         requestObject.getClass().getDeclaredMethod("setActionIdentifiers", ActionIdentifiers.class)
114                                         .invoke(requestObject, identifier);
115                 } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
116                         throw new Exception("Error Building AppC Request: " + e.getMessage());
117                 }
118                 return requestObject;
119         }
120
121         private org.openecomp.appc.client.lcm.model.CommonHeader buildCommonHeader(String requestId) {
122                 org.openecomp.appc.client.lcm.model.CommonHeader commonHeader = new org.openecomp.appc.client.lcm.model.CommonHeader();
123                 commonHeader.setApiVer(apiVer);
124                 commonHeader.setOriginatorId(originatorId);
125                 commonHeader.setRequestId(requestId == null ? UUID.randomUUID().toString() : requestId);
126                 commonHeader.setSubRequestId(requestId);
127                 org.openecomp.appc.client.lcm.model.Flags flags = new org.openecomp.appc.client.lcm.model.Flags();
128                 String flagsMode = "NORMAL";
129                 Mode mode = Mode.valueOf(flagsMode);
130                 flags.setMode(mode);
131                 String flagsForce = "FALSE";
132                 Force force = Force.valueOf(flagsForce);
133                 flags.setForce(force);
134                 flags.setTtl(flagsTTL);
135                 commonHeader.setFlags(flags);
136                 Instant timestamp = Instant.now();
137                 ZULU zulu = new ZULU(timestamp.toString());
138                 commonHeader.setTimestamp(zulu);
139                 return commonHeader;
140         }
141
142         public Flags createRequestFlags() {
143                 Flags flags = new Flags();
144                 flags.setTtl(6000);
145                 return flags;
146         }
147 }