Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Foundation |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.yms.app.yob; |
| 18 | |
| 19 | import org.onosproject.yms.app.ydt.YdtExtendedContext; |
| 20 | import org.onosproject.yms.app.ydt.YdtExtendedListener; |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 21 | import org.onosproject.yms.app.yob.exception.YobException; |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 22 | import org.onosproject.yms.app.ysr.YangSchemaRegistry; |
| 23 | import org.onosproject.yms.ydt.YdtContext; |
| 24 | |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 25 | import static org.onosproject.yms.app.yob.YobConstants.E_MISSING_DATA_IN_NODE; |
| 26 | import static org.onosproject.yms.app.yob.YobHandlerFactory.instance; |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Represents implementation of YANG object builder listener. |
| 30 | */ |
| 31 | class YobListener implements YdtExtendedListener { |
| 32 | |
| 33 | /** |
| 34 | * Reference to the ydt root node. |
| 35 | */ |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 36 | private YdtExtendedContext rootNode; |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Reference to YANG schema registry. |
| 40 | */ |
| 41 | private YangSchemaRegistry schemaRegistry; |
| 42 | |
| 43 | /** |
| 44 | * Reference to YOB handler. |
| 45 | */ |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 46 | private YobHandlerFactory handlerFactory; |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Creates an instance of YANG object builder listener. |
| 50 | * |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 51 | * @param rootNode refers to YDT context |
| 52 | * @param schemaRegistry refers to YANG schema registry |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 53 | */ |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 54 | YobListener(YdtExtendedContext rootNode, |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 55 | YangSchemaRegistry schemaRegistry) { |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 56 | this.rootNode = rootNode; |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 57 | this.schemaRegistry = schemaRegistry; |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 58 | this.handlerFactory = instance(); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 62 | public void enterYdtNode(YdtExtendedContext node) { |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 63 | |
| 64 | YobHandler nodeHandler = |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 65 | handlerFactory.getYobHandlerForContext(node); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 66 | |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 67 | nodeHandler.createBuilder(node, rootNode, schemaRegistry); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 68 | |
| 69 | } |
| 70 | |
| 71 | @Override |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 72 | public void exitYdtNode(YdtExtendedContext node) { |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 73 | YobHandler nodeHandler = |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 74 | 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-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 82 | } |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 83 | |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 84 | } |
| 85 | |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 86 | /** |
| 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-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 92 | @Override |
| 93 | public void enterYdtNode(YdtContext ydtContext) { |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 94 | throw new YobException(E_MISSING_DATA_IN_NODE); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 95 | } |
| 96 | |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 97 | /** |
| 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-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 103 | @Override |
| 104 | public void exitYdtNode(YdtContext ydtContext) { |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 105 | throw new YobException(E_MISSING_DATA_IN_NODE); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 106 | } |
| 107 | } |