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 org.onap.appc.client.lcm.api.AppcClientServiceFactoryProvider;
24 import org.onap.appc.client.lcm.api.AppcLifeCycleManagerServiceFactory;
25 import org.onap.appc.client.lcm.api.ApplicationContext;
26 import org.onap.appc.client.lcm.api.LifeCycleManagerStateful;
27 import org.onap.appc.client.lcm.exceptions.AppcClientException;
28 import org.onap.appc.client.lcm.model.*;
29 import org.onap.appc.client.lcm.model.Flags.Force;
30 import org.onap.appc.client.lcm.model.Flags.Mode;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Component;
35 import javax.annotation.PostConstruct;
36 import java.lang.reflect.InvocationTargetException;
37 import java.lang.reflect.Method;
38 import java.time.Instant;
39 import java.util.Properties;
40 import java.util.UUID;
44 public class ApplicationControllerClientV2 {
46 private static final String CLIENT_NAME = "MSO";
47 private static final String API_VER = "2.00";
48 private static final String ORIGINATOR_ID = "MSO";
49 private static final int FLAGS_TTL = 65000;
50 private static Logger logger = LoggerFactory.getLogger(ApplicationControllerClientV2.class);
53 ApplicationControllerConfiguration applicationControllerConfiguration;
56 private ApplicationControllerSupport appCSupport;
58 private static LifeCycleManagerStateful client;
61 public void buildClient() {
62 client = this.getAppCClient("");
66 public void buildClient(String controllerType) {
67 client = this.getAppCClient(controllerType);
70 public Status runCommand(Action action, ActionIdentifiers actionIdentifiers, 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(String controllerType) {
87 client = AppcClientServiceFactoryProvider.getFactory(AppcLifeCycleManagerServiceFactory.class)
88 .createLifeCycleManagerStateful(new ApplicationContext(), getLCMProperties(controllerType));
89 } catch (AppcClientException e) {
90 logger.error("Error in getting LifeCycleManagerStateful Client", e);
95 protected Properties getLCMProperties(String controllerType) {
96 Properties properties = new Properties();
97 properties.put("topic.read", applicationControllerConfiguration.getReadTopic());
98 properties.put("topic.read.timeout", applicationControllerConfiguration.getReadTimeout());
99 properties.put("client.response.timeout", applicationControllerConfiguration.getResponseTimeout());
100 properties.put("topic.write", applicationControllerConfiguration.getWrite());
101 properties.put("poolMembers", applicationControllerConfiguration.getPoolMembers());
102 properties.put("client.key", applicationControllerConfiguration.getClientKey());
103 properties.put("client.secret", applicationControllerConfiguration.getClientSecret());
104 properties.put("client.name", CLIENT_NAME);
105 properties.put("service", applicationControllerConfiguration.getService());
109 public Object createRequest(Action action, ActionIdentifiers identifier, Payload payload, String requestId) {
110 Object requestObject = appCSupport.getInput(action.name());
112 CommonHeader commonHeader = buildCommonHeader(requestId);
113 requestObject.getClass().getDeclaredMethod("setCommonHeader", CommonHeader.class).invoke(requestObject,
115 requestObject.getClass().getDeclaredMethod("setAction", Action.class).invoke(requestObject, action);
116 requestObject.getClass().getDeclaredMethod("setActionIdentifiers", ActionIdentifiers.class)
117 .invoke(requestObject, identifier);
118 if (payload != null) {
119 requestObject.getClass().getDeclaredMethod("setPayload", Payload.class).invoke(requestObject, payload);
121 } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
122 logger.error("Error building Appc request", e);
124 return requestObject;
127 private CommonHeader buildCommonHeader(String requestId) {
128 CommonHeader commonHeader = new CommonHeader();
129 commonHeader.setApiVer(API_VER);
130 commonHeader.setOriginatorId(ORIGINATOR_ID);
131 commonHeader.setRequestId(requestId == null ? UUID.randomUUID().toString() : requestId);
132 commonHeader.setSubRequestId(requestId);
133 Flags flags = new Flags();
134 String flagsMode = "NORMAL";
135 Mode mode = Mode.valueOf(flagsMode);
137 String flagsForce = "FALSE";
138 Force force = Force.valueOf(flagsForce);
139 flags.setForce(force);
140 flags.setTtl(FLAGS_TTL);
141 commonHeader.setFlags(flags);
142 Instant timestamp = Instant.now();
143 ZULU zulu = new ZULU(timestamp.toString());
144 commonHeader.setTimestamp(zulu);
148 public Flags createRequestFlags() {
149 Flags flags = new Flags();