Update POM to inherit from oparent
[so.git] / common / src / main / java / org / openecomp / mso / yangDecoder / transform / impl / YangDataTransformNN2JsonServiceImpl.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 com.google.common.base.Charsets;
24 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
25 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
26 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import javax.ws.rs.core.MediaType;
31 import java.io.ByteArrayInputStream;
32 import java.io.ByteArrayOutputStream;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.lang.annotation.Annotation;
36
37 /**
38  * Created by Administrator on 2017/3/17.
39  */
40 public class YangDataTransformNN2JsonServiceImpl {
41     private static final Logger LOG = LoggerFactory.getLogger(YangDataTransformNN2JsonServiceImpl.class);
42     private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0];
43
44     public NormalizedNodeContext transformNNCFromString(String uriPath,String jsonpayload,boolean isPost){
45
46         InputStream entityStream = new ByteArrayInputStream(jsonpayload.getBytes(Charsets.UTF_8));
47         NormalizedNodeContext normalnodes3 = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, isPost);
48         return normalnodes3;
49     }
50     public String transformNNCToString(NormalizedNodeContext readData) throws IOException {
51         NormalizedNodeJsonBodyWriter writer = new NormalizedNodeJsonBodyWriter();
52         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
53         //readData.getWriterParameters().isPrettyPrint()
54         writer.writeTo(readData, NormalizedNodeContext.class, null, EMPTY_ANNOTATIONS,
55                 MediaType.APPLICATION_JSON_TYPE, null, outputStream );
56         return outputStream.toString(Charsets.UTF_8.name());
57     }
58     public NormalizedNodeContext transformRPCNNCFromString(String uriPath,String jsonpayload)
59     {
60         return transformNNCFromString(uriPath,jsonpayload,true);
61     }
62     public NormalizedNodeContext transformDataObjectNNCFromString(String uriPath,String jsonpayload,boolean ispost)
63     {
64         return transformNNCFromString(uriPath,jsonpayload,ispost);
65     }
66     public NormalizedNodeContext transformNotficationNNCFromString(String uriPath,String jsonpayload)
67     {
68         return transformNNCFromString(uriPath,jsonpayload,true);
69     }
70 }