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 a {@link QName} from identifier
72 * @return {@link QName}
74 public static QName makeQNameFromIdentifier(final String identifier) {
75 // check if more than one slash is not used as path separator
76 if (identifier.contains(
77 String.valueOf(RestconfConstants.SLASH).concat(String.valueOf(RestconfConstants.SLASH)))) {
78 LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
79 throw new RestconfDocumentedException(
80 "URI has bad format. End of URI should be in format \'moduleName/yyyy-MM-dd\'", ErrorType.PROTOCOL,
81 ErrorTag.INVALID_VALUE);
84 final int mountIndex = identifier.indexOf(RestconfConstants.MOUNT);
85 String moduleNameAndRevision = "";
86 if (mountIndex >= 0) {
87 moduleNameAndRevision = identifier.substring(mountIndex + RestconfConstants.MOUNT.length())
88 .replaceFirst(String.valueOf(RestconfConstants.SLASH), "");
90 moduleNameAndRevision = identifier;
93 final Splitter splitter = Splitter.on(RestconfConstants.SLASH);
94 final Iterable<String> split = splitter.split(moduleNameAndRevision);
95 final List<String> pathArgs = Lists.newArrayList(split);
96 if (pathArgs.size() != 2) {
97 LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
98 throw new RestconfDocumentedException(
99 "URI has bad format. End of URI should be in format \'moduleName/yyyy-MM-dd\'", ErrorType.PROTOCOL,
100 ErrorTag.INVALID_VALUE);
104 final String moduleName = pathArgs.get(0);
105 final String revision = pathArgs.get(1);
106 final Date moduleRevision = RestconfConstants.REVISION_FORMAT.parse(revision);
108 return QName.create(null, moduleRevision, moduleName);
109 } catch (final ParseException e) {
110 LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
111 throw new RestconfDocumentedException("URI has bad format. It should be \'moduleName/yyyy-MM-dd\'",
112 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
117 * Parsing {@link Module} module by {@link String} module name and
118 * {@link Date} revision and from the parsed module create
119 * {@link SchemaExportContext}
121 * @param schemaContext
122 * - {@link SchemaContext}
125 * @param domMountPointService
126 * - {@link DOMMountPointService}
127 * @return {@link SchemaExportContext}
129 public static SchemaExportContext toSchemaExportContextFromIdentifier(final SchemaContext schemaContext,
130 final String identifier, final DOMMountPointService domMountPointService) {
131 final Iterable<String> pathComponents = RestconfConstants.SLASH_SPLITTER.split(identifier);
132 final Iterator<String> componentIter = pathComponents.iterator();
133 if (!Iterables.contains(pathComponents, RestconfConstants.MOUNT)) {
134 final String moduleName = RestconfValidation.validateAndGetModulName(componentIter);
135 final Date revision = RestconfValidation.validateAndGetRevision(componentIter);
136 final Module module = schemaContext.findModuleByName(moduleName, revision);
137 return new SchemaExportContext(schemaContext, module);
139 final StringBuilder pathBuilder = new StringBuilder();
140 while (componentIter.hasNext()) {
141 final String current = componentIter.next();
142 pathBuilder.append("/");
143 pathBuilder.append(current);
144 if (RestconfConstants.MOUNT.equals(current)) {
148 final InstanceIdentifierContext<?> point = ParserIdentifier
149 .toInstanceIdentifier(pathBuilder.toString(), schemaContext);
150 final DOMMountPoint mountPoint = domMountPointService.getMountPoint(point.getInstanceIdentifier()).get();
151 final String moduleName = RestconfValidation.validateAndGetModulName(componentIter);
152 final Date revision = RestconfValidation.validateAndGetRevision(componentIter);
153 final Module module = mountPoint.getSchemaContext().findModuleByName(moduleName, revision);
154 return new SchemaExportContext(mountPoint.getSchemaContext(), module);