2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.client.appc;
23 import java.beans.BeanInfo;
27 import org.openecomp.mso.bpmn.core.PropertyConfiguration;
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;
38 import org.springframework.beans.factory.annotation.Autowired;
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;
62 public class ApplicationControllerClient {
64 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
66 private static final int PARTIAL_SERIES = 500;
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";
74 public ApplicationControllerSupport appCSupport;
76 private LifeCycleManagerStateful client;
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);
88 public void shutdownclient() {
89 AppcClientServiceFactoryProvider.getFactory(AppcLifeCycleManagerServiceFactory.class)
90 .shutdownLifeCycleManager(false);
93 public LifeCycleManagerStateful getAppCClient() throws AppcClientException {
95 client = AppcClientServiceFactoryProvider.getFactory(AppcLifeCycleManagerServiceFactory.class)
96 .createLifeCycleManagerStateful(new ApplicationContext(), getLCMProperties());
100 private Properties getLCMProperties() {
101 return getLCMPropertiesHelper();
104 protected Properties getLCMPropertiesHelper() {
105 Properties properties = new Properties();
106 Map<String, String> globalProperties = PropertyConfiguration.getInstance()
107 .getProperties("mso.bpmn.urn.properties");
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);
120 public Object createRequest(Action action, ActionIdentifiers identifier, Flags flags, Payload payload,
121 String requestId) throws Exception {
122 Object requestObject = appCSupport.getInput(action.name());
124 org.openecomp.appc.client.lcm.model.CommonHeader commonHeader = buildCommonHeader(requestId);
125 requestObject.getClass().getDeclaredMethod("setCommonHeader", CommonHeader.class).invoke(requestObject,
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());
134 return requestObject;
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);
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);
158 public Flags createRequestFlags() {
159 Flags flags = new Flags();