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