93c99146c49bf1a43ef030ad5b7b8b01342677ea
[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 SchemaContextHandler schemaContextHandler;
50     private 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 synchronized void updateHandlers(final Object... handlers) {
68         for (final Object object : handlers) {
69             if (object instanceof SchemaContextHandler) {
70                 schemaContextHandler = (SchemaContextHandler) object;
71             } else if (object instanceof DOMMountPointServiceHandler) {
72                 domMountPointServiceHandler = (DOMMountPointServiceHandler) object;
73             }
74         }
75     }
76
77     @Override
78     public NormalizedNodeContext getOperations(final UriInfo uriInfo) {
79         final SchemaContextRef ref = new SchemaContextRef(this.schemaContextHandler.get());
80         return getOperations(ref.getModules(), null);
81     }
82
83     @Override
84     public NormalizedNodeContext getOperations(final String identifier, final UriInfo uriInfo) {
85         final Set<Module> modules;
86         final DOMMountPoint mountPoint;
87         final SchemaContextRef ref = new SchemaContextRef(this.schemaContextHandler.get());
88         if (identifier.contains(RestconfConstants.MOUNT)) {
89             final InstanceIdentifierContext<?> mountPointIdentifier = ParserIdentifier.toInstanceIdentifier(identifier,
90                     ref.get(), Optional.of(this.domMountPointServiceHandler.get()));
91             mountPoint = mountPointIdentifier.getMountPoint();
92             modules = ref.getModules(mountPoint);
93         } else {
94             final String errMsg =
95                     "URI has bad format. If operations behind mount point should be showed, URI has to end with ";
96             LOG.debug(errMsg + RestconfConstants.MOUNT + " for " + identifier);
97             throw new RestconfDocumentedException(errMsg + RestconfConstants.MOUNT, ErrorType.PROTOCOL,
98                     ErrorTag.INVALID_VALUE);
99         }
100
101         return getOperations(modules, mountPoint);
102     }
103
104     /**
105      * Special case only for GET restconf/operations use (since moment of old
106      * Yang parser and old Yang model API removal). The method is creating fake
107      * schema context with fake module and fake data by use own implementations
108      * of schema nodes and module.
109      *
110      * @param modules
111      *             set of modules for get RPCs from every module
112      * @param mountPoint
113      *             mount point, if in use otherwise null
114      * @return {@link NormalizedNodeContext}
115      */
116     private static NormalizedNodeContext getOperations(final Set<Module> modules, final DOMMountPoint mountPoint) {
117         final Collection<Module> neededModules = new ArrayList<>(modules.size());
118         final ArrayList<LeafSchemaNode> fakeRpcSchema = new ArrayList<>();
119
120         for (final Module m : modules) {
121             final Set<RpcDefinition> rpcs = m.getRpcs();
122             if (!rpcs.isEmpty()) {
123                 neededModules.add(m);
124
125                 fakeRpcSchema.ensureCapacity(fakeRpcSchema.size() + rpcs.size());
126                 rpcs.forEach(rpc -> fakeRpcSchema.add(new FakeLeafSchemaNode(rpc.getQName())));
127             }
128         }
129
130         final ContainerSchemaNode fakeCont = new FakeContainerSchemaNode(fakeRpcSchema);
131         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> containerBuilder =
132                 Builders.containerBuilder(fakeCont);
133
134         for (final LeafSchemaNode leaf : fakeRpcSchema) {
135             containerBuilder.withChild(Builders.leafBuilder(leaf).build());
136         }
137
138         final Collection<Module> fakeModules = new ArrayList<>(neededModules.size() + 1);
139         neededModules.forEach(imp -> fakeModules.add(new FakeImportedModule(imp)));
140         fakeModules.add(new FakeRestconfModule(neededModules, fakeCont));
141
142         final SchemaContext fakeSchemaCtx = SimpleSchemaContext.forModules(ImmutableSet.copyOf(fakeModules));
143         final InstanceIdentifierContext<ContainerSchemaNode> instanceIdentifierContext =
144                 new InstanceIdentifierContext<>(null, fakeCont, mountPoint, fakeSchemaCtx);
145         return new NormalizedNodeContext(instanceIdentifierContext, containerBuilder.build());
146     }
147
148 }