Bug 5679 - implement ietf-restconf-monitoring - streams
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / restconf / base / 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.base.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.base.services.api.RestconfOperationsService;
24 import org.opendaylight.restconf.common.references.SchemaContextRef;
25 import org.opendaylight.restconf.handlers.DOMMountPointServiceHandler;
26 import org.opendaylight.restconf.handlers.SchemaContextHandler;
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(identifier,
80                     ref.get(), Optional.of(this.domMountPointServiceHandler.get()));
81             mountPoint = mountPointIdentifier.getMountPoint();
82             modules = ref.getModules(mountPoint);
83
84         } else {
85             final String errMsg =
86                     "URI has bad format. If operations behind mount point should be showed, URI has to end with ";
87             LOG.debug(errMsg + ControllerContext.MOUNT + " for " + identifier);
88             throw new RestconfDocumentedException(errMsg + ControllerContext.MOUNT, ErrorType.PROTOCOL,
89                     ErrorTag.INVALID_VALUE);
90         }
91
92         return getOperations(modules, mountPoint);
93     }
94
95     /**
96      * Special case only for GET restconf/operations use (since moment of old
97      * Yang parser and old Yang model API removal). The method is creating fake
98      * schema context with fake module and fake data by use own implementations
99      * of schema nodes and module.
100      *
101      * @param modules
102      *            - set of modules for get RPCs from every module
103      * @param mountPoint
104      *            - mount point, if in use otherwise null
105      * @return {@link NormalizedNodeContext}
106      */
107     private static NormalizedNodeContext getOperations(final Set<Module> modules, final DOMMountPoint mountPoint) {
108         final Collection<Module> neededModules = new ArrayList<>(modules.size());
109         final ArrayList<LeafSchemaNode> fakeRpcSchema = new ArrayList<>();
110
111         for (final Module m : modules) {
112             final Set<RpcDefinition> rpcs = m.getRpcs();
113             if (!rpcs.isEmpty()) {
114                 neededModules.add(m);
115
116                 fakeRpcSchema.ensureCapacity(fakeRpcSchema.size() + rpcs.size());
117                 rpcs.forEach(rpc -> fakeRpcSchema.add(new FakeLeafSchemaNode(rpc.getQName())));
118             }
119         }
120
121         final ContainerSchemaNode fakeCont = new FakeContainerSchemaNode(fakeRpcSchema);
122         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> containerBuilder =
123                 Builders.containerBuilder(fakeCont);
124
125         for (final LeafSchemaNode leaf : fakeRpcSchema) {
126             containerBuilder.withChild(Builders.leafBuilder(leaf).build());
127         }
128
129         final Collection<Module> fakeModules = new ArrayList<>(neededModules.size() + 1);
130         neededModules.forEach(imp -> fakeModules.add(new FakeImportedModule(imp)));
131         fakeModules.add(new FakeRestconfModule(neededModules, fakeCont));
132
133         final SchemaContext fakeSchemaCtx =
134                 EffectiveSchemaContext.resolveSchemaContext(ImmutableSet.copyOf(fakeModules));
135         final InstanceIdentifierContext<ContainerSchemaNode> instanceIdentifierContext =
136                 new InstanceIdentifierContext<>(null, fakeCont, mountPoint, fakeSchemaCtx);
137         return new NormalizedNodeContext(instanceIdentifierContext, containerBuilder.build());
138     }
139
140 }