blob: 3c6f37c1021750277b82e0998ca3be897ead9d2d [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
18import com.fasterxml.jackson.databind.node.ObjectNode;
Jian Li6282c802016-01-19 19:38:40 -080019import org.apache.commons.lang3.StringEscapeUtils;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080020import org.onosproject.app.ApplicationService;
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.core.Application;
24
25import static com.google.common.base.Preconditions.checkNotNull;
26
27/**
28 * Application JSON codec.
29 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080030public final class ApplicationCodec extends JsonCodec<Application> {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080031
32 @Override
33 public ObjectNode encode(Application app, CodecContext context) {
34 checkNotNull(app, "Application cannot be null");
Ray Milkey3078fc02015-05-06 16:14:14 -070035 ApplicationService service = context.getService(ApplicationService.class);
Thomas Vachuska761f0042015-11-11 19:10:17 -080036 return context.mapper().createObjectNode()
Thomas Vachuska02aeb032015-01-06 22:36:30 -080037 .put("name", app.id().name())
38 .put("id", app.id().id())
39 .put("version", app.version().toString())
Jian Li6282c802016-01-19 19:38:40 -080040 .put("category", app.category())
41 .put("description", StringEscapeUtils.escapeJson(app.description()))
42 .put("readme", StringEscapeUtils.escapeJson(app.readme()))
Thomas Vachuska02aeb032015-01-06 22:36:30 -080043 .put("origin", app.origin())
Jian Li6282c802016-01-19 19:38:40 -080044 .put("url", app.url())
Thomas Vachuska761f0042015-11-11 19:10:17 -080045 .put("permissions", app.permissions().toString()) // FIXME: change to an array
Thomas Vachuska02aeb032015-01-06 22:36:30 -080046 .put("featuresRepo", app.featuresRepo().isPresent() ?
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080047 app.featuresRepo().get().toString() : "")
Thomas Vachuska761f0042015-11-11 19:10:17 -080048 .put("features", app.features().toString()) // FIXME: change to an array
49 .put("requiredApps", app.requiredApps().toString()) // FIXME: change to an array
Thomas Vachuska02aeb032015-01-06 22:36:30 -080050 .put("state", service.getState(app.id()).toString());
Thomas Vachuska02aeb032015-01-06 22:36:30 -080051 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -080052}