diff options
Diffstat (limited to 'bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/appc')
3 files changed, 392 insertions, 0 deletions
diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/appc/ApplicationControllerCallback.java b/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/appc/ApplicationControllerCallback.java new file mode 100644 index 0000000000..794f112fa9 --- /dev/null +++ b/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/appc/ApplicationControllerCallback.java @@ -0,0 +1,19 @@ +package org.openecomp.mso.client.appc; +import org.openecomp.appc.client.lcm.api.ResponseHandler; +import org.openecomp.appc.client.lcm.exceptions.AppcClientException; + +public class ApplicationControllerCallback<T> implements ResponseHandler<T> { + + @Override + public void onResponse(T response) { + System.out.println("ON RESPONSE"); + + } + + @Override + public void onException(AppcClientException exception) { + System.out.println("ON EXCEPTION"); + + } + +} diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/appc/ApplicationControllerClient.java b/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/appc/ApplicationControllerClient.java new file mode 100644 index 0000000000..e8dc5af90c --- /dev/null +++ b/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/appc/ApplicationControllerClient.java @@ -0,0 +1,147 @@ +package org.openecomp.mso.client.appc; + +import java.beans.BeanInfo; + +import java.util.Map; + +import org.openecomp.mso.bpmn.core.PropertyConfiguration; + +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.time.Instant; +import java.util.Properties; +import java.util.UUID; + +import org.springframework.beans.factory.annotation.Autowired; + +import org.openecomp.appc.client.lcm.api.AppcClientServiceFactoryProvider; +import org.openecomp.appc.client.lcm.api.AppcLifeCycleManagerServiceFactory; +import org.openecomp.appc.client.lcm.api.ApplicationContext; +import org.openecomp.appc.client.lcm.api.LifeCycleManagerStateful; +import org.openecomp.appc.client.lcm.api.ResponseHandler; +import org.openecomp.appc.client.lcm.exceptions.AppcClientException; +import org.openecomp.appc.client.lcm.model.Action; +import org.openecomp.appc.client.lcm.model.ActionIdentifiers; +import org.openecomp.appc.client.lcm.model.AuditOutput; +import org.openecomp.appc.client.lcm.model.CommonHeader; +import org.openecomp.appc.client.lcm.model.Flags; +import org.openecomp.appc.client.lcm.model.Flags.Force; +import org.openecomp.appc.client.lcm.model.Flags.Mode; +import org.openecomp.appc.client.lcm.model.Payload; +import org.openecomp.appc.client.lcm.model.Status; +import org.openecomp.appc.client.lcm.model.ZULU; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; + +public class ApplicationControllerClient { + + private static final int ACCEPT_SERIES = 100; + private static final int ERROR_SERIES = 200; + private static final int REJECT_SERIES = 300; + private static final int SUCCESS_SERIES = 400; + private static final int SUCCESS_STATUS = SUCCESS_SERIES + 0; + private static final int PARTIAL_SERIES = 500; + private static final int PARTIAL_SUCCESS_STATUS = PARTIAL_SERIES + 0; + + private final boolean useLCMBypass = false; + + private final String apiVer = "2.00"; + private final String originatorId = "MSO"; + private final int flagsTTL = 65000; + private final static String clientName = "MSO"; + + @Autowired + public ApplicationControllerSupport appCSupport; + + private LifeCycleManagerStateful client; + + public Status runCommand(Action action, ActionIdentifiers identifier, Flags flags, Payload payload, + String requestID) throws Exception { + Object requestObject = createRequest(action, identifier, flags, payload, requestID); + client = getAppCClient(); + Method lcmMethod = appCSupport.getAPIMethod(action.name(), client, false); + appCSupport.logLCMMessage(requestObject); + Object response = lcmMethod.invoke(client, requestObject); + return appCSupport.getStatusFromGenericResponse(response); + } + + public void shutdownclient() { + AppcClientServiceFactoryProvider.getFactory(AppcLifeCycleManagerServiceFactory.class) + .shutdownLifeCycleManager(false); + } + + public LifeCycleManagerStateful getAppCClient() throws AppcClientException { + if (client == null) + client = AppcClientServiceFactoryProvider.getFactory(AppcLifeCycleManagerServiceFactory.class) + .createLifeCycleManagerStateful(new ApplicationContext(), getLCMProperties()); + return client; + } + + private Properties getLCMProperties() { + return getLCMPropertiesHelper(); + } + + protected Properties getLCMPropertiesHelper() { + Properties properties = new Properties(); + Map<String, String> globalProperties = PropertyConfiguration.getInstance() + .getProperties("mso.bpmn.urn.properties"); + + properties.put("topic.read", globalProperties.get("appc.topic.read")); + properties.put("topic.read.timeout", globalProperties.get("appc.topic.read.timeout")); + properties.put("client.response.timeout", globalProperties.get("appc.client.response.timeout")); + properties.put("topic.write", globalProperties.get("appc.topic.write")); + properties.put("poolMembers", globalProperties.get("appc.pool.members")); + properties.put("client.key", globalProperties.get("appc.client.key")); + properties.put("client.secret", globalProperties.get("appc.client.secret")); + properties.put("client.name", clientName); + return properties; + } + + public Object createRequest(Action action, ActionIdentifiers identifier, Flags flags, Payload payload, + String requestId) throws Exception { + Object requestObject = appCSupport.getInput(action.name()); + try { + org.openecomp.appc.client.lcm.model.CommonHeader commonHeader = buildCommonHeader(requestId); + requestObject.getClass().getDeclaredMethod("setCommonHeader", CommonHeader.class).invoke(requestObject, + commonHeader); + requestObject.getClass().getDeclaredMethod("setAction", Action.class).invoke(requestObject, action); + requestObject.getClass().getDeclaredMethod("setActionIdentifiers", ActionIdentifiers.class) + .invoke(requestObject, identifier); + } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { + throw new Exception("Error Building AppC Request: " + e.getMessage()); + } + return requestObject; + } + + private org.openecomp.appc.client.lcm.model.CommonHeader buildCommonHeader(String requestId) { + org.openecomp.appc.client.lcm.model.CommonHeader commonHeader = new org.openecomp.appc.client.lcm.model.CommonHeader(); + commonHeader.setApiVer(apiVer); + commonHeader.setOriginatorId(originatorId); + commonHeader.setRequestId(requestId == null ? UUID.randomUUID().toString() : requestId); + commonHeader.setSubRequestId(requestId); + org.openecomp.appc.client.lcm.model.Flags flags = new org.openecomp.appc.client.lcm.model.Flags(); + String flagsMode = "NORMAL"; + Mode mode = Mode.valueOf(flagsMode); + flags.setMode(mode); + String flagsForce = "FALSE"; + Force force = Force.valueOf(flagsForce); + flags.setForce(force); + flags.setTtl(flagsTTL); + commonHeader.setFlags(flags); + Instant timestamp = Instant.now(); + ZULU zulu = new ZULU(timestamp.toString()); + commonHeader.setTimestamp(zulu); + return commonHeader; + } + + public Flags createRequestFlags() { + Flags flags = new Flags(); + flags.setTtl(6000); + return flags; + } +} diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/appc/ApplicationControllerSupport.java b/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/appc/ApplicationControllerSupport.java new file mode 100644 index 0000000000..c0f1a2067e --- /dev/null +++ b/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/appc/ApplicationControllerSupport.java @@ -0,0 +1,226 @@ +package org.openecomp.mso.client.appc; + + +import java.beans.BeanInfo; +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Properties; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.core.io.support.PropertiesLoaderUtils; +import org.springframework.stereotype.Component; + +import org.openecomp.appc.client.lcm.api.AppcClientServiceFactoryProvider; +import org.openecomp.appc.client.lcm.api.AppcLifeCycleManagerServiceFactory; +import org.openecomp.appc.client.lcm.api.ApplicationContext; +import org.openecomp.appc.client.lcm.api.LifeCycleManagerStateful; +import org.openecomp.appc.client.lcm.api.ResponseHandler; +import org.openecomp.appc.client.lcm.exceptions.AppcClientException; +import org.openecomp.appc.client.lcm.model.Status; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; + + +@Component +public class ApplicationControllerSupport { + + private static final int ACCEPT_SERIES = 100; + private static final int ERROR_SERIES = 200; + private static final int REJECT_SERIES = 300; + private static final int SUCCESS_SERIES = 400; + private static final int SUCCESS_STATUS = SUCCESS_SERIES + 0; + private static final int PARTIAL_SERIES = 500; + private static final int PARTIAL_SUCCESS_STATUS = PARTIAL_SERIES + 0; + private static final int PARTIAL_FAILURE_STATUS = PARTIAL_SERIES + 1; + + @Value("${lcm.model.package:org.openecomp.appc.client.lcm.model}") + private String lcmModelPackage; + + public LifeCycleManagerStateful createService() throws AppcClientException, IOException { + AppcLifeCycleManagerServiceFactory factory = AppcClientServiceFactoryProvider + .getFactory(AppcLifeCycleManagerServiceFactory.class); + return factory.createLifeCycleManagerStateful(new ApplicationContext(), getLCMProperties()); + } + + /** + * @param inputClass + * @return + * @throws ClassNotFoundException + * @throws InstantiationException + * @throws IllegalAccessException + */ + public Object getInput(String action) { + try { + return getInputClass(action).newInstance(); + } catch (IllegalAccessException | InstantiationException e) { + throw new RuntimeException( + String.format("%s : %s", "Unable to instantiate viable LCM Kit input class for action", action), e); + } + } + + /** + * @param action + * @return + * @throws ClassNotFoundException + */ + public Method getAPIMethod(String action, LifeCycleManagerStateful lcmStateful, boolean async) { + Method[] methods = lcmStateful.getClass().getMethods(); + for (Method method : methods) { + if (method.getName().equalsIgnoreCase(action)) { + Class<?>[] methodParameterTypes = method.getParameterTypes(); + if (methodParameterTypes.length > 0) { + if (getInputClass(action).equals(methodParameterTypes[0])) { + if (async) { + if (methodParameterTypes.length == 2 + && ResponseHandler.class.isAssignableFrom(methodParameterTypes[1])) { + return method; + } + } else if (methodParameterTypes.length == 1) { + return method; + } + } + } + } + } + throw new RuntimeException(String.format("%s : %s, async=%b", + "Unable to derive viable LCM Kit API method for action", action, async)); + } + + public Method getCommonHeaderSetterMethod(String action) { + return getBeanPropertyMethodFor(getInputClass(action), "commonHeader", true); + } + + public Method getPayloadSetterMethod(String action) { + return getBeanPropertyMethodFor(getInputClass(action), "payload", true); + } + + public Status getStatusFromGenericResponse(Object response) { + Method statusReader = getBeanPropertyMethodFor(response.getClass(), "status", false); + if (statusReader != null) { + try { + return (Status) statusReader.invoke(response); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + throw new RuntimeException(String.format("Unable to obtain status from LCM Kit response", e)); + } + } + return new Status(); + } + + public static StatusCategory getCategoryOf(Status status) { + int codeSeries = status.getCode() - (status.getCode() % 100); + switch (codeSeries) { + case ACCEPT_SERIES: + return StatusCategory.NORMAL; + case ERROR_SERIES: + case REJECT_SERIES: + return StatusCategory.ERROR; + case SUCCESS_SERIES: + return status.getCode() == SUCCESS_STATUS ? StatusCategory.NORMAL : StatusCategory.ERROR; + case PARTIAL_SERIES: + switch (status.getCode()) { + case PARTIAL_SUCCESS_STATUS: + return StatusCategory.NORMAL; + case PARTIAL_FAILURE_STATUS: + return StatusCategory.ERROR; + default: + return StatusCategory.WARNING; + } + default: + return StatusCategory.WARNING; + } + } + + public static boolean getFinalityOf(Status status) { + int codeSeries = status.getCode() - (status.getCode() % 100); + switch (codeSeries) { + case ACCEPT_SERIES: + case PARTIAL_SERIES: + return false; + case ERROR_SERIES: + case REJECT_SERIES: + case SUCCESS_SERIES: + return true; + default: + return true; + } + } + + /** + * @return + * @throws IOException + */ + private Properties getLCMProperties() throws IOException { + Resource resource = new ClassPathResource("/lcm.properties"); + Properties properties = PropertiesLoaderUtils.loadProperties(resource); + return properties; + } + + private Method getBeanPropertyMethodFor(Class<?> clazz, String propertyName, boolean isWriter) { + BeanInfo beanInfo; + try { + beanInfo = Introspector.getBeanInfo(clazz, Object.class); + } catch (IntrospectionException e) { + throw new RuntimeException( + String.format("Unable to produce bean property method for class : %s, property : %s, writer=%b", + clazz.getName(), propertyName, isWriter), + e); + } + PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); + for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { + if (propertyDescriptor.getName().equals(propertyName)) { + return isWriter ? propertyDescriptor.getWriteMethod() : propertyDescriptor.getReadMethod(); + } + } + throw new RuntimeException( + String.format("Unable to produce bean property method for class : %s, property : %s, writer=%b", + clazz.getName(), propertyName, isWriter)); + } + + /** + * @param action + * @return + * @throws ClassNotFoundException + */ + private Class<?> getInputClass(String action) { + try { + return Class.forName(lcmModelPackage + '.' + action + "Input"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(String.format("%s : %s using package : ", + "Unable to identify viable LCM Kit input class for action", action, lcmModelPackage), e); + } + } + + public static enum StatusCategory { + NORMAL("normal"), + WARNING("warning"), + ERROR("error"); + + private final String category; + + private StatusCategory(final String category) { + this.category = category; + } + + @Override + public String toString() { + return category; + } + } + + public void logLCMMessage(Object message) throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.setSerializationInclusion(Include.NON_NULL); + ObjectWriter writer = objectMapper.writerWithDefaultPrettyPrinter(); + String inputAsJSON = writer.writeValueAsString(message); + System.out.println("LCM Kit input message follows."); + System.out.println(inputAsJSON); + } +} |