blob: f0411cc2dc3c71cdadfc77ed6b08a6605580c6ba [file] [log] [blame]
Rama-Huaweib711e5c2016-08-31 07:55:46 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Rama-Huaweib711e5c2016-08-31 07:55:46 +05303 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yms.app.yob;
18
19import org.onosproject.yms.app.ydt.YdtExtendedContext;
20import org.onosproject.yms.app.ydt.YdtExtendedListener;
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053021import org.onosproject.yms.app.yob.exception.YobException;
Rama-Huaweib711e5c2016-08-31 07:55:46 +053022import org.onosproject.yms.app.ysr.YangSchemaRegistry;
23import org.onosproject.yms.ydt.YdtContext;
24
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053025import static org.onosproject.yms.app.yob.YobConstants.E_MISSING_DATA_IN_NODE;
26import static org.onosproject.yms.app.yob.YobHandlerFactory.instance;
Rama-Huaweib711e5c2016-08-31 07:55:46 +053027
28/**
29 * Represents implementation of YANG object builder listener.
30 */
31class YobListener implements YdtExtendedListener {
32
33 /**
34 * Reference to the ydt root node.
35 */
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053036 private YdtExtendedContext rootNode;
Rama-Huaweib711e5c2016-08-31 07:55:46 +053037
38 /**
39 * Reference to YANG schema registry.
40 */
41 private YangSchemaRegistry schemaRegistry;
42
43 /**
44 * Reference to YOB handler.
45 */
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053046 private YobHandlerFactory handlerFactory;
Rama-Huaweib711e5c2016-08-31 07:55:46 +053047
48 /**
49 * Creates an instance of YANG object builder listener.
50 *
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053051 * @param rootNode refers to YDT context
52 * @param schemaRegistry refers to YANG schema registry
Rama-Huaweib711e5c2016-08-31 07:55:46 +053053 */
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053054 YobListener(YdtExtendedContext rootNode,
Rama-Huaweib711e5c2016-08-31 07:55:46 +053055 YangSchemaRegistry schemaRegistry) {
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053056 this.rootNode = rootNode;
Rama-Huaweib711e5c2016-08-31 07:55:46 +053057 this.schemaRegistry = schemaRegistry;
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053058 this.handlerFactory = instance();
Rama-Huaweib711e5c2016-08-31 07:55:46 +053059 }
60
61 @Override
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053062 public void enterYdtNode(YdtExtendedContext node) {
Rama-Huaweib711e5c2016-08-31 07:55:46 +053063
64 YobHandler nodeHandler =
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053065 handlerFactory.getYobHandlerForContext(node);
Rama-Huaweib711e5c2016-08-31 07:55:46 +053066
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053067 nodeHandler.createBuilder(node, rootNode, schemaRegistry);
Rama-Huaweib711e5c2016-08-31 07:55:46 +053068
69 }
70
71 @Override
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053072 public void exitYdtNode(YdtExtendedContext node) {
Rama-Huaweib711e5c2016-08-31 07:55:46 +053073 YobHandler nodeHandler =
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053074 handlerFactory.getYobHandlerForContext(node);
75
76 nodeHandler.buildObject(node, rootNode, schemaRegistry);
77
78 // The current ydt context node and root node are same then built
79 // object needs to be returned.
80 if (!node.equals(rootNode)) {
81 nodeHandler.setInParent(node, schemaRegistry);
Rama-Huaweib711e5c2016-08-31 07:55:46 +053082 }
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053083
Rama-Huaweib711e5c2016-08-31 07:55:46 +053084 }
85
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053086 /**
87 * Does not support walking of non extended context YDT.
88 *
89 * @param ydtContext YANG data tree context
90 * @throws YobException if YDT walker is not using extended context walker
91 */
Rama-Huaweib711e5c2016-08-31 07:55:46 +053092 @Override
93 public void enterYdtNode(YdtContext ydtContext) {
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053094 throw new YobException(E_MISSING_DATA_IN_NODE);
Rama-Huaweib711e5c2016-08-31 07:55:46 +053095 }
96
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053097 /**
98 * Does not support walking of non extended context YDT.
99 *
100 * @param ydtContext YANG data tree context
101 * @throws YobException if YDT walker is not using extended context walker
102 */
Rama-Huaweib711e5c2016-08-31 07:55:46 +0530103 @Override
104 public void exitYdtNode(YdtContext ydtContext) {
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530105 throw new YobException(E_MISSING_DATA_IN_NODE);
Rama-Huaweib711e5c2016-08-31 07:55:46 +0530106 }
107}