blob: f426982b8c7cccef48d179f64ea3097f76d8acdb [file] [log] [blame]
shivani vaidya530917c2017-07-11 11:27:48 -07001/*
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.net;
Jian Li21fa4fe2017-11-21 11:23:19 +090017
shivani vaidya530917c2017-07-11 11:27:48 -070018import com.google.common.base.Strings;
adibrastegarniac02358a2018-11-06 18:02:00 -050019import org.onosproject.incubator.protobuf.models.net.region.RegionEnumsProtoTranslator;
shivani vaidya530917c2017-07-11 11:27:48 -070020import org.onosproject.cluster.NodeId;
21import org.onosproject.grpc.net.models.RegionProtoOuterClass;
22import org.onosproject.net.Annotations;
23import org.onosproject.net.region.DefaultRegion;
24import org.onosproject.net.region.Region;
25import org.onosproject.net.region.RegionId;
26
27import java.util.ArrayList;
28import java.util.HashSet;
29import java.util.List;
30import java.util.Set;
31import java.util.stream.Collectors;
32
33/**
34 * gRPC Region message to @link org.onosproject.net.region.Region conversion related utilities for region service.
35 */
36public final class RegionProtoTranslator {
37
38 /**
39 * Translates gRPC RegionProto message to {@link org.onosproject.net.region.Region}.
40 *
41 * @param region gRPC message
42 * @return {@link org.onosproject.net.region.Region}
43 */
44 public static Region translate(RegionProtoOuterClass.RegionProto region) {
45 RegionId id = RegionId.regionId(region.getRegionId());
46 Region.Type type = RegionEnumsProtoTranslator.translate(region.getType()).get();
47 String name = Strings.nullToEmpty(region.getName());
48
49 List<Set<NodeId>> masters = new ArrayList<>();
50
51 region.getMastersList().forEach(s -> {
52 Set<NodeId> nodeIdSet = new HashSet<NodeId>();
53 s.getNodeIdList().forEach(n -> {
54 nodeIdSet.add(new NodeId(n));
55 });
56 masters.add(nodeIdSet);
57 });
58
Jian Li21fa4fe2017-11-21 11:23:19 +090059 Annotations annots = AnnotationsTranslator.asAnnotations(region.getAnnotations());
shivani vaidya530917c2017-07-11 11:27:48 -070060
61 return new DefaultRegion(id, name, type, annots, masters);
62 }
63
64 /**
65 * Translates {@link org.onosproject.net.region.Region} to gRPC RegionProto message.
66 *
67 * @param region {@link org.onosproject.net.region.Region}
68 * @return gRPC RegionProto message
69 */
70 public static RegionProtoOuterClass.RegionProto translate(Region region) {
71 return RegionProtoOuterClass.RegionProto.newBuilder()
72 .setRegionId(region.id().toString())
73 .setType(RegionEnumsProtoTranslator.translate(region.type()))
74 .setName(region.name().isEmpty() ? null : region.name())
75 .addAllMasters(region.masters()
76 .stream()
77 .map(s -> RegionProtoOuterClass.RegionProto.NodeIdSet
78 .newBuilder()
Jian Li1aaa64d2017-12-14 11:25:39 +090079 .addAllNodeId(s.stream().map(id ->
80 id.toString()).collect(Collectors.toList()))
shivani vaidya530917c2017-07-11 11:27:48 -070081 .build())
82 .collect(Collectors.toList()))
83 .build();
84 }
85
shivani vaidya530917c2017-07-11 11:27:48 -070086 // Utility class not intended for instantiation.
87 private RegionProtoTranslator() {}
88
89}
90