Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [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.netconf.client.impl; |
| 18 | |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 19 | import com.google.common.annotations.Beta; |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Thomas Vachuska | 59d24eb | 2017-03-22 10:57:34 -0700 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Service; |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 26 | import org.onosproject.cluster.NodeId; |
| 27 | import org.onosproject.net.DeviceId; |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 28 | import org.onosproject.netconf.NetconfController; |
| 29 | import org.onosproject.netconf.NetconfDevice; |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 30 | import org.onosproject.netconf.NetconfException; |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 31 | import org.onosproject.netconf.NetconfSession; |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 32 | import org.onosproject.netconf.client.NetconfTranslator; |
| 33 | import org.onosproject.yang.model.DataNode; |
| 34 | import org.onosproject.yang.model.InnerNode; |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 35 | import org.onosproject.yang.model.KeyLeaf; |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 36 | import org.onosproject.yang.model.LeafListKey; |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 37 | import org.onosproject.yang.model.LeafNode; |
| 38 | import org.onosproject.yang.model.ListKey; |
| 39 | import org.onosproject.yang.model.NodeKey; |
| 40 | import org.onosproject.yang.model.ResourceData; |
| 41 | import org.onosproject.yang.model.ResourceId; |
Aaron Kruglikov | d24dc98 | 2017-03-23 18:48:58 -0700 | [diff] [blame] | 42 | import org.onosproject.yang.model.SchemaContext; |
| 43 | import org.onosproject.yang.model.SchemaContextProvider; |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 44 | import org.onosproject.yang.model.SchemaId; |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 45 | import org.onosproject.yang.runtime.CompositeStream; |
| 46 | import org.onosproject.yang.runtime.DefaultAnnotatedNodeInfo; |
| 47 | import org.onosproject.yang.runtime.DefaultAnnotation; |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 48 | import org.onosproject.yang.runtime.DefaultCompositeData; |
| 49 | import org.onosproject.yang.runtime.DefaultCompositeStream; |
Thomas Vachuska | a4d5a49 | 2017-05-10 12:02:43 -0700 | [diff] [blame] | 50 | import org.onosproject.yang.model.DefaultResourceData; |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 51 | import org.onosproject.yang.runtime.DefaultRuntimeContext; |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 52 | import org.onosproject.yang.runtime.DefaultYangSerializerContext; |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 53 | import org.onosproject.yang.runtime.YangRuntimeService; |
| 54 | import org.onosproject.yang.runtime.YangSerializerContext; |
Ray Milkey | 86d1b0a | 2017-05-16 15:15:08 -0700 | [diff] [blame] | 55 | import org.onosproject.yang.runtime.SerializerHelper; |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 56 | import org.osgi.service.component.ComponentContext; |
| 57 | import org.slf4j.Logger; |
| 58 | import org.slf4j.LoggerFactory; |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 59 | |
| 60 | import java.io.BufferedReader; |
| 61 | import java.io.ByteArrayInputStream; |
| 62 | import java.io.IOException; |
| 63 | import java.io.InputStream; |
| 64 | import java.io.InputStreamReader; |
| 65 | import java.nio.charset.StandardCharsets; |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 66 | import java.util.Iterator; |
| 67 | import java.util.List; |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 68 | import java.util.regex.Matcher; |
| 69 | import java.util.regex.Pattern; |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 70 | |
| 71 | import static com.google.common.base.Preconditions.checkNotNull; |
Yuta HIGUCHI | dd7c3f8 | 2017-09-03 14:18:01 -0700 | [diff] [blame] | 72 | import static org.onosproject.netconf.DatastoreId.RUNNING; |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 73 | import static org.onosproject.yang.model.DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE; |
Ray Milkey | 86d1b0a | 2017-05-16 15:15:08 -0700 | [diff] [blame] | 74 | import static org.onosproject.yang.runtime.SerializerHelper.addDataNode; |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 75 | |
| 76 | /*FIXME these imports are not visible using OSGI*/ |
| 77 | |
| 78 | /*FIXME these imports are not visible using OSGI*/ |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 79 | |
| 80 | /*TODO once the API's are finalized this comment will be made more specified.*/ |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 81 | |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 82 | /** |
| 83 | * Translator which accepts data types defined for the DynamicConfigService and |
| 84 | * makes the appropriate calls to NETCONF devices before encoding and returning |
| 85 | * responses in formats suitable for the DynamicConfigService. |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 86 | * <p> |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 87 | * NOTE: This entity does not ensure you are the master of a device you attempt |
| 88 | * to contact. If you are not the master an error will be thrown because there |
| 89 | * will be no session available. |
| 90 | */ |
| 91 | @Beta |
Thomas Vachuska | 59d24eb | 2017-03-22 10:57:34 -0700 | [diff] [blame] | 92 | @Service |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 93 | @Component(immediate = true) |
| 94 | public class NetconfTranslatorImpl implements NetconfTranslator { |
| 95 | |
Ray Milkey | ba547f9 | 2018-02-01 15:22:31 -0800 | [diff] [blame] | 96 | private static final Logger log = LoggerFactory |
| 97 | .getLogger(NetconfTranslator.class); |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 98 | |
| 99 | private NodeId localNodeId; |
| 100 | |
| 101 | private static final String GET_CONFIG_MESSAGE_REGEX = |
| 102 | "<data>\n?\\s*(.*?)\n?\\s*</data>"; |
| 103 | private static final int GET_CONFIG_CORE_MESSAGE_GROUP = 1; |
| 104 | private static final Pattern GET_CONFIG_CORE_MESSAGE_PATTERN = |
| 105 | Pattern.compile(GET_CONFIG_MESSAGE_REGEX, Pattern.DOTALL); |
| 106 | private static final String GET_CORE_MESSAGE_REGEX = "<data>\n?\\s*(.*?)\n?\\s*</data>"; |
| 107 | private static final int GET_CORE_MESSAGE_GROUP = 1; |
| 108 | private static final Pattern GET_CORE_MESSAGE_PATTERN = |
| 109 | Pattern.compile(GET_CORE_MESSAGE_REGEX, Pattern.DOTALL); |
| 110 | |
| 111 | private static final String NETCONF_1_0_BASE_NAMESPACE = |
| 112 | "urn:ietf:params:xml:ns:netconf:base:1.0"; |
| 113 | |
| 114 | private static final String GET_URI = "urn:ietf:params:xml:ns:yang:" + |
| 115 | "yrt-ietf-network:networks/network/node"; |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 116 | private static final String XML_ENCODING_SPECIFIER = "xml"; |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 117 | private static final String OP_SPECIFIER = "xc:operation"; |
| 118 | private static final String REPLACE_OP_SPECIFIER = "replace"; |
| 119 | private static final String DELETE_OP_SPECIFIER = "delete"; |
| 120 | private static final String XMLNS_XC_SPECIFIER = "xmlns:xc"; |
| 121 | private static final String XMLNS_SPECIFIER = "xmlns"; |
| 122 | |
| 123 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 124 | protected NetconfController netconfController; |
| 125 | |
| 126 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 127 | protected YangRuntimeService yangRuntimeService; |
| 128 | |
| 129 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Aaron Kruglikov | d24dc98 | 2017-03-23 18:48:58 -0700 | [diff] [blame] | 130 | protected SchemaContextProvider schemaContextProvider; |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 131 | |
| 132 | @Activate |
| 133 | public void activate(ComponentContext context) { |
| 134 | log.info("Started"); |
| 135 | } |
| 136 | |
| 137 | @Deactivate |
| 138 | public void deactivate() { |
| 139 | log.info("Stopped"); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public ResourceData getDeviceConfig(DeviceId deviceId) throws IOException { |
| 144 | NetconfSession session = getNetconfSession(deviceId); |
| 145 | /*FIXME "running" will be replaced with an enum once netconf supports multiple datastores.*/ |
Andrei Mihaescu | ac542ca | 2017-03-26 21:36:25 +0300 | [diff] [blame] | 146 | String reply = session.getConfig(RUNNING); |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 147 | Matcher protocolStripper = GET_CONFIG_CORE_MESSAGE_PATTERN.matcher(reply); |
| 148 | reply = protocolStripper.group(GET_CONFIG_CORE_MESSAGE_GROUP); |
| 149 | return yangRuntimeService.decode( |
| 150 | new DefaultCompositeStream( |
| 151 | null, |
| 152 | /*FIXME is UTF_8 the appropriate encoding? */ |
| 153 | new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))), |
| 154 | new DefaultRuntimeContext.Builder() |
| 155 | .setDataFormat(XML_ENCODING_SPECIFIER) |
| 156 | .addAnnotation( |
| 157 | new DefaultAnnotation(XMLNS_SPECIFIER, |
| 158 | NETCONF_1_0_BASE_NAMESPACE)) |
| 159 | .build()).resourceData(); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public boolean editDeviceConfig(DeviceId deviceId, ResourceData resourceData, |
| 164 | NetconfTranslator.OperationType operationType) throws IOException { |
| 165 | NetconfSession session = getNetconfSession(deviceId); |
Aaron Kruglikov | d24dc98 | 2017-03-23 18:48:58 -0700 | [diff] [blame] | 166 | SchemaContext context = schemaContextProvider |
| 167 | .getSchemaContext(ResourceId.builder().addBranchPointSchema("/", null).build()); |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 168 | ResourceData modifiedPathResourceData = getResourceData(resourceData.resourceId(), |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 169 | resourceData.dataNodes(), |
| 170 | new DefaultYangSerializerContext(context, null)); |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 171 | DefaultCompositeData.Builder compositeDataBuilder = DefaultCompositeData |
| 172 | .builder() |
| 173 | .resourceData(modifiedPathResourceData); |
| 174 | for (DataNode node : resourceData.dataNodes()) { |
| 175 | ResourceId resourceId = resourceData.resourceId(); |
| 176 | if (operationType != OperationType.DELETE) { |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 177 | resourceId = getAnnotatedNodeResourceId( |
| 178 | resourceData.resourceId(), node); |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 179 | } |
| 180 | if (resourceId != null) { |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 181 | DefaultAnnotatedNodeInfo.Builder annotatedNodeInfo = |
| 182 | DefaultAnnotatedNodeInfo.builder(); |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 183 | annotatedNodeInfo.resourceId(resourceId); |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 184 | annotatedNodeInfo.addAnnotation( |
| 185 | new DefaultAnnotation( |
| 186 | OP_SPECIFIER, operationType == OperationType.DELETE ? |
| 187 | DELETE_OP_SPECIFIER : REPLACE_OP_SPECIFIER)); |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 188 | compositeDataBuilder.addAnnotatedNodeInfo(annotatedNodeInfo.build()); |
| 189 | } |
| 190 | } |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 191 | CompositeStream config = yangRuntimeService.encode( |
| 192 | compositeDataBuilder.build(), |
| 193 | new DefaultRuntimeContext.Builder() |
| 194 | .setDataFormat(XML_ENCODING_SPECIFIER) |
| 195 | .addAnnotation(new DefaultAnnotation( |
| 196 | XMLNS_XC_SPECIFIER, NETCONF_1_0_BASE_NAMESPACE)) |
| 197 | .build()); |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 198 | /* FIXME need to fix to string conversion. */ |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 199 | |
| 200 | try { |
| 201 | String reply = session.requestSync(Utils.editConfig(streamToString( |
| 202 | config.resourceData()))); |
| 203 | } catch (NetconfException e) { |
| 204 | log.error("failed to send a request sync", e); |
| 205 | return false; |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 206 | } |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 207 | /* NOTE: a failure to edit is reflected as a NetconfException.*/ |
| 208 | return true; |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public ResourceData getDeviceState(DeviceId deviceId) throws IOException { |
| 213 | NetconfSession session = getNetconfSession(deviceId); |
| 214 | /*TODO the first parameter will come into use if get is required to support filters.*/ |
| 215 | String reply = session.get(null, null); |
| 216 | Matcher protocolStripper = GET_CORE_MESSAGE_PATTERN.matcher(reply); |
| 217 | reply = protocolStripper.group(GET_CORE_MESSAGE_GROUP); |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 218 | return yangRuntimeService.decode( |
| 219 | new DefaultCompositeStream( |
| 220 | null, |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 221 | /*FIXME is UTF_8 the appropriate encoding? */ |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 222 | new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))), |
| 223 | new DefaultRuntimeContext.Builder() |
| 224 | .setDataFormat(XML_ENCODING_SPECIFIER) |
| 225 | .addAnnotation( |
| 226 | new DefaultAnnotation( |
| 227 | XMLNS_SPECIFIER, |
| 228 | NETCONF_1_0_BASE_NAMESPACE)) |
| 229 | .build()).resourceData(); |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 230 | /* NOTE: a failure to get is reflected as a NetconfException.*/ |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Returns a session for the specified deviceId if this node is its master, |
| 235 | * returns null otherwise. |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 236 | * |
sangyun-han | 43c5670 | 2017-07-11 11:55:28 +0900 | [diff] [blame] | 237 | * @param deviceId the id of node for which we wish to retrieve a session |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 238 | * @return a NetconfSession with the specified node or null |
| 239 | */ |
| 240 | private NetconfSession getNetconfSession(DeviceId deviceId) { |
| 241 | NetconfDevice device = netconfController.getNetconfDevice(deviceId); |
| 242 | checkNotNull(device, "The specified deviceId could not be found by the NETCONF controller."); |
| 243 | NetconfSession session = device.getSession(); |
| 244 | checkNotNull(session, "A session could not be retrieved for the specified deviceId."); |
| 245 | return session; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Accepts a stream and converts it to a string. |
| 250 | * |
| 251 | * @param stream the stream to be converted |
| 252 | * @return a string with the same sequence of characters as the stream |
| 253 | * @throws IOException if reading from the stream fails |
| 254 | */ |
| 255 | private String streamToString(InputStream stream) throws IOException { |
| 256 | BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); |
| 257 | StringBuilder builder = new StringBuilder(); |
| 258 | |
| 259 | String nextLine = reader.readLine(); |
| 260 | while (nextLine != null) { |
| 261 | builder.append(nextLine); |
| 262 | nextLine = reader.readLine(); |
| 263 | } |
| 264 | return builder.toString(); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Returns resource data having resource id as "/" and data node tree |
| 269 | * starting from "/" by creating data nodes for given resource id(parent |
| 270 | * node for given list of nodes) and list of child nodes. |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 271 | * <p> |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 272 | * This api will be used in encode flow only. |
| 273 | * |
| 274 | * @param rid resource identifier till parent node |
| 275 | * @param nodes list of data nodes |
| 276 | * @param cont yang serializer context |
| 277 | * @return resource data. |
| 278 | */ |
| 279 | public static ResourceData getResourceData( |
| 280 | ResourceId rid, List<DataNode> nodes, YangSerializerContext cont) { |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 281 | if (rid == null) { |
| 282 | ResourceData.Builder resData = DefaultResourceData.builder(); |
| 283 | for (DataNode node : nodes) { |
| 284 | resData.addDataNode(node); |
| 285 | } |
| 286 | return resData.build(); |
| 287 | } |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 288 | List<NodeKey> keys = rid.nodeKeys(); |
| 289 | Iterator<NodeKey> it = keys.iterator(); |
| 290 | DataNode.Builder dbr = SerializerHelper.initializeDataNode(cont); |
| 291 | |
| 292 | // checking the resource id weather it is getting started from / or not |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 293 | |
| 294 | while (it.hasNext()) { |
| 295 | NodeKey nodekey = it.next(); |
| 296 | SchemaId sid = nodekey.schemaId(); |
| 297 | dbr = addDataNode(dbr, sid.name(), sid.namespace(), |
| 298 | null, null); |
| 299 | if (nodekey instanceof ListKey) { |
| 300 | for (KeyLeaf keyLeaf : ((ListKey) nodekey).keyLeafs()) { |
| 301 | String val; |
| 302 | if (keyLeaf.leafValue() == null) { |
| 303 | val = null; |
| 304 | } else { |
| 305 | val = keyLeaf.leafValAsString(); |
| 306 | } |
| 307 | dbr = addDataNode(dbr, keyLeaf.leafSchema().name(), |
| 308 | sid.namespace(), val, |
| 309 | SINGLE_INSTANCE_LEAF_VALUE_NODE); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | if (dbr instanceof LeafNode.Builder && |
Ray Milkey | 57add21 | 2018-01-16 13:04:42 -0800 | [diff] [blame] | 315 | (nodes != null && !nodes.isEmpty())) { |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 316 | //exception "leaf/leaf-list can not have child node" |
| 317 | } |
| 318 | |
| 319 | if (nodes != null && !nodes.isEmpty()) { |
| 320 | // adding the parent node for given list of nodes |
| 321 | for (DataNode node : nodes) { |
| 322 | dbr = ((InnerNode.Builder) dbr).addNode(node); |
| 323 | } |
| 324 | } |
Aaron Kruglikov | d24dc98 | 2017-03-23 18:48:58 -0700 | [diff] [blame] | 325 | /*FIXME this can be uncommented for use with versions of onos-yang-tools newer than 1.12.0-b6*/ |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 326 | // while (dbr.parent() != null) { |
| 327 | // dbr = SerializerHelper.exitDataNode(dbr); |
| 328 | // } |
| 329 | |
| 330 | ResourceData.Builder resData = DefaultResourceData.builder(); |
| 331 | |
| 332 | resData.addDataNode(dbr.build()); |
| 333 | resData.resourceId(null); |
| 334 | return resData.build(); |
| 335 | } |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 336 | |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 337 | /** |
| 338 | * Returns resource id for annotated data node by adding resource id of top |
| 339 | * level data node to given resource id. |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 340 | * <p> |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 341 | * Annotation will be added to node based on the updated resource id. |
| 342 | * This api will be used in encode flow only. |
| 343 | * |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 344 | * @param rid resource identifier till parent node |
| 345 | * @param node data node |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 346 | * @return updated resource id. |
| 347 | */ |
| 348 | public static ResourceId getAnnotatedNodeResourceId(ResourceId rid, |
| 349 | DataNode node) { |
| 350 | |
| 351 | String val; |
| 352 | ResourceId.Builder rIdBldr = ResourceId.builder(); |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 353 | if (rid != null) { |
| 354 | try { |
| 355 | rIdBldr = rid.copyBuilder(); |
| 356 | } catch (CloneNotSupportedException e) { |
Ray Milkey | ba547f9 | 2018-02-01 15:22:31 -0800 | [diff] [blame] | 357 | log.debug("clone not supported", e); |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 358 | } |
| 359 | } else { |
| 360 | rIdBldr.addBranchPointSchema("/", null); |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 361 | } |
| 362 | DataNode.Type type = node.type(); |
| 363 | NodeKey k = node.key(); |
| 364 | SchemaId sid = k.schemaId(); |
| 365 | |
| 366 | switch (type) { |
| 367 | |
| 368 | case MULTI_INSTANCE_LEAF_VALUE_NODE: |
| 369 | val = ((LeafListKey) k).value().toString(); |
| 370 | rIdBldr.addLeafListBranchPoint(sid.name(), sid.namespace(), val); |
| 371 | break; |
| 372 | |
| 373 | case MULTI_INSTANCE_NODE: |
| 374 | rIdBldr.addBranchPointSchema(sid.name(), sid.namespace()); |
| 375 | // Preparing the list of key values for multiInstanceNode |
| 376 | for (KeyLeaf keyLeaf : ((ListKey) node.key()).keyLeafs()) { |
| 377 | val = keyLeaf.leafValAsString(); |
| 378 | rIdBldr.addKeyLeaf(keyLeaf.leafSchema().name(), sid.namespace(), val); |
| 379 | } |
| 380 | break; |
| 381 | case SINGLE_INSTANCE_LEAF_VALUE_NODE: |
| 382 | case SINGLE_INSTANCE_NODE: |
| 383 | rIdBldr.addBranchPointSchema(sid.name(), sid.namespace()); |
| 384 | break; |
| 385 | |
| 386 | default: |
| 387 | throw new IllegalArgumentException(); |
| 388 | } |
| 389 | return rIdBldr.build(); |
| 390 | } |
| 391 | } |