Bug 5526 - Implementations of services
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / restconf / utils / parser / ParserIdentifier.java
1 /*
2  * Copyright (c) 2016 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.restconf.utils.parser;
9
10 import com.google.common.base.Splitter;
11 import com.google.common.collect.Iterables;
12 import com.google.common.collect.Lists;
13 import java.text.ParseException;
14 import java.util.Date;
15 import java.util.Iterator;
16 import java.util.List;
17 import org.opendaylight.netconf.md.sal.rest.schema.SchemaExportContext;
18 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
19 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
20 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
21 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
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.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Util class for parsing identifier
32  *
33  */
34 public final class ParserIdentifier {
35
36     private static final Logger LOG = LoggerFactory.getLogger(ParserIdentifier.class);
37
38     public static InstanceIdentifierContext<?> toInstanceIdentifier(final String identifier) {
39         throw new UnsupportedOperationException("Not yet implemented");
40     }
41
42     /**
43      * Make a {@link QName} from identifier
44      *
45      * @param identifier
46      *            - path parameter
47      * @return {@link QName}
48      */
49     public static QName makeQNameFromIdentifier(final String identifier) {
50         final int mountIndex = identifier.indexOf(RestconfConstants.MOUNT);
51         String moduleNameAndRevision = "";
52         if (mountIndex >= 0) {
53             moduleNameAndRevision = identifier.substring(mountIndex + RestconfConstants.MOUNT.length());
54         } else {
55             moduleNameAndRevision = identifier;
56         }
57
58         final Splitter splitter = Splitter.on("/").omitEmptyStrings();
59         final Iterable<String> split = splitter.split(moduleNameAndRevision);
60         final List<String> pathArgs = Lists.<String> newArrayList(split);
61         if (pathArgs.size() < 2) {
62             LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
63             throw new RestconfDocumentedException(
64                     "URI has bad format. End of URI should be in format \'moduleName/yyyy-MM-dd\'", ErrorType.PROTOCOL,
65                     ErrorTag.INVALID_VALUE);
66         }
67
68         try {
69             final String moduleName = pathArgs.get(0);
70             final String revision = pathArgs.get(1);
71             final Date moduleRevision = RestconfConstants.REVISION_FORMAT.parse(revision);
72
73             return QName.create(null, moduleRevision, moduleName);
74         } catch (final ParseException e) {
75             LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
76             throw new RestconfDocumentedException("URI has bad format. It should be \'moduleName/yyyy-MM-dd\'",
77                     ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
78         }
79     }
80
81     /**
82      * Parsing {@link Module} module by {@link String} module name and
83      * {@link Date} revision and from the parsed module create
84      * {@link SchemaExportContext}
85      *
86      * @param schemaContext
87      *            - {@link SchemaContext}
88      * @param identifier
89      *            - path parameter
90      * @return {@link SchemaExportContext}
91      */
92     public static SchemaExportContext toSchemaExportContextFromIdentifier(final SchemaContext schemaContext,
93             final String identifier) {
94         final Iterable<String> pathComponents = RestconfConstants.SLASH_SPLITTER.split(identifier);
95         final Iterator<String> componentIter = pathComponents.iterator();
96         if (!Iterables.contains(pathComponents, RestconfConstants.MOUNT)) {
97             final String moduleName = RestconfValidation.validateAndGetModulName(componentIter);
98             final Date revision = RestconfValidation.validateAndGetRevision(componentIter);
99             final Module module = schemaContext.findModuleByName(moduleName, revision);
100             return new SchemaExportContext(schemaContext, module);
101         } else {
102             final StringBuilder pathBuilder = new StringBuilder();
103             while (componentIter.hasNext()) {
104                 final String current = componentIter.next();
105                 if (pathBuilder.length() != 0) {
106                     pathBuilder.append("/");
107                 }
108                 pathBuilder.append(current);
109                 if (RestconfConstants.MOUNT.equals(current)) {
110                     break;
111                 }
112             }
113             final InstanceIdentifierContext<?> mountPoint = ParserIdentifier
114                     .toInstanceIdentifier(pathBuilder.toString());
115             final String moduleName = RestconfValidation.validateAndGetModulName(componentIter);
116             final Date revision = RestconfValidation.validateAndGetRevision(componentIter);
117             final Module module = mountPoint.getSchemaContext().findModuleByName(moduleName, revision);
118             return new SchemaExportContext(mountPoint.getSchemaContext(), module);
119         }
120     }
121 }