f6a70a9ace136b2a5cde731a909fec172e453c67
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / restconf / rest / services / impl / RestconfOperationsServiceImpl.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.rest.services.impl;
9
10 import com.google.common.base.Optional;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Set;
15 import javax.ws.rs.core.UriInfo;
16 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
17 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
18 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
19 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
20 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
21 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
22 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
23 import org.opendaylight.restconf.common.references.SchemaContextRef;
24 import org.opendaylight.restconf.handlers.DOMMountPointServiceHandler;
25 import org.opendaylight.restconf.handlers.SchemaContextHandler;
26 import org.opendaylight.restconf.rest.services.api.RestconfOperationsService;
27 import org.opendaylight.restconf.utils.RestconfConstants;
28 import org.opendaylight.restconf.utils.parser.ParserIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
31 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
33 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.Module;
36 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * Implementation of {@link RestconfOperationsService}
44  *
45  */
46 public class RestconfOperationsServiceImpl implements RestconfOperationsService {
47
48     private static final Logger LOG = LoggerFactory.getLogger(RestconfOperationsServiceImpl.class);
49
50     private final SchemaContextHandler schemaContextHandler;
51     private final DOMMountPointServiceHandler domMountPointServiceHandler;
52
53     /**
54      * Set {@link SchemaContextHandler} for getting actual {@link SchemaContext}
55      *
56      * @param schemaContextHandler
57      *            - handling schema context
58      * @param domMountPointServiceHandler
59      *            - handling dom mount point service
60      */
61     public RestconfOperationsServiceImpl(final SchemaContextHandler schemaContextHandler,
62             final DOMMountPointServiceHandler domMountPointServiceHandler) {
63         this.schemaContextHandler = schemaContextHandler;
64         this.domMountPointServiceHandler = domMountPointServiceHandler;
65     }
66
67     @Override
68     public NormalizedNodeContext getOperations(final UriInfo uriInfo) {
69         final SchemaContextRef ref = new SchemaContextRef(this.schemaContextHandler.get());
70         return getOperations(ref.getModules(), null);
71     }
72
73     @Override
74     public NormalizedNodeContext getOperations(final String identifier, final UriInfo uriInfo) {
75         final Set<Module> modules;
76         final DOMMountPoint mountPoint;
77         final SchemaContextRef ref = new SchemaContextRef(this.schemaContextHandler.get());
78         if (identifier.contains(RestconfConstants.MOUNT)) {
79             final InstanceIdentifierContext<?> mountPointIdentifier = ParserIdentifier.toInstanceIdentifier(
80                     identifier, ref.get(), Optional.of(this.domMountPointServiceHandler.get()));
81             mountPoint = mountPointIdentifier.getMountPoint();
82             modules = ref.getModules(mountPoint);
83
84         } else {
85             final String errMsg = "URI has bad format. If operations behind mount point should be showed, URI has to end with ";
86             LOG.debug(errMsg + ControllerContext.MOUNT + " for " + identifier);
87             throw new RestconfDocumentedException(errMsg + ControllerContext.MOUNT, ErrorType.PROTOCOL,
88                     ErrorTag.INVALID_VALUE);
89         }
90
91         return getOperations(modules, mountPoint);
92     }
93
94     /**
95      * Special case only for GET restconf/operations use (since moment of old
96      * Yang parser and old Yang model API removal). The method is creating fake
97      * schema context with fake module and fake data by use own implementations
98      * of schema nodes and module.
99      *
100      * @param modules
101      *            - set of modules for get RPCs from every module
102      * @param mountPoint
103      *            - mount point, if in use otherwise null
104      * @return {@link NormalizedNodeContext}
105      */
106     private static NormalizedNodeContext getOperations(final Set<Module> modules, final DOMMountPoint mountPoint) {
107         final Collection<Module> neededModules = new ArrayList<>(modules.size());
108         final ArrayList<LeafSchemaNode> fakeRpcSchema = new ArrayList<>();
109
110         for (final Module m : modules) {
111             final Set<RpcDefinition> rpcs = m.getRpcs();
112             if (!rpcs.isEmpty()) {
113                 neededModules.add(m);
114
115                 fakeRpcSchema.ensureCapacity(fakeRpcSchema.size() + rpcs.size());
116                 rpcs.forEach(rpc -> fakeRpcSchema.add(new FakeLeafSchemaNode(rpc.getQName())));
117             }
118         }
119
120         final ContainerSchemaNode fakeCont = new FakeContainerSchemaNode(fakeRpcSchema);
121         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> containerBuilder = Builders
122                 .containerBuilder(fakeCont);
123
124         for (final LeafSchemaNode leaf : fakeRpcSchema) {
125             containerBuilder.withChild(Builders.leafBuilder(leaf).build());
126         }
127
128         final Collection<Module> fakeModules = new ArrayList<>(neededModules.size() + 1);
129         neededModules.forEach(imp -> fakeModules.add(new FakeImportedModule(imp)));
130         fakeModules.add(new FakeRestconfModule(neededModules, fakeCont));
131
132         final SchemaContext fakeSchemaCtx = EffectiveSchemaContext.resolveSchemaContext(
133             ImmutableSet.copyOf(fakeModules));
134         final InstanceIdentifierContext<ContainerSchemaNode> instanceIdentifierContext = new InstanceIdentifierContext<>(
135                 null, fakeCont, mountPoint, fakeSchemaCtx);
136         return new NormalizedNodeContext(instanceIdentifierContext, containerBuilder.build());
137     }
138
139 }