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