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