Update POM to inherit from oparent
[so.git] / common / src / main / java / org / openecomp / mso / yangDecoder / transform / impl / TransformJava2JsonServiceImpl.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 org.openecomp.mso.yangDecoder.transform.api.ITransformJava2StringService;
24 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
25 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
26 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
27 import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.opendaylight.yangtools.yang.binding.Notification;
31 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.model.api.*;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Created by Administrator on 2017/3/20.
39  */
40 public class TransformJava2JsonServiceImpl  implements ITransformJava2StringService {
41     private static final Logger LOG = LoggerFactory.getLogger(TransformJava2JsonServiceImpl.class);
42     BindingNormalizedNodeSerializer mappingservice;
43     SchemaContext schemaContext;
44     YangDataTransformNN2JsonServiceImpl nn2jsonService;
45     YangDataTransformJava2NNServiceImpl java2nnService;
46     public TransformJava2JsonServiceImpl(BindingNormalizedNodeSerializer mappingservice, SchemaContext schemaContext){
47        this.mappingservice=mappingservice;
48         this.schemaContext=schemaContext;
49         nn2jsonService=new YangDataTransformNN2JsonServiceImpl();
50         java2nnService=new YangDataTransformJava2NNServiceImpl(mappingservice);
51     }
52     @Override
53     public <T extends DataObject> String transformContrainerDataObjectToString(InstanceIdentifier<T> instanceIdentifier, String uriPath,T dataObject) throws Exception {
54         // TODO Auto-generated method stub
55         NormalizedNode nn = java2nnService.yangDataObjecttoNN(instanceIdentifier, dataObject);
56         NormalizedNodeContext nnc = java2nnService.yangNNtoNNC(nn, uriPath);
57         String sjson = nn2jsonService.transformNNCToString(nnc);
58         return sjson;
59     }
60     @Override
61     public <T extends Notification> String transformNotificationToString(String uriPath,T notification)throws Exception  {
62         NormalizedNode nn = mappingservice.toNormalizedNodeNotification(notification);
63         NormalizedNodeContext nnc = java2nnService.yangNNtoNNC(nn, uriPath);
64         String sjson = nn2jsonService.transformNNCToString(nnc);
65         return sjson;
66     }
67     @Override
68     public <T extends DataObject> String transformRpcDataObjectToString(String uriPath,T dataObject) throws Exception  {
69         NormalizedNode nn = mappingservice.toNormalizedNodeRpcData(dataObject);
70         NormalizedNodeContext nnc = java2nnService.yangNNtoNNC(nn, uriPath);
71         String sjson = nn2jsonService.transformNNCToString(nnc);
72         return sjson;
73     }
74     @Override
75     public  DataObject  transformContrainerDataObjectFromString(String uriPath,String sjson,boolean ispost) throws Exception {
76         NormalizedNodeContext nnc= nn2jsonService.transformDataObjectNNCFromString(uriPath,sjson,ispost);
77         return java2nnService.yangDataObjectfromNNC(nnc);
78     }
79     @Override
80     public Notification transformNotificationFromString(String notficationName,String sjson) throws Exception {
81         final InstanceIdentifierContext<?> iicontext = ControllerContext.getInstance().toInstanceIdentifier(notficationName);
82         NormalizedNodeContext nnc= nn2jsonService.transformNotficationNNCFromString(notficationName,sjson);
83         ContainerNode contn= (ContainerNode)nnc.getData();
84         return mappingservice.fromNormalizedNodeNotification(iicontext.getSchemaNode().getPath(),contn);
85     }
86     @Override
87     public DataObject transformRpcDataObjectFromString(String rpcName,String sjson) throws Exception {
88         final InstanceIdentifierContext<?> iicontext = ControllerContext.getInstance().toInstanceIdentifier(rpcName);
89         NormalizedNodeContext nnc= nn2jsonService.transformRPCNNCFromString(rpcName,sjson);
90         return  java2nnService.yangRpcDatafromNN(iicontext,nnc.getData());
91     /*    ContainerNode contn= (ContainerNode)nnc.getData();
92         DataSchemaNode schemaNode;
93         final SchemaNode schemaNode0 = iicontext.getSchemaNode();
94         boolean isInput = false;
95         if (schemaNode0 instanceof RpcDefinition) {
96             if (contn.getNodeType().getLocalName().contains("output")) {
97                 schemaNode = ((RpcDefinition) schemaNode0).getOutput();
98                 isInput = false;
99             } else {
100                 schemaNode = ((RpcDefinition) schemaNode0).getInput();
101                 isInput = true;
102             }
103
104         } else if (schemaNode0 instanceof DataSchemaNode) {
105             schemaNode = (DataSchemaNode) schemaNode0;
106         } else {
107             throw new IllegalStateException("Unknow SchemaNode");
108         }
109        return mappingservice.fromNormalizedNodeRpcData(schemaNode.getPath(),contn); */
110         //return mappingservice.toNormalizedNodeRpcData((DataContainer) nnc.getData());
111       //  return  java2nnService.yangDataObjectfromNNC(nnc);
112     }
113 }