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 java.text.ParseException;
11 import java.util.Date;
12 import java.util.Iterator;
13 import java.util.List;
14 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
15 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
16 import org.opendaylight.netconf.md.sal.rest.schema.SchemaExportContext;
17 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
18 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
19 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
20 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
21 import org.opendaylight.restconf.parser.IdentifierCodec;
22 import org.opendaylight.restconf.utils.RestconfConstants;
23 import org.opendaylight.restconf.utils.validation.RestconfValidation;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
27 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import com.google.common.base.Optional;
35 import com.google.common.base.Splitter;
36 import com.google.common.collect.Iterables;
37 import com.google.common.collect.Lists;
40 * Util class for parsing identifier
43 public final class ParserIdentifier {
45 private static final Logger LOG = LoggerFactory.getLogger(ParserIdentifier.class);
47 private ParserIdentifier() {
48 throw new UnsupportedOperationException("Util class.");
52 * Make {@link InstanceIdentifierContext} from {@link String} identifier
54 * For identifiers of data NOT behind mount points returned
55 * {@link InstanceIdentifierContext} is prepared with {@code null} reference of {@link DOMMountPoint} and with
56 * controller's {@link SchemaContext}.
58 * For identifiers of data behind mount points returned
59 * {@link InstanceIdentifierContext} is prepared with reference of {@link DOMMountPoint} and its
60 * own {@link SchemaContext}.
64 * @param schemaContext
65 * - controller schema context
66 * @param mountPointService
67 * - mount point service
68 * @return {@link InstanceIdentifierContext}
70 public static InstanceIdentifierContext<?> toInstanceIdentifier(
71 final String identifier,
72 final SchemaContext schemaContext,
73 final Optional<DOMMountPointService> mountPointService) {
74 if ((identifier != null) && identifier.contains(RestconfConstants.MOUNT)) {
75 if (!mountPointService.isPresent()) {
76 throw new RestconfDocumentedException("Mount point service is not available");
79 final Iterator<String> pathsIt = Splitter.on("/" + RestconfConstants.MOUNT).split(identifier).iterator();
81 final String mountPointId = pathsIt.next();
82 final YangInstanceIdentifier mountYangInstanceIdentifier = IdentifierCodec.deserialize(
83 mountPointId, schemaContext);
84 final Optional<DOMMountPoint> mountPoint = mountPointService.get().getMountPoint(mountYangInstanceIdentifier);
86 if (!mountPoint.isPresent()) {
87 throw new RestconfDocumentedException(
88 "Mount point does not exist.", ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
91 final String pathId = pathsIt.next().replaceFirst("/", "");
92 final YangInstanceIdentifier pathYangInstanceIdentifier = IdentifierCodec.deserialize(
93 pathId, mountPoint.get().getSchemaContext());
95 final DataSchemaContextNode<?> child = DataSchemaContextTree.from(
96 mountPoint.get().getSchemaContext()).getChild(pathYangInstanceIdentifier);
98 return new InstanceIdentifierContext<SchemaNode>(pathYangInstanceIdentifier, child.getDataSchemaNode(),
99 mountPoint.get(), mountPoint.get().getSchemaContext());
101 final QName rpcQName = mountYangInstanceIdentifier.getLastPathArgument().getNodeType();
102 RpcDefinition def = null;
103 for (final RpcDefinition rpcDefinition : schemaContext
104 .findModuleByNamespaceAndRevision(rpcQName.getNamespace(), rpcQName.getRevision()).getRpcs()) {
105 if (rpcDefinition.getQName().getLocalName().equals(rpcQName.getLocalName())) {
110 return new InstanceIdentifierContext<RpcDefinition>(mountYangInstanceIdentifier, def, mountPoint.get(),
111 mountPoint.get().getSchemaContext());
113 final YangInstanceIdentifier deserialize = IdentifierCodec.deserialize(identifier, schemaContext);
114 final DataSchemaContextNode<?> child = DataSchemaContextTree.from(schemaContext).getChild(deserialize);
117 return new InstanceIdentifierContext<SchemaNode>(deserialize, child.getDataSchemaNode(), null, schemaContext);
119 final QName rpcQName = deserialize.getLastPathArgument().getNodeType();
120 RpcDefinition def = null;
121 for (final RpcDefinition rpcDefinition : schemaContext.findModuleByNamespaceAndRevision(rpcQName.getNamespace(), rpcQName.getRevision()).getRpcs()) {
122 if (rpcDefinition.getQName().getLocalName().equals(rpcQName.getLocalName())) {
127 return new InstanceIdentifierContext<RpcDefinition>(deserialize, def, null, schemaContext);
132 * Make {@link String} from {@link YangInstanceIdentifier}
134 * @param instanceIdentifier
135 * @param schemaContext
138 public static String stringFromYangInstanceIdentifier(final YangInstanceIdentifier instanceIdentifier,
139 final SchemaContext schemaContext) {
140 return IdentifierCodec.serialize(instanceIdentifier, schemaContext);
144 * Make a {@link QName} from identifier
148 * @return {@link QName}
150 public static QName makeQNameFromIdentifier(final String identifier) {
151 // check if more than one slash is not used as path separator
152 if (identifier.contains(
153 String.valueOf(RestconfConstants.SLASH).concat(String.valueOf(RestconfConstants.SLASH)))) {
154 LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
155 throw new RestconfDocumentedException(
156 "URI has bad format. End of URI should be in format \'moduleName/yyyy-MM-dd\'", ErrorType.PROTOCOL,
157 ErrorTag.INVALID_VALUE);
160 final int mountIndex = identifier.indexOf(RestconfConstants.MOUNT);
161 String moduleNameAndRevision = "";
162 if (mountIndex >= 0) {
163 moduleNameAndRevision = identifier.substring(mountIndex + RestconfConstants.MOUNT.length())
164 .replaceFirst(String.valueOf(RestconfConstants.SLASH), "");
166 moduleNameAndRevision = identifier;
169 final Splitter splitter = Splitter.on(RestconfConstants.SLASH);
170 final Iterable<String> split = splitter.split(moduleNameAndRevision);
171 final List<String> pathArgs = Lists.newArrayList(split);
172 if (pathArgs.size() != 2) {
173 LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
174 throw new RestconfDocumentedException(
175 "URI has bad format. End of URI should be in format \'moduleName/yyyy-MM-dd\'", ErrorType.PROTOCOL,
176 ErrorTag.INVALID_VALUE);
180 final String moduleName = pathArgs.get(0);
181 final String revision = pathArgs.get(1);
182 final Date moduleRevision = RestconfConstants.REVISION_FORMAT.parse(revision);
184 return QName.create(null, moduleRevision, moduleName);
185 } catch (final ParseException e) {
186 LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
187 throw new RestconfDocumentedException("URI has bad format. It should be \'moduleName/yyyy-MM-dd\'",
188 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
193 * Parsing {@link Module} module by {@link String} module name and
194 * {@link Date} revision and from the parsed module create
195 * {@link SchemaExportContext}
197 * @param schemaContext
198 * - {@link SchemaContext}
201 * @param domMountPointService
202 * - {@link DOMMountPointService}
203 * @return {@link SchemaExportContext}
205 public static SchemaExportContext toSchemaExportContextFromIdentifier(final SchemaContext schemaContext,
206 final String identifier, final DOMMountPointService domMountPointService) {
207 final Iterable<String> pathComponents = RestconfConstants.SLASH_SPLITTER.split(identifier);
208 final Iterator<String> componentIter = pathComponents.iterator();
209 if (!Iterables.contains(pathComponents, RestconfConstants.MOUNT)) {
210 final String moduleName = RestconfValidation.validateAndGetModulName(componentIter);
211 final Date revision = RestconfValidation.validateAndGetRevision(componentIter);
212 final Module module = schemaContext.findModuleByName(moduleName, revision);
213 return new SchemaExportContext(schemaContext, module);
215 final StringBuilder pathBuilder = new StringBuilder();
216 while (componentIter.hasNext()) {
217 final String current = componentIter.next();
219 if (RestconfConstants.MOUNT.equals(current)) {
220 pathBuilder.append("/");
221 pathBuilder.append(RestconfConstants.MOUNT);
225 if (pathBuilder.length() != 0) {
226 pathBuilder.append("/");
229 pathBuilder.append(current);
231 final InstanceIdentifierContext<?> point = ParserIdentifier
232 .toInstanceIdentifier(pathBuilder.toString(), schemaContext, Optional.of(domMountPointService));
233 final String moduleName = RestconfValidation.validateAndGetModulName(componentIter);
234 final Date revision = RestconfValidation.validateAndGetRevision(componentIter);
235 final Module module = point.getMountPoint().getSchemaContext().findModuleByName(moduleName, revision);
236 return new SchemaExportContext(point.getMountPoint().getSchemaContext(), module);