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