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.onap.so.client.appc;
23 import java.lang.reflect.InvocationTargetException;
24 import java.lang.reflect.Method;
25 import java.time.Instant;
26 import java.util.Properties;
27 import java.util.UUID;
28 import java.util.concurrent.ConcurrentHashMap;
30 import org.onap.so.bpmn.core.UrnPropertiesReader;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
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;
48 public class ApplicationControllerClient {
50 public static final String DEFAULT_CONTROLLER_TYPE = "APPC";
52 private static final String CLIENT_NAME = "MSO";
54 private static final String API_VER = "2.00";
55 private static final String ORIGINATOR_ID = "MSO";
56 private static final int FLAGS_TTL = 65000;
57 private static Logger logger = LoggerFactory.getLogger(ApplicationControllerClient.class);
59 private ApplicationControllerSupport appCSupport;
61 // APPC gave us an API where the controllerType is configured in the
62 // client object, which is not what we asked for. We asked for an API
63 // in which the client would have additional methods that could take
64 // the controllerType as a parameter, so that we would not need to
65 // maintain multiple client objects. This map should be removed when
66 // the (hopefully short-term) controllerType becomes obsolete.
68 private final String controllerType;
70 private static ConcurrentHashMap<String, LifeCycleManagerStateful> appCClients = new ConcurrentHashMap<>();
73 * Creates an ApplicationControllerClient for communication with APP-C.
75 public ApplicationControllerClient() {
76 this(DEFAULT_CONTROLLER_TYPE);
80 * Creates an ApplicationControllerClient for the specified controller type.
81 * @param controllerType the controller type: "appc" or "sdnc".
83 public ApplicationControllerClient(String controllerType) {
84 if (controllerType == null) {
85 controllerType = DEFAULT_CONTROLLER_TYPE;
87 this.controllerType = controllerType.toUpperCase();
88 appCSupport = new ApplicationControllerSupport();
92 * Gets the controller type.
93 * @return the controllertype
95 public String getControllerType() {
96 return controllerType;
100 * Returns the AppC client object associated with this ApplicationControllerClient.
101 * AppC client objects are shared objects. One is created if it does not exist.
102 * @return the client object, or null if creation failed
104 public LifeCycleManagerStateful getAppCClient() {
105 return appCClients.computeIfAbsent(controllerType, k -> createAppCClient(k));
108 protected LifeCycleManagerStateful createAppCClient(String controllerType) {
110 if (controllerType == null) {
111 controllerType = DEFAULT_CONTROLLER_TYPE;
113 controllerType = controllerType.toUpperCase();
114 return AppcClientServiceFactoryProvider.getFactory(AppcLifeCycleManagerServiceFactory.class)
115 .createLifeCycleManagerStateful(new ApplicationContext(), getLCMProperties(controllerType));
116 } catch (AppcClientException e) {
117 logger.error("Error in getting LifeCycleManagerStateful: {}",e.getMessage(), e);
118 // This null value will cause NullPointerException when used later.
119 // Error handling could certainly be improved here.
124 public Status runCommand(Action action, org.onap.appc.client.lcm.model.ActionIdentifiers actionIdentifiers,
125 org.onap.appc.client.lcm.model.Payload payload, String requestID)
126 throws ApplicationControllerOrchestratorException {
127 Object requestObject;
128 requestObject = createRequest(action, actionIdentifiers, payload, requestID);
129 appCSupport.logLCMMessage(requestObject);
130 LifeCycleManagerStateful client = getAppCClient();
131 Method lcmMethod = appCSupport.getAPIMethod(action.name(), client, false);
133 Object response = lcmMethod.invoke(client, requestObject);
134 return appCSupport.getStatusFromGenericResponse(response);
135 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
136 throw new RuntimeException(String.format("%s : %s", "Unable to invoke action", action.toString()), e);
140 protected Properties getLCMProperties() {
141 return getLCMProperties("appc");
144 protected Properties getLCMProperties(String controllerType) {
145 Properties properties = new Properties();
147 properties.put("topic.read", UrnPropertiesReader.getVariable("appc.client.topic.read.name"));
148 properties.put("topic.write", UrnPropertiesReader.getVariable("appc.client.topic.write"));
149 properties.put("SDNC-topic.read", UrnPropertiesReader.getVariable("appc.client.topic.sdnc.read"));
150 properties.put("SDNC-topic.write", UrnPropertiesReader.getVariable("appc.client.topic.sdnc.write"));
151 properties.put("topic.read.timeout", UrnPropertiesReader.getVariable("appc.client.topic.read.timeout"));
152 properties.put("client.response.timeout", UrnPropertiesReader.getVariable("appc.client.response.timeout"));
153 properties.put("poolMembers", UrnPropertiesReader.getVariable("appc.client.poolMembers"));
154 properties.put("controllerType", controllerType);
155 properties.put("client.key", UrnPropertiesReader.getVariable("appc.client.key"));
156 properties.put("client.secret", UrnPropertiesReader.getVariable("appc.client.secret"));
157 properties.put("client.name", CLIENT_NAME);
158 properties.put("service", UrnPropertiesReader.getVariable("appc.client.service"));
162 public Object createRequest(Action action, ActionIdentifiers identifier, Payload payload, String requestId) {
163 Object requestObject = appCSupport.getInput(action.name());
165 CommonHeader commonHeader = buildCommonHeader(requestId);
166 requestObject.getClass().getDeclaredMethod("setCommonHeader", CommonHeader.class).invoke(requestObject,
168 requestObject.getClass().getDeclaredMethod("setAction", Action.class).invoke(requestObject, action);
169 requestObject.getClass().getDeclaredMethod("setActionIdentifiers", ActionIdentifiers.class)
170 .invoke(requestObject, identifier);
171 if (payload != null) {
172 requestObject.getClass().getDeclaredMethod("setPayload", Payload.class).invoke(requestObject, payload);
174 } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
175 logger.error("Error building Appc request", e);
177 return requestObject;
180 private CommonHeader buildCommonHeader(String requestId) {
181 CommonHeader commonHeader = new CommonHeader();
182 commonHeader.setApiVer(API_VER);
183 commonHeader.setOriginatorId(ORIGINATOR_ID);
184 commonHeader.setRequestId(requestId == null ? UUID.randomUUID().toString() : requestId);
185 commonHeader.setSubRequestId(UUID.randomUUID().toString());
186 Flags flags = new Flags();
187 String flagsMode = "NORMAL";
188 Mode mode = Mode.valueOf(flagsMode);
190 String flagsForce = "FALSE";
191 Force force = Force.valueOf(flagsForce);
192 flags.setForce(force);
193 flags.setTtl(FLAGS_TTL);
194 commonHeader.setFlags(flags);
195 Instant timestamp = Instant.now();
196 ZULU zulu = new ZULU(timestamp.toString());
197 commonHeader.setTimestamp(zulu);
201 public Flags createRequestFlags() {
202 Flags flags = new Flags();