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