6fb58f5a2b923677ccceead051460b8b7bc9ea89
[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.controller.md.sal.dom.api.DOMMountPoint;
18 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
19 import org.opendaylight.netconf.md.sal.rest.schema.SchemaExportContext;
20 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
21 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
22 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
23 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
24 import org.opendaylight.restconf.parser.IdentifierCodec;
25 import org.opendaylight.restconf.utils.RestconfConstants;
26 import org.opendaylight.restconf.utils.validation.RestconfValidation;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
30 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Util class for parsing identifier
39  *
40  */
41 public final class ParserIdentifier {
42
43     private static final Logger LOG = LoggerFactory.getLogger(ParserIdentifier.class);
44
45     /**
46      * Make {@link InstanceIdentifierContext} from identifier.
47      *
48      * @param identifier
49      *            - path identifier
50      * @param schemaContext
51      *            - {@link SchemaContext}
52      * @return {@link InstanceIdentifierContext}
53      */
54     public static InstanceIdentifierContext<?> toInstanceIdentifier(final String identifier,
55             final SchemaContext schemaContext) {
56         final YangInstanceIdentifier deserialize;
57         if (identifier.contains(RestconfConstants.MOUNT)) {
58             final String mountPointId = identifier.substring(0, identifier.indexOf("/" + RestconfConstants.MOUNT));
59             deserialize = IdentifierCodec.deserialize(mountPointId, schemaContext);
60         } else {
61             deserialize = IdentifierCodec.deserialize(identifier, schemaContext);
62         }
63         final DataSchemaContextNode<?> child = DataSchemaContextTree.from(schemaContext).getChild(deserialize);
64         return new InstanceIdentifierContext<SchemaNode>(deserialize, child.getDataSchemaNode(), null, schemaContext);
65     }
66
67     /**
68      * Make {@link String} from {@link YangInstanceIdentifier}
69      *
70      * @param instanceIdentifier
71      * @param schemaContext
72      * @return
73      */
74     public static String stringFromYangInstanceIdentifier(final YangInstanceIdentifier instanceIdentifier,
75             final SchemaContext schemaContext) {
76         return IdentifierCodec.serialize(instanceIdentifier, schemaContext);
77     }
78
79     /**
80      * Make a {@link QName} from identifier
81      *
82      * @param identifier
83      *            - path parameter
84      * @return {@link QName}
85      */
86     public static QName makeQNameFromIdentifier(final String identifier) {
87         // check if more than one slash is not used as path separator
88         if (identifier.contains(
89                 String.valueOf(RestconfConstants.SLASH).concat(String.valueOf(RestconfConstants.SLASH)))) {
90             LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
91             throw new RestconfDocumentedException(
92                     "URI has bad format. End of URI should be in format \'moduleName/yyyy-MM-dd\'", ErrorType.PROTOCOL,
93                     ErrorTag.INVALID_VALUE);
94         }
95
96         final int mountIndex = identifier.indexOf(RestconfConstants.MOUNT);
97         String moduleNameAndRevision = "";
98         if (mountIndex >= 0) {
99             moduleNameAndRevision = identifier.substring(mountIndex + RestconfConstants.MOUNT.length())
100                     .replaceFirst(String.valueOf(RestconfConstants.SLASH), "");
101         } else {
102             moduleNameAndRevision = identifier;
103         }
104
105         final Splitter splitter = Splitter.on(RestconfConstants.SLASH);
106         final Iterable<String> split = splitter.split(moduleNameAndRevision);
107         final List<String> pathArgs = Lists.newArrayList(split);
108         if (pathArgs.size() != 2) {
109             LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
110             throw new RestconfDocumentedException(
111                     "URI has bad format. End of URI should be in format \'moduleName/yyyy-MM-dd\'", ErrorType.PROTOCOL,
112                     ErrorTag.INVALID_VALUE);
113         }
114
115         try {
116             final String moduleName = pathArgs.get(0);
117             final String revision = pathArgs.get(1);
118             final Date moduleRevision = RestconfConstants.REVISION_FORMAT.parse(revision);
119
120             return QName.create(null, moduleRevision, moduleName);
121         } catch (final ParseException e) {
122             LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
123             throw new RestconfDocumentedException("URI has bad format. It should be \'moduleName/yyyy-MM-dd\'",
124                     ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
125         }
126     }
127
128     /**
129      * Parsing {@link Module} module by {@link String} module name and
130      * {@link Date} revision and from the parsed module create
131      * {@link SchemaExportContext}
132      *
133      * @param schemaContext
134      *            - {@link SchemaContext}
135      * @param identifier
136      *            - path parameter
137      * @param domMountPointService
138      *            - {@link DOMMountPointService}
139      * @return {@link SchemaExportContext}
140      */
141     public static SchemaExportContext toSchemaExportContextFromIdentifier(final SchemaContext schemaContext,
142             final String identifier, final DOMMountPointService domMountPointService) {
143         final Iterable<String> pathComponents = RestconfConstants.SLASH_SPLITTER.split(identifier);
144         final Iterator<String> componentIter = pathComponents.iterator();
145         if (!Iterables.contains(pathComponents, RestconfConstants.MOUNT)) {
146             final String moduleName = RestconfValidation.validateAndGetModulName(componentIter);
147             final Date revision = RestconfValidation.validateAndGetRevision(componentIter);
148             final Module module = schemaContext.findModuleByName(moduleName, revision);
149             return new SchemaExportContext(schemaContext, module);
150         } else {
151             final StringBuilder pathBuilder = new StringBuilder();
152             while (componentIter.hasNext()) {
153                 final String current = componentIter.next();
154                 pathBuilder.append("/");
155                 pathBuilder.append(current);
156                 if (RestconfConstants.MOUNT.equals(current)) {
157                     break;
158                 }
159             }
160             final InstanceIdentifierContext<?> point = ParserIdentifier
161                     .toInstanceIdentifier(pathBuilder.toString(), schemaContext);
162             final DOMMountPoint mountPoint = domMountPointService.getMountPoint(point.getInstanceIdentifier()).get();
163             final String moduleName = RestconfValidation.validateAndGetModulName(componentIter);
164             final Date revision = RestconfValidation.validateAndGetRevision(componentIter);
165             final Module module = mountPoint.getSchemaContext().findModuleByName(moduleName, revision);
166             return new SchemaExportContext(mountPoint.getSchemaContext(), module);
167         }
168     }
169 }