2 * Copyright (c) 2014 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.netconf.md.sal.rest.schema;
10 import com.google.common.base.Splitter;
11 import com.google.common.collect.Iterables;
12 import java.text.ParseException;
13 import java.util.Date;
14 import java.util.Iterator;
15 import org.opendaylight.netconf.md.sal.rest.common.RestconfValidationUtils;
16 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
17 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
18 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
19 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
20 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
21 import org.opendaylight.restconf.common.schema.SchemaExportContext;
22 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 public class SchemaRetrievalServiceImpl implements SchemaRetrievalService {
28 private final ControllerContext salContext;
30 private static final Splitter SLASH_SPLITTER = Splitter.on("/");
31 private static final String MOUNT_ARG = ControllerContext.MOUNT;
33 public SchemaRetrievalServiceImpl(final ControllerContext controllerContext) {
34 salContext = controllerContext;
39 public SchemaExportContext getSchema(final String mountAndModule) {
40 final SchemaContext schemaContext;
41 final Iterable<String> pathComponents = SLASH_SPLITTER.split(mountAndModule);
42 final Iterator<String> componentIter = pathComponents.iterator();
43 if (!Iterables.contains(pathComponents, MOUNT_ARG)) {
44 schemaContext = salContext.getGlobalSchema();
46 final StringBuilder pathBuilder = new StringBuilder();
47 while (componentIter.hasNext()) {
48 final String current = componentIter.next();
49 // It is argument, not last element.
50 if (pathBuilder.length() != 0) {
51 pathBuilder.append("/");
53 pathBuilder.append(current);
54 if (MOUNT_ARG.equals(current)) {
55 // We stop right at mountpoint, last two arguments should
56 // be module name and revision
60 schemaContext = getMountSchemaContext(pathBuilder.toString());
64 RestconfValidationUtils.checkDocumentedError(componentIter.hasNext(),
65 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, "Module name must be supplied.");
66 final String moduleName = componentIter.next();
67 RestconfValidationUtils.checkDocumentedError(componentIter.hasNext(),
68 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, "Revision date must be supplied.");
69 final String revisionString = componentIter.next();
70 return getExportUsingNameAndRevision(schemaContext, moduleName, revisionString);
73 private static SchemaExportContext getExportUsingNameAndRevision(final SchemaContext schemaContext,
74 final String moduleName, final String revisionStr) {
76 final Date revision = SimpleDateFormatUtil.getRevisionFormat().parse(revisionStr);
77 final Module module = schemaContext.findModuleByName(moduleName, revision);
78 return new SchemaExportContext(
79 schemaContext, RestconfValidationUtils.checkNotNullDocumented(module, moduleName));
80 } catch (final ParseException e) {
81 throw new RestconfDocumentedException("Supplied revision is not in expected date format YYYY-mm-dd", e);
85 private SchemaContext getMountSchemaContext(final String identifier) {
86 final InstanceIdentifierContext<?> mountContext = salContext.toMountPointIdentifier(identifier);
87 return mountContext.getSchemaContext();