Merge "Reorder modifiers"
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / openecomp / mso / 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.openecomp.mso.client.appc;
22
23 import java.lang.reflect.InvocationTargetException;
24 import java.lang.reflect.Method;
25 import java.time.Instant;
26 import java.util.Map;
27 import java.util.Properties;
28 import java.util.UUID;
29 import java.util.concurrent.ConcurrentHashMap;
30
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;
47
48 import com.att.eelf.configuration.EELFLogger;
49 import com.att.eelf.configuration.EELFLogger.Level;
50 import com.att.eelf.configuration.EELFManager;
51
52 public class ApplicationControllerClient {
53         
54         public static final String DEFAULT_CONTROLLER_TYPE = "SDNC";
55
56         private static final String CLIENT_NAME = "MSO";
57
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();
62
63         @Autowired
64         public ApplicationControllerSupport appCSupport;
65
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.
72
73         private final String controllerType;
74
75         private static ConcurrentHashMap<String, LifeCycleManagerStateful> appCClients = new ConcurrentHashMap<>();
76
77         /**
78          * Creates an ApplicationControllerClient for communication with APP-C.
79          */
80         public ApplicationControllerClient() {
81                 this(DEFAULT_CONTROLLER_TYPE);
82         }
83         
84         /**
85          * Creates an ApplicationControllerClient for the specified controller type.
86          * @param controllerType the controller type: "appc" or "sdnc".
87          */
88         public ApplicationControllerClient(String controllerType) {
89                 if (controllerType == null) {
90                         controllerType = DEFAULT_CONTROLLER_TYPE;
91                 }
92                 this.controllerType = controllerType.toUpperCase();
93                 appCSupport = new ApplicationControllerSupport();
94         }
95         
96         /**
97          * Gets the controller type.
98          * @return the controllertype
99          */
100         public String getControllerType() {
101                 return controllerType;
102         }
103
104         /**
105          * Returns the AppC client object associated with this ApplicationControllerClient.
106          * AppC client objects are shared objects.  One is created if it does not exist.
107          * @return the client object, or null if creation failed
108          */
109         public LifeCycleManagerStateful getAppCClient() {
110                 return appCClients.computeIfAbsent(controllerType, k -> createAppCClient(k));
111         }
112
113         protected LifeCycleManagerStateful createAppCClient(String controllerType) {
114                 try {
115                         if (controllerType == null) {
116                                 controllerType = DEFAULT_CONTROLLER_TYPE;
117                         }
118                         controllerType = controllerType.toUpperCase();
119                         return AppcClientServiceFactoryProvider.getFactory(AppcLifeCycleManagerServiceFactory.class)
120                                         .createLifeCycleManagerStateful(new ApplicationContext(), getLCMProperties(controllerType));
121                 } catch (AppcClientException e) {
122                         auditLogger.log(Level.ERROR, "Error in getting LifeCycleManagerStateful: ", e, e.getMessage());
123                         // This null value will cause NullPointerException when used later.
124                         // Error handling could certainly be improved here.
125                         return null;
126                 }
127         }
128
129         public Status runCommand(Action action, org.onap.appc.client.lcm.model.ActionIdentifiers actionIdentifiers,
130                         org.onap.appc.client.lcm.model.Payload payload, String requestID)
131                         throws ApplicationControllerOrchestratorException {
132                 Object requestObject = createRequest(action, actionIdentifiers, payload, requestID);
133                 appCSupport.logLCMMessage(requestObject);
134                 LifeCycleManagerStateful client = getAppCClient();
135                 Method lcmMethod = appCSupport.getAPIMethod(action.name(), client, false);
136                 try {
137                         Object response = lcmMethod.invoke(client, requestObject);
138                         return appCSupport.getStatusFromGenericResponse(response);
139                 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
140                         throw new RuntimeException(String.format("%s : %s", "Unable to invoke action", action.toString()), e);
141                 }
142         }
143
144         protected Properties getLCMProperties() {
145                 return getLCMProperties("appc");
146         }
147         
148         protected Properties getLCMProperties(String controllerType) {
149                 Properties properties = new Properties();
150                 Map<String, String> globalProperties = PropertyConfiguration.getInstance()
151                                 .getProperties("mso.bpmn.urn.properties");
152                 
153                 properties.put("topic.read", globalProperties.get("appc.client.topic.read"));
154                 properties.put("topic.write", globalProperties.get("appc.client.topic.write"));
155                 properties.put("SDNC-topic.read", globalProperties.get("appc.client.topic.sdnc.read"));
156                 properties.put("SDNC-topic.write", globalProperties.get("appc.client.topic.sdnc.write"));
157                 properties.put("topic.read.timeout", globalProperties.get("appc.client.topic.read.timeout"));
158                 properties.put("client.response.timeout", globalProperties.get("appc.client.response.timeout"));
159                 properties.put("poolMembers", globalProperties.get("appc.client.poolMembers"));
160                 properties.put("controllerType", controllerType);
161                 properties.put("client.key", globalProperties.get("appc.client.key"));
162                 properties.put("client.secret", globalProperties.get("appc.client.secret"));
163                 properties.put("client.name", CLIENT_NAME);
164                 properties.put("service", globalProperties.get("appc.client.service"));
165                 return properties;
166         }
167
168         public Object createRequest(Action action, ActionIdentifiers identifier, Payload payload, String requestId) {
169                 Object requestObject = appCSupport.getInput(action.name());
170                 try {
171                         CommonHeader commonHeader = buildCommonHeader(requestId);
172                         requestObject.getClass().getDeclaredMethod("setCommonHeader", CommonHeader.class).invoke(requestObject,
173                                         commonHeader);
174                         requestObject.getClass().getDeclaredMethod("setAction", Action.class).invoke(requestObject, action);
175                         requestObject.getClass().getDeclaredMethod("setActionIdentifiers", ActionIdentifiers.class)
176                                         .invoke(requestObject, identifier);
177                         if (payload != null) {
178                                 requestObject.getClass().getDeclaredMethod("setPayload", Payload.class).invoke(requestObject, payload);
179                         }
180                 } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
181                         auditLogger.log(Level.ERROR, "Error building Appc request: ", e, e.getMessage());
182                 }
183                 return requestObject;
184         }
185
186         private CommonHeader buildCommonHeader(String requestId) {
187                 CommonHeader commonHeader = new CommonHeader();
188                 commonHeader.setApiVer(API_VER);
189                 commonHeader.setOriginatorId(ORIGINATOR_ID);
190                 commonHeader.setRequestId(requestId == null ? UUID.randomUUID().toString() : requestId);
191                 commonHeader.setSubRequestId(requestId);
192                 Flags flags = new Flags();
193                 String flagsMode = "NORMAL";
194                 Mode mode = Mode.valueOf(flagsMode);
195                 flags.setMode(mode);
196                 String flagsForce = "FALSE";
197                 Force force = Force.valueOf(flagsForce);
198                 flags.setForce(force);
199                 flags.setTtl(FLAGS_TTL);
200                 commonHeader.setFlags(flags);
201                 Instant timestamp = Instant.now();
202                 ZULU zulu = new ZULU(timestamp.toString());
203                 commonHeader.setTimestamp(zulu);
204                 return commonHeader;
205         }
206
207         public Flags createRequestFlags() {
208                 Flags flags = new Flags();
209                 flags.setTtl(6000);
210                 return flags;
211         }
212 }