Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.codec.impl; |
| 17 | |
Jian Li | c67ca3c | 2016-01-21 13:42:19 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Jian Li | 6282c80 | 2016-01-19 19:38:40 -0800 | [diff] [blame] | 20 | import org.apache.commons.lang3.StringEscapeUtils; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 21 | import org.onosproject.app.ApplicationService; |
| 22 | import org.onosproject.codec.CodecContext; |
| 23 | import org.onosproject.codec.JsonCodec; |
| 24 | import org.onosproject.core.Application; |
| 25 | |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | |
| 28 | /** |
| 29 | * Application JSON codec. |
| 30 | */ |
Ray Milkey | 540b2ce | 2015-02-04 17:50:20 -0800 | [diff] [blame] | 31 | public final class ApplicationCodec extends JsonCodec<Application> { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 32 | |
| 33 | @Override |
| 34 | public ObjectNode encode(Application app, CodecContext context) { |
| 35 | checkNotNull(app, "Application cannot be null"); |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 36 | ApplicationService service = context.getService(ApplicationService.class); |
Jian Li | c67ca3c | 2016-01-21 13:42:19 -0800 | [diff] [blame] | 37 | |
| 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 Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 47 | .put("name", app.id().name()) |
| 48 | .put("id", app.id().id()) |
| 49 | .put("version", app.version().toString()) |
Jian Li | 6282c80 | 2016-01-19 19:38:40 -0800 | [diff] [blame] | 50 | .put("category", app.category()) |
| 51 | .put("description", StringEscapeUtils.escapeJson(app.description())) |
| 52 | .put("readme", StringEscapeUtils.escapeJson(app.readme())) |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 53 | .put("origin", app.origin()) |
Jian Li | 6282c80 | 2016-01-19 19:38:40 -0800 | [diff] [blame] | 54 | .put("url", app.url()) |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 55 | .put("featuresRepo", app.featuresRepo().isPresent() ? |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 56 | app.featuresRepo().get().toString() : "") |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 57 | .put("state", service.getState(app.id()).toString()); |
Jian Li | c67ca3c | 2016-01-21 13:42:19 -0800 | [diff] [blame] | 58 | |
| 59 | result.set("features", features); |
| 60 | result.set("permissions", permissions); |
| 61 | result.set("requiredApps", requiredApps); |
| 62 | |
| 63 | return result; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 64 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 65 | } |