Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 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 | |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 26 | import java.net.URI; |
| 27 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 28 | import static com.google.common.base.Preconditions.checkNotNull; |
| 29 | |
| 30 | /** |
| 31 | * Application JSON codec. |
| 32 | */ |
Ray Milkey | 540b2ce | 2015-02-04 17:50:20 -0800 | [diff] [blame] | 33 | public final class ApplicationCodec extends JsonCodec<Application> { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 34 | |
| 35 | @Override |
| 36 | public ObjectNode encode(Application app, CodecContext context) { |
| 37 | checkNotNull(app, "Application cannot be null"); |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 38 | ApplicationService service = context.getService(ApplicationService.class); |
Jian Li | c67ca3c | 2016-01-21 13:42:19 -0800 | [diff] [blame] | 39 | |
| 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 Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 49 | .put("name", app.id().name()) |
| 50 | .put("id", app.id().id()) |
| 51 | .put("version", app.version().toString()) |
Jian Li | 6282c80 | 2016-01-19 19:38:40 -0800 | [diff] [blame] | 52 | .put("category", app.category()) |
| 53 | .put("description", StringEscapeUtils.escapeJson(app.description())) |
| 54 | .put("readme", StringEscapeUtils.escapeJson(app.readme())) |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 55 | .put("origin", app.origin()) |
Jian Li | 6282c80 | 2016-01-19 19:38:40 -0800 | [diff] [blame] | 56 | .put("url", app.url()) |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 57 | .put("featuresRepo", app.featuresRepo().map(URI::toString).orElse("")) |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 58 | .put("state", service.getState(app.id()).toString()); |
Jian Li | c67ca3c | 2016-01-21 13:42:19 -0800 | [diff] [blame] | 59 | |
| 60 | result.set("features", features); |
| 61 | result.set("permissions", permissions); |
| 62 | result.set("requiredApps", requiredApps); |
| 63 | |
| 64 | return result; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 65 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 66 | } |