Adding APIs for YANG based data format serializers.
Change-Id: I8e17c475e636aa9bbf39be52d31583565bae8e92
Issue-ID: CCSDK-375
Signed-off-by: Gaurav Agrawal <gaurav.agrawal@huawei.com>
--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - CCSDK
+ * ================================================================================
+ * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.ccsdk.sli.plugins.yangserializers;
+
+/**
+ * Representation of an entity that represents annotated attribute.
+ */
+public class Annotation {
+
+ private String name;
+ private String value;
+
+ /**
+ * Creates an instance of annotation.
+ *
+ * @param n annotation name
+ * @param v annotation value
+ */
+ public Annotation(String n, String v) {
+ name = n;
+ value = v;
+ }
+
+ /**
+ * Returns name of annotation.
+ *
+ * @return name of annotation
+ */
+ public String name() {
+ return name;
+ }
+
+ /**
+ * Returns value of annotation.
+ *
+ * @return value of annotation
+ */
+ public String value() {
+ return value;
+ }
+}
--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - CCSDK
+ * ================================================================================
+ * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.ccsdk.sli.plugins.yangserializers;
+
+/**
+ * Representation of data format.
+ */
+public enum DataFormat {
+ XML,
+ JSON
+}
--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - CCSDK
+ * ================================================================================
+ * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.ccsdk.sli.plugins.yangserializers;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Abstraction of serializer to encode/decode context memory parameters
+ * to/from specified data format.
+ */
+public abstract class DataFormatSerializer {
+
+ private DataFormat dataFormat;
+ private DataFormatSerializerContext serializerContext;
+
+ /**
+ * Creates an instance of data format serializer.
+ *
+ * @param dataFormat type of data format
+ * @param serializerContext data format serializer context
+ */
+ protected DataFormatSerializer(DataFormat dataFormat,
+ DataFormatSerializerContext serializerContext) {
+ this.dataFormat = dataFormat;
+ this.serializerContext = serializerContext;
+ }
+
+ /**
+ * Encodes context memory parameters to data format.
+ *
+ * @param param context memory parameter
+ * @param annotations annotations
+ * @return data format body
+ */
+ public abstract String encode(Map<String, String> param,
+ Map<String, List<Annotation>> annotations);
+
+ /**
+ * Decodes data format body to context memory parameters.
+ *
+ * @param dataFormatBody abstract node
+ * @return context memory parameters
+ */
+ public abstract Map<String, String> decode(String dataFormatBody);
+
+ /**
+ * Returns data format serializer context.
+ *
+ * @return data format serializer context
+ */
+ public DataFormatSerializerContext serializerContext() {
+ return serializerContext;
+ }
+
+ /**
+ * Returns supported data format.
+ *
+ * @return supported data format
+ */
+ public DataFormat dataFormat() {
+ return dataFormat;
+ }
+}
\ No newline at end of file
--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - CCSDK
+ * ================================================================================
+ * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.ccsdk.sli.plugins.yangserializers;
+
+import java.util.Map;
+
+/**
+ * Abstraction of data format serializer context.
+ */
+public class DataFormatSerializerContext {
+
+ private Listener listener;
+ private String uri;
+ private Map<String, String> protocolAnnotation;
+
+ /**
+ * Creates an instance of data format serializer context.
+ *
+ * @param listener data format listener
+ * @param uri URI corresponding to instance identifier
+ * @param protocolAnnotation protocol annotations
+ */
+ public DataFormatSerializerContext(Listener listener, String uri,
+ Map<String, String> protocolAnnotation) {
+ this.listener = listener;
+ this.uri = uri;
+ this.protocolAnnotation = protocolAnnotation;
+ }
+
+ /**
+ * Retruns data format listener.
+ *
+ * @return data format listener
+ */
+ public Listener listener() {
+ return listener;
+ }
+
+ /**
+ * Returns URI.
+ *
+ * @return URI
+ */
+ public String uri() {
+ return uri;
+ }
+
+ /**
+ * Returns protocol annotations.
+ *
+ * @return protocol annotations
+ */
+ public Map<String, String> getProtocolAnnotation() {
+ return protocolAnnotation;
+ }
+}
\ No newline at end of file
--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - CCSDK
+ * ================================================================================
+ * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.ccsdk.sli.plugins.yangserializers;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Representation of JSON serializer.
+ */
+public class JsonSerializer extends DataFormatSerializer {
+
+ /**
+ * Creates an instance of data format serializer.
+ *
+ * @param serializerContext data format serializer context
+ */
+ protected JsonSerializer(DataFormatSerializerContext serializerContext) {
+ super(DataFormat.JSON, serializerContext);
+ }
+
+ @Override
+ public String encode(Map<String, String> param, Map<String, List<Annotation>> annotations) {
+ return null;
+ }
+
+ @Override
+ public Map<String, String> decode(String dataFormatBody) {
+ return null;
+ }
+}
--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - CCSDK
+ * ================================================================================
+ * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.ccsdk.sli.plugins.yangserializers;
+
+/**
+ * Abstraction of listener.
+ */
+public interface Listener {
+
+ /**
+ * Returns serializer helper for this listener.
+ *
+ * @return serializer helper
+ */
+ SerializerHelper serializerHelper();
+}
--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - CCSDK
+ * ================================================================================
+ * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.ccsdk.sli.plugins.yangserializers;
+
+import java.util.Map;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
+
+/**
+ * Representation of MDSAL based schema helper.
+ */
+public class MdsalSerializerHelper extends SerializerHelper<SchemaNode> {
+
+ protected MdsalSerializerHelper(SchemaNode node, String uri) {
+ super(node, uri);
+ }
+
+ @Override
+ public SchemaNode getRootContext() {
+ return null;
+ }
+
+ @Override
+ public SchemaNode getCurContext() {
+ return null;
+ }
+
+ @Override
+ public SchemaNode getChildContext(SchemaNode schemaNode, String name, String namespace) {
+ return null;
+ }
+
+ @Override
+ public NodeType getNodeType(SchemaNode schemaNode) {
+ return null;
+ }
+
+ @Override
+ public void addNode(String name, String namespace, String value, String valNamespace, NodeType type) {
+ }
+
+ @Override
+ public void exitNode() {
+ }
+
+ @Override
+ public Map<String, String> getProperties() {
+ return null;
+ }
+}
--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - CCSDK
+ * ================================================================================
+ * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.ccsdk.sli.plugins.yangserializers;
+
+/**
+ * Representation of type of data node.
+ */
+public enum NodeType {
+ SINGLE_INSTANCE,
+ MULTI_INSTANCE,
+ SINGLE_INSTANCE_LEAF,
+ MULTI_INSTANCE_LEAF
+}
--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - CCSDK
+ * ================================================================================
+ * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.ccsdk.sli.plugins.yangserializers;
+
+import java.util.Map;
+
+/**
+ * Abstraction of an entity which helps the data format serializers to obtain
+ * schema context details and to build properties from data.
+ *
+ * @param <T> type of schema node
+ */
+public abstract class SerializerHelper<T> {
+
+ private T rootSchema;
+ private String rootURI;
+
+ protected SerializerHelper(T t, String uri) {
+ rootSchema = t;
+ }
+
+ /**
+ * Returns root schema context node.
+ *
+ * @return root schema context node
+ */
+ protected abstract T getRootContext();
+
+ /**
+ * Returns current schema context node.
+ *
+ * @return current schema context node
+ */
+ protected abstract T getCurContext();
+
+ /**
+ * Returns child schema context node.
+ *
+ * @return child schema context node
+ */
+ protected abstract T getChildContext(T t, String name, String namespace);
+
+ /**
+ * Returns type of node
+ * @param t node
+ * @return node type
+ */
+ protected abstract NodeType getNodeType(T t);
+
+ /**
+ * Adds a node to current tree.
+ *
+ * @param name name of node
+ * @param namespace namespace of node, it can be either module name or
+ * namespace, null indicates parent namespace
+ * @param value value of node, in case it's leaf/leaf-list node
+ * @param valNamespace value namespace for identityref, could be module
+ * name or namespace
+ * @param type type of node if known like in case of JSON
+ */
+ protected abstract void addNode(String name, String namespace, String value,
+ String valNamespace, NodeType type);
+
+ /**
+ * Exits the node, in case if it's leaf node add to properties map.
+ */
+ protected abstract void exitNode();
+
+ /**
+ * Returns the properties built corresponding to data.
+ *
+ * @return properties map
+ */
+ protected abstract Map<String, String> getProperties();
+}
--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - CCSDK
+ * ================================================================================
+ * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.ccsdk.sli.plugins.yangserializers;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Representation of XML serializer.
+ */
+public class XmlSerializer extends DataFormatSerializer {
+
+ /**
+ * Creates an instance of XML serializer.
+ *
+ * @param serializerContext data format serializer context
+ */
+ protected XmlSerializer(DataFormatSerializerContext serializerContext) {
+ super(DataFormat.XML, serializerContext);
+ }
+
+ @Override
+ public String encode(Map<String, String> param, Map<String, List<Annotation>> annotations) {
+ return null;
+ }
+
+ @Override
+ public Map<String, String> decode(String dataFormatBody) {
+ return null;
+ }
+}
\ No newline at end of file