import org.opendaylight.yangtools.yang.model.util.EnumerationType
import static com.google.common.base.Preconditions.*
import org.opendaylight.yangtools.yang.model.api.SchemaPath
+import javassist.CtMethod
+import javassist.CannotCompileException
+import java.util.concurrent.locks.Lock
+import java.util.concurrent.Callable
+import org.opendaylight.controller.sal.binding.impl.util.ClassLoaderUtils
class TransformerGenerator {
@Property
var GeneratorListener listener;
-
+
public static val CLASS_TYPE = Types.typeForClass(Class);
public new(ClassPool pool) {
implementsType(BINDING_CODEC)
method(Object, "toDomStatic", QName, Object) [
modifiers = PUBLIC + FINAL + STATIC
- body = '''
+ bodyChecked = '''
{
«QName.name» _resultName;
if($1 != null) {
]
method(Object, "fromDomStatic", QName, Object) [
modifiers = PUBLIC + FINAL + STATIC
- body = '''
+ bodyChecked = '''
{
if($2 == null){
return null;
'''
]
method(Object, "serialize", Object) [
- body = '''
+ bodyChecked = '''
{
java.util.Map.Entry _input = (java.util.Map.Entry) $1;
«QName.name» _localQName = («QName.name») _input.getKey();
'''
]
method(Object, "deserialize", Object) [
- body = '''
+ bodyChecked = '''
return fromDomStatic(QNAME,$1);
'''
]
staticField(it, IDENTITYREF_CODEC, BindingCodec)
method(Object, "toDomStatic", QName, Object) [
modifiers = PUBLIC + FINAL + STATIC
- body = '''
+ bodyChecked = '''
{
«QName.name» _resultName = «QName.name».create($1,QNAME.getLocalName());
java.util.List _childNodes = new java.util.ArrayList();
'''
]
method(Object, "serialize", Object) [
- body = '''
+ bodyChecked = '''
{
java.util.Map.Entry _input = (java.util.Map.Entry) $1;
«QName.name» _localName = QNAME;
]
method(Object, "fromDomStatic", QName, Object) [
modifiers = PUBLIC + FINAL + STATIC
- body = deserializeBody(type, node)
+ bodyChecked = deserializeBody(type, node)
]
method(Object, "deserialize", Object) [
- body = '''
+ bodyChecked = '''
{
//System.out.println("«type.name»#deserialize: " +$1);
java.util.Map.Entry _input = (java.util.Map.Entry) $1;
implementsType(BINDING_CODEC)
method(Object, "toDomStatic", QName, Object) [
modifiers = PUBLIC + FINAL + STATIC
- body = serializeBodyFacade(typeSpec, node)
+ bodyChecked = serializeBodyFacade(typeSpec, node)
]
method(Object, "serialize", Object) [
- body = '''
+ bodyChecked = '''
{
java.util.Map.Entry _input = (java.util.Map.Entry) $1;
«QName.name» _localName = QNAME;
]
method(Object, "fromDomStatic", QName, Object) [
modifiers = PUBLIC + FINAL + STATIC
- body = deserializeBody(typeSpec, node)
+ bodyChecked = deserializeBody(typeSpec, node)
]
method(Object, "deserialize", Object) [
- body = '''
+ bodyChecked = '''
return fromDomStatic(QNAME,$1);
'''
]
implementsType(BINDING_CODEC)
method(Object, "toDomStatic", QName, Object) [
modifiers = PUBLIC + FINAL + STATIC
- body = '''
+ bodyChecked = '''
{
//System.out.println("Qname " + $1);
//System.out.println("Value " + $2);
'''
]
method(Object, "serialize", Object) [
- body = '''
+ bodyChecked = '''
{
java.util.Map.Entry _input = (java.util.Map.Entry) $1;
«QName.name» _localName = QNAME;
]
method(Object, "fromDomStatic", QName, Object) [
modifiers = PUBLIC + FINAL + STATIC
- body = '''
+ bodyChecked = '''
{
«QName.name» _localQName = QNAME;
'''
]
method(Object, "deserialize", Object) [
- body = '''
+ bodyChecked = '''
return fromDomStatic(QNAME,$1);
'''
]
implementsType(BINDING_CODEC)
method(List, "toDomStatic", QName, Object) [
modifiers = PUBLIC + FINAL + STATIC
- body = '''
+ bodyChecked = '''
{
if($2 == null) {
return null;
'''
]
method(Object, "serialize", Object) [
- body = '''
+ bodyChecked = '''
throw new «UnsupportedOperationException.name»("Direct invocation not supported.");
'''
]
method(Object, "fromDomStatic", QName, Map) [
modifiers = PUBLIC + FINAL + STATIC
- body = '''
+ bodyChecked = '''
{
«BINDING_CODEC.name» _codec = («BINDING_CODEC.name») «COMPOSITE_TO_CASE».get($2);
if(_codec != null) {
'''
]
method(Object, "deserialize", Object) [
- body = '''
+ bodyChecked = '''
throw new «UnsupportedOperationException.name»("Direct invocation not supported.");
'''
]
val ret = ctCls.toClassImpl(inputType.classLoader, inputType.protectionDomain)
return ret as Class<? extends BindingCodec<Map<QName,Object>, Object>>;
}
- var hasBinding = false;
- try {
- val bindingCodecClass = loadClassWithTCCL(BINDING_CODEC.name);
- hasBinding = bindingCodecClass !== null;
- } catch (ClassNotFoundException e) {
- hasBinding = false;
- }
- val hasYangBinding = hasBinding
+
val ctCls = createClass(typeSpec.codecClassName) [
//staticField(Map,"AUGMENTATION_SERIALIZERS");
- if (hasYangBinding) {
+ if (inputType.isYangBindingAvailable) {
implementsType(BINDING_CODEC)
staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
staticField(it, IDENTITYREF_CODEC, BindingCodec)
}
method(Object, "toDomValue", Object) [
modifiers = PUBLIC + FINAL + STATIC
- body = '''
+ val ctSpec = typeSpec.asCtClass;
+ bodyChecked = '''
{
//System.out.println("«inputType.simpleName»#toDomValue: "+$1);
'''
]
method(Object, "serialize", Object) [
- body = '''
+ bodyChecked = '''
{
return toDomValue($1);
}
]
method(Object, "fromDomValue", Object) [
modifiers = PUBLIC + FINAL + STATIC
- body = '''
+ bodyChecked = '''
{
//System.out.println("«inputType.simpleName»#fromDomValue: "+$1);
'''
]
method(Object, "deserialize", Object) [
- body = '''{
+ bodyChecked = '''{
return fromDomValue($1);
}
'''
}
+ def boolean isYangBindingAvailable(Class<?> class1) {
+ try {
+ val bindingCodecClass = class1.classLoader.loadClass(BINDING_CODEC.name);
+ return bindingCodecClass !== null;
+ } catch (ClassNotFoundException e) {
+ return false;
+ }
+ }
+
private def createDummyImplementation(Class<?> object, GeneratedTransferObject typeSpec) {
log.info("Generating Dummy DOM Codec for {} with {}", object, object.classLoader)
return createClass(typeSpec.codecClassName) [
- //staticField(Map,"AUGMENTATION_SERIALIZERS");
- implementsType(BINDING_CODEC)
- implementsType(BindingDeserializer.asCtClass)
+ if (object.isYangBindingAvailable) {
+ implementsType(BINDING_CODEC)
+ staticField(it, INSTANCE_IDENTIFIER_CODEC, BindingCodec)
+ staticField(it, IDENTITYREF_CODEC, BindingCodec)
+ implementsType(BindingDeserializer.asCtClass)
+ }
+ //implementsType(BindingDeserializer.asCtClass)
method(Object, "toDomValue", Object) [
modifiers = PUBLIC + FINAL + STATIC
- body = '''return null;'''
+ bodyChecked = '''{
+ if($1 == null) {
+ return null;
+ }
+ return $1.toString();
+
+ }'''
]
method(Object, "serialize", Object) [
- body = '''
+ bodyChecked = '''
{
return toDomValue($1);
}
]
method(Object, "fromDomValue", Object) [
modifiers = PUBLIC + FINAL + STATIC
- body = '''return null;'''
+ bodyChecked = '''return null;'''
]
method(Object, "deserialize", Object) [
- body = '''{
+ bodyChecked = '''{
return fromDomValue($1);
}
'''
//implementsType(BINDING_CODEC)
method(Object, "toDomValue", Object) [
modifiers = PUBLIC + FINAL + STATIC
- body = '''{
+ bodyChecked = '''{
if($1 == null) {
return null;
}
'''
]
method(Object, "serialize", Object) [
- body = '''
+ bodyChecked = '''
return toDomValue($1);
'''
]
method(Object, "fromDomValue", Object) [
modifiers = PUBLIC + FINAL + STATIC
- body = '''
+ bodyChecked = '''
{
if($1 == null) {
return null;
'''
]
method(Object, "deserialize", Object) [
- body = '''
+ bodyChecked = '''
return fromDomValue($1);
'''
]
private def dispatch serializeValue(Type signature, String property) {
if (INSTANCE_IDENTIFIER == signature) {
return '''«INSTANCE_IDENTIFIER_CODEC».serialize(«property»)'''
- }else if (CLASS_TYPE.equals(signature)) {
+ } else if (CLASS_TYPE.equals(signature)) {
return '''(«QName.resolvedName») «IDENTITYREF_CODEC».serialize(«property»)'''
}
return '''«property»''';
throw exception;
}
+ private def setBodyChecked(CtMethod method, String body) {
+ try {
+ method.setBody(body);
+ } catch (CannotCompileException e) {
+ log.error("Cannot compile method: {}#{} {}, Reason: {} Body: {}", method.declaringClass, method.name,
+ method.signature, e.message, body)
+ throw e;
+ }
+ }
+
+ private def <V> V withClassLoaderAndLock(ClassLoader cls, Lock lock, Callable<V> function) throws Exception {
+ appendClassLoaderIfMissing(cls);
+ ClassLoaderUtils.withClassLoaderAndLock(cls, lock, function);
+ }
+
}
@Data
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
+import javassist.ClassPool;
+
+import org.junit.Ignore;
import org.junit.Test;
import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
import org.opendaylight.controller.sal.binding.test.AbstractDataServiceTest;
+import org.opendaylight.controller.sal.binding.test.util.BindingBrokerTestFactory;
import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.config.rev130819.flows.Flow;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
import org.opendaylight.yangtools.yang.common.RpcResult;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
+
import static org.junit.Assert.*;
public class DOMCodecBug02Test extends AbstractDataServiceTest {
private static final NodeRef NODE_REF = new NodeRef(NODE_INSTANCE_ID_BA);
/**
- *
+ * This test is ignored, till found out better way to test generation
+ * of classes without leaking of instances from previous run
*
* @throws Exception
*/
+
+ public void setUp() {
+ ListeningExecutorService executor = MoreExecutors.sameThreadExecutor();
+ BindingBrokerTestFactory factory = new BindingBrokerTestFactory();
+ factory.setExecutor(executor);
+ factory.setClassPool(new ClassPool());
+ factory.setStartWithParsedSchema(getStartWithSchema());
+ testContext = factory.getTestContext();
+ testContext.start();
+
+ baDataService = testContext.getBindingDataBroker();
+ biDataService = testContext.getDomDataBroker();
+ dataStore = testContext.getDomDataStore();
+ mappingService = testContext.getBindingToDomMappingService();
+ };
+
@Test
public void testSchemaContextNotAvailable() throws Exception {
ExecutorService testExecutor = Executors.newFixedThreadPool(1);
-
+ testContext.loadYangSchemaFromClasspath();
Future<Future<RpcResult<TransactionStatus>>> future = testExecutor.submit(new Callable<Future<RpcResult<TransactionStatus>>>() {
@Override
public Future<RpcResult<TransactionStatus>> call() throws Exception {
}
});
- testContext.loadYangSchemaFromClasspath();
+
RpcResult<TransactionStatus> result = future.get().get();
assertEquals(TransactionStatus.COMMITED, result.getResult());
-
Nodes nodes = checkForNodes();
assertNotNull(nodes);