shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -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; |
| 17 | |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 18 | import org.onosproject.incubator.protobuf.models.net.link.LinkEnumsProtoTranslator; |
adibrastegarnia | c02358a | 2018-11-06 18:02:00 -0500 | [diff] [blame] | 19 | import org.onosproject.grpc.net.models.LinkProtoOuterClass; |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 20 | import org.onosproject.net.Annotations; |
| 21 | import org.onosproject.net.ConnectPoint; |
Jian Li | d8e7207 | 2017-11-21 10:50:02 +0900 | [diff] [blame] | 22 | import org.onosproject.net.DefaultAnnotations; |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 23 | import org.onosproject.net.DefaultLink; |
| 24 | import org.onosproject.net.Link; |
Jian Li | d8e7207 | 2017-11-21 10:50:02 +0900 | [diff] [blame] | 25 | import org.onosproject.net.SparseAnnotations; |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 26 | import org.onosproject.net.provider.ProviderId; |
| 27 | |
Jian Li | d40252c | 2017-12-08 04:13:20 +0900 | [diff] [blame] | 28 | import java.util.HashMap; |
Jian Li | d8e7207 | 2017-11-21 10:50:02 +0900 | [diff] [blame] | 29 | import java.util.Map; |
| 30 | |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 31 | /** |
| 32 | * gRPC LinkProto message to equivalent ONOS Link conversion related utilities. |
| 33 | */ |
| 34 | public final class LinkProtoTranslator { |
| 35 | |
| 36 | /** |
Jian Li | d40252c | 2017-12-08 04:13:20 +0900 | [diff] [blame] | 37 | * Translates gRPC LinkCore message to {@link Link}. |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 38 | * |
| 39 | * @param link gRPC message |
Jian Li | d40252c | 2017-12-08 04:13:20 +0900 | [diff] [blame] | 40 | * @return {@link Link} null if link is a default instance |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 41 | */ |
| 42 | public static Link translate(LinkProtoOuterClass.LinkProto link) { |
| 43 | if (link.equals(LinkProtoOuterClass.LinkProto.getDefaultInstance())) { |
| 44 | return null; |
| 45 | } |
| 46 | ProviderId providerId = ProviderIdProtoTranslator.translate(link.getProviderId()); |
| 47 | Link.State state = LinkEnumsProtoTranslator.translate(link.getState()).get(); |
| 48 | ConnectPoint src = ConnectPointProtoTranslator.translate(link.getSrc()).get(); |
| 49 | ConnectPoint dst = ConnectPointProtoTranslator.translate(link.getDst()).get(); |
| 50 | Link.Type type = LinkEnumsProtoTranslator.translate(link.getType()).get(); |
Jian Li | d8e7207 | 2017-11-21 10:50:02 +0900 | [diff] [blame] | 51 | Annotations annots = asAnnotations(link.getAnnotations()); |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 52 | Boolean isExpected = link.getIsExpected(); |
| 53 | return DefaultLink.builder().state(state) |
| 54 | .annotations(annots) |
| 55 | .providerId(providerId) |
| 56 | .src(src) |
| 57 | .dst(dst) |
| 58 | .type(type) |
| 59 | .isExpected(isExpected) |
| 60 | .build(); |
| 61 | } |
| 62 | |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 63 | /** |
Jian Li | d40252c | 2017-12-08 04:13:20 +0900 | [diff] [blame] | 64 | * Translates {@link Link} to gRPC LinkCore message. |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 65 | * |
Jian Li | d40252c | 2017-12-08 04:13:20 +0900 | [diff] [blame] | 66 | * @param link {@link Link} |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 67 | * @return gRPC LinkCore message |
| 68 | */ |
| 69 | public static LinkProtoOuterClass.LinkProto translate(Link link) { |
| 70 | if (link == null) { |
| 71 | return LinkProtoOuterClass.LinkProto.getDefaultInstance(); |
| 72 | } |
| 73 | return LinkProtoOuterClass.LinkProto.newBuilder() |
| 74 | .setProviderId(ProviderIdProtoTranslator.translate(link.providerId())) |
| 75 | .setState(LinkEnumsProtoTranslator.translate(link.state())) |
| 76 | .setSrc(ConnectPointProtoTranslator.translate(link.src())) |
| 77 | .setDst(ConnectPointProtoTranslator.translate(link.dst())) |
| 78 | .setType(LinkEnumsProtoTranslator.translate(link.type())) |
| 79 | .setIsExpected(link.isExpected()) |
| 80 | .build(); |
| 81 | } |
| 82 | |
Jian Li | d8e7207 | 2017-11-21 10:50:02 +0900 | [diff] [blame] | 83 | // may be this can be moved to Annotation itself or AnnotationsUtils |
Jian Li | d40252c | 2017-12-08 04:13:20 +0900 | [diff] [blame] | 84 | |
Jian Li | d8e7207 | 2017-11-21 10:50:02 +0900 | [diff] [blame] | 85 | /** |
| 86 | * Converts Map of Strings to {@link SparseAnnotations}. |
| 87 | * |
| 88 | * @param annotations Map of annotation key and values |
| 89 | * @return {@link SparseAnnotations} |
| 90 | */ |
| 91 | public static SparseAnnotations asAnnotations(Map<String, String> annotations) { |
| 92 | DefaultAnnotations.Builder builder = DefaultAnnotations.builder(); |
| 93 | annotations.entrySet().forEach(e -> { |
| 94 | if (e.getValue() != null) { |
| 95 | builder.set(e.getKey(), e.getValue()); |
| 96 | } else { |
| 97 | builder.remove(e.getKey()); |
| 98 | } |
| 99 | }); |
| 100 | return builder.build(); |
| 101 | } |
| 102 | |
Jian Li | d40252c | 2017-12-08 04:13:20 +0900 | [diff] [blame] | 103 | /** |
| 104 | * Converts Annotations to Map of Strings. |
| 105 | * |
| 106 | * @param annotations {@link Annotations} |
| 107 | * @return Map of annotation key and values |
| 108 | */ |
| 109 | public static Map<String, String> asMap(Annotations annotations) { |
| 110 | if (annotations instanceof DefaultAnnotations) { |
| 111 | return ((DefaultAnnotations) annotations).asMap(); |
| 112 | } |
| 113 | Map<String, String> map = new HashMap<>(); |
| 114 | annotations.keys() |
| 115 | .forEach(k -> map.put(k, annotations.value(k))); |
| 116 | |
| 117 | return map; |
| 118 | } |
| 119 | |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 120 | // Utility class not intended for instantiation. |
Jian Li | d40252c | 2017-12-08 04:13:20 +0900 | [diff] [blame] | 121 | private LinkProtoTranslator() { |
| 122 | } |
shivani vaidya | 9632b5f | 2017-06-27 11:00:04 -0700 | [diff] [blame] | 123 | |
| 124 | } |
| 125 | |