1 package org.openecomp.mso.yangDecoder.transform.impl;
3 import org.openecomp.mso.yangDecoder.transform.api.ITransformJava2StringService;
4 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
5 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
6 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
7 import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer;
8 import org.opendaylight.yangtools.yang.binding.DataObject;
9 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
10 import org.opendaylight.yangtools.yang.binding.Notification;
11 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
12 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
13 import org.opendaylight.yangtools.yang.model.api.*;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
18 * Created by Administrator on 2017/3/20.
20 public class TransformJava2JsonServiceImpl implements ITransformJava2StringService {
21 private static final Logger LOG = LoggerFactory.getLogger(TransformJava2JsonServiceImpl.class);
22 BindingNormalizedNodeSerializer mappingservice;
23 SchemaContext schemaContext;
24 YangDataTransformNN2JsonServiceImpl nn2jsonService;
25 YangDataTransformJava2NNServiceImpl java2nnService;
26 public TransformJava2JsonServiceImpl(BindingNormalizedNodeSerializer mappingservice, SchemaContext schemaContext){
27 this.mappingservice=mappingservice;
28 this.schemaContext=schemaContext;
29 nn2jsonService=new YangDataTransformNN2JsonServiceImpl();
30 java2nnService=new YangDataTransformJava2NNServiceImpl(mappingservice);
33 public <T extends DataObject> String transformContrainerDataObjectToString(InstanceIdentifier<T> instanceIdentifier, String uriPath,T dataObject) throws Exception {
34 // TODO Auto-generated method stub
35 NormalizedNode nn = java2nnService.yangDataObjecttoNN(instanceIdentifier, dataObject);
36 NormalizedNodeContext nnc = java2nnService.yangNNtoNNC(nn, uriPath);
37 String sjson = nn2jsonService.transformNNCToString(nnc);
41 public <T extends Notification> String transformNotificationToString(String uriPath,T notification)throws Exception {
42 NormalizedNode nn = mappingservice.toNormalizedNodeNotification(notification);
43 NormalizedNodeContext nnc = java2nnService.yangNNtoNNC(nn, uriPath);
44 String sjson = nn2jsonService.transformNNCToString(nnc);
48 public <T extends DataObject> String transformRpcDataObjectToString(String uriPath,T dataObject) throws Exception {
49 NormalizedNode nn = mappingservice.toNormalizedNodeRpcData(dataObject);
50 NormalizedNodeContext nnc = java2nnService.yangNNtoNNC(nn, uriPath);
51 String sjson = nn2jsonService.transformNNCToString(nnc);
55 public DataObject transformContrainerDataObjectFromString(String uriPath,String sjson,boolean ispost) throws Exception {
56 NormalizedNodeContext nnc= nn2jsonService.transformDataObjectNNCFromString(uriPath,sjson,ispost);
57 return java2nnService.yangDataObjectfromNNC(nnc);
60 public Notification transformNotificationFromString(String notficationName,String sjson) throws Exception {
61 final InstanceIdentifierContext<?> iicontext = ControllerContext.getInstance().toInstanceIdentifier(notficationName);
62 NormalizedNodeContext nnc= nn2jsonService.transformNotficationNNCFromString(notficationName,sjson);
63 ContainerNode contn= (ContainerNode)nnc.getData();
64 return mappingservice.fromNormalizedNodeNotification(iicontext.getSchemaNode().getPath(),contn);
67 public DataObject transformRpcDataObjectFromString(String rpcName,String sjson) throws Exception {
68 final InstanceIdentifierContext<?> iicontext = ControllerContext.getInstance().toInstanceIdentifier(rpcName);
69 NormalizedNodeContext nnc= nn2jsonService.transformRPCNNCFromString(rpcName,sjson);
70 return java2nnService.yangRpcDatafromNN(iicontext,nnc.getData());
71 /* ContainerNode contn= (ContainerNode)nnc.getData();
72 DataSchemaNode schemaNode;
73 final SchemaNode schemaNode0 = iicontext.getSchemaNode();
74 boolean isInput = false;
75 if (schemaNode0 instanceof RpcDefinition) {
76 if (contn.getNodeType().getLocalName().contains("output")) {
77 schemaNode = ((RpcDefinition) schemaNode0).getOutput();
80 schemaNode = ((RpcDefinition) schemaNode0).getInput();
84 } else if (schemaNode0 instanceof DataSchemaNode) {
85 schemaNode = (DataSchemaNode) schemaNode0;
87 throw new IllegalStateException("Unknow SchemaNode");
89 return mappingservice.fromNormalizedNodeRpcData(schemaNode.getPath(),contn); */
90 //return mappingservice.toNormalizedNodeRpcData((DataContainer) nnc.getData());
91 // return java2nnService.yangDataObjectfromNNC(nnc);