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;
29 import java.util.concurrent.ConcurrentHashMap;
31 import org.onap.appc.client.lcm.api.AppcClientServiceFactoryProvider;
32 import org.onap.appc.client.lcm.api.AppcLifeCycleManagerServiceFactory;
33 import org.onap.appc.client.lcm.api.ApplicationContext;
34 import org.onap.appc.client.lcm.api.LifeCycleManagerStateful;
35 import org.onap.appc.client.lcm.exceptions.AppcClientException;
36 import org.onap.appc.client.lcm.model.Action;
37 import org.onap.appc.client.lcm.model.ActionIdentifiers;
38 import org.onap.appc.client.lcm.model.CommonHeader;
39 import org.onap.appc.client.lcm.model.Flags;
40 import org.onap.appc.client.lcm.model.Flags.Force;
41 import org.onap.appc.client.lcm.model.Flags.Mode;
42 import org.onap.appc.client.lcm.model.Payload;
43 import org.onap.appc.client.lcm.model.Status;
44 import org.onap.appc.client.lcm.model.ZULU;
45 import org.openecomp.mso.bpmn.core.PropertyConfiguration;
46 import org.springframework.beans.factory.annotation.Autowired;
48 import com.att.eelf.configuration.EELFLogger;
49 import com.att.eelf.configuration.EELFLogger.Level;
50 import com.att.eelf.configuration.EELFManager;
52 public class ApplicationControllerClient {
54 public static final String DEFAULT_CONTROLLER_TYPE = "appc";
56 private static final String CLIENT_NAME = "MSO";
58 private static final String API_VER = "2.00";
59 private static final String ORIGINATOR_ID = "MSO";
60 private static final int FLAGS_TTL = 65000;
61 protected final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
64 public ApplicationControllerSupport appCSupport;
66 // APPC gave us an API where the controllerType is configured in the
67 // client object, which is not what we asked for. We asked for an API
68 // in which the client would have additional methods that could take
69 // the controllerType as a parameter, so that we would not need to
70 // maintain multiple client objects. This map should be removed when
71 // the (hopefully short-term) controllerType becomes obsolete.
73 private final String controllerType;
75 private static ConcurrentHashMap<String, LifeCycleManagerStateful> appCClients = new ConcurrentHashMap<>();
78 * Creates an ApplicationControllerClient for communication with APP-C.
80 public ApplicationControllerClient() {
81 this(DEFAULT_CONTROLLER_TYPE);
85 * Creates an ApplicationControllerClient for the specified controller type.
86 * @param controllerType the controller type: "appc" or "sdnc".
88 public ApplicationControllerClient(String controllerType) {
89 this.controllerType = controllerType;
90 appCSupport = new ApplicationControllerSupport();
94 * Gets the controller type.
95 * @return the controllertype
97 public String getControllerType() {
98 return controllerType;
102 * Returns the AppC client object associated with this ApplicationControllerClient.
103 * AppC client objects are shared objects. One is created if it does not exist.
104 * @return the client object, or null if creation failed
106 public LifeCycleManagerStateful getAppCClient() {
107 return appCClients.computeIfAbsent(controllerType, k -> createAppCClient(k));
110 protected LifeCycleManagerStateful createAppCClient(String controllerType) {
112 return AppcClientServiceFactoryProvider.getFactory(AppcLifeCycleManagerServiceFactory.class)
113 .createLifeCycleManagerStateful(new ApplicationContext(), getLCMProperties(controllerType));
114 } catch (AppcClientException e) {
115 auditLogger.log(Level.ERROR, "Error in getting LifeCycleManagerStateful: ", e, e.getMessage());
116 // This null value will cause NullPointerException when used later.
117 // Error handling could certainly be improved here.
122 public Status runCommand(Action action, org.onap.appc.client.lcm.model.ActionIdentifiers actionIdentifiers,
123 org.onap.appc.client.lcm.model.Payload payload, String requestID)
124 throws ApplicationControllerOrchestratorException {
125 Object requestObject = createRequest(action, actionIdentifiers, payload, requestID);
126 appCSupport.logLCMMessage(requestObject);
127 LifeCycleManagerStateful client = getAppCClient();
128 Method lcmMethod = appCSupport.getAPIMethod(action.name(), client, false);
130 Object response = lcmMethod.invoke(client, requestObject);
131 return appCSupport.getStatusFromGenericResponse(response);
132 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
133 throw new RuntimeException(String.format("%s : %s", "Unable to invoke action", action.toString()), e);
137 protected Properties getLCMProperties() {
138 return getLCMProperties("appc");
141 protected Properties getLCMProperties(String controllerType) {
142 Properties properties = new Properties();
143 Map<String, String> globalProperties = PropertyConfiguration.getInstance()
144 .getProperties("mso.bpmn.urn.properties");
145 if (controllerType==null || controllerType.length()==0 || controllerType.equalsIgnoreCase("appc")) {
146 properties.put("topic.read", globalProperties.get("appc.client.topic.read"));
147 properties.put("topic.write", globalProperties.get("appc.client.topic.write"));
149 properties.put("topic.read", globalProperties.get("appc.client.topic." + controllerType + ".read"));
150 properties.put("topic.write", globalProperties.get("appc.client.topic." + controllerType + ".write"));
152 properties.put("topic.sdnc.read", globalProperties.get("appc.client.topic.sdnc.read"));
153 properties.put("topic.sdnc.write", globalProperties.get("appc.client.topic.sdnc.write"));
154 properties.put("sdnc-topic.read", globalProperties.get("appc.client.topic.sdnc.read"));
155 properties.put("sdnc-topic.write", globalProperties.get("appc.client.topic.sdnc.write"));
156 properties.put("topic.read.timeout", globalProperties.get("appc.client.topic.read.timeout"));
157 properties.put("client.response.timeout", globalProperties.get("appc.client.response.timeout"));
158 properties.put("poolMembers", globalProperties.get("appc.client.poolMembers"));
159 properties.put("client.controllerType", controllerType);
160 properties.put("client.key", globalProperties.get("appc.client.key"));
161 properties.put("client.secret", globalProperties.get("appc.client.secret"));
162 properties.put("client.name", CLIENT_NAME);
163 properties.put("service", globalProperties.get("appc.client.service"));
167 public Object createRequest(Action action, ActionIdentifiers identifier, Payload payload, String requestId) {
168 Object requestObject = appCSupport.getInput(action.name());
170 CommonHeader commonHeader = buildCommonHeader(requestId);
171 requestObject.getClass().getDeclaredMethod("setCommonHeader", CommonHeader.class).invoke(requestObject,
173 requestObject.getClass().getDeclaredMethod("setAction", Action.class).invoke(requestObject, action);
174 requestObject.getClass().getDeclaredMethod("setActionIdentifiers", ActionIdentifiers.class)
175 .invoke(requestObject, identifier);
176 if (payload != null) {
177 requestObject.getClass().getDeclaredMethod("setPayload", Payload.class).invoke(requestObject, payload);
179 } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
180 auditLogger.log(Level.ERROR, "Error building Appc request: ", e, e.getMessage());
182 return requestObject;
185 private CommonHeader buildCommonHeader(String requestId) {
186 CommonHeader commonHeader = new CommonHeader();
187 commonHeader.setApiVer(API_VER);
188 commonHeader.setOriginatorId(ORIGINATOR_ID);
189 commonHeader.setRequestId(requestId == null ? UUID.randomUUID().toString() : requestId);
190 commonHeader.setSubRequestId(requestId);
191 Flags flags = new Flags();
192 String flagsMode = "NORMAL";
193 Mode mode = Mode.valueOf(flagsMode);
195 String flagsForce = "FALSE";
196 Force force = Force.valueOf(flagsForce);
197 flags.setForce(force);
198 flags.setTtl(FLAGS_TTL);
199 commonHeader.setFlags(flags);
200 Instant timestamp = Instant.now();
201 ZULU zulu = new ZULU(timestamp.toString());
202 commonHeader.setTimestamp(zulu);
206 public Flags createRequestFlags() {
207 Flags flags = new Flags();