89eff5f65a557e5df35a9a62be9d18330589f44d
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / client / appc / ApplicationControllerClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.client.appc;
22
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;
29
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;
47
48 public class ApplicationControllerClient {
49
50         public static final String DEFAULT_CONTROLLER_TYPE = "APPC";
51
52         private static final String CLIENT_NAME = "MSO";
53
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);
58
59         private ApplicationControllerSupport appCSupport;
60
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.
67
68         private final String controllerType;
69
70         private static ConcurrentHashMap<String, LifeCycleManagerStateful> appCClients = new ConcurrentHashMap<>();
71
72         /**
73          * Creates an ApplicationControllerClient for communication with APP-C.
74          */
75         public ApplicationControllerClient() {
76                 this(DEFAULT_CONTROLLER_TYPE);
77         }
78         
79         /**
80          * Creates an ApplicationControllerClient for the specified controller type.
81          * @param controllerType the controller type: "appc" or "sdnc".
82          */
83         public ApplicationControllerClient(String controllerType) {
84                 if (controllerType == null) {
85                         controllerType = DEFAULT_CONTROLLER_TYPE;
86                 }
87                 this.controllerType = controllerType.toUpperCase();
88                 appCSupport = new ApplicationControllerSupport();
89         }
90
91         /**
92          * Gets the controller type.
93          * @return the controllertype
94          */
95         public String getControllerType() {
96                 return controllerType;
97         }
98
99         /**
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
103          */
104         public LifeCycleManagerStateful getAppCClient() {
105                 return appCClients.computeIfAbsent(controllerType, k -> createAppCClient(k));
106         }
107
108         protected LifeCycleManagerStateful createAppCClient(String controllerType) {
109                 try {
110                         if (controllerType == null) {
111                                 controllerType = DEFAULT_CONTROLLER_TYPE;
112                         }
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.
120                         return null;
121                 }
122         }
123
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);
132                 try {
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);
137                 }
138         }
139
140         protected Properties getLCMProperties() {
141                 return getLCMProperties("appc");
142         }
143         
144         protected Properties getLCMProperties(String controllerType) {
145                 Properties properties = new Properties();
146
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"));
159                 return properties;
160         }
161
162         public Object createRequest(Action action, ActionIdentifiers identifier, Payload payload, String requestId) {
163                 Object requestObject = appCSupport.getInput(action.name());
164                 try {
165                         CommonHeader commonHeader = buildCommonHeader(requestId);
166                         requestObject.getClass().getDeclaredMethod("setCommonHeader", CommonHeader.class).invoke(requestObject,
167                                         commonHeader);
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);
173                         }
174                 } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
175                         logger.error("Error building Appc request", e);
176                 }
177                 return requestObject;
178         }
179
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);
189                 flags.setMode(mode);
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);
198                 return commonHeader;
199         }
200
201         public Flags createRequestFlags() {
202                 Flags flags = new Flags();
203                 flags.setTtl(6000);
204                 return flags;
205         }
206 }