e20e8a79e100a12752009bcff2f03fa4c5c0680b
[controller.git] / opendaylight / md-sal / sal-rest-docgen / src / main / java / org / opendaylight / controller / sal / rest / doc / mountpoints / MountPointSwagger.java
1 /*
2  * Copyright (c) 2014 Brocade Communications 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.controller.sal.rest.doc.mountpoints;
9
10 import com.google.common.base.Optional;
11 import java.util.Collections;
12 import java.util.Comparator;
13 import java.util.HashMap;
14 import java.util.LinkedList;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import java.util.TreeMap;
19 import java.util.concurrent.atomic.AtomicLong;
20 import java.util.concurrent.atomic.AtomicReference;
21 import javax.ws.rs.core.UriInfo;
22 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
23 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
24 import org.opendaylight.controller.sal.core.api.model.SchemaService;
25 import org.opendaylight.controller.sal.core.api.mount.MountProvisionListener;
26 import org.opendaylight.controller.sal.rest.doc.impl.BaseYangSwaggerGenerator;
27 import org.opendaylight.controller.sal.rest.doc.swagger.Api;
28 import org.opendaylight.controller.sal.rest.doc.swagger.ApiDeclaration;
29 import org.opendaylight.controller.sal.rest.doc.swagger.Operation;
30 import org.opendaylight.controller.sal.rest.doc.swagger.Resource;
31 import org.opendaylight.controller.sal.rest.doc.swagger.ResourceList;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
36 import org.opendaylight.yangtools.yang.model.api.Module;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38
39 public class MountPointSwagger extends BaseYangSwaggerGenerator implements MountProvisionListener {
40
41     private static final String DATASTORES_REVISION = "-";
42     private static final String DATASTORES_LABEL = "Datastores";
43
44     private DOMMountPointService mountService;
45     private final Map<YangInstanceIdentifier, Long> instanceIdToLongId = new TreeMap<>(
46             new Comparator<YangInstanceIdentifier>() {
47                 @Override
48                 public int compare(final YangInstanceIdentifier o1, final YangInstanceIdentifier o2) {
49                     return o1.toString().compareToIgnoreCase(o2.toString());
50                 }
51             });
52     private final Map<Long, YangInstanceIdentifier> longIdToInstanceId = new HashMap<>();
53     private final Object lock = new Object();
54
55     private final AtomicLong idKey = new AtomicLong(0);
56
57     private static AtomicReference<MountPointSwagger> selfRef = new AtomicReference<>();
58     private SchemaService globalSchema;
59
60     public Map<String, Long> getInstanceIdentifiers() {
61         Map<String, Long> urlToId = new HashMap<>();
62         synchronized (lock) {
63             SchemaContext context = globalSchema.getGlobalContext();
64             for (Entry<YangInstanceIdentifier, Long> entry : instanceIdToLongId.entrySet()) {
65                 String modName = findModuleName(entry.getKey(), context);
66                 urlToId.put(generateUrlPrefixFromInstanceID(entry.getKey(), modName),
67                         entry.getValue());
68             }
69         }
70         return urlToId;
71     }
72
73     public void setGlobalSchema(final SchemaService globalSchema) {
74         this.globalSchema = globalSchema;
75     }
76
77     private String findModuleName(final YangInstanceIdentifier id, final SchemaContext context) {
78         PathArgument rootQName = id.getPathArguments().iterator().next();
79         for (Module mod : context.getModules()) {
80             if (mod.getDataChildByName(rootQName.getNodeType()) != null) {
81                 return mod.getName();
82             }
83         }
84         return null;
85     }
86
87     private String generateUrlPrefixFromInstanceID(final YangInstanceIdentifier key, final String moduleName) {
88         StringBuilder builder = new StringBuilder();
89         if (moduleName != null) {
90             builder.append(moduleName);
91             builder.append(':');
92         }
93         for (PathArgument arg : key.getPathArguments()) {
94             String name = arg.getNodeType().getLocalName();
95             if (arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
96                 NodeIdentifierWithPredicates nodeId = (NodeIdentifierWithPredicates) arg;
97                 for (Entry<QName, Object> entry : nodeId.getKeyValues().entrySet()) {
98                     builder.append(entry.getValue()).append('/');
99                 }
100             } else {
101                 builder.append(name);
102                 builder.append('/');
103             }
104         }
105         return builder.toString();
106     }
107
108     private String getYangMountUrl(final YangInstanceIdentifier key) {
109         String modName = findModuleName(key, globalSchema.getGlobalContext());
110         return generateUrlPrefixFromInstanceID(key, modName) + "yang-ext:mount/";
111     }
112
113     public ResourceList getResourceList(final UriInfo uriInfo, final Long id) {
114         YangInstanceIdentifier iid = getInstanceId(id);
115         if (iid == null) {
116             return null; // indicating not found.
117         }
118         SchemaContext context = getSchemaContext(iid);
119         String urlPrefix = getYangMountUrl(iid);
120         if (context == null) {
121             return createResourceList();
122         }
123         List<Resource> resources = new LinkedList<>();
124         Resource dataStores = new Resource();
125         dataStores.setDescription("Provides methods for accessing the data stores.");
126         dataStores.setPath(generatePath(uriInfo, DATASTORES_LABEL, DATASTORES_REVISION));
127         resources.add(dataStores);
128         ResourceList list = super.getResourceListing(uriInfo, context, urlPrefix);
129         resources.addAll(list.getApis());
130         list.setApis(resources);
131         return list;
132     }
133
134     private YangInstanceIdentifier getInstanceId(final Long id) {
135         YangInstanceIdentifier instanceId;
136         synchronized (lock) {
137             instanceId = longIdToInstanceId.get(id);
138         }
139         return instanceId;
140     }
141
142     private SchemaContext getSchemaContext(final YangInstanceIdentifier id) {
143
144         if (id == null) {
145             return null;
146         }
147
148         Optional<DOMMountPoint> mountPoint = mountService.getMountPoint(id);
149         if (!mountPoint.isPresent()) {
150             return null;
151         }
152
153         SchemaContext context = mountPoint.get().getSchemaContext();
154         if (context == null) {
155             return null;
156         }
157         return context;
158     }
159
160     public ApiDeclaration getMountPointApi(final UriInfo uriInfo, final Long id, final String module, final String revision) {
161         YangInstanceIdentifier iid = getInstanceId(id);
162         SchemaContext context = getSchemaContext(iid);
163         String urlPrefix = getYangMountUrl(iid);
164         if (context == null) {
165             return null;
166         }
167
168         if (DATASTORES_LABEL.equals(module) && DATASTORES_REVISION.equals(revision)) {
169             return generateDataStoreApiDoc(uriInfo, urlPrefix);
170         }
171         return super.getApiDeclaration(module, revision, uriInfo, context, urlPrefix);
172     }
173
174     private ApiDeclaration generateDataStoreApiDoc(final UriInfo uriInfo, final String context) {
175
176         ApiDeclaration declaration = super.createApiDeclaration(createBasePathFromUriInfo(uriInfo));
177         List<Api> apis = new LinkedList<>();
178         apis.add(createGetApi("config",
179                 "Queries the config (startup) datastore on the mounted hosted.", context));
180         apis.add(createGetApi("operational",
181                 "Queries the operational (running) datastore on the mounted hosted.", context));
182         apis.add(createGetApi("operations",
183                 "Queries the available operations (RPC calls) on the mounted hosted.", context));
184         declaration.setApis(apis);
185         return declaration;
186
187     }
188
189     private Api createGetApi(final String datastore, final String note, final String context) {
190         Operation getConfig = new Operation();
191         getConfig.setMethod("GET");
192         getConfig.setNickname("GET " + datastore);
193         getConfig.setNotes(note);
194
195         Api api = new Api();
196         api.setPath(getDataStorePath("/" + datastore + "/", context));
197         api.setOperations(Collections.singletonList(getConfig));
198
199         return api;
200     }
201
202     public void setMountService(final DOMMountPointService mountService) {
203         this.mountService = mountService;
204     }
205
206     @Override
207     public void onMountPointCreated(final YangInstanceIdentifier path) {
208         synchronized (lock) {
209             Long idLong = idKey.incrementAndGet();
210             instanceIdToLongId.put(path, idLong);
211             longIdToInstanceId.put(idLong, path);
212         }
213     }
214
215     @Override
216     public void onMountPointRemoved(final YangInstanceIdentifier path) {
217         synchronized (lock) {
218             Long id = instanceIdToLongId.remove(path);
219             longIdToInstanceId.remove(id);
220         }
221     }
222
223     public static MountPointSwagger getInstance() {
224         MountPointSwagger swagger = selfRef.get();
225         if (swagger == null) {
226             selfRef.compareAndSet(null, new MountPointSwagger());
227             swagger = selfRef.get();
228         }
229         return swagger;
230     }
231
232 }