blob: a4b2ca6401f0f1809e8ab3b5762f46d314d34bc0 [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
26import java.util.Optional;
27import java.util.Set;
28
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
49 return new DefaultApplication(ApplicationIdProtoTranslator.translate(app.getAppId()),
50 VersionProtoTranslator.translate(app.getVersion()), app.getTitle(),
51 app.getDescription(), app.getOrigin(), app.getCategory(), app.getUrl(),
52 app.getReadme(), app.toByteArray(),
53 (ApplicationRole) ApplicationEnumsProtoTranslator.translate(app.getRole()).get(),
54 permissions, Optional.empty(), app.getFeaturesList(), app.getRequiredAppsList());
55 }
56
57 /**
58 * Translates {@link Application} to gRPC Application message.
59 *
60 * @param application {@link Application}
61 * @return gRPC message
62 */
63 public static ApplicationProto translate(Application application) {
64
65 if (application != null) {
66 return ApplicationProto.newBuilder()
67 .setAppId(ApplicationIdProtoTranslator.translate(application.id()))
68 .setCategory(application.category())
69 .setDescription(application.description())
70 .setOrigin(application.origin())
71 .setReadme(application.readme())
72 .setTitle(application.title())
73 .setUrl(application.url())
74 .setVersion(VersionProtoTranslator.translate(application.version()))
75 .setRole(ApplicationEnumsProtoTranslator.translate(application.role()))
76 .build();
77 }
78
79 return getDefaultInstance();
80 }
81
82 // Utility class not intended for instantiation.
83 private ApplicationProtoTranslator() {}
84}