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 final int mountIndex = identifier.indexOf(RestconfConstants.MOUNT);
76 String moduleNameAndRevision = "";
77 if (mountIndex >= 0) {
78 moduleNameAndRevision = identifier.substring(mountIndex + RestconfConstants.MOUNT.length());
80 moduleNameAndRevision = identifier;
83 final Splitter splitter = Splitter.on("/").omitEmptyStrings();
84 final Iterable<String> split = splitter.split(moduleNameAndRevision);
85 final List<String> pathArgs = Lists.<String> newArrayList(split);
86 if (pathArgs.size() < 2) {
87 LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
88 throw new RestconfDocumentedException(
89 "URI has bad format. End of URI should be in format \'moduleName/yyyy-MM-dd\'", ErrorType.PROTOCOL,
90 ErrorTag.INVALID_VALUE);
94 final String moduleName = pathArgs.get(0);
95 final String revision = pathArgs.get(1);
96 final Date moduleRevision = RestconfConstants.REVISION_FORMAT.parse(revision);
98 return QName.create(null, moduleRevision, moduleName);
99 } catch (final ParseException e) {
100 LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
101 throw new RestconfDocumentedException("URI has bad format. It should be \'moduleName/yyyy-MM-dd\'",
102 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
107 * Parsing {@link Module} module by {@link String} module name and
108 * {@link Date} revision and from the parsed module create
109 * {@link SchemaExportContext}
111 * @param schemaContext
112 * - {@link SchemaContext}
115 * @param domMountPointService
116 * - {@link DOMMountPointService}
117 * @return {@link SchemaExportContext}
119 public static SchemaExportContext toSchemaExportContextFromIdentifier(final SchemaContext schemaContext,
120 final String identifier, final DOMMountPointService domMountPointService) {
121 final Iterable<String> pathComponents = RestconfConstants.SLASH_SPLITTER.split(identifier);
122 final Iterator<String> componentIter = pathComponents.iterator();
123 if (!Iterables.contains(pathComponents, RestconfConstants.MOUNT)) {
124 final String moduleName = RestconfValidation.validateAndGetModulName(componentIter);
125 final Date revision = RestconfValidation.validateAndGetRevision(componentIter);
126 final Module module = schemaContext.findModuleByName(moduleName, revision);
127 return new SchemaExportContext(schemaContext, module);
129 final StringBuilder pathBuilder = new StringBuilder();
130 while (componentIter.hasNext()) {
131 final String current = componentIter.next();
132 pathBuilder.append("/");
133 pathBuilder.append(current);
134 if (RestconfConstants.MOUNT.equals(current)) {
138 final InstanceIdentifierContext<?> point = ParserIdentifier
139 .toInstanceIdentifier(pathBuilder.toString(), schemaContext);
140 final DOMMountPoint mountPoint = domMountPointService.getMountPoint(point.getInstanceIdentifier()).get();
141 final String moduleName = RestconfValidation.validateAndGetModulName(componentIter);
142 final Date revision = RestconfValidation.validateAndGetRevision(componentIter);
143 final Module module = mountPoint.getSchemaContext().findModuleByName(moduleName, revision);
144 return new SchemaExportContext(mountPoint.getSchemaContext(), module);