Merge "Fixed Blocker issues. IssueId: SO-165"
[so.git] / common / src / main / java / org / openecomp / mso / yangDecoder / transform / impl / TransformJava2XMLFactory.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.openecomp.mso.yangDecoder.transform.impl;
22
23 import javassist.ClassPool;
24 import org.openecomp.mso.yangDecoder.base.TYangJsonXmlBase;
25 import org.opendaylight.mdsal.binding.api.DataBroker;
26 import org.opendaylight.mdsal.binding.dom.adapter.BindingToNormalizedNodeCodec;
27 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
28 import org.opendaylight.yangtools.binding.data.codec.gen.impl.StreamWriterGenerator;
29 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
30 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
31 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
32 import org.opendaylight.mdsal.binding.generator.util.JavassistUtils;
33 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
34
35 /**
36  * Created by 10112215 on 2017/3/26.
37  */
38 public class TransformJava2XMLFactory extends TYangJsonXmlBase {
39     BindingToNormalizedNodeCodec mappingservice;
40     ModuleInfoBackedContext moduleInfoBackedContext;
41     protected final static ControllerContext controllerContext = ControllerContext.getInstance();
42
43     public TransformJava2XMLServiceImpl getJava2xmlService() {
44         if (java2xmlService == null) {
45             try {
46                 setup2();
47             } catch (Exception e) {
48                 e.printStackTrace();
49                 return null;
50             }
51         }
52         return java2xmlService;
53     }
54
55     TransformJava2XMLServiceImpl java2xmlService;
56
57     @Override
58     protected void setupWithDataBroker(final DataBroker dataBroker) {
59         // Intentionally left No-op, subclasses may customize it
60         
61         // moduleInfoBackedContext.registerModuleInfo(BindingReflections.getModuleInfo(SncTunnels.class));
62         try {
63                 mappingservice = new BindingToNormalizedNodeCodec(GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy(),
64                     new BindingNormalizedNodeCodecRegistry(StreamWriterGenerator.create(JavassistUtils.forClassPool(ClassPool.getDefault()))));
65             moduleInfoBackedContext = ModuleInfoBackedContext.create();
66             
67             for (YangModuleInfo yangModuleInfo : getModuleInfos()) {
68                 moduleInfoBackedContext.registerModuleInfo(yangModuleInfo);
69             }
70             
71             schemaContext = moduleInfoBackedContext.tryToCreateSchemaContext().get();
72             mappingservice.onGlobalContextUpdated(schemaContext);
73             controllerContext.setSchemas(schemaContext);
74         } catch (Exception e) {
75             e.printStackTrace();
76         } finally {
77                 if(null != mappingservice){
78                         mappingservice.close();
79                 }
80         }
81         
82
83     }
84
85     public final void setup2() throws Exception {
86         super.setup();
87         if(java2xmlService==null)
88             java2xmlService = new TransformJava2XMLServiceImpl(mappingservice, schemaContext);
89     }
90
91
92 }