blob: 4886e604c53904bc7780fdac2abc2cb4da954e97 [file] [log] [blame]
Thomas Vachuska02aeb032015-01-06 22:36:30 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.codec.impl;
17
Jian Lic67ca3c2016-01-21 13:42:19 -080018import com.fasterxml.jackson.databind.node.ArrayNode;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080019import com.fasterxml.jackson.databind.node.ObjectNode;
Jian Li6282c802016-01-19 19:38:40 -080020import org.apache.commons.lang3.StringEscapeUtils;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080021import org.onosproject.app.ApplicationService;
22import org.onosproject.codec.CodecContext;
23import org.onosproject.codec.JsonCodec;
24import org.onosproject.core.Application;
25
26import static com.google.common.base.Preconditions.checkNotNull;
27
28/**
29 * Application JSON codec.
30 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080031public final class ApplicationCodec extends JsonCodec<Application> {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080032
33 @Override
34 public ObjectNode encode(Application app, CodecContext context) {
35 checkNotNull(app, "Application cannot be null");
Ray Milkey3078fc02015-05-06 16:14:14 -070036 ApplicationService service = context.getService(ApplicationService.class);
Jian Lic67ca3c2016-01-21 13:42:19 -080037
38 ArrayNode permissions = context.mapper().createArrayNode();
39 ArrayNode features = context.mapper().createArrayNode();
40 ArrayNode requiredApps = context.mapper().createArrayNode();
41
42 app.permissions().forEach(p -> permissions.add(p.toString()));
43 app.features().forEach(f -> features.add(f));
44 app.requiredApps().forEach(a -> requiredApps.add(a));
45
46 ObjectNode result = context.mapper().createObjectNode()
Thomas Vachuska02aeb032015-01-06 22:36:30 -080047 .put("name", app.id().name())
48 .put("id", app.id().id())
49 .put("version", app.version().toString())
Jian Li6282c802016-01-19 19:38:40 -080050 .put("category", app.category())
51 .put("description", StringEscapeUtils.escapeJson(app.description()))
52 .put("readme", StringEscapeUtils.escapeJson(app.readme()))
Thomas Vachuska02aeb032015-01-06 22:36:30 -080053 .put("origin", app.origin())
Jian Li6282c802016-01-19 19:38:40 -080054 .put("url", app.url())
Thomas Vachuska02aeb032015-01-06 22:36:30 -080055 .put("featuresRepo", app.featuresRepo().isPresent() ?
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080056 app.featuresRepo().get().toString() : "")
Thomas Vachuska02aeb032015-01-06 22:36:30 -080057 .put("state", service.getState(app.id()).toString());
Jian Lic67ca3c2016-01-21 13:42:19 -080058
59 result.set("features", features);
60 result.set("permissions", permissions);
61 result.set("requiredApps", requiredApps);
62
63 return result;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080064 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -080065}