Delete netconf
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / rest / impl / JsonNormalizedNodeBodyReader.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.controller.sal.rest.impl;
9
10 import com.google.common.collect.Iterables;
11 import com.google.gson.stream.JsonReader;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.io.InputStreamReader;
15 import java.lang.annotation.Annotation;
16 import java.lang.reflect.Type;
17 import java.util.ArrayList;
18 import java.util.List;
19 import javax.ws.rs.Consumes;
20 import javax.ws.rs.WebApplicationException;
21 import javax.ws.rs.core.MediaType;
22 import javax.ws.rs.core.MultivaluedMap;
23 import javax.ws.rs.ext.MessageBodyReader;
24 import javax.ws.rs.ext.Provider;
25 import org.opendaylight.controller.sal.rest.api.Draft02;
26 import org.opendaylight.controller.sal.rest.api.RestconfService;
27 import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext;
28 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
29 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
30 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
31 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
40 import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream;
41 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
42 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
43 import org.opendaylight.yangtools.yang.data.impl.schema.ResultAlreadySetException;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
46 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
47 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 @Provider
52 @Consumes({ Draft02.MediaTypes.DATA + RestconfService.JSON, Draft02.MediaTypes.OPERATION + RestconfService.JSON,
53         MediaType.APPLICATION_JSON })
54 public class JsonNormalizedNodeBodyReader extends AbstractIdentifierAwareJaxRsProvider implements MessageBodyReader<NormalizedNodeContext> {
55
56     private final static Logger LOG = LoggerFactory.getLogger(JsonNormalizedNodeBodyReader.class);
57
58     @Override
59     public boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
60             final MediaType mediaType) {
61         return true;
62     }
63
64     @Override
65     public NormalizedNodeContext readFrom(final Class<NormalizedNodeContext> type, final Type genericType,
66             final Annotation[] annotations, final MediaType mediaType,
67             final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException,
68             WebApplicationException {
69         try {
70             final InstanceIdentifierContext<?> path = getInstanceIdentifierContext();
71             if (entityStream.available() < 1) {
72                 return new NormalizedNodeContext(path, null);
73             }
74             final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
75             final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
76
77             final SchemaNode parentSchema;
78             if(isPost()) {
79                 // FIXME: We need dispatch for RPC.
80                 parentSchema = path.getSchemaNode();
81             } else if(path.getSchemaNode() instanceof SchemaContext) {
82                 parentSchema = path.getSchemaContext();
83             } else {
84                 if (SchemaPath.ROOT.equals(path.getSchemaNode().getPath().getParent())) {
85                     parentSchema = path.getSchemaContext();
86                 } else {
87                     parentSchema = SchemaContextUtil.findDataSchemaNode(path.getSchemaContext(), path.getSchemaNode().getPath().getParent());
88                 }
89             }
90
91             final JsonParserStream jsonParser = JsonParserStream.create(writer, path.getSchemaContext(), parentSchema);
92             final JsonReader reader = new JsonReader(new InputStreamReader(entityStream));
93             jsonParser.parse(reader);
94
95             NormalizedNode<?, ?> result = resultHolder.getResult();
96             final List<YangInstanceIdentifier.PathArgument> iiToDataList = new ArrayList<>();
97             InstanceIdentifierContext<? extends SchemaNode> newIIContext;
98
99             while (result instanceof AugmentationNode || result instanceof ChoiceNode) {
100                 final Object childNode = ((DataContainerNode) result).getValue().iterator().next();
101                 if (isPost()) {
102                     iiToDataList.add(result.getIdentifier());
103                 }
104                 result = (NormalizedNode<?, ?>) childNode;
105             }
106
107             if (isPost()) {
108                 if (result instanceof MapEntryNode) {
109                     iiToDataList.add(new YangInstanceIdentifier.NodeIdentifier(result.getNodeType()));
110                     iiToDataList.add(result.getIdentifier());
111                 } else {
112                     iiToDataList.add(result.getIdentifier());
113                 }
114             } else {
115                 if (result instanceof MapNode) {
116                     result = Iterables.getOnlyElement(((MapNode) result).getValue());
117                 }
118             }
119
120             final YangInstanceIdentifier fullIIToData = YangInstanceIdentifier.create(Iterables.concat(
121                             path.getInstanceIdentifier().getPathArguments(), iiToDataList));
122
123             newIIContext = new InstanceIdentifierContext<>(fullIIToData, path.getSchemaNode(), path.getMountPoint(),
124                     path.getSchemaContext());
125
126             return new NormalizedNodeContext(newIIContext, result);
127         } catch (final RestconfDocumentedException e) {
128             throw e;
129         } catch (final ResultAlreadySetException e) {
130             LOG.debug("Error parsing json input:", e);
131
132             throw new RestconfDocumentedException("Error parsing json input: Failed to create new parse result data. " +
133                     "Are you creating multiple resources/subresources in POST request?");
134         } catch (final Exception e) {
135             LOG.debug("Error parsing json input", e);
136
137             throw new RestconfDocumentedException("Error parsing input: " + e.getMessage(), ErrorType.PROTOCOL,
138                     ErrorTag.MALFORMED_MESSAGE);
139         }
140     }
141 }
142