AT&T 1712 and 1802 release code
[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
30 import org.openecomp.mso.bpmn.core.PropertyConfiguration;
31 import org.springframework.beans.factory.annotation.Autowired;
32
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 import com.att.eelf.configuration.EELFLogger;
48 import com.att.eelf.configuration.EELFLogger.Level;
49 import com.att.eelf.configuration.EELFManager;
50
51 public class ApplicationControllerClient {
52
53         private static final String CLIENT_NAME = "MSO";
54
55         private static final String API_VER = "2.00";
56         private static final String ORIGINATOR_ID = "MSO";
57         private static final int FLAGS_TTL = 65000;
58         protected final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
59
60         @Autowired
61         public ApplicationControllerSupport appCSupport;
62
63         private static LifeCycleManagerStateful client;
64
65         public ApplicationControllerClient() {
66                 appCSupport = new ApplicationControllerSupport();
67                 client = this.getAppCClient();
68         }
69
70         public Status runCommand(Action action, org.onap.appc.client.lcm.model.ActionIdentifiers actionIdentifiers, org.onap.appc.client.lcm.model.Payload payload, String requestID)
71                         throws ApplicationControllerOrchestratorException {
72                 Object requestObject;
73                 requestObject = createRequest(action, actionIdentifiers, payload, requestID);
74                 appCSupport.logLCMMessage(requestObject);
75                 Method lcmMethod = appCSupport.getAPIMethod(action.name(), client, false);
76                 try {
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);
81                 }
82         }
83
84         public LifeCycleManagerStateful getAppCClient() {
85                 if (client == null)
86                         try {
87                                 client = AppcClientServiceFactoryProvider.getFactory(AppcLifeCycleManagerServiceFactory.class)
88                                                 .createLifeCycleManagerStateful(new ApplicationContext(), getLCMProperties());
89                         } catch (AppcClientException e) {
90                                 auditLogger.log(Level.ERROR, "Error in getting LifeCycleManagerStateful: ", e, e.getMessage());
91                         }
92                 return client;
93         }
94
95         protected Properties getLCMProperties() {
96                 Properties properties = new Properties();
97                 Map<String, String> globalProperties = PropertyConfiguration.getInstance()
98                                 .getProperties("mso.bpmn.urn.properties");
99
100                 properties.put("topic.read", globalProperties.get("appc.topic.read"));
101                 properties.put("topic.read.timeout", globalProperties.get("appc.topic.read.timeout"));
102                 properties.put("client.response.timeout", globalProperties.get("appc.client.response.timeout"));
103                 properties.put("topic.write", globalProperties.get("appc.topic.write"));
104                 properties.put("poolMembers", globalProperties.get("appc.poolMembers"));
105                 properties.put("client.key", globalProperties.get("appc.client.key"));
106                 properties.put("client.secret", globalProperties.get("appc.client.secret"));
107                 properties.put("client.name", CLIENT_NAME);
108                 properties.put("service", globalProperties.get("appc.service"));
109                 return properties;
110         }
111
112         public Object createRequest(Action action, ActionIdentifiers identifier, Payload payload, String requestId) {
113                 Object requestObject = appCSupport.getInput(action.name());
114                 try {
115                         CommonHeader commonHeader = buildCommonHeader(requestId);
116                         requestObject.getClass().getDeclaredMethod("setCommonHeader", CommonHeader.class).invoke(requestObject,
117                                         commonHeader);
118                         requestObject.getClass().getDeclaredMethod("setAction", Action.class).invoke(requestObject, action);
119                         requestObject.getClass().getDeclaredMethod("setActionIdentifiers", ActionIdentifiers.class)
120                                         .invoke(requestObject, identifier);
121                         if (payload != null) {
122                                 requestObject.getClass().getDeclaredMethod("setPayload", Payload.class).invoke(requestObject, payload);
123                         }
124                 } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
125                         auditLogger.log(Level.ERROR, "Error building Appc request: ", e, e.getMessage());
126                 }
127                 return requestObject;
128         }
129
130         private CommonHeader buildCommonHeader(String requestId) {
131                 CommonHeader commonHeader = new CommonHeader();
132                 commonHeader.setApiVer(API_VER);
133                 commonHeader.setOriginatorId(ORIGINATOR_ID);
134                 commonHeader.setRequestId(requestId == null ? UUID.randomUUID().toString() : requestId);
135                 commonHeader.setSubRequestId(requestId);
136                 Flags flags = new Flags();
137                 String flagsMode = "NORMAL";
138                 Mode mode = Mode.valueOf(flagsMode);
139                 flags.setMode(mode);
140                 String flagsForce = "FALSE";
141                 Force force = Force.valueOf(flagsForce);
142                 flags.setForce(force);
143                 flags.setTtl(FLAGS_TTL);
144                 commonHeader.setFlags(flags);
145                 Instant timestamp = Instant.now();
146                 ZULU zulu = new ZULU(timestamp.toString());
147                 commonHeader.setTimestamp(zulu);
148                 return commonHeader;
149         }
150
151         public Flags createRequestFlags() {
152                 Flags flags = new Flags();
153                 flags.setTtl(6000);
154                 return flags;
155         }
156 }