Loading src/main/java/org/etsi/osl/osom/configuration/ActiveMQComponentConfig.java +41 Viewed Changes for src/main/java/org/etsi/osl/osom/configuration/ActiveMQComponentConfig.java: 41 added lines, 0 removed lines. Original line number Diff line number Diff line /*- * ========================LICENSE_START================================= * org.etsi.osl.bugzilla * %% * Copyright (C) 2019 openslice.io * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * =========================LICENSE_END================================== */ package org.etsi.osl.osom.configuration; import org.apache.camel.component.activemq.ActiveMQComponent; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import jakarta.jms.ConnectionFactory; /** * @author ctranoris * */ @Configuration public class ActiveMQComponentConfig { @Bean(name = "activemq") public ActiveMQComponent createComponent(ConnectionFactory factory) { ActiveMQComponent activeMQComponent = new ActiveMQComponent(); activeMQComponent.setConnectionFactory(factory); return activeMQComponent; } } src/main/java/org/etsi/osl/osom/configuration/MvcConfig.java +24 Viewed Changes for src/main/java/org/etsi/osl/osom/configuration/MvcConfig.java: 24 added lines, 0 removed lines. Original line number Diff line number Diff line /*- * ========================LICENSE_START================================= * org.etsi.osl.osom * %% * Copyright (C) 2019 openslice.io * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * =========================LICENSE_END================================== */ package org.etsi.osl.osom.configuration; public class MvcConfig { } src/main/java/org/etsi/osl/osom/configuration/OSOMRouteBuilder.java +169 Viewed Changes for src/main/java/org/etsi/osl/osom/configuration/OSOMRouteBuilder.java: 169 added lines, 0 removed lines. Original line number Diff line number Diff line /*- * ========================LICENSE_START================================= * org.etsi.osl.osom * %% * Copyright (C) 2019 openslice.io * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * =========================LICENSE_END================================== */ package org.etsi.osl.osom.configuration; import java.util.Date; import java.util.Map; import org.apache.camel.Exchange; import org.apache.camel.LoggingLevel; import org.apache.camel.Processor; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.model.dataformat.JsonLibrary; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.etsi.osl.osom.management.ServiceOrderManager; import org.etsi.osl.osom.serviceactions.ServiceActionCheck; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; import org.etsi.osl.model.ExperimentMetadata; import org.etsi.osl.model.Product; import org.etsi.osl.model.ValidationJob; import org.etsi.osl.model.ValidationStatus; import org.etsi.osl.model.VxFMetadata; import org.etsi.osl.tmf.am642.model.AlarmCreate; import org.etsi.osl.tmf.so641.model.ServiceOrder; @Configuration //@RefreshScope @Component public class OSOMRouteBuilder extends RouteBuilder { private static final transient Log logger = LogFactory.getLog(OSOMRouteBuilder.class.getName()); @Value("${CRD_DEPLOY_CR_REQ}") private String CRD_DEPLOY_CR_REQ = ""; public void configure() { from("direct:retriesCRD_DEPLOY_CR_REQ") .errorHandler(deadLetterChannel("direct:retriesDeadLetters") .maximumRedeliveries( 10 ) //let's try 10 times to send it.... .redeliveryDelay( 30000 ).useOriginalMessage() //.deadLetterHandleNewException( false ) //.logExhaustedMessageHistory(false) .logExhausted(true) .logHandled(true) //.retriesExhaustedLogLevel(LoggingLevel.WARN) .retryAttemptedLogLevel( LoggingLevel.WARN) ) .to(CRD_DEPLOY_CR_REQ); /** * dead Letter Queue Users if everything fails to connect */ from("direct:retriesDeadLetters") //.setBody() //.body(String.class) .process( ErroneousValidationProcessor ) .to("stream:out"); // .errorHandler(deadLetterChannel("direct:dlq_bugzilla") // .maximumRedeliveries( 4 ) //let's try for the next 120 mins to send it.... // .redeliveryDelay( 60000 ).useOriginalMessage() // .deadLetterHandleNewException( false ) // //.logExhaustedMessageHistory(false) // .logExhausted(true) // .logHandled(true) // //.retriesExhaustedLogLevel(LoggingLevel.WARN) // .retryAttemptedLogLevel( LoggingLevel.WARN) ) // from("jms:queue:OSOM.NEW_SERVICEORDER_PROCESS") // .log(LoggingLevel.INFO, log, "New OSOM.IN.SERVICEORDER message received!") // .to("log:DEBUG?showBody=true&showHeaders=true") // .unmarshal().json( JsonLibrary.Jackson, ServiceOrder.class, true) // .log(LoggingLevel.INFO, log, "Order id = ${body.id} ") // .bean( ServiceOrderManager.class, "processOrder") // ; // // from("jms:queue:OSOM.NEW_SERVICEORDER_PROCESS.LIST_PENDING") // .log(LoggingLevel.INFO, log, "New OSOM.NEW_SERVICEORDER_PROCESS.LIST_PENDING message received!") // .to("log:DEBUG?showBody=true&showHeaders=true") // .bean( ServiceOrderManager.class, "getTasks") // ; // // // // // from("jms:queue:OSOM.HUMAN_COMPLETE_ORDER_TASK") // .log(LoggingLevel.INFO, log, "New OSOM.HUMAN_COMPLETE_ORDER_TASK message received!") // .to("log:DEBUG?showBody=true&showHeaders=true") // //.unmarshal().json( JsonLibrary.Jackson, ServiceOrder.class, true) // //.log(LoggingLevel.INFO, log, "Order id = ${body.id}") // .bean( ServiceOrderManager.class, "humanComplete"); //create route here to get service spec by id from model via bus // // from("activemq:OSOMIN_TEXT") // .log(LoggingLevel.INFO, log, "New activemq:OSOMIN_TEXT message received") // .setBody(constant("46")) // .to("stream:out") // .end(); // // from("seda:OSOMIN_SERVICEORDERTEXT").log(LoggingLevel.INFO, log, "New seda:OSOMIN_SERVICEORDERTEXT message received") // .to("stream:out");; // } Processor ErroneousValidationProcessor = new Processor() { @Override public void process(Exchange exchange) throws Exception { Map<String, Object> headers = exchange.getIn().getHeaders(); Product aProd = exchange.getIn().getBody( Product.class ); if (aProd instanceof VxFMetadata) { ((VxFMetadata) aProd).setValidationStatus( ValidationStatus.COMPLETED ); } else if (aProd instanceof ExperimentMetadata) { ((ExperimentMetadata) aProd).setValidationStatus( ValidationStatus.COMPLETED ); } if ( aProd.getValidationJobs() != null ) { ValidationJob j = new ValidationJob(); j.setDateCreated( new Date() ); j.setJobid("ERROR"); j.setValidationStatus(false); j.setOutputLog( "Error from the OSOM Route builder Service" ); aProd.getValidationJobs().add(j); } exchange. getOut().setBody( aProd ); // copy attachements from IN to OUT to propagate them //exchange.getOut().setAttachments(exchange.getIn().getAttachments()); } }; } src/main/java/org/etsi/osl/osom/lcm/LCMRulesController.java +143 Viewed Changes for src/main/java/org/etsi/osl/osom/lcm/LCMRulesController.java: 143 added lines, 0 removed lines. Original line number Diff line number Diff line package org.etsi.osl.osom.lcm; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.camel.ProducerTemplate; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.etsi.osl.osom.management.ServiceOrderManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.etsi.osl.tmf.lcm.model.ELCMRulePhase; import org.etsi.osl.tmf.lcm.model.LCMRuleSpecification; /** * @author ctranoris * */ @Service(value = "lcmRulesController") public class LCMRulesController { private static final transient Log logger = LogFactory.getLog( LCMRulesController.class.getName() ); @Autowired private ProducerTemplate template; @Value("${CATALOG_GET_LCMRULE_BY_ID}") private String CATALOG_GET_LCMRULE_BY_ID = ""; @Value("${CATALOG_GET_LCMRULES_BY_SPECID_PHASE}") private String CATALOG_GET_LCMRULES_BY_SPECID_PHASE = ""; @Autowired private ServiceOrderManager serviceOrderManager; /** * execute rules of a phase * @param phase * @param spec * @param sor * @param serviceToCreate */ public LCMRulesExecutorVariables execPhase(ELCMRulePhase phase, LCMRulesExecutorVariables vars) { logger.debug("In execPhase phase=" + phase + ", spec name = " + vars.getSpec().getName() + ", orderid = " + vars.getSorder().getId() ); List<LCMRuleSpecification> lcmspecs = retrieveLCMRulesOfSpecification_Phase( vars.getSpec().getId(), phase.getValue() ); if ( lcmspecs == null) { logger.debug("No LCMRuleSpecs for phase="+ phase + ", spec name = " + vars.getSpec().getName() + ", orderid = " + vars.getSorder().getId() ); return vars; } for (LCMRuleSpecification lcmRuleSpecification : lcmspecs) { logger.info("Prepare to execute LCMRuleSpecification =" + lcmRuleSpecification.getName() ); LCMRuleSpecification lcmspec = retrieveLCMRuleSpecificationById( lcmRuleSpecification.getId() ); LCMRulesExecutor lcmRulesExecutor = new LCMRulesExecutor(); vars = lcmRulesExecutor.executeLCMRuleCode(lcmspec, vars); } return vars; } /** * get LCMRuleSpecification spec by id from model via bus * @param id * @return * @throws IOException */ public LCMRuleSpecification retrieveLCMRuleSpecificationById(String lcmspecid) { logger.info("will retrieve LCMRuleSpecification from catalog lcmspecid=" + lcmspecid ); try { Object response = template. requestBody( CATALOG_GET_LCMRULE_BY_ID, lcmspecid); if ( !(response instanceof String)) { logger.error("LCMRuleSpecification object is wrong."); return null; } LCMRuleSpecification ls = toJsonObj( (String)response, new TypeReference<LCMRuleSpecification>() {}); return ls; }catch (Exception e) { logger.error("Cannot retrieve Service Specification details from catalog. " + e.toString()); } return null; } public List<LCMRuleSpecification> retrieveLCMRulesOfSpecification_Phase(String servicespecid, String phasename ) { logger.info("will retrieveLCMRulesOfSpecification_Phase from catalog serviceSpecid=" + servicespecid ); try { Map<String, Object> map = new HashMap<>(); map.put("servicespecid", servicespecid ); map.put("phasename", phasename ); Object response = template. requestBodyAndHeaders( CATALOG_GET_LCMRULES_BY_SPECID_PHASE, "", map); if ( !(response instanceof String)) { logger.error("LCMRuleSpecifications objects are wrong."); return null; } List<LCMRuleSpecification> ls = toJsonObj( (String)response,new TypeReference<List<LCMRuleSpecification>>() {} ); return ls; }catch (Exception e) { logger.error("Cannot retrieve Service Specification details from catalog. " + e.toString()); } return null; } static <T> T toJsonObj(String content, TypeReference<T> valueType) throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); return mapper.readValue( content, valueType); } } src/main/java/org/etsi/osl/osom/lcm/LCMRulesExecutor.java +248 Viewed Changes for src/main/java/org/etsi/osl/osom/lcm/LCMRulesExecutor.java: 248 added lines, 0 removed lines. Original line number Diff line number Diff line package org.etsi.osl.osom.lcm; import java.io.File; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import javax.tools.Diagnostic; import javax.tools.DiagnosticCollector; import javax.tools.JavaCompiler; import javax.tools.JavaFileObject; import javax.tools.StandardJavaFileManager; import javax.tools.ToolProvider; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.etsi.osl.tmf.lcm.model.LCMRuleSpecification; /** * @author ctranoris * */ public class LCMRulesExecutor { private static final transient Log logger = LogFactory.getLog( LCMRulesExecutor.class.getName() ); public LCMRulesExecutorVariables executeLCMRuleCode(LCMRuleSpecification lcmspec, LCMRulesExecutorVariables vars) { logger.info("executeLCMRuleCode lcmspecId =" + lcmspec.getId() + "lcmspec =" + lcmspec.getName() ); //Prepare code final String className = "ExecRule_"+lcmspec.getId().replace("-", "_"); String code= //"package " + className + "; \n\n" + "import org.etsi.osl.osom.lcm.LcmBaseExecutor;\n\n" + "public class "+className+" extends LcmBaseExecutor{\n\n" + """ @Override public void exec() { //SNIP STARTS """ + preprocess( lcmspec.getCode() ) +""" //SNIP ENDS } } """; logger.debug("code dump:"); logger.debug( code ); try { vars = execudeCode(className, code, vars, lcmspec); }catch (Exception e) { e.printStackTrace(); } return vars; } /** * * Any code preprocessing before compilation * @param aCode * @return */ private String preprocess(final String aCode) { String newCode = aCode; newCode = preprocess_ClearAllEVALVariables(aCode ); return newCode; } /** * * inside Strings we may have parameters from the code. * The parameters are in the form $EVAL_param3_EVAL$ * They should be properly replaced inside the String * @param newValue * @return */ private String preprocess_ClearAllEVALVariables(String newValue) { String anewValue = newValue; logger.debug("clearEVALVariables before " + newValue); anewValue = anewValue.replace("$QUOTESTR$", "\""); // anewValue = anewValue.replace("$$XVALS_", " \"\"\" +"); // anewValue = anewValue.replace("_XVALE$$", "+ \"\"\"\n"); anewValue = anewValue.replace("$$XVALS_", "\"+"); anewValue = anewValue.replace("_XVALE$$", "+\""); logger.debug("clearEVALVariables after " + anewValue); return anewValue; } /** * @param className * @param code * @param vars * @param lcmspec * @return * @throws IOException * @throws ClassNotFoundException * @throws SecurityException * @throws NoSuchMethodException * @throws InvocationTargetException * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InstantiationException */ private LCMRulesExecutorVariables execudeCode(String className, String code, LCMRulesExecutorVariables vars, LCMRuleSpecification lcmspec) throws IOException, ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { // A temporary directory where the java code and class will be located Path temp = Paths.get(System.getProperty("java.io.tmpdir"), "openslice", className); Files.createDirectories(temp); // Creation of the java source file // You could also extends the SimpleJavaFileObject object as shown in the doc. // See SimpleJavaFileObject at https://docs.oracle.com/javase/8/docs/api/javax/tools/JavaCompiler.html Path javaSourceFile = Paths.get(temp.normalize().toAbsolutePath().toString(), className + ".java"); Files.write(javaSourceFile, code.getBytes()); logger.debug("For lcmspec " + lcmspec.getName() + " The java source file is located at "+javaSourceFile); // Verification of the presence of the compilation tool archive final String toolsJarFileName = "tools.jar"; final String javaHome = System.getProperty("java.home"); Path toolsJarFilePath = Paths.get(javaHome, "lib", toolsJarFileName); if (!Files.exists(toolsJarFilePath)){ logger.warn( "The tools jar file ("+toolsJarFileName+") could not be found at ("+toolsJarFilePath+")."); } // The compile part // Definition of the files to compile File[] files1 = {javaSourceFile.toFile()}; // Get the compiler JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); logger.debug("compiler = "+ compiler); // Get the file system manager of the compiler StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); // Create a compilation unit (files) Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(files1)); // A feedback object (diagnostic) to get errors DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); // Compilation unit can be created and called only once List<String> optionList = new ArrayList<String>(); // set compiler's classpath to be same as the runtime's /** * this is a current solution found. * We need here to add to classpath the jar with the classes, so that the compile can be performed in runtime. * This is the location of the jar inside the running container */ File classesJar = new File("/opt/openslice/lib/org.etsi.osl.osom-1.2.0-SNAPSHOT.jar"); if ( classesJar.exists() ) { optionList.addAll(Arrays.asList("-classpath", classesJar.getAbsoluteFile().toString() )); } logger.debug("optionList = "+ optionList.toString()); JavaCompiler.CompilationTask task = compiler.getTask( null, fileManager, diagnostics, optionList, null, compilationUnits ); logger.debug("task1 = "+ task); // The compile task is called task.call(); logger.debug("task2 = "+ task); // Printing of any compile problems for (Diagnostic diagnostic : diagnostics.getDiagnostics()) { String err = String.format("LCMRule: %s, Phase: %s -> Error on line %d in %s, %s, %s %n", lcmspec.getName(), lcmspec.getLcmrulephase() , diagnostic.getLineNumber(), diagnostic.getSource(), diagnostic.getCode(), diagnostic.getMessage(null) ); logger.error( err ); vars.getCompileDiagnosticErrors().add(err); } logger.debug("task3 = "+ task); if ( diagnostics.getDiagnostics().size()>0 ) { logger.error("LCMRule:" + lcmspec.getName() + "execudeCode compile error. Will just return"); return vars; } // Close the compile resources fileManager.close(); URL[] classpath = new URL[] { temp.toUri().toURL() }; if ( classesJar.exists() ) { classpath = new URL[] { temp.toUri().toURL(), classesJar.toURI().toURL() }; } logger.debug("classpath = "+ classpath.toString()); // Now that the class was created, we will load it and run it ClassLoader classLoader = LCMRulesExecutor.class.getClassLoader(); logger.debug("classLoader = "+ classLoader); @SuppressWarnings("resource") URLClassLoader urlClassLoader = new URLClassLoader( classpath, classLoader); Class javaDemoClass = urlClassLoader.loadClass(className); logger.debug("javaDemoClass = "+ javaDemoClass); Object obj = javaDemoClass.getDeclaredConstructor().newInstance(); logger.debug("obj = "+ obj); Method method = javaDemoClass.getMethod("run", LCMRulesExecutorVariables.class, LCMRuleSpecification.class); logger.debug("method = "+ method); ArrayList<Object> methodArgs = new ArrayList<Object>(); methodArgs.add( vars ); methodArgs.add( lcmspec ); Object response = method.invoke(obj, methodArgs.toArray()); if ( response instanceof LCMRulesExecutorVariables ) { return (LCMRulesExecutorVariables) response; } return vars; } } Loading
src/main/java/org/etsi/osl/osom/configuration/ActiveMQComponentConfig.java +41 Viewed Changes for src/main/java/org/etsi/osl/osom/configuration/ActiveMQComponentConfig.java: 41 added lines, 0 removed lines. Original line number Diff line number Diff line /*- * ========================LICENSE_START================================= * org.etsi.osl.bugzilla * %% * Copyright (C) 2019 openslice.io * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * =========================LICENSE_END================================== */ package org.etsi.osl.osom.configuration; import org.apache.camel.component.activemq.ActiveMQComponent; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import jakarta.jms.ConnectionFactory; /** * @author ctranoris * */ @Configuration public class ActiveMQComponentConfig { @Bean(name = "activemq") public ActiveMQComponent createComponent(ConnectionFactory factory) { ActiveMQComponent activeMQComponent = new ActiveMQComponent(); activeMQComponent.setConnectionFactory(factory); return activeMQComponent; } }
src/main/java/org/etsi/osl/osom/configuration/MvcConfig.java +24 Viewed Changes for src/main/java/org/etsi/osl/osom/configuration/MvcConfig.java: 24 added lines, 0 removed lines. Original line number Diff line number Diff line /*- * ========================LICENSE_START================================= * org.etsi.osl.osom * %% * Copyright (C) 2019 openslice.io * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * =========================LICENSE_END================================== */ package org.etsi.osl.osom.configuration; public class MvcConfig { }
src/main/java/org/etsi/osl/osom/configuration/OSOMRouteBuilder.java +169 Viewed Changes for src/main/java/org/etsi/osl/osom/configuration/OSOMRouteBuilder.java: 169 added lines, 0 removed lines. Original line number Diff line number Diff line /*- * ========================LICENSE_START================================= * org.etsi.osl.osom * %% * Copyright (C) 2019 openslice.io * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * =========================LICENSE_END================================== */ package org.etsi.osl.osom.configuration; import java.util.Date; import java.util.Map; import org.apache.camel.Exchange; import org.apache.camel.LoggingLevel; import org.apache.camel.Processor; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.model.dataformat.JsonLibrary; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.etsi.osl.osom.management.ServiceOrderManager; import org.etsi.osl.osom.serviceactions.ServiceActionCheck; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; import org.etsi.osl.model.ExperimentMetadata; import org.etsi.osl.model.Product; import org.etsi.osl.model.ValidationJob; import org.etsi.osl.model.ValidationStatus; import org.etsi.osl.model.VxFMetadata; import org.etsi.osl.tmf.am642.model.AlarmCreate; import org.etsi.osl.tmf.so641.model.ServiceOrder; @Configuration //@RefreshScope @Component public class OSOMRouteBuilder extends RouteBuilder { private static final transient Log logger = LogFactory.getLog(OSOMRouteBuilder.class.getName()); @Value("${CRD_DEPLOY_CR_REQ}") private String CRD_DEPLOY_CR_REQ = ""; public void configure() { from("direct:retriesCRD_DEPLOY_CR_REQ") .errorHandler(deadLetterChannel("direct:retriesDeadLetters") .maximumRedeliveries( 10 ) //let's try 10 times to send it.... .redeliveryDelay( 30000 ).useOriginalMessage() //.deadLetterHandleNewException( false ) //.logExhaustedMessageHistory(false) .logExhausted(true) .logHandled(true) //.retriesExhaustedLogLevel(LoggingLevel.WARN) .retryAttemptedLogLevel( LoggingLevel.WARN) ) .to(CRD_DEPLOY_CR_REQ); /** * dead Letter Queue Users if everything fails to connect */ from("direct:retriesDeadLetters") //.setBody() //.body(String.class) .process( ErroneousValidationProcessor ) .to("stream:out"); // .errorHandler(deadLetterChannel("direct:dlq_bugzilla") // .maximumRedeliveries( 4 ) //let's try for the next 120 mins to send it.... // .redeliveryDelay( 60000 ).useOriginalMessage() // .deadLetterHandleNewException( false ) // //.logExhaustedMessageHistory(false) // .logExhausted(true) // .logHandled(true) // //.retriesExhaustedLogLevel(LoggingLevel.WARN) // .retryAttemptedLogLevel( LoggingLevel.WARN) ) // from("jms:queue:OSOM.NEW_SERVICEORDER_PROCESS") // .log(LoggingLevel.INFO, log, "New OSOM.IN.SERVICEORDER message received!") // .to("log:DEBUG?showBody=true&showHeaders=true") // .unmarshal().json( JsonLibrary.Jackson, ServiceOrder.class, true) // .log(LoggingLevel.INFO, log, "Order id = ${body.id} ") // .bean( ServiceOrderManager.class, "processOrder") // ; // // from("jms:queue:OSOM.NEW_SERVICEORDER_PROCESS.LIST_PENDING") // .log(LoggingLevel.INFO, log, "New OSOM.NEW_SERVICEORDER_PROCESS.LIST_PENDING message received!") // .to("log:DEBUG?showBody=true&showHeaders=true") // .bean( ServiceOrderManager.class, "getTasks") // ; // // // // // from("jms:queue:OSOM.HUMAN_COMPLETE_ORDER_TASK") // .log(LoggingLevel.INFO, log, "New OSOM.HUMAN_COMPLETE_ORDER_TASK message received!") // .to("log:DEBUG?showBody=true&showHeaders=true") // //.unmarshal().json( JsonLibrary.Jackson, ServiceOrder.class, true) // //.log(LoggingLevel.INFO, log, "Order id = ${body.id}") // .bean( ServiceOrderManager.class, "humanComplete"); //create route here to get service spec by id from model via bus // // from("activemq:OSOMIN_TEXT") // .log(LoggingLevel.INFO, log, "New activemq:OSOMIN_TEXT message received") // .setBody(constant("46")) // .to("stream:out") // .end(); // // from("seda:OSOMIN_SERVICEORDERTEXT").log(LoggingLevel.INFO, log, "New seda:OSOMIN_SERVICEORDERTEXT message received") // .to("stream:out");; // } Processor ErroneousValidationProcessor = new Processor() { @Override public void process(Exchange exchange) throws Exception { Map<String, Object> headers = exchange.getIn().getHeaders(); Product aProd = exchange.getIn().getBody( Product.class ); if (aProd instanceof VxFMetadata) { ((VxFMetadata) aProd).setValidationStatus( ValidationStatus.COMPLETED ); } else if (aProd instanceof ExperimentMetadata) { ((ExperimentMetadata) aProd).setValidationStatus( ValidationStatus.COMPLETED ); } if ( aProd.getValidationJobs() != null ) { ValidationJob j = new ValidationJob(); j.setDateCreated( new Date() ); j.setJobid("ERROR"); j.setValidationStatus(false); j.setOutputLog( "Error from the OSOM Route builder Service" ); aProd.getValidationJobs().add(j); } exchange. getOut().setBody( aProd ); // copy attachements from IN to OUT to propagate them //exchange.getOut().setAttachments(exchange.getIn().getAttachments()); } }; }
src/main/java/org/etsi/osl/osom/lcm/LCMRulesController.java +143 Viewed Changes for src/main/java/org/etsi/osl/osom/lcm/LCMRulesController.java: 143 added lines, 0 removed lines. Original line number Diff line number Diff line package org.etsi.osl.osom.lcm; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.camel.ProducerTemplate; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.etsi.osl.osom.management.ServiceOrderManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.etsi.osl.tmf.lcm.model.ELCMRulePhase; import org.etsi.osl.tmf.lcm.model.LCMRuleSpecification; /** * @author ctranoris * */ @Service(value = "lcmRulesController") public class LCMRulesController { private static final transient Log logger = LogFactory.getLog( LCMRulesController.class.getName() ); @Autowired private ProducerTemplate template; @Value("${CATALOG_GET_LCMRULE_BY_ID}") private String CATALOG_GET_LCMRULE_BY_ID = ""; @Value("${CATALOG_GET_LCMRULES_BY_SPECID_PHASE}") private String CATALOG_GET_LCMRULES_BY_SPECID_PHASE = ""; @Autowired private ServiceOrderManager serviceOrderManager; /** * execute rules of a phase * @param phase * @param spec * @param sor * @param serviceToCreate */ public LCMRulesExecutorVariables execPhase(ELCMRulePhase phase, LCMRulesExecutorVariables vars) { logger.debug("In execPhase phase=" + phase + ", spec name = " + vars.getSpec().getName() + ", orderid = " + vars.getSorder().getId() ); List<LCMRuleSpecification> lcmspecs = retrieveLCMRulesOfSpecification_Phase( vars.getSpec().getId(), phase.getValue() ); if ( lcmspecs == null) { logger.debug("No LCMRuleSpecs for phase="+ phase + ", spec name = " + vars.getSpec().getName() + ", orderid = " + vars.getSorder().getId() ); return vars; } for (LCMRuleSpecification lcmRuleSpecification : lcmspecs) { logger.info("Prepare to execute LCMRuleSpecification =" + lcmRuleSpecification.getName() ); LCMRuleSpecification lcmspec = retrieveLCMRuleSpecificationById( lcmRuleSpecification.getId() ); LCMRulesExecutor lcmRulesExecutor = new LCMRulesExecutor(); vars = lcmRulesExecutor.executeLCMRuleCode(lcmspec, vars); } return vars; } /** * get LCMRuleSpecification spec by id from model via bus * @param id * @return * @throws IOException */ public LCMRuleSpecification retrieveLCMRuleSpecificationById(String lcmspecid) { logger.info("will retrieve LCMRuleSpecification from catalog lcmspecid=" + lcmspecid ); try { Object response = template. requestBody( CATALOG_GET_LCMRULE_BY_ID, lcmspecid); if ( !(response instanceof String)) { logger.error("LCMRuleSpecification object is wrong."); return null; } LCMRuleSpecification ls = toJsonObj( (String)response, new TypeReference<LCMRuleSpecification>() {}); return ls; }catch (Exception e) { logger.error("Cannot retrieve Service Specification details from catalog. " + e.toString()); } return null; } public List<LCMRuleSpecification> retrieveLCMRulesOfSpecification_Phase(String servicespecid, String phasename ) { logger.info("will retrieveLCMRulesOfSpecification_Phase from catalog serviceSpecid=" + servicespecid ); try { Map<String, Object> map = new HashMap<>(); map.put("servicespecid", servicespecid ); map.put("phasename", phasename ); Object response = template. requestBodyAndHeaders( CATALOG_GET_LCMRULES_BY_SPECID_PHASE, "", map); if ( !(response instanceof String)) { logger.error("LCMRuleSpecifications objects are wrong."); return null; } List<LCMRuleSpecification> ls = toJsonObj( (String)response,new TypeReference<List<LCMRuleSpecification>>() {} ); return ls; }catch (Exception e) { logger.error("Cannot retrieve Service Specification details from catalog. " + e.toString()); } return null; } static <T> T toJsonObj(String content, TypeReference<T> valueType) throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); return mapper.readValue( content, valueType); } }
src/main/java/org/etsi/osl/osom/lcm/LCMRulesExecutor.java +248 Viewed Changes for src/main/java/org/etsi/osl/osom/lcm/LCMRulesExecutor.java: 248 added lines, 0 removed lines. Original line number Diff line number Diff line package org.etsi.osl.osom.lcm; import java.io.File; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import javax.tools.Diagnostic; import javax.tools.DiagnosticCollector; import javax.tools.JavaCompiler; import javax.tools.JavaFileObject; import javax.tools.StandardJavaFileManager; import javax.tools.ToolProvider; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.etsi.osl.tmf.lcm.model.LCMRuleSpecification; /** * @author ctranoris * */ public class LCMRulesExecutor { private static final transient Log logger = LogFactory.getLog( LCMRulesExecutor.class.getName() ); public LCMRulesExecutorVariables executeLCMRuleCode(LCMRuleSpecification lcmspec, LCMRulesExecutorVariables vars) { logger.info("executeLCMRuleCode lcmspecId =" + lcmspec.getId() + "lcmspec =" + lcmspec.getName() ); //Prepare code final String className = "ExecRule_"+lcmspec.getId().replace("-", "_"); String code= //"package " + className + "; \n\n" + "import org.etsi.osl.osom.lcm.LcmBaseExecutor;\n\n" + "public class "+className+" extends LcmBaseExecutor{\n\n" + """ @Override public void exec() { //SNIP STARTS """ + preprocess( lcmspec.getCode() ) +""" //SNIP ENDS } } """; logger.debug("code dump:"); logger.debug( code ); try { vars = execudeCode(className, code, vars, lcmspec); }catch (Exception e) { e.printStackTrace(); } return vars; } /** * * Any code preprocessing before compilation * @param aCode * @return */ private String preprocess(final String aCode) { String newCode = aCode; newCode = preprocess_ClearAllEVALVariables(aCode ); return newCode; } /** * * inside Strings we may have parameters from the code. * The parameters are in the form $EVAL_param3_EVAL$ * They should be properly replaced inside the String * @param newValue * @return */ private String preprocess_ClearAllEVALVariables(String newValue) { String anewValue = newValue; logger.debug("clearEVALVariables before " + newValue); anewValue = anewValue.replace("$QUOTESTR$", "\""); // anewValue = anewValue.replace("$$XVALS_", " \"\"\" +"); // anewValue = anewValue.replace("_XVALE$$", "+ \"\"\"\n"); anewValue = anewValue.replace("$$XVALS_", "\"+"); anewValue = anewValue.replace("_XVALE$$", "+\""); logger.debug("clearEVALVariables after " + anewValue); return anewValue; } /** * @param className * @param code * @param vars * @param lcmspec * @return * @throws IOException * @throws ClassNotFoundException * @throws SecurityException * @throws NoSuchMethodException * @throws InvocationTargetException * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InstantiationException */ private LCMRulesExecutorVariables execudeCode(String className, String code, LCMRulesExecutorVariables vars, LCMRuleSpecification lcmspec) throws IOException, ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { // A temporary directory where the java code and class will be located Path temp = Paths.get(System.getProperty("java.io.tmpdir"), "openslice", className); Files.createDirectories(temp); // Creation of the java source file // You could also extends the SimpleJavaFileObject object as shown in the doc. // See SimpleJavaFileObject at https://docs.oracle.com/javase/8/docs/api/javax/tools/JavaCompiler.html Path javaSourceFile = Paths.get(temp.normalize().toAbsolutePath().toString(), className + ".java"); Files.write(javaSourceFile, code.getBytes()); logger.debug("For lcmspec " + lcmspec.getName() + " The java source file is located at "+javaSourceFile); // Verification of the presence of the compilation tool archive final String toolsJarFileName = "tools.jar"; final String javaHome = System.getProperty("java.home"); Path toolsJarFilePath = Paths.get(javaHome, "lib", toolsJarFileName); if (!Files.exists(toolsJarFilePath)){ logger.warn( "The tools jar file ("+toolsJarFileName+") could not be found at ("+toolsJarFilePath+")."); } // The compile part // Definition of the files to compile File[] files1 = {javaSourceFile.toFile()}; // Get the compiler JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); logger.debug("compiler = "+ compiler); // Get the file system manager of the compiler StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); // Create a compilation unit (files) Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(files1)); // A feedback object (diagnostic) to get errors DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); // Compilation unit can be created and called only once List<String> optionList = new ArrayList<String>(); // set compiler's classpath to be same as the runtime's /** * this is a current solution found. * We need here to add to classpath the jar with the classes, so that the compile can be performed in runtime. * This is the location of the jar inside the running container */ File classesJar = new File("/opt/openslice/lib/org.etsi.osl.osom-1.2.0-SNAPSHOT.jar"); if ( classesJar.exists() ) { optionList.addAll(Arrays.asList("-classpath", classesJar.getAbsoluteFile().toString() )); } logger.debug("optionList = "+ optionList.toString()); JavaCompiler.CompilationTask task = compiler.getTask( null, fileManager, diagnostics, optionList, null, compilationUnits ); logger.debug("task1 = "+ task); // The compile task is called task.call(); logger.debug("task2 = "+ task); // Printing of any compile problems for (Diagnostic diagnostic : diagnostics.getDiagnostics()) { String err = String.format("LCMRule: %s, Phase: %s -> Error on line %d in %s, %s, %s %n", lcmspec.getName(), lcmspec.getLcmrulephase() , diagnostic.getLineNumber(), diagnostic.getSource(), diagnostic.getCode(), diagnostic.getMessage(null) ); logger.error( err ); vars.getCompileDiagnosticErrors().add(err); } logger.debug("task3 = "+ task); if ( diagnostics.getDiagnostics().size()>0 ) { logger.error("LCMRule:" + lcmspec.getName() + "execudeCode compile error. Will just return"); return vars; } // Close the compile resources fileManager.close(); URL[] classpath = new URL[] { temp.toUri().toURL() }; if ( classesJar.exists() ) { classpath = new URL[] { temp.toUri().toURL(), classesJar.toURI().toURL() }; } logger.debug("classpath = "+ classpath.toString()); // Now that the class was created, we will load it and run it ClassLoader classLoader = LCMRulesExecutor.class.getClassLoader(); logger.debug("classLoader = "+ classLoader); @SuppressWarnings("resource") URLClassLoader urlClassLoader = new URLClassLoader( classpath, classLoader); Class javaDemoClass = urlClassLoader.loadClass(className); logger.debug("javaDemoClass = "+ javaDemoClass); Object obj = javaDemoClass.getDeclaredConstructor().newInstance(); logger.debug("obj = "+ obj); Method method = javaDemoClass.getMethod("run", LCMRulesExecutorVariables.class, LCMRuleSpecification.class); logger.debug("method = "+ method); ArrayList<Object> methodArgs = new ArrayList<Object>(); methodArgs.add( vars ); methodArgs.add( lcmspec ); Object response = method.invoke(obj, methodArgs.toArray()); if ( response instanceof LCMRulesExecutorVariables ) { return (LCMRulesExecutorVariables) response; } return vars; } }