Merge "Fixed Blocker issues. IssueId: SO-165"
[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.beans.BeanInfo;
24
25 import java.util.Map;
26
27 import org.openecomp.mso.bpmn.core.PropertyConfiguration;
28
29 import java.beans.IntrospectionException;
30 import java.beans.Introspector;
31 import java.beans.PropertyDescriptor;
32 import java.lang.reflect.InvocationTargetException;
33 import java.lang.reflect.Method;
34 import java.time.Instant;
35 import java.util.Properties;
36 import java.util.UUID;
37
38 import org.springframework.beans.factory.annotation.Autowired;
39
40 import org.openecomp.appc.client.lcm.api.AppcClientServiceFactoryProvider;
41 import org.openecomp.appc.client.lcm.api.AppcLifeCycleManagerServiceFactory;
42 import org.openecomp.appc.client.lcm.api.ApplicationContext;
43 import org.openecomp.appc.client.lcm.api.LifeCycleManagerStateful;
44 import org.openecomp.appc.client.lcm.api.ResponseHandler;
45 import org.openecomp.appc.client.lcm.exceptions.AppcClientException;
46 import org.openecomp.appc.client.lcm.model.Action;
47 import org.openecomp.appc.client.lcm.model.ActionIdentifiers;
48 import org.openecomp.appc.client.lcm.model.AuditOutput;
49 import org.openecomp.appc.client.lcm.model.CommonHeader;
50 import org.openecomp.appc.client.lcm.model.Flags;
51 import org.openecomp.appc.client.lcm.model.Flags.Force;
52 import org.openecomp.appc.client.lcm.model.Flags.Mode;
53 import org.openecomp.appc.client.lcm.model.Payload;
54 import org.openecomp.appc.client.lcm.model.Status;
55 import org.openecomp.appc.client.lcm.model.ZULU;
56 import com.fasterxml.jackson.annotation.JsonInclude.Include;
57 import com.fasterxml.jackson.core.JsonProcessingException;
58 import com.fasterxml.jackson.databind.ObjectMapper;
59 import com.fasterxml.jackson.databind.ObjectWriter;
60 import org.openecomp.mso.logger.MsoLogger;
61
62 public class ApplicationControllerClient {
63
64     private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
65     
66         private static final int ACCEPT_SERIES = 100;
67         private static final int ERROR_SERIES = 200;
68         private static final int REJECT_SERIES = 300;
69         private static final int SUCCESS_SERIES = 400;
70         private static final int SUCCESS_STATUS = SUCCESS_SERIES + 0;
71         private static final int PARTIAL_SERIES = 500;
72         private static final int PARTIAL_SUCCESS_STATUS = PARTIAL_SERIES + 0;
73
74         private final boolean useLCMBypass = false;
75
76         private final String apiVer = "2.00";
77         private final String originatorId = "MSO";
78         private final int flagsTTL = 65000;
79         private final static String clientName = "MSO";
80
81         @Autowired
82         public ApplicationControllerSupport appCSupport;
83
84         private LifeCycleManagerStateful client;
85
86         public Status runCommand(Action action, ActionIdentifiers identifier, Flags flags, Payload payload,
87                         String requestID) throws Exception {
88                 Object requestObject = createRequest(action, identifier, flags, payload, requestID);
89                 client = getAppCClient();
90                 Method lcmMethod = appCSupport.getAPIMethod(action.name(), client, false);
91                 appCSupport.logLCMMessage(requestObject);
92                 Object response = lcmMethod.invoke(client, requestObject);
93                 return appCSupport.getStatusFromGenericResponse(response);
94         }
95
96         public void shutdownclient() {
97                 AppcClientServiceFactoryProvider.getFactory(AppcLifeCycleManagerServiceFactory.class)
98                                 .shutdownLifeCycleManager(false);
99         }
100
101         public LifeCycleManagerStateful getAppCClient() throws AppcClientException {
102                 if (client == null)
103                         client = AppcClientServiceFactoryProvider.getFactory(AppcLifeCycleManagerServiceFactory.class)
104                                         .createLifeCycleManagerStateful(new ApplicationContext(), getLCMProperties());
105                 return client;
106         }
107
108         private Properties getLCMProperties() {
109                 return getLCMPropertiesHelper();
110         }
111
112         protected Properties getLCMPropertiesHelper() {
113                 Properties properties = new Properties();
114                 Map<String, String> globalProperties = PropertyConfiguration.getInstance()
115                                 .getProperties("mso.bpmn.urn.properties");
116
117                 properties.put("topic.read", globalProperties.get("appc.topic.read"));
118                 properties.put("topic.read.timeout", globalProperties.get("appc.topic.read.timeout"));
119                 properties.put("client.response.timeout", globalProperties.get("appc.client.response.timeout"));
120                 properties.put("topic.write", globalProperties.get("appc.topic.write"));
121                 properties.put("poolMembers", globalProperties.get("appc.pool.members"));
122                 properties.put("client.key", globalProperties.get("appc.client.key"));
123                 properties.put("client.secret", globalProperties.get("appc.client.secret"));
124                 properties.put("client.name", clientName);
125                 return properties;
126         }
127
128         public Object createRequest(Action action, ActionIdentifiers identifier, Flags flags, Payload payload,
129                         String requestId) throws Exception {
130                 Object requestObject = appCSupport.getInput(action.name());
131                 try {
132                         org.openecomp.appc.client.lcm.model.CommonHeader commonHeader = buildCommonHeader(requestId);
133                         requestObject.getClass().getDeclaredMethod("setCommonHeader", CommonHeader.class).invoke(requestObject,
134                                         commonHeader);
135                         requestObject.getClass().getDeclaredMethod("setAction", Action.class).invoke(requestObject, action);
136                         requestObject.getClass().getDeclaredMethod("setActionIdentifiers", ActionIdentifiers.class)
137                                         .invoke(requestObject, identifier);
138                 } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
139                     LOGGER.debug("Exception:", e);
140                         throw new Exception("Error Building AppC Request: " + e.getMessage());
141                 }
142                 return requestObject;
143         }
144
145         private org.openecomp.appc.client.lcm.model.CommonHeader buildCommonHeader(String requestId) {
146                 org.openecomp.appc.client.lcm.model.CommonHeader commonHeader = new org.openecomp.appc.client.lcm.model.CommonHeader();
147                 commonHeader.setApiVer(apiVer);
148                 commonHeader.setOriginatorId(originatorId);
149                 commonHeader.setRequestId(requestId == null ? UUID.randomUUID().toString() : requestId);
150                 commonHeader.setSubRequestId(requestId);
151                 org.openecomp.appc.client.lcm.model.Flags flags = new org.openecomp.appc.client.lcm.model.Flags();
152                 String flagsMode = "NORMAL";
153                 Mode mode = Mode.valueOf(flagsMode);
154                 flags.setMode(mode);
155                 String flagsForce = "FALSE";
156                 Force force = Force.valueOf(flagsForce);
157                 flags.setForce(force);
158                 flags.setTtl(flagsTTL);
159                 commonHeader.setFlags(flags);
160                 Instant timestamp = Instant.now();
161                 ZULU zulu = new ZULU(timestamp.toString());
162                 commonHeader.setTimestamp(zulu);
163                 return commonHeader;
164         }
165
166         public Flags createRequestFlags() {
167                 Flags flags = new Flags();
168                 flags.setTtl(6000);
169                 return flags;
170         }
171 }