blob: ea423ef2003363d6ad2c9430b29e598c5b50a7f7 [file] [log] [blame]
Thomas Vachuska02aeb032015-01-06 22:36:30 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska02aeb032015-01-06 22:36:30 -08003 *
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
Sho SHIMIZUef7e2902016-02-12 18:38:29 -080026import java.net.URI;
27
Thomas Vachuska02aeb032015-01-06 22:36:30 -080028import static com.google.common.base.Preconditions.checkNotNull;
29
30/**
31 * Application JSON codec.
32 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080033public final class ApplicationCodec extends JsonCodec<Application> {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080034
35 @Override
36 public ObjectNode encode(Application app, CodecContext context) {
37 checkNotNull(app, "Application cannot be null");
Ray Milkey3078fc02015-05-06 16:14:14 -070038 ApplicationService service = context.getService(ApplicationService.class);
Jian Lic67ca3c2016-01-21 13:42:19 -080039
40 ArrayNode permissions = context.mapper().createArrayNode();
41 ArrayNode features = context.mapper().createArrayNode();
42 ArrayNode requiredApps = context.mapper().createArrayNode();
43
44 app.permissions().forEach(p -> permissions.add(p.toString()));
45 app.features().forEach(f -> features.add(f));
46 app.requiredApps().forEach(a -> requiredApps.add(a));
47
48 ObjectNode result = context.mapper().createObjectNode()
Thomas Vachuska02aeb032015-01-06 22:36:30 -080049 .put("name", app.id().name())
50 .put("id", app.id().id())
51 .put("version", app.version().toString())
Jian Li6282c802016-01-19 19:38:40 -080052 .put("category", app.category())
53 .put("description", StringEscapeUtils.escapeJson(app.description()))
54 .put("readme", StringEscapeUtils.escapeJson(app.readme()))
Thomas Vachuska02aeb032015-01-06 22:36:30 -080055 .put("origin", app.origin())
Jian Li6282c802016-01-19 19:38:40 -080056 .put("url", app.url())
Sho SHIMIZUef7e2902016-02-12 18:38:29 -080057 .put("featuresRepo", app.featuresRepo().map(URI::toString).orElse(""))
Thomas Vachuska02aeb032015-01-06 22:36:30 -080058 .put("state", service.getState(app.id()).toString());
Jian Lic67ca3c2016-01-21 13:42:19 -080059
60 result.set("features", features);
61 result.set("permissions", permissions);
62 result.set("requiredApps", requiredApps);
63
64 return result;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080065 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -080066}