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