blob: 6d6468a28de0e35f6800fc08e49f939d705e49f9 [file] [log] [blame]
Mahesh Poojary Huaweia330c582016-08-26 10:38:10 +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.ypm;
18
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Service;
23import org.onosproject.yms.ydt.YdtContext;
24import org.onosproject.yms.ypm.YpmContext;
25import org.onosproject.yms.ypm.YpmService;
26import org.onosproject.yms.ypm.DefaultYpmNode;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30/**
31 * Represents implementation of YANG protocol metadata manager.
32 */
33@Service
34@Component(immediate = true)
35public class YpmManager implements YpmService {
36
37 private final Logger log = LoggerFactory.getLogger(getClass());
38 private YpmContext rootYpmNode;
39
40 @Activate
41 public void activate() {
42 log.info("Started");
43 }
44
45 @Deactivate
46 public void deactivate() {
47 log.info("Stopped");
48 }
49
50 @Override
51 public YpmContext getProtocolData(YdtContext rootYdtNode) {
52 if (rootYdtNode == null) {
53 log.debug("Input data is null. So, will not proceed.");
54 return null;
55 }
56
57 // Iterate through YDT and search the path in YPM tree and create new YPM tree equivalent to given YDT
58 // and return it.
59 if (rootYpmNode == null) {
60 log.debug("YPM tree has no data.");
61 return null;
62 }
63
64 YdtContext currentYdtNode = rootYdtNode;
65 YpmContext currentYpmNode = rootYpmNode;
66 YpmContext rootRetYpmNode = new DefaultYpmNode(currentYdtNode.getName());
67 YpmContext currRetYpmNode = rootRetYpmNode;
68 while (currentYdtNode != null) {
69 // Each ypm node can have children. So, create new corresponding ypm child
70 // and add it to new returning ypm tree, otherwise return new ypm tree.
71 YdtContext nextNode = currentYdtNode.getFirstChild();
72 if (nextNode != null) {
73 YpmContext ypmChild = currentYpmNode.getChild(nextNode.getName());
74 if (ypmChild != null) {
75 currRetYpmNode.addChild(ypmChild.getName());
76 } else {
77 return rootRetYpmNode;
78 }
79 currentYdtNode = nextNode;
80 currentYpmNode = currentYpmNode.getChild(nextNode.getName());
81 currRetYpmNode = currRetYpmNode.getChild(nextNode.getName());
82 currRetYpmNode.setMetaData(currentYpmNode.getMetaData());
83 continue;
84 }
85
86 // No child nodes, so walk tree
87 while (currentYdtNode != null) {
88 // Each ypm node can have sibling. So, create new corresponding ypm sibling node
89 // and add it to new returning ypm tree, otherwise return new ypm tree.
90 nextNode = currentYdtNode.getNextSibling();
91 if (nextNode != null) {
92 YpmContext ypmSibling = currentYpmNode.getSibling(nextNode.getName());
93 if (ypmSibling != null) {
94 currRetYpmNode.addSibling(ypmSibling.getName());
95 } else {
96 return rootRetYpmNode;
97 }
98 currentYdtNode = nextNode;
99 currentYpmNode = currentYpmNode.getSibling(nextNode.getName());
100 currRetYpmNode = currRetYpmNode.getSibling(nextNode.getName());
101 currRetYpmNode.setMetaData(currentYpmNode.getMetaData());
102 break;
103 }
104
105 // Move up
106 if (currentYdtNode == rootYdtNode) {
107 currentYdtNode = null;
108 } else {
109 currentYdtNode = currentYdtNode.getParent();
110 currentYpmNode = currentYpmNode.getParent();
111 currRetYpmNode = currRetYpmNode.getParent();
112 }
113 }
114 }
115
116 return rootRetYpmNode;
117 }
118
119 @Override
120 public void setProtocolData(YdtContext rootYdtNode, Object data) {
121 if (rootYdtNode == null || data == null) {
122 log.debug("Input data is null. So, will not proceed.");
123 return;
124 }
125
126 // Iterate through YDT and add each node of this path to ypm tree if it is not exists
127 YdtContext currentYdtNode = rootYdtNode;
128 YpmContext currentYpmNode = rootYpmNode;
129 while (currentYdtNode != null) {
130 // Check the ypm root node first
131 if (rootYpmNode == null) {
132 rootYpmNode = new DefaultYpmNode(currentYdtNode.getName());
133 currentYpmNode = rootYpmNode;
134 // In logical node, should not set protocol metadata
135 }
136
137 // Move down to first child
138 YdtContext nextNode = currentYdtNode.getFirstChild();
139 if (nextNode != null) {
140 // Each ypm node can have sibling. So, get corresponding sibling node, otherwise create it.
141 if (currentYpmNode.getChild(nextNode.getName()) == null) {
142 currentYpmNode.addChild(nextNode.getName());
143 }
144 currentYpmNode = currentYpmNode.getChild(nextNode.getName());
145 // Set/update protocol metadata
146 currentYpmNode.setMetaData(data);
147 currentYdtNode = nextNode;
148 continue;
149 }
150
151 // No child nodes, so walk tree
152 while (currentYdtNode != null) {
153 // Move to sibling if possible.
154 nextNode = currentYdtNode.getNextSibling();
155 if (nextNode != null) {
156 // Each ypm node can have children (sibling). So, get corresponding ypm child, otherwise create it.
157 if (currentYpmNode.getSibling(nextNode.getName()) == null) {
158 currentYpmNode.addSibling(nextNode.getName());
159 }
160 currentYpmNode = currentYpmNode.getSibling(nextNode.getName());
161 // Set/update protocol metadata
162 currentYpmNode.setMetaData(data);
163 currentYdtNode = nextNode;
164 break;
165 }
166
167 // Move up
168 if (currentYdtNode == rootYdtNode) {
169 currentYdtNode = null;
170 } else {
171 currentYdtNode = currentYdtNode.getParent();
172 currentYpmNode = currentYpmNode.getParent();
173 }
174 }
175 }
176 }
177}