2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.yangDecoder.transform.impl;
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;
38 * Created by Administrator on 2017/3/20.
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);
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);
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);
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);
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);
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);
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();
100 schemaNode = ((RpcDefinition) schemaNode0).getInput();
104 } else if (schemaNode0 instanceof DataSchemaNode) {
105 schemaNode = (DataSchemaNode) schemaNode0;
107 throw new IllegalStateException("Unknow SchemaNode");
109 return mappingservice.fromNormalizedNodeRpcData(schemaNode.getPath(),contn); */
110 //return mappingservice.toNormalizedNodeRpcData((DataContainer) nnc.getData());
111 // return java2nnService.yangDataObjectfromNNC(nnc);