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