blob: be523258b7ae07f8ebafaadd1706e22e11e850d0 [file] [log] [blame]
Rama-Huaweib711e5c2016-08-31 07:55:46 +05301/*
2 * Copyright 2016-present Open Networking Laboratory
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
17package org.onosproject.yms.app.yob;
18
19import org.onosproject.yms.app.ydt.YdtExtendedContext;
20import org.onosproject.yms.app.ydt.YdtExtendedListener;
21import org.onosproject.yms.app.yob.exception.YobExceptions;
22import org.onosproject.yms.app.ysr.YangSchemaRegistry;
23import org.onosproject.yms.ydt.YdtContext;
24
25import static org.onosproject.yms.app.yob.YobConstants.NO_HANDLE_FOR_YDT;
26
27/**
28 * Represents implementation of YANG object builder listener.
29 */
30class YobListener implements YdtExtendedListener {
31
32 /**
33 * Reference to the ydt root node.
34 */
35 private YdtExtendedContext ydtRootNode;
36
37 /**
38 * Reference to YANG schema registry.
39 */
40 private YangSchemaRegistry schemaRegistry;
41
42 /**
43 * Reference to YOB handler.
44 */
45 private YobHandlerFactory yobHandlerFactory;
46
47 /**
48 * Creates an instance of YANG object builder listener.
49 *
50 * @param ydtRootExtendedContext refers to YDT context
51 * @param schemaRegistry refers to YANG schema registry
52 */
53 YobListener(YdtExtendedContext ydtRootExtendedContext,
54 YangSchemaRegistry schemaRegistry) {
55 this.ydtRootNode = ydtRootExtendedContext;
56 this.schemaRegistry = schemaRegistry;
57 this.yobHandlerFactory = new YobHandlerFactory();
58 }
59
60 @Override
61 public void enterYdtNode(YdtExtendedContext ydtExtendedContext) {
62
63 YobHandler nodeHandler =
64 yobHandlerFactory.getYobHandlerForContext(ydtExtendedContext);
65
66 if (nodeHandler == null) {
67 throw new YobExceptions(NO_HANDLE_FOR_YDT);
68 }
69 nodeHandler.createYangBuilderObject(ydtExtendedContext,
70 ydtRootNode, schemaRegistry);
71
72 }
73
74 @Override
75 public void exitYdtNode(YdtExtendedContext ydtExtendedContext) {
76 YobHandler nodeHandler =
77 yobHandlerFactory.getYobHandlerForContext(ydtExtendedContext);
78 if (nodeHandler != null) {
79 nodeHandler.buildObjectFromBuilder(ydtExtendedContext,
80 ydtRootNode, schemaRegistry);
81 // The current ydt context node and root node are same then return.
82 if (!ydtExtendedContext.equals(ydtRootNode)) {
83 nodeHandler.setObjectInParent(ydtExtendedContext,
84 schemaRegistry);
85 }
86 }
87 }
88
89 @Override
90 public void enterYdtNode(YdtContext ydtContext) {
91 }
92
93 @Override
94 public void exitYdtNode(YdtContext ydtContext) {
95 }
96}