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.adapters.appc.orchestrator.client;
23 import java.lang.reflect.InvocationTargetException;
24 import java.lang.reflect.Method;
25 import java.time.Instant;
26 import java.util.Optional;
27 import java.util.Properties;
28 import java.util.UUID;
29 import java.util.concurrent.ConcurrentHashMap;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.core.env.Environment;
34 import org.springframework.stereotype.Component;
35 import org.onap.appc.client.lcm.api.AppcClientServiceFactoryProvider;
36 import org.onap.appc.client.lcm.api.AppcLifeCycleManagerServiceFactory;
37 import org.onap.appc.client.lcm.api.ApplicationContext;
38 import org.onap.appc.client.lcm.api.LifeCycleManagerStateful;
39 import org.onap.appc.client.lcm.exceptions.AppcClientException;
40 import org.onap.appc.client.lcm.model.Action;
41 import org.onap.appc.client.lcm.model.ActionIdentifiers;
42 import org.onap.appc.client.lcm.model.CommonHeader;
43 import org.onap.appc.client.lcm.model.Flags;
44 import org.onap.appc.client.lcm.model.Flags.Force;
45 import org.onap.appc.client.lcm.model.Flags.Mode;
46 import org.onap.so.adapters.appc.orchestrator.client.ApplicationControllerOrchestratorException;
47 import org.onap.so.adapters.appc.orchestrator.client.ApplicationControllerSupport;
48 import org.onap.so.adapters.appc.orchestrator.client.StatusCategory;
49 import org.onap.appc.client.lcm.model.Payload;
50 import org.onap.appc.client.lcm.model.Status;
51 import org.onap.appc.client.lcm.model.ZULU;
54 public class ApplicationControllerClient {
56 public Environment env;
58 public static final String DEFAULT_CONTROLLER_TYPE = "APPC";
60 private static final String CLIENT_NAME = "MSO";
62 private static final String API_VER = "2.00";
63 private static final String ORIGINATOR_ID = "MSO";
64 private static final int FLAGS_TTL = 65000;
65 private static Logger logger = LoggerFactory.getLogger(ApplicationControllerClient.class);
68 private ApplicationControllerSupport appCSupport;
70 // APPC gave us an API where the controllerType is configured in the
71 // client object, which is not what we asked for. We asked for an API
72 // in which the client would have additional methods that could take
73 // the controllerType as a parameter, so that we would not need to
74 // maintain multiple client objects. This map should be removed when
75 // the (hopefully short-term) controllerType becomes obsolete.
77 private String controllerType = DEFAULT_CONTROLLER_TYPE;
79 private static ConcurrentHashMap<String, LifeCycleManagerStateful> appCClients = new ConcurrentHashMap<>();
82 * Creates an ApplicationControllerClient for the specified controller type.
84 * @param controllerType the controller type: "appc" or "sdnc".
86 public void setControllerType(String controllerType) {
87 if (controllerType == null) {
88 controllerType = DEFAULT_CONTROLLER_TYPE;
90 this.controllerType = controllerType.toUpperCase();
94 * Gets the controller type.
96 * @return the controllertype
98 public String getControllerType() {
99 return controllerType;
103 * Returns the AppC client object associated with this ApplicationControllerClient. AppC client objects are shared
104 * objects. One is created if it does not exist.
106 * @return the client object, or null if creation failed
108 public LifeCycleManagerStateful getAppCClient() {
109 return appCClients.computeIfAbsent(controllerType, k -> createAppCClient(k));
112 protected LifeCycleManagerStateful createAppCClient(String controllerType) {
115 if (controllerType == null) {
116 controllerType = DEFAULT_CONTROLLER_TYPE;
118 controllerType = controllerType.toUpperCase();
120 return AppcClientServiceFactoryProvider.getFactory(AppcLifeCycleManagerServiceFactory.class)
121 .createLifeCycleManagerStateful(new ApplicationContext(), getLCMProperties(controllerType));
122 } catch (AppcClientException e) {
123 logger.error("Error in getting LifeCycleManagerStateful: {}", e.getMessage(), e);
124 // This null value will cause NullPointerException when used later.
125 // Error handling could certainly be improved here.
130 public Status vnfCommand(Action action, String requestId, String vnfId, Optional<String> vserverId,
131 Optional<String> request, String controllerType, ApplicationControllerCallback listener, String requestorId)
132 throws ApplicationControllerOrchestratorException {
133 this.setControllerType(controllerType);
135 ActionIdentifiers actionIdentifiers = new ActionIdentifiers();
136 actionIdentifiers.setVnfId(vnfId);
137 if (vserverId.isPresent()) {
138 actionIdentifiers.setVserverId(vserverId.get());
140 Payload payload = null;
141 if (request.isPresent()) {
142 payload = new Payload(request.get());
145 status = runCommand(action, actionIdentifiers, payload, requestId, listener, requestorId);
146 if (appCSupport.getCategoryOf(status).equals(StatusCategory.ERROR)) {
147 throw new ApplicationControllerOrchestratorException(status.getMessage(), status.getCode());
154 public Status runCommand(Action action, org.onap.appc.client.lcm.model.ActionIdentifiers actionIdentifiers,
155 org.onap.appc.client.lcm.model.Payload payload, String requestID, ApplicationControllerCallback listener,
156 String requestorId) throws ApplicationControllerOrchestratorException {
158 Object requestObject;
159 requestObject = createRequest(action, actionIdentifiers, payload, requestID, requestorId);
160 appCSupport.logLCMMessage(requestObject);
161 LifeCycleManagerStateful client = getAppCClient();
162 Method lcmMethod = appCSupport.getAPIMethod(action.name(), client, true);
164 Object response = lcmMethod.invoke(client, requestObject, listener);
165 if (response != null) {
166 return appCSupport.getStatusFromGenericResponse(response);
170 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
171 throw new RuntimeException(String.format("%s : %s", "Unable to invoke action", action.toString()), e);
175 protected Properties getLCMProperties() {
176 return getLCMProperties("appc");
179 protected Properties getLCMProperties(String controllerType) {
180 Properties properties = new Properties();
182 properties.put("topic.read", this.env.getProperty("appc.client.topic.read.name"));
183 properties.put("topic.write", this.env.getProperty("appc.client.topic.write"));
184 properties.put("SDNC-topic.read", this.env.getProperty("appc.client.topic.sdnc.read"));
185 properties.put("SDNC-topic.write", this.env.getProperty("appc.client.topic.sdnc.write"));
186 properties.put("topic.read.timeout", this.env.getProperty("appc.client.topic.read.timeout"));
187 properties.put("client.response.timeout", this.env.getProperty("appc.client.response.timeout"));
188 properties.put("poolMembers", this.env.getProperty("appc.client.poolMembers"));
189 properties.put("controllerType", controllerType);
190 properties.put("client.key", this.env.getProperty("appc.client.key"));
191 properties.put("client.secret", this.env.getProperty("appc.client.secret"));
192 properties.put("client.name", CLIENT_NAME);
193 properties.put("service", this.env.getProperty("appc.client.service"));
197 public Object createRequest(Action action, ActionIdentifiers identifier, Payload payload, String requestId,
198 String requestorId) {
199 Object requestObject = appCSupport.getInput(action.name());
203 CommonHeader commonHeader = buildCommonHeader(requestId, requestorId);
204 requestObject.getClass().getDeclaredMethod("setCommonHeader", CommonHeader.class).invoke(requestObject,
206 requestObject.getClass().getDeclaredMethod("setAction", Action.class).invoke(requestObject, action);
207 requestObject.getClass().getDeclaredMethod("setActionIdentifiers", ActionIdentifiers.class)
208 .invoke(requestObject, identifier);
209 if (payload != null) {
210 logger.info("payload in RunCommand: " + payload.getValue());
211 requestObject.getClass().getDeclaredMethod("setPayload", Payload.class).invoke(requestObject, payload);
213 } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
214 logger.error("Error building Appc request", e);
216 return requestObject;
219 private CommonHeader buildCommonHeader(String requestId, String requestorId) {
220 CommonHeader commonHeader = new CommonHeader();
221 commonHeader.setApiVer(API_VER);
222 commonHeader.setOriginatorId(ORIGINATOR_ID);
223 commonHeader.setRequestId(requestId == null ? UUID.randomUUID().toString() : requestId);
224 commonHeader.setSubRequestId(UUID.randomUUID().toString());
225 // commonHeader.setXOnapRequestorid(requestorId);
226 Flags flags = new Flags();
227 String flagsMode = "NORMAL";
228 Mode mode = Mode.valueOf(flagsMode);
230 String flagsForce = "FALSE";
231 Force force = Force.valueOf(flagsForce);
232 flags.setForce(force);
233 flags.setTtl(FLAGS_TTL);
234 commonHeader.setFlags(flags);
235 Instant timestamp = Instant.now();
236 ZULU zulu = new ZULU(timestamp.toString());
237 commonHeader.setTimestamp(zulu);
241 public Flags createRequestFlags() {
242 Flags flags = new Flags();