2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.restconf.utils.parser;
10 import com.google.common.base.Splitter;
11 import com.google.common.collect.Iterables;
12 import com.google.common.collect.Lists;
13 import java.text.ParseException;
14 import java.util.Date;
15 import java.util.Iterator;
16 import java.util.List;
17 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
18 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
19 import org.opendaylight.netconf.md.sal.rest.schema.SchemaExportContext;
20 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
21 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
22 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
23 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
24 import org.opendaylight.restconf.parser.IdentifierCodec;
25 import org.opendaylight.restconf.utils.RestconfConstants;
26 import org.opendaylight.restconf.utils.validation.RestconfValidation;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
30 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Util class for parsing identifier
41 public final class ParserIdentifier {
43 private static final Logger LOG = LoggerFactory.getLogger(ParserIdentifier.class);
46 * Make {@link InstanceIdentifierContext} from identifier.
50 * @param schemaContext
51 * - {@link SchemaContext}
52 * @return {@link InstanceIdentifierContext}
54 public static InstanceIdentifierContext<?> toInstanceIdentifier(final String identifier,
55 final SchemaContext schemaContext) {
56 final YangInstanceIdentifier deserialize;
57 if (identifier.contains(RestconfConstants.MOUNT)) {
58 final String mountPointId = identifier.substring(0, identifier.indexOf("/" + RestconfConstants.MOUNT));
59 deserialize = IdentifierCodec.deserialize(mountPointId, schemaContext);
61 deserialize = IdentifierCodec.deserialize(identifier, schemaContext);
63 final DataSchemaContextNode<?> child = DataSchemaContextTree.from(schemaContext).getChild(deserialize);
64 return new InstanceIdentifierContext<SchemaNode>(deserialize, child.getDataSchemaNode(), null, schemaContext);
68 * Make {@link String} from {@link YangInstanceIdentifier}
70 * @param instanceIdentifier
71 * @param schemaContext
74 public static String stringFromYangInstanceIdentifier(final YangInstanceIdentifier instanceIdentifier,
75 final SchemaContext schemaContext) {
76 return IdentifierCodec.serialize(instanceIdentifier, schemaContext);
80 * Make a {@link QName} from identifier
84 * @return {@link QName}
86 public static QName makeQNameFromIdentifier(final String identifier) {
87 // check if more than one slash is not used as path separator
88 if (identifier.contains(
89 String.valueOf(RestconfConstants.SLASH).concat(String.valueOf(RestconfConstants.SLASH)))) {
90 LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
91 throw new RestconfDocumentedException(
92 "URI has bad format. End of URI should be in format \'moduleName/yyyy-MM-dd\'", ErrorType.PROTOCOL,
93 ErrorTag.INVALID_VALUE);
96 final int mountIndex = identifier.indexOf(RestconfConstants.MOUNT);
97 String moduleNameAndRevision = "";
98 if (mountIndex >= 0) {
99 moduleNameAndRevision = identifier.substring(mountIndex + RestconfConstants.MOUNT.length())
100 .replaceFirst(String.valueOf(RestconfConstants.SLASH), "");
102 moduleNameAndRevision = identifier;
105 final Splitter splitter = Splitter.on(RestconfConstants.SLASH);
106 final Iterable<String> split = splitter.split(moduleNameAndRevision);
107 final List<String> pathArgs = Lists.newArrayList(split);
108 if (pathArgs.size() != 2) {
109 LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
110 throw new RestconfDocumentedException(
111 "URI has bad format. End of URI should be in format \'moduleName/yyyy-MM-dd\'", ErrorType.PROTOCOL,
112 ErrorTag.INVALID_VALUE);
116 final String moduleName = pathArgs.get(0);
117 final String revision = pathArgs.get(1);
118 final Date moduleRevision = RestconfConstants.REVISION_FORMAT.parse(revision);
120 return QName.create(null, moduleRevision, moduleName);
121 } catch (final ParseException e) {
122 LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
123 throw new RestconfDocumentedException("URI has bad format. It should be \'moduleName/yyyy-MM-dd\'",
124 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
129 * Parsing {@link Module} module by {@link String} module name and
130 * {@link Date} revision and from the parsed module create
131 * {@link SchemaExportContext}
133 * @param schemaContext
134 * - {@link SchemaContext}
137 * @param domMountPointService
138 * - {@link DOMMountPointService}
139 * @return {@link SchemaExportContext}
141 public static SchemaExportContext toSchemaExportContextFromIdentifier(final SchemaContext schemaContext,
142 final String identifier, final DOMMountPointService domMountPointService) {
143 final Iterable<String> pathComponents = RestconfConstants.SLASH_SPLITTER.split(identifier);
144 final Iterator<String> componentIter = pathComponents.iterator();
145 if (!Iterables.contains(pathComponents, RestconfConstants.MOUNT)) {
146 final String moduleName = RestconfValidation.validateAndGetModulName(componentIter);
147 final Date revision = RestconfValidation.validateAndGetRevision(componentIter);
148 final Module module = schemaContext.findModuleByName(moduleName, revision);
149 return new SchemaExportContext(schemaContext, module);
151 final StringBuilder pathBuilder = new StringBuilder();
152 while (componentIter.hasNext()) {
153 final String current = componentIter.next();
154 pathBuilder.append("/");
155 pathBuilder.append(current);
156 if (RestconfConstants.MOUNT.equals(current)) {
160 final InstanceIdentifierContext<?> point = ParserIdentifier
161 .toInstanceIdentifier(pathBuilder.toString(), schemaContext);
162 final DOMMountPoint mountPoint = domMountPointService.getMountPoint(point.getInstanceIdentifier()).get();
163 final String moduleName = RestconfValidation.validateAndGetModulName(componentIter);
164 final Date revision = RestconfValidation.validateAndGetRevision(componentIter);
165 final Module module = mountPoint.getSchemaContext().findModuleByName(moduleName, revision);
166 return new SchemaExportContext(mountPoint.getSchemaContext(), module);