shivani vaidya | 530917c | 2017-07-11 11:27:48 -0700 | [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.net; |
Jian Li | 21fa4fe | 2017-11-21 11:23:19 +0900 | [diff] [blame] | 17 | |
shivani vaidya | 530917c | 2017-07-11 11:27:48 -0700 | [diff] [blame] | 18 | import com.google.common.base.Strings; |
adibrastegarnia | c02358a | 2018-11-06 18:02:00 -0500 | [diff] [blame] | 19 | import org.onosproject.incubator.protobuf.models.net.region.RegionEnumsProtoTranslator; |
shivani vaidya | 530917c | 2017-07-11 11:27:48 -0700 | [diff] [blame] | 20 | import org.onosproject.cluster.NodeId; |
| 21 | import org.onosproject.grpc.net.models.RegionProtoOuterClass; |
| 22 | import org.onosproject.net.Annotations; |
| 23 | import org.onosproject.net.region.DefaultRegion; |
| 24 | import org.onosproject.net.region.Region; |
| 25 | import org.onosproject.net.region.RegionId; |
| 26 | |
| 27 | import java.util.ArrayList; |
| 28 | import java.util.HashSet; |
| 29 | import java.util.List; |
| 30 | import java.util.Set; |
| 31 | import java.util.stream.Collectors; |
| 32 | |
| 33 | /** |
| 34 | * gRPC Region message to @link org.onosproject.net.region.Region conversion related utilities for region service. |
| 35 | */ |
| 36 | public 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 Li | 21fa4fe | 2017-11-21 11:23:19 +0900 | [diff] [blame] | 59 | Annotations annots = AnnotationsTranslator.asAnnotations(region.getAnnotations()); |
shivani vaidya | 530917c | 2017-07-11 11:27:48 -0700 | [diff] [blame] | 60 | |
| 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 Li | 1aaa64d | 2017-12-14 11:25:39 +0900 | [diff] [blame] | 79 | .addAllNodeId(s.stream().map(id -> |
| 80 | id.toString()).collect(Collectors.toList())) |
shivani vaidya | 530917c | 2017-07-11 11:27:48 -0700 | [diff] [blame] | 81 | .build()) |
| 82 | .collect(Collectors.toList())) |
| 83 | .build(); |
| 84 | } |
| 85 | |
shivani vaidya | 530917c | 2017-07-11 11:27:48 -0700 | [diff] [blame] | 86 | // Utility class not intended for instantiation. |
| 87 | private RegionProtoTranslator() {} |
| 88 | |
| 89 | } |
| 90 | |