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 javax.annotation.Nullable;
18 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
19 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
20 import org.opendaylight.netconf.md.sal.rest.schema.SchemaExportContext;
21 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
22 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
23 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
24 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
25 import org.opendaylight.restconf.parser.IdentifierCodec;
26 import org.opendaylight.restconf.utils.RestconfConstants;
27 import org.opendaylight.restconf.utils.validation.RestconfValidation;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
31 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * Util class for parsing identifier
42 public final class ParserIdentifier {
44 private static final Logger LOG = LoggerFactory.getLogger(ParserIdentifier.class);
46 private ParserIdentifier() {
47 throw new UnsupportedOperationException("Util class.");
51 * Make {@link InstanceIdentifierContext} from identifier.
55 * @param schemaContext
56 * - {@link SchemaContext}
57 * @return {@link InstanceIdentifierContext}
59 public static InstanceIdentifierContext<?> toInstanceIdentifier(@Nullable final String identifier,
60 final SchemaContext schemaContext) {
61 final YangInstanceIdentifier deserialize;
62 if (identifier != null && identifier.contains(RestconfConstants.MOUNT)) {
63 final String mountPointId = identifier.substring(0, identifier.indexOf("/" + RestconfConstants.MOUNT));
64 deserialize = IdentifierCodec.deserialize(mountPointId, schemaContext);
66 deserialize = IdentifierCodec.deserialize(identifier, schemaContext);
68 final DataSchemaContextNode<?> child = DataSchemaContextTree.from(schemaContext).getChild(deserialize);
69 return new InstanceIdentifierContext<SchemaNode>(deserialize, child.getDataSchemaNode(), null, schemaContext);
73 * Make {@link String} from {@link YangInstanceIdentifier}
75 * @param instanceIdentifier
76 * @param schemaContext
79 public static String stringFromYangInstanceIdentifier(final YangInstanceIdentifier instanceIdentifier,
80 final SchemaContext schemaContext) {
81 return IdentifierCodec.serialize(instanceIdentifier, schemaContext);
85 * Make a {@link QName} from identifier
89 * @return {@link QName}
91 public static QName makeQNameFromIdentifier(final String identifier) {
92 // check if more than one slash is not used as path separator
93 if (identifier.contains(
94 String.valueOf(RestconfConstants.SLASH).concat(String.valueOf(RestconfConstants.SLASH)))) {
95 LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
96 throw new RestconfDocumentedException(
97 "URI has bad format. End of URI should be in format \'moduleName/yyyy-MM-dd\'", ErrorType.PROTOCOL,
98 ErrorTag.INVALID_VALUE);
101 final int mountIndex = identifier.indexOf(RestconfConstants.MOUNT);
102 String moduleNameAndRevision = "";
103 if (mountIndex >= 0) {
104 moduleNameAndRevision = identifier.substring(mountIndex + RestconfConstants.MOUNT.length())
105 .replaceFirst(String.valueOf(RestconfConstants.SLASH), "");
107 moduleNameAndRevision = identifier;
110 final Splitter splitter = Splitter.on(RestconfConstants.SLASH);
111 final Iterable<String> split = splitter.split(moduleNameAndRevision);
112 final List<String> pathArgs = Lists.newArrayList(split);
113 if (pathArgs.size() != 2) {
114 LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
115 throw new RestconfDocumentedException(
116 "URI has bad format. End of URI should be in format \'moduleName/yyyy-MM-dd\'", ErrorType.PROTOCOL,
117 ErrorTag.INVALID_VALUE);
121 final String moduleName = pathArgs.get(0);
122 final String revision = pathArgs.get(1);
123 final Date moduleRevision = RestconfConstants.REVISION_FORMAT.parse(revision);
125 return QName.create(null, moduleRevision, moduleName);
126 } catch (final ParseException e) {
127 LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
128 throw new RestconfDocumentedException("URI has bad format. It should be \'moduleName/yyyy-MM-dd\'",
129 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
134 * Parsing {@link Module} module by {@link String} module name and
135 * {@link Date} revision and from the parsed module create
136 * {@link SchemaExportContext}
138 * @param schemaContext
139 * - {@link SchemaContext}
142 * @param domMountPointService
143 * - {@link DOMMountPointService}
144 * @return {@link SchemaExportContext}
146 public static SchemaExportContext toSchemaExportContextFromIdentifier(final SchemaContext schemaContext,
147 final String identifier, final DOMMountPointService domMountPointService) {
148 final Iterable<String> pathComponents = RestconfConstants.SLASH_SPLITTER.split(identifier);
149 final Iterator<String> componentIter = pathComponents.iterator();
150 if (!Iterables.contains(pathComponents, RestconfConstants.MOUNT)) {
151 final String moduleName = RestconfValidation.validateAndGetModulName(componentIter);
152 final Date revision = RestconfValidation.validateAndGetRevision(componentIter);
153 final Module module = schemaContext.findModuleByName(moduleName, revision);
154 return new SchemaExportContext(schemaContext, module);
156 final StringBuilder pathBuilder = new StringBuilder();
157 while (componentIter.hasNext()) {
158 final String current = componentIter.next();
160 if (RestconfConstants.MOUNT.equals(current)) {
161 pathBuilder.append("/");
162 pathBuilder.append(RestconfConstants.MOUNT);
166 if (pathBuilder.length() != 0) {
167 pathBuilder.append("/");
170 pathBuilder.append(current);
172 final InstanceIdentifierContext<?> point = ParserIdentifier
173 .toInstanceIdentifier(pathBuilder.toString(), schemaContext);
174 final DOMMountPoint mountPoint = domMountPointService.getMountPoint(point.getInstanceIdentifier()).get();
175 final String moduleName = RestconfValidation.validateAndGetModulName(componentIter);
176 final Date revision = RestconfValidation.validateAndGetRevision(componentIter);
177 final Module module = mountPoint.getSchemaContext().findModuleByName(moduleName, revision);
178 return new SchemaExportContext(mountPoint.getSchemaContext(), module);