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.opendaylight.mdsal.binding.dom.adapter.BindingToNormalizedNodeCodec;
25 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
26 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
27 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
28 import org.opendaylight.netconf.sal.restconf.impl.WriterParameters;
29 import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer;
30 import org.opendaylight.yangtools.yang.binding.DataContainer;
31 import org.opendaylight.yangtools.yang.binding.DataObject;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.model.api.*;
41 * Created by Administrator on 2017/3/17.
43 public class YangDataTransformJava2NNServiceImpl {
44 BindingNormalizedNodeSerializer mappingService;
45 public YangDataTransformJava2NNServiceImpl(BindingNormalizedNodeSerializer mappingService)
47 this.mappingService=mappingService;
49 public <T extends DataObject> NormalizedNodeContext yangDataObjecttoNNC(InstanceIdentifier<T> identifier, String uri, T dobj)
51 final InstanceIdentifierContext<?> iiContext = ControllerContext.getInstance().toInstanceIdentifier(uri);
52 Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> temp = mappingService.toNormalizedNode(identifier, dobj);
53 WriterParameters.WriterParametersBuilder aa=new WriterParameters.WriterParametersBuilder();
54 aa.setPrettyPrint(true);
55 return new NormalizedNodeContext( iiContext, temp.getValue() ,aa.build());
57 public <T extends DataObject> NormalizedNode yangDataObjecttoNN( InstanceIdentifier<T> identifier,T dobj) {
58 Map.Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> temp = mappingService.toNormalizedNode(identifier, dobj);
62 return temp.getValue();
65 public NormalizedNodeContext yangNNtoNNC(NormalizedNode nn, String uri){
66 final InstanceIdentifierContext<?> iiContext = ControllerContext.getInstance().toInstanceIdentifier(uri);
67 WriterParameters.WriterParametersBuilder aa=new WriterParameters.WriterParametersBuilder();
68 aa.setPrettyPrint(true);
69 return new NormalizedNodeContext(iiContext, nn,aa.build());
71 public <T extends DataObject> T yangDataObjectfromNN(YangInstanceIdentifier identifier,NormalizedNode node) {
72 Map.Entry<InstanceIdentifier<?>, DataObject> temp = mappingService.fromNormalizedNode(identifier, node);
76 return (T) temp.getValue();
79 public <T extends DataObject> T yangDataObjectfromNNC(NormalizedNodeContext nnc)
81 return yangDataObjectfromNN(nnc.getInstanceIdentifierContext().getInstanceIdentifier(),nnc.getData());
83 public ContainerNode yangRpcDatatoNN(final DataContainer rpcdata){
84 return mappingService.toNormalizedNodeRpcData(rpcdata);
86 public DataObject yangRpcDatafromNN(SchemaPath path, final ContainerNode data)
88 return mappingService.fromNormalizedNodeRpcData(path,data);
90 public static DataSchemaNode findDataSchemaNode(String uriPath,String inputoroutput)
92 final InstanceIdentifierContext<?> iicontext = ControllerContext.getInstance().toInstanceIdentifier(uriPath);
93 DataSchemaNode dsn=JsonParserStream.getWrapSchemaNode( iicontext.getSchemaNode());
96 public static SchemaNode findSchemaNode(final InstanceIdentifierContext<?> iicontext ,String inputoroutput)
98 SchemaNode schemaNode;
100 final SchemaNode schemaNode0 = iicontext.getSchemaNode();
101 if (schemaNode0 instanceof RpcDefinition) {
102 if (inputoroutput.contains("output")) {
103 schemaNode = ((RpcDefinition) schemaNode0).getOutput();
105 schemaNode = ((RpcDefinition) schemaNode0).getInput();
108 } else if(schemaNode0 instanceof NotificationDefinition)
110 schemaNode=schemaNode0;
112 else if (schemaNode0 instanceof DataSchemaNode) {
113 schemaNode = schemaNode0;
115 throw new IllegalStateException("Unknow SchemaNode");
120 public static DataSchemaNode findRpcSchemaNode(final InstanceIdentifierContext<?> iicontext,String inputoroutput)
122 DataSchemaNode schemaNode;
123 final SchemaNode schemaNode0 = iicontext.getSchemaNode();
124 boolean isInput = false;
125 if (schemaNode0 instanceof RpcDefinition) {
126 if (inputoroutput.contains("output")) {
127 schemaNode = ((RpcDefinition) schemaNode0).getOutput();
130 schemaNode = ((RpcDefinition) schemaNode0).getInput();
134 } else if (schemaNode0 instanceof DataSchemaNode) {
135 schemaNode = (DataSchemaNode) schemaNode0;
137 throw new IllegalStateException("Unknow SchemaNode");
141 public DataObject yangRpcDatafromNN(final InstanceIdentifierContext<?> iicontext , final NormalizedNode data) {
142 // final InstanceIdentifierContext<?> iicontext = ControllerContext.getInstance().toInstanceIdentifier(uriPath);
143 ContainerNode contn= (ContainerNode)data;
144 DataSchemaNode schemaNode= findRpcSchemaNode(iicontext,contn.getNodeType().getLocalName());;
145 /* final SchemaNode schemaNode0 = iicontext.getSchemaNode();
146 boolean isInput = false;
147 if (schemaNode0 instanceof RpcDefinition) {
148 if (contn.getNodeType().getLocalName().contains("output")) {
149 schemaNode = ((RpcDefinition) schemaNode0).getOutput();
152 schemaNode = ((RpcDefinition) schemaNode0).getInput();
156 } else if (schemaNode0 instanceof DataSchemaNode) {
157 schemaNode = (DataSchemaNode) schemaNode0;
159 throw new IllegalStateException("Unknow SchemaNode");
161 SchemaPath path=schemaNode.getPath();
162 return mappingService.fromNormalizedNodeRpcData(path,contn);