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.controller.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.controller.md.sal.rest.common.RestconfValidationUtils;
16 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
17 import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext;
18 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
19 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
20 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
21 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 public class SchemaRetrievalServiceImpl implements SchemaRetrievalService {
27 private final ControllerContext salContext;
29 private static final Splitter SLASH_SPLITTER = Splitter.on("/");
30 private static final String MOUNT_ARG = ControllerContext.MOUNT;
32 public SchemaRetrievalServiceImpl(final ControllerContext controllerContext) {
33 salContext = controllerContext;
38 public SchemaExportContext getSchema(final String mountAndModule) {
39 final SchemaContext schemaContext;
40 final Iterable<String> pathComponents = SLASH_SPLITTER.split(mountAndModule);
41 final Iterator<String> componentIter = pathComponents.iterator();
42 if(!Iterables.contains(pathComponents, MOUNT_ARG)) {
43 schemaContext = salContext.getGlobalSchema();
45 final StringBuilder pathBuilder = new StringBuilder();
46 while(componentIter.hasNext()) {
47 final String current = componentIter.next();
48 // It is argument, not last element.
49 if(pathBuilder.length() != 0) {
50 pathBuilder.append("/");
52 pathBuilder.append(current);
53 if(MOUNT_ARG.equals(current)) {
54 // We stop right at mountpoint, last two arguments should
55 // be module name and revision
59 schemaContext = getMountSchemaContext(pathBuilder.toString());
63 RestconfValidationUtils.checkDocumentedError(componentIter.hasNext(),
64 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, "Module name must be supplied.");
65 final String moduleName = componentIter.next();
66 RestconfValidationUtils.checkDocumentedError(componentIter.hasNext(),
67 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, "Revision date must be supplied.");
68 final String revisionString = componentIter.next();
69 return getExportUsingNameAndRevision(schemaContext, moduleName, revisionString);
72 private SchemaExportContext getExportUsingNameAndRevision(final SchemaContext schemaContext, final String moduleName,
73 final String revisionStr) {
75 final Date revision = SimpleDateFormatUtil.getRevisionFormat().parse(revisionStr);
76 final Module module = schemaContext.findModuleByName(moduleName, revision);
77 return new SchemaExportContext(schemaContext, RestconfValidationUtils.checkNotNullDocumented(module, moduleName));
78 } catch (final ParseException e) {
79 throw new RestconfDocumentedException("Supplied revision is not in expected date format YYYY-mm-dd", e);
83 private SchemaContext getMountSchemaContext(final String identifier) {
84 final InstanceIdentifierContext<?> mountContext = salContext.toMountPointIdentifier(identifier);
85 return mountContext.getSchemaContext();