Containerization feature of SO
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / client / appc / ApplicationControllerClientV2.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 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
36 import javax.annotation.PostConstruct;
37 import java.lang.reflect.InvocationTargetException;
38 import java.lang.reflect.Method;
39 import java.time.Instant;
40 import java.util.Properties;
41 import java.util.UUID;
42
43 @Component
44 @Deprecated
45 public class ApplicationControllerClientV2 {
46
47         private static final String CLIENT_NAME = "MSO";
48         private static final String API_VER = "2.00";
49         private static final String ORIGINATOR_ID = "MSO";
50         private static final int FLAGS_TTL = 65000;
51         private static Logger logger = LoggerFactory.getLogger(ApplicationControllerClientV2.class);
52
53         //@Autowired
54         ApplicationControllerConfiguration applicationControllerConfiguration;
55
56         //@Autowired
57         private ApplicationControllerSupport appCSupport;
58
59         private static LifeCycleManagerStateful client;
60
61         //@PostConstruct
62         public void buildClient() {
63                 client = this.getAppCClient("");
64         }
65         
66         //@PostConstruct
67         public void buildClient(String controllerType) {
68                 client = this.getAppCClient(controllerType);
69         }
70
71         public Status runCommand(Action action, ActionIdentifiers actionIdentifiers, Payload payload, String requestID)
72                         throws ApplicationControllerOrchestratorException {
73                 Object requestObject;
74                 requestObject = createRequest(action, actionIdentifiers, payload, requestID);
75                 appCSupport.logLCMMessage(requestObject);
76                 Method lcmMethod = appCSupport.getAPIMethod(action.name(), client, false);
77                 try {
78                         Object response = lcmMethod.invoke(client, requestObject);
79                         return appCSupport.getStatusFromGenericResponse(response);
80                 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
81                         throw new RuntimeException(String.format("%s : %s", "Unable to invoke action", action.toString()), e);
82                 }
83         }
84
85         public LifeCycleManagerStateful getAppCClient(String controllerType) {
86                 if (client == null)
87                         try {
88                                 client = AppcClientServiceFactoryProvider.getFactory(AppcLifeCycleManagerServiceFactory.class)
89                                                 .createLifeCycleManagerStateful(new ApplicationContext(), getLCMProperties(controllerType));
90                         } catch (AppcClientException e) {
91                                 logger.error("Error in getting LifeCycleManagerStateful Client", e);
92                         }
93                 return client;
94         }
95
96         protected Properties getLCMProperties(String controllerType) {
97                 Properties properties = new Properties();
98                 properties.put("topic.read", applicationControllerConfiguration.getReadTopic());
99                 properties.put("topic.read.timeout", applicationControllerConfiguration.getReadTimeout());
100                 properties.put("client.response.timeout",  applicationControllerConfiguration.getResponseTimeout());
101                 properties.put("topic.write",  applicationControllerConfiguration.getWrite());
102                 properties.put("poolMembers", applicationControllerConfiguration.getPoolMembers());
103                 properties.put("client.key", applicationControllerConfiguration.getClientKey());
104                 properties.put("client.secret", applicationControllerConfiguration.getClientSecret());
105                 properties.put("client.name", CLIENT_NAME);
106                 properties.put("service", applicationControllerConfiguration.getService());
107                 return properties;
108         }
109
110         public Object createRequest(Action action, ActionIdentifiers identifier, Payload payload, String requestId) {
111                 Object requestObject = appCSupport.getInput(action.name());
112                 try {
113                         CommonHeader commonHeader = buildCommonHeader(requestId);
114                         requestObject.getClass().getDeclaredMethod("setCommonHeader", CommonHeader.class).invoke(requestObject,
115                                         commonHeader);
116                         requestObject.getClass().getDeclaredMethod("setAction", Action.class).invoke(requestObject, action);
117                         requestObject.getClass().getDeclaredMethod("setActionIdentifiers", ActionIdentifiers.class)
118                                         .invoke(requestObject, identifier);
119                         if (payload != null) {
120                                 requestObject.getClass().getDeclaredMethod("setPayload", Payload.class).invoke(requestObject, payload);
121                         }
122                 } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
123                         logger.error("Error building Appc request", e);
124                 }
125                 return requestObject;
126         }
127
128         private CommonHeader buildCommonHeader(String requestId) {
129                 CommonHeader commonHeader = new CommonHeader();
130                 commonHeader.setApiVer(API_VER);
131                 commonHeader.setOriginatorId(ORIGINATOR_ID);
132                 commonHeader.setRequestId(requestId == null ? UUID.randomUUID().toString() : requestId);
133                 commonHeader.setSubRequestId(requestId);
134                 Flags flags = new Flags();
135                 String flagsMode = "NORMAL";
136                 Mode mode = Mode.valueOf(flagsMode);
137                 flags.setMode(mode);
138                 String flagsForce = "FALSE";
139                 Force force = Force.valueOf(flagsForce);
140                 flags.setForce(force);
141                 flags.setTtl(FLAGS_TTL);
142                 commonHeader.setFlags(flags);
143                 Instant timestamp = Instant.now();
144                 ZULU zulu = new ZULU(timestamp.toString());
145                 commonHeader.setTimestamp(zulu);
146                 return commonHeader;
147         }
148
149         public Flags createRequestFlags() {
150                 Flags flags = new Flags();
151                 flags.setTtl(6000);
152                 return flags;
153         }
154 }