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.Optional;
11 import com.google.common.base.Splitter;
12 import com.google.common.collect.Iterables;
13 import com.google.common.collect.Lists;
14 import java.text.ParseException;
15 import java.util.Date;
16 import java.util.Iterator;
17 import java.util.List;
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 {@link String} identifier
53 * For identifiers of data NOT behind mount points returned
54 * {@link InstanceIdentifierContext} is prepared with {@code null} reference of {@link DOMMountPoint} and with
55 * controller's {@link SchemaContext}.
57 * For identifiers of data behind mount points returned
58 * {@link InstanceIdentifierContext} is prepared with reference of {@link DOMMountPoint} and its
59 * own {@link SchemaContext}.
63 * @param schemaContext
64 * - controller schema context
65 * @param mountPointService
66 * - mount point service
67 * @return {@link InstanceIdentifierContext}
69 public static InstanceIdentifierContext<?> toInstanceIdentifier(
70 final String identifier,
71 final SchemaContext schemaContext,
72 final Optional<DOMMountPointService> mountPointService) {
73 if (identifier != null && identifier.contains(RestconfConstants.MOUNT)) {
74 if (!mountPointService.isPresent()) {
75 throw new RestconfDocumentedException("Mount point service is not available");
78 final Iterator<String> pathsIt = Splitter.on("/" + RestconfConstants.MOUNT).split(identifier).iterator();
80 final String mountPointId = pathsIt.next();
81 final YangInstanceIdentifier mountYangInstanceIdentifier = IdentifierCodec.deserialize(
82 mountPointId, schemaContext);
83 final Optional<DOMMountPoint> mountPoint = mountPointService.get().getMountPoint(mountYangInstanceIdentifier);
85 if (!mountPoint.isPresent()) {
86 throw new RestconfDocumentedException(
87 "Mount point does not exist.", ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
90 final String pathId = pathsIt.next().replaceFirst("/", "");
91 final YangInstanceIdentifier pathYangInstanceIdentifier = IdentifierCodec.deserialize(
92 pathId, mountPoint.get().getSchemaContext());
94 final DataSchemaContextNode<?> child = DataSchemaContextTree.from(
95 mountPoint.get().getSchemaContext()).getChild(pathYangInstanceIdentifier);
97 return new InstanceIdentifierContext<SchemaNode>(
98 pathYangInstanceIdentifier, child.getDataSchemaNode(), mountPoint.get(),
99 mountPoint.get().getSchemaContext());
101 final YangInstanceIdentifier deserialize = IdentifierCodec.deserialize(identifier, schemaContext);
102 final DataSchemaContextNode<?> child = DataSchemaContextTree.from(schemaContext).getChild(deserialize);
104 return new InstanceIdentifierContext<SchemaNode>(
105 deserialize, child.getDataSchemaNode(), null, schemaContext);
110 * Make {@link String} from {@link YangInstanceIdentifier}
112 * @param instanceIdentifier
113 * @param schemaContext
116 public static String stringFromYangInstanceIdentifier(final YangInstanceIdentifier instanceIdentifier,
117 final SchemaContext schemaContext) {
118 return IdentifierCodec.serialize(instanceIdentifier, schemaContext);
122 * Make a {@link QName} from identifier
126 * @return {@link QName}
128 public static QName makeQNameFromIdentifier(final String identifier) {
129 // check if more than one slash is not used as path separator
130 if (identifier.contains(
131 String.valueOf(RestconfConstants.SLASH).concat(String.valueOf(RestconfConstants.SLASH)))) {
132 LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
133 throw new RestconfDocumentedException(
134 "URI has bad format. End of URI should be in format \'moduleName/yyyy-MM-dd\'", ErrorType.PROTOCOL,
135 ErrorTag.INVALID_VALUE);
138 final int mountIndex = identifier.indexOf(RestconfConstants.MOUNT);
139 String moduleNameAndRevision = "";
140 if (mountIndex >= 0) {
141 moduleNameAndRevision = identifier.substring(mountIndex + RestconfConstants.MOUNT.length())
142 .replaceFirst(String.valueOf(RestconfConstants.SLASH), "");
144 moduleNameAndRevision = identifier;
147 final Splitter splitter = Splitter.on(RestconfConstants.SLASH);
148 final Iterable<String> split = splitter.split(moduleNameAndRevision);
149 final List<String> pathArgs = Lists.newArrayList(split);
150 if (pathArgs.size() != 2) {
151 LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
152 throw new RestconfDocumentedException(
153 "URI has bad format. End of URI should be in format \'moduleName/yyyy-MM-dd\'", ErrorType.PROTOCOL,
154 ErrorTag.INVALID_VALUE);
158 final String moduleName = pathArgs.get(0);
159 final String revision = pathArgs.get(1);
160 final Date moduleRevision = RestconfConstants.REVISION_FORMAT.parse(revision);
162 return QName.create(null, moduleRevision, moduleName);
163 } catch (final ParseException e) {
164 LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
165 throw new RestconfDocumentedException("URI has bad format. It should be \'moduleName/yyyy-MM-dd\'",
166 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
171 * Parsing {@link Module} module by {@link String} module name and
172 * {@link Date} revision and from the parsed module create
173 * {@link SchemaExportContext}
175 * @param schemaContext
176 * - {@link SchemaContext}
179 * @param domMountPointService
180 * - {@link DOMMountPointService}
181 * @return {@link SchemaExportContext}
183 public static SchemaExportContext toSchemaExportContextFromIdentifier(final SchemaContext schemaContext,
184 final String identifier, final DOMMountPointService domMountPointService) {
185 final Iterable<String> pathComponents = RestconfConstants.SLASH_SPLITTER.split(identifier);
186 final Iterator<String> componentIter = pathComponents.iterator();
187 if (!Iterables.contains(pathComponents, RestconfConstants.MOUNT)) {
188 final String moduleName = RestconfValidation.validateAndGetModulName(componentIter);
189 final Date revision = RestconfValidation.validateAndGetRevision(componentIter);
190 final Module module = schemaContext.findModuleByName(moduleName, revision);
191 return new SchemaExportContext(schemaContext, module);
193 final StringBuilder pathBuilder = new StringBuilder();
194 while (componentIter.hasNext()) {
195 final String current = componentIter.next();
197 if (RestconfConstants.MOUNT.equals(current)) {
198 pathBuilder.append("/");
199 pathBuilder.append(RestconfConstants.MOUNT);
203 if (pathBuilder.length() != 0) {
204 pathBuilder.append("/");
207 pathBuilder.append(current);
209 final InstanceIdentifierContext<?> point = ParserIdentifier
210 .toInstanceIdentifier(pathBuilder.toString(), schemaContext, Optional.of(domMountPointService));
211 final String moduleName = RestconfValidation.validateAndGetModulName(componentIter);
212 final Date revision = RestconfValidation.validateAndGetRevision(componentIter);
213 final Module module = point.getMountPoint().getSchemaContext().findModuleByName(moduleName, revision);
214 return new SchemaExportContext(point.getMountPoint().getSchemaContext(), module);