blob: 25a95e4e11ab9ebccc981878203e8635bb5a81cd [file] [log] [blame]
Jian Li8780edc2017-08-26 02:44:29 +09001/*
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 */
16package org.onosproject.incubator.protobuf.models.core;
17
18import com.google.common.collect.Sets;
19import org.onosproject.core.Application;
Jian Li8780edc2017-08-26 02:44:29 +090020import org.onosproject.core.DefaultApplication;
21import org.onosproject.grpc.core.models.ApplicationProtoOuterClass.ApplicationProto;
22import org.onosproject.incubator.protobuf.models.security.PermissionProtoTranslator;
23import org.onosproject.security.Permission;
24
Jian Li3168e8b2017-10-18 02:54:14 +090025import java.util.Optional;
Jian Li8780edc2017-08-26 02:44:29 +090026import java.util.Set;
Jian Li3168e8b2017-10-18 02:54:14 +090027import java.util.stream.Collectors;
Jian Li8780edc2017-08-26 02:44:29 +090028
29import static org.onosproject.grpc.core.models.ApplicationProtoOuterClass.ApplicationProto.getDefaultInstance;
30
31/**
32 * gRPC ApplicationProto message to equivalent ONOS Application conversion related utilities.
33 */
34public 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 Milkey47c95412017-09-15 10:40:48 -070049 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 Li1aaa64d2017-12-14 11:25:39 +090059 .withRole(ApplicationEnumsProtoTranslator.translate(app.getRole()).get())
Ray Milkey47c95412017-09-15 10:40:48 -070060 .withPermissions(permissions)
61 .withFeatures(app.getFeaturesList())
Jian Li3168e8b2017-10-18 02:54:14 +090062 .withFeaturesRepo(Optional.empty()) // TODO: need to add features repo
Ray Milkey47c95412017-09-15 10:40:48 -070063 .withRequiredApps(app.getRequiredAppsList())
64 .build();
Jian Li8780edc2017-08-26 02:44:29 +090065 }
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 Li3168e8b2017-10-18 02:54:14 +090086 .addAllFeatures(application.features())
87 .addAllPermissions(application.permissions().stream().map(p ->
88 PermissionProtoTranslator.translate(p)).collect(Collectors.toList()))
89 .addAllRequiredApps(application.requiredApps())
Jian Li8780edc2017-08-26 02:44:29 +090090 .build();
91 }
92
93 return getDefaultInstance();
94 }
95
96 // Utility class not intended for instantiation.
97 private ApplicationProtoTranslator() {}
98}