ad239927a773db15047b46fe766ba855c57c959e
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / netconf / md / sal / rest / schema / SchemaRetrievalServiceImpl.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.md.sal.rest.schema;
9
10 import com.google.common.base.Splitter;
11 import com.google.common.collect.Iterables;
12 import java.time.format.DateTimeParseException;
13 import java.util.Iterator;
14 import org.opendaylight.mdsal.dom.api.DOMYangTextSourceProvider;
15 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
16 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
17 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
18 import org.opendaylight.restconf.common.schema.SchemaExportContext;
19 import org.opendaylight.restconf.common.validation.RestconfValidationUtils;
20 import org.opendaylight.yangtools.yang.common.ErrorTag;
21 import org.opendaylight.yangtools.yang.common.ErrorType;
22 import org.opendaylight.yangtools.yang.common.Revision;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25
26 public class SchemaRetrievalServiceImpl implements SchemaRetrievalService {
27
28     private final ControllerContext salContext;
29
30     private static final Splitter SLASH_SPLITTER = Splitter.on("/");
31     private static final String MOUNT_ARG = ControllerContext.MOUNT;
32
33     public SchemaRetrievalServiceImpl(final ControllerContext controllerContext) {
34         salContext = controllerContext;
35     }
36
37
38     @Override
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();
45         } else {
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("/");
52                 }
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
57                     break;
58                 }
59             }
60             schemaContext = getMountSchemaContext(pathBuilder.toString());
61
62         }
63
64         RestconfDocumentedException.throwIf(!componentIter.hasNext(), "Module name must be supplied.",
65                 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
66         final String moduleName = componentIter.next();
67         RestconfDocumentedException.throwIf(!componentIter.hasNext(), "Revision date must be supplied.",
68                 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
69         final String revisionString = componentIter.next();
70         return getExportUsingNameAndRevision(schemaContext, moduleName, revisionString,
71                 salContext.getYangTextSourceProvider());
72     }
73
74     private static SchemaExportContext getExportUsingNameAndRevision(final SchemaContext schemaContext,
75              final String moduleName, final String revisionStr,
76              final DOMYangTextSourceProvider yangTextSourceProvider) {
77         try {
78             final Module module = schemaContext.findModule(moduleName, Revision.of(revisionStr)).orElse(null);
79             return new SchemaExportContext(
80                     schemaContext, RestconfValidationUtils.checkNotNullDocumented(module, moduleName),
81                     yangTextSourceProvider);
82         } catch (final DateTimeParseException e) {
83             throw new RestconfDocumentedException("Supplied revision is not in expected date format YYYY-mm-dd", e);
84         }
85     }
86
87     private SchemaContext getMountSchemaContext(final String identifier) {
88         final InstanceIdentifierContext mountContext = salContext.toMountPointIdentifier(identifier);
89         return mountContext.getSchemaContext();
90     }
91 }
92