Merge "Fix the arquillian framework"
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / openecomp / mso / client / appc / ApplicationControllerClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.client.appc;
22
23 import java.beans.BeanInfo;
24
25 import java.util.Map;
26
27 import org.openecomp.mso.bpmn.core.PropertyConfiguration;
28
29 import java.beans.IntrospectionException;
30 import java.beans.Introspector;
31 import java.beans.PropertyDescriptor;
32 import java.lang.reflect.InvocationTargetException;
33 import java.lang.reflect.Method;
34 import java.time.Instant;
35 import java.util.Properties;
36 import java.util.UUID;
37
38 import org.springframework.beans.factory.annotation.Autowired;
39
40 import org.openecomp.appc.client.lcm.api.AppcClientServiceFactoryProvider;
41 import org.openecomp.appc.client.lcm.api.AppcLifeCycleManagerServiceFactory;
42 import org.openecomp.appc.client.lcm.api.ApplicationContext;
43 import org.openecomp.appc.client.lcm.api.LifeCycleManagerStateful;
44 import org.openecomp.appc.client.lcm.api.ResponseHandler;
45 import org.openecomp.appc.client.lcm.exceptions.AppcClientException;
46 import org.openecomp.appc.client.lcm.model.Action;
47 import org.openecomp.appc.client.lcm.model.ActionIdentifiers;
48 import org.openecomp.appc.client.lcm.model.AuditOutput;
49 import org.openecomp.appc.client.lcm.model.CommonHeader;
50 import org.openecomp.appc.client.lcm.model.Flags;
51 import org.openecomp.appc.client.lcm.model.Flags.Force;
52 import org.openecomp.appc.client.lcm.model.Flags.Mode;
53 import org.openecomp.appc.client.lcm.model.Payload;
54 import org.openecomp.appc.client.lcm.model.Status;
55 import org.openecomp.appc.client.lcm.model.ZULU;
56 import com.fasterxml.jackson.annotation.JsonInclude.Include;
57 import com.fasterxml.jackson.core.JsonProcessingException;
58 import com.fasterxml.jackson.databind.ObjectMapper;
59 import com.fasterxml.jackson.databind.ObjectWriter;
60 import org.openecomp.mso.logger.MsoLogger;
61
62 public class ApplicationControllerClient {
63
64     private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
65     
66         private static final int PARTIAL_SERIES = 500;
67
68         private final String apiVer = "2.00";
69         private final String originatorId = "MSO";
70         private final int flagsTTL = 65000;
71         private final static String clientName = "MSO";
72
73         @Autowired
74         public ApplicationControllerSupport appCSupport;
75
76         private LifeCycleManagerStateful client;
77
78         public Status runCommand(Action action, ActionIdentifiers identifier, Flags flags, Payload payload,
79                         String requestID) throws Exception {
80                 Object requestObject = createRequest(action, identifier, flags, payload, requestID);
81                 client = getAppCClient();
82                 Method lcmMethod = appCSupport.getAPIMethod(action.name(), client, false);
83                 appCSupport.logLCMMessage(requestObject);
84                 Object response = lcmMethod.invoke(client, requestObject);
85                 return appCSupport.getStatusFromGenericResponse(response);
86         }
87
88         public void shutdownclient() {
89                 AppcClientServiceFactoryProvider.getFactory(AppcLifeCycleManagerServiceFactory.class)
90                                 .shutdownLifeCycleManager(false);
91         }
92
93         public LifeCycleManagerStateful getAppCClient() throws AppcClientException {
94                 if (client == null)
95                         client = AppcClientServiceFactoryProvider.getFactory(AppcLifeCycleManagerServiceFactory.class)
96                                         .createLifeCycleManagerStateful(new ApplicationContext(), getLCMProperties());
97                 return client;
98         }
99
100         private Properties getLCMProperties() {
101                 return getLCMPropertiesHelper();
102         }
103
104         protected Properties getLCMPropertiesHelper() {
105                 Properties properties = new Properties();
106                 Map<String, String> globalProperties = PropertyConfiguration.getInstance()
107                                 .getProperties("mso.bpmn.urn.properties");
108
109                 properties.put("topic.read", globalProperties.get("appc.topic.read"));
110                 properties.put("topic.read.timeout", globalProperties.get("appc.topic.read.timeout"));
111                 properties.put("client.response.timeout", globalProperties.get("appc.client.response.timeout"));
112                 properties.put("topic.write", globalProperties.get("appc.topic.write"));
113                 properties.put("poolMembers", globalProperties.get("appc.pool.members"));
114                 properties.put("client.key", globalProperties.get("appc.client.key"));
115                 properties.put("client.secret", globalProperties.get("appc.client.secret"));
116                 properties.put("client.name", clientName);
117                 return properties;
118         }
119
120         public Object createRequest(Action action, ActionIdentifiers identifier, Flags flags, Payload payload,
121                         String requestId) throws Exception {
122                 Object requestObject = appCSupport.getInput(action.name());
123                 try {
124                         org.openecomp.appc.client.lcm.model.CommonHeader commonHeader = buildCommonHeader(requestId);
125                         requestObject.getClass().getDeclaredMethod("setCommonHeader", CommonHeader.class).invoke(requestObject,
126                                         commonHeader);
127                         requestObject.getClass().getDeclaredMethod("setAction", Action.class).invoke(requestObject, action);
128                         requestObject.getClass().getDeclaredMethod("setActionIdentifiers", ActionIdentifiers.class)
129                                         .invoke(requestObject, identifier);
130                 } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
131                     LOGGER.debug("Exception:", e);
132                         throw new Exception("Error Building AppC Request: " + e.getMessage());
133                 }
134                 return requestObject;
135         }
136
137         private org.openecomp.appc.client.lcm.model.CommonHeader buildCommonHeader(String requestId) {
138                 org.openecomp.appc.client.lcm.model.CommonHeader commonHeader = new org.openecomp.appc.client.lcm.model.CommonHeader();
139                 commonHeader.setApiVer(apiVer);
140                 commonHeader.setOriginatorId(originatorId);
141                 commonHeader.setRequestId(requestId == null ? UUID.randomUUID().toString() : requestId);
142                 commonHeader.setSubRequestId(requestId);
143                 org.openecomp.appc.client.lcm.model.Flags flags = new org.openecomp.appc.client.lcm.model.Flags();
144                 String flagsMode = "NORMAL";
145                 Mode mode = Mode.valueOf(flagsMode);
146                 flags.setMode(mode);
147                 String flagsForce = "FALSE";
148                 Force force = Force.valueOf(flagsForce);
149                 flags.setForce(force);
150                 flags.setTtl(flagsTTL);
151                 commonHeader.setFlags(flags);
152                 Instant timestamp = Instant.now();
153                 ZULU zulu = new ZULU(timestamp.toString());
154                 commonHeader.setTimestamp(zulu);
155                 return commonHeader;
156         }
157
158         public Flags createRequestFlags() {
159                 Flags flags = new Flags();
160                 flags.setTtl(6000);
161                 return flags;
162         }
163 }