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