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.lang.reflect.InvocationTargetException;
24 import java.lang.reflect.Method;
25 import java.time.Instant;
27 import java.util.Properties;
28 import java.util.UUID;
30 import org.openecomp.mso.bpmn.core.PropertyConfiguration;
31 import org.springframework.beans.factory.annotation.Autowired;
33 import org.onap.appc.client.lcm.api.AppcClientServiceFactoryProvider;
34 import org.onap.appc.client.lcm.api.AppcLifeCycleManagerServiceFactory;
35 import org.onap.appc.client.lcm.api.ApplicationContext;
36 import org.onap.appc.client.lcm.api.LifeCycleManagerStateful;
37 import org.onap.appc.client.lcm.exceptions.AppcClientException;
38 import org.onap.appc.client.lcm.model.Action;
39 import org.onap.appc.client.lcm.model.ActionIdentifiers;
40 import org.onap.appc.client.lcm.model.CommonHeader;
41 import org.onap.appc.client.lcm.model.Flags;
42 import org.onap.appc.client.lcm.model.Flags.Force;
43 import org.onap.appc.client.lcm.model.Flags.Mode;
44 import org.onap.appc.client.lcm.model.Payload;
45 import org.onap.appc.client.lcm.model.Status;
46 import org.onap.appc.client.lcm.model.ZULU;
47 import com.att.eelf.configuration.EELFLogger;
48 import com.att.eelf.configuration.EELFLogger.Level;
49 import com.att.eelf.configuration.EELFManager;
51 public class ApplicationControllerClient {
53 private static final String CLIENT_NAME = "MSO";
55 private static final String API_VER = "2.00";
56 private static final String ORIGINATOR_ID = "MSO";
57 private static final int FLAGS_TTL = 65000;
58 protected final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
61 public ApplicationControllerSupport appCSupport;
63 private static LifeCycleManagerStateful client;
65 public ApplicationControllerClient() {
66 appCSupport = new ApplicationControllerSupport();
67 client = this.getAppCClient();
70 public Status runCommand(Action action, org.onap.appc.client.lcm.model.ActionIdentifiers actionIdentifiers, org.onap.appc.client.lcm.model.Payload payload, String requestID)
71 throws ApplicationControllerOrchestratorException {
73 requestObject = createRequest(action, actionIdentifiers, payload, requestID);
74 appCSupport.logLCMMessage(requestObject);
75 Method lcmMethod = appCSupport.getAPIMethod(action.name(), client, false);
77 Object response = lcmMethod.invoke(client, requestObject);
78 return appCSupport.getStatusFromGenericResponse(response);
79 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
80 throw new RuntimeException(String.format("%s : %s", "Unable to invoke action", action.toString()), e);
84 public LifeCycleManagerStateful getAppCClient() {
87 client = AppcClientServiceFactoryProvider.getFactory(AppcLifeCycleManagerServiceFactory.class)
88 .createLifeCycleManagerStateful(new ApplicationContext(), getLCMProperties());
89 } catch (AppcClientException e) {
90 auditLogger.log(Level.ERROR, "Error in getting LifeCycleManagerStateful: ", e, e.getMessage());
95 protected Properties getLCMProperties() {
96 Properties properties = new Properties();
97 Map<String, String> globalProperties = PropertyConfiguration.getInstance()
98 .getProperties("mso.bpmn.urn.properties");
100 properties.put("topic.read", globalProperties.get("appc.topic.read"));
101 properties.put("topic.read.timeout", globalProperties.get("appc.topic.read.timeout"));
102 properties.put("client.response.timeout", globalProperties.get("appc.client.response.timeout"));
103 properties.put("topic.write", globalProperties.get("appc.topic.write"));
104 properties.put("poolMembers", globalProperties.get("appc.poolMembers"));
105 properties.put("client.key", globalProperties.get("appc.client.key"));
106 properties.put("client.secret", globalProperties.get("appc.client.secret"));
107 properties.put("client.name", CLIENT_NAME);
108 properties.put("service", globalProperties.get("appc.service"));
112 public Object createRequest(Action action, ActionIdentifiers identifier, Payload payload, String requestId) {
113 Object requestObject = appCSupport.getInput(action.name());
115 CommonHeader commonHeader = buildCommonHeader(requestId);
116 requestObject.getClass().getDeclaredMethod("setCommonHeader", CommonHeader.class).invoke(requestObject,
118 requestObject.getClass().getDeclaredMethod("setAction", Action.class).invoke(requestObject, action);
119 requestObject.getClass().getDeclaredMethod("setActionIdentifiers", ActionIdentifiers.class)
120 .invoke(requestObject, identifier);
121 if (payload != null) {
122 requestObject.getClass().getDeclaredMethod("setPayload", Payload.class).invoke(requestObject, payload);
124 } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
125 auditLogger.log(Level.ERROR, "Error building Appc request: ", e, e.getMessage());
127 return requestObject;
130 private CommonHeader buildCommonHeader(String requestId) {
131 CommonHeader commonHeader = new CommonHeader();
132 commonHeader.setApiVer(API_VER);
133 commonHeader.setOriginatorId(ORIGINATOR_ID);
134 commonHeader.setRequestId(requestId == null ? UUID.randomUUID().toString() : requestId);
135 commonHeader.setSubRequestId(requestId);
136 Flags flags = new Flags();
137 String flagsMode = "NORMAL";
138 Mode mode = Mode.valueOf(flagsMode);
140 String flagsForce = "FALSE";
141 Force force = Force.valueOf(flagsForce);
142 flags.setForce(force);
143 flags.setTtl(FLAGS_TTL);
144 commonHeader.setFlags(flags);
145 Instant timestamp = Instant.now();
146 ZULU zulu = new ZULU(timestamp.toString());
147 commonHeader.setTimestamp(zulu);
151 public Flags createRequestFlags() {
152 Flags flags = new Flags();