Jian Li | 8780edc | 2017-08-26 02:44:29 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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.incubator.protobuf.models.core; |
| 17 | |
| 18 | import com.google.common.collect.Sets; |
| 19 | import org.onosproject.core.Application; |
Jian Li | 8780edc | 2017-08-26 02:44:29 +0900 | [diff] [blame] | 20 | import org.onosproject.core.DefaultApplication; |
| 21 | import org.onosproject.grpc.core.models.ApplicationProtoOuterClass.ApplicationProto; |
| 22 | import org.onosproject.incubator.protobuf.models.security.PermissionProtoTranslator; |
| 23 | import org.onosproject.security.Permission; |
| 24 | |
Jian Li | 3168e8b | 2017-10-18 02:54:14 +0900 | [diff] [blame] | 25 | import java.util.Optional; |
Jian Li | 8780edc | 2017-08-26 02:44:29 +0900 | [diff] [blame] | 26 | import java.util.Set; |
Jian Li | 3168e8b | 2017-10-18 02:54:14 +0900 | [diff] [blame] | 27 | import java.util.stream.Collectors; |
Jian Li | 8780edc | 2017-08-26 02:44:29 +0900 | [diff] [blame] | 28 | |
| 29 | import static org.onosproject.grpc.core.models.ApplicationProtoOuterClass.ApplicationProto.getDefaultInstance; |
| 30 | |
| 31 | /** |
| 32 | * gRPC ApplicationProto message to equivalent ONOS Application conversion related utilities. |
| 33 | */ |
| 34 | public final class ApplicationProtoTranslator { |
| 35 | |
| 36 | /** |
| 37 | * Translates gRPC Application to {@link Application}. |
| 38 | * |
| 39 | * @param app gRPC message |
| 40 | * @return {@link Application} |
| 41 | */ |
| 42 | public static Application translate(ApplicationProto app) { |
| 43 | |
| 44 | Set<Permission> permissions = Sets.newHashSet(); |
| 45 | |
| 46 | app.getPermissionsList().forEach(p -> |
| 47 | permissions.add(PermissionProtoTranslator.translate(p))); |
| 48 | |
Ray Milkey | 47c9541 | 2017-09-15 10:40:48 -0700 | [diff] [blame] | 49 | return DefaultApplication.builder() |
| 50 | .withAppId(ApplicationIdProtoTranslator.translate(app.getAppId())) |
| 51 | .withVersion(VersionProtoTranslator.translate(app.getVersion())) |
| 52 | .withTitle(app.getTitle()) |
| 53 | .withDescription(app.getDescription()) |
| 54 | .withOrigin(app.getOrigin()) |
| 55 | .withCategory(app.getCategory()) |
| 56 | .withUrl(app.getUrl()) |
| 57 | .withReadme(app.getReadme()) |
| 58 | .withIcon(app.toByteArray()) |
Jian Li | 1aaa64d | 2017-12-14 11:25:39 +0900 | [diff] [blame] | 59 | .withRole(ApplicationEnumsProtoTranslator.translate(app.getRole()).get()) |
Ray Milkey | 47c9541 | 2017-09-15 10:40:48 -0700 | [diff] [blame] | 60 | .withPermissions(permissions) |
| 61 | .withFeatures(app.getFeaturesList()) |
Jian Li | 3168e8b | 2017-10-18 02:54:14 +0900 | [diff] [blame] | 62 | .withFeaturesRepo(Optional.empty()) // TODO: need to add features repo |
Ray Milkey | 47c9541 | 2017-09-15 10:40:48 -0700 | [diff] [blame] | 63 | .withRequiredApps(app.getRequiredAppsList()) |
| 64 | .build(); |
Jian Li | 8780edc | 2017-08-26 02:44:29 +0900 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Translates {@link Application} to gRPC Application message. |
| 69 | * |
| 70 | * @param application {@link Application} |
| 71 | * @return gRPC message |
| 72 | */ |
| 73 | public static ApplicationProto translate(Application application) { |
| 74 | |
| 75 | if (application != null) { |
| 76 | return ApplicationProto.newBuilder() |
| 77 | .setAppId(ApplicationIdProtoTranslator.translate(application.id())) |
| 78 | .setCategory(application.category()) |
| 79 | .setDescription(application.description()) |
| 80 | .setOrigin(application.origin()) |
| 81 | .setReadme(application.readme()) |
| 82 | .setTitle(application.title()) |
| 83 | .setUrl(application.url()) |
| 84 | .setVersion(VersionProtoTranslator.translate(application.version())) |
| 85 | .setRole(ApplicationEnumsProtoTranslator.translate(application.role())) |
Jian Li | 3168e8b | 2017-10-18 02:54:14 +0900 | [diff] [blame] | 86 | .addAllFeatures(application.features()) |
| 87 | .addAllPermissions(application.permissions().stream().map(p -> |
| 88 | PermissionProtoTranslator.translate(p)).collect(Collectors.toList())) |
| 89 | .addAllRequiredApps(application.requiredApps()) |
Jian Li | 8780edc | 2017-08-26 02:44:29 +0900 | [diff] [blame] | 90 | .build(); |
| 91 | } |
| 92 | |
| 93 | return getDefaultInstance(); |
| 94 | } |
| 95 | |
| 96 | // Utility class not intended for instantiation. |
| 97 | private ApplicationProtoTranslator() {} |
| 98 | } |