blob: 56804c7ec5fbe8a4a7b7faf1d1fa17768cc101d9 [file] [log] [blame]
/*
* Copyright 2017-present Open Networking Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.incubator.protobuf.models.net;
import org.onosproject.grpc.net.models.LinkProtoOuterClass;
import org.onosproject.incubator.protobuf.models.ProtobufUtils;
import org.onosproject.incubator.protobuf.models.net.link.LinkEnumsProtoTranslator;
import org.onosproject.net.Annotations;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DefaultLink;
import org.onosproject.net.Link;
import org.onosproject.net.provider.ProviderId;
/**
* gRPC LinkProto message to equivalent ONOS Link conversion related utilities.
*/
public final class LinkProtoTranslator {
/**
* Translates gRPC LinkCore message to {@link org.onosproject.net.Link}.
*
* @param link gRPC message
* @return {@link org.onosproject.net.Link} null if link is a default instance
*/
public static Link translate(LinkProtoOuterClass.LinkProto link) {
if (link.equals(LinkProtoOuterClass.LinkProto.getDefaultInstance())) {
return null;
}
ProviderId providerId = ProviderIdProtoTranslator.translate(link.getProviderId());
Link.State state = LinkEnumsProtoTranslator.translate(link.getState()).get();
ConnectPoint src = ConnectPointProtoTranslator.translate(link.getSrc()).get();
ConnectPoint dst = ConnectPointProtoTranslator.translate(link.getDst()).get();
Link.Type type = LinkEnumsProtoTranslator.translate(link.getType()).get();
Annotations annots = ProtobufUtils.asAnnotations(link.getAnnotations());
Boolean isExpected = link.getIsExpected();
return DefaultLink.builder().state(state)
.annotations(annots)
.providerId(providerId)
.src(src)
.dst(dst)
.type(type)
.isExpected(isExpected)
.build();
}
/**
* Translates {@link org.onosproject.net.Link} to gRPC LinkCore message.
*
* @param link {@link org.onosproject.net.Link}
* @return gRPC LinkCore message
*/
public static LinkProtoOuterClass.LinkProto translate(Link link) {
if (link == null) {
return LinkProtoOuterClass.LinkProto.getDefaultInstance();
}
return LinkProtoOuterClass.LinkProto.newBuilder()
.setProviderId(ProviderIdProtoTranslator.translate(link.providerId()))
.setState(LinkEnumsProtoTranslator.translate(link.state()))
.setSrc(ConnectPointProtoTranslator.translate(link.src()))
.setDst(ConnectPointProtoTranslator.translate(link.dst()))
.setType(LinkEnumsProtoTranslator.translate(link.type()))
.setIsExpected(link.isExpected())
.build();
}
// Utility class not intended for instantiation.
private LinkProtoTranslator() {}
}