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