Skip to content
Permalink
Browse files

Merge pull request #3308 from telstra/feature/packet_in_on_stats

Added PacketInOutStats to GRPC API
  • Loading branch information
nikitamarchenko committed Mar 23, 2020
2 parents 921d109 + 5ab97ad commit 8d2c6e75afc139ce929d7673c252b478d4e48e60
@@ -0,0 +1,44 @@
/* Copyright 2019 Telstra Open Source
*
* 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.openkilda.grpc.speaker.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategy.SnakeCaseStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonNaming(SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class PacketInOutStatsResponse {

private long packetInTotalPackets;
private long packetInTotalPacketsDataplane;
private long packetInNoMatchPackets;
private long packetInApplyActionPackets;
private long packetInInvalidTtlPackets;
private long packetInActionSetPackets;
private long packetInGroupPackets;
private long packetInPacketOutPackets;
private long packetOutTotalPacketsDataplane;
private long packetOutTotalPacketsHost;
private Boolean packetOutEth0InterfaceUp;
private int replyStatus;
}
@@ -35,11 +35,13 @@
import io.grpc.noviflow.LogicalPort;
import io.grpc.noviflow.NoviFlowGrpcGrpc;
import io.grpc.noviflow.OnOff;
import io.grpc.noviflow.PacketInOutStats;
import io.grpc.noviflow.PortConfig;
import io.grpc.noviflow.PortMode;
import io.grpc.noviflow.PortPause;
import io.grpc.noviflow.PortSpeed;
import io.grpc.noviflow.RemoteLogServer;
import io.grpc.noviflow.ShowPacketInOutStats;
import io.grpc.noviflow.ShowRemoteLogServer;
import io.grpc.noviflow.StatusSwitch;
import lombok.extern.slf4j.Slf4j;
@@ -373,4 +375,16 @@ public void shutdown() {
return observer.future
.thenApply(responses -> responses.stream().findFirst());
}

/**
* Performs packet in out stats request.
*
* @return {@link CompletableFuture} with operation result.
*/
public CompletableFuture<Optional<PacketInOutStats>> getPacketInOutStats() {
GrpcResponseObserver<PacketInOutStats> observer = new GrpcResponseObserver<>();
log.info("Getting packet in out stats for switch {}", address);
stub.showStatsPacketInOut(ShowPacketInOutStats.newBuilder().build(), observer);
return observer.future.thenApply(responses -> responses.stream().findFirst());
}
}
@@ -22,6 +22,7 @@
import org.openkilda.grpc.speaker.model.LogMessagesDto;
import org.openkilda.grpc.speaker.model.LogOferrorsDto;
import org.openkilda.grpc.speaker.model.LogicalPortDto;
import org.openkilda.grpc.speaker.model.PacketInOutStatsResponse;
import org.openkilda.grpc.speaker.model.PortConfigDto;
import org.openkilda.grpc.speaker.model.PortConfigSetupResponse;
import org.openkilda.grpc.speaker.model.RemoteLogServerDto;
@@ -175,4 +176,12 @@
@RequestBody LicenseDto licenseDto) {
return grpcService.setConfigLicense(switchAddress, licenseDto);
}

@ApiOperation(value = "Get packet in out stats for switch", response = PacketInOutStatsResponse.class)
@GetMapping(path = "{switch_address}/packet-in-out-stats")
@ResponseStatus(HttpStatus.OK)
public CompletableFuture<PacketInOutStatsResponse> packetInOutStats(
@PathVariable("switch_address") String switchAddress) {
return grpcService.getPacketInOutStats(switchAddress);
}
}
@@ -15,15 +15,18 @@

package org.openkilda.grpc.speaker.mapper;

import org.openkilda.grpc.speaker.model.PacketInOutStatsResponse;
import org.openkilda.messaging.model.grpc.SwitchInfoStatus;
import org.openkilda.messaging.model.grpc.SwitchInfoStatus.SwitchBuildInfoStatus;
import org.openkilda.messaging.model.grpc.SwitchInfoStatus.SwitchEthLinkInfoStatus;

import io.grpc.noviflow.LogicalPort;
import io.grpc.noviflow.PacketInOutStats;
import io.grpc.noviflow.RemoteLogServer;
import io.grpc.noviflow.StatusSwitch;
import io.grpc.noviflow.StatusSwitchBuild;
import io.grpc.noviflow.StatusSwitchEthLink;
import io.grpc.noviflow.YesNo;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@@ -44,4 +47,23 @@
SwitchEthLinkInfoStatus toSwitchEthLink(StatusSwitchEthLink statusSwitchEthLink);

SwitchBuildInfoStatus toSwitchBuildInfo(StatusSwitchBuild statusSwitchBuild);

PacketInOutStatsResponse toPacketInOutStatsResponse(PacketInOutStats stats);

/**
* Maps YesNo enum to Boolean value.
*/
default Boolean toBoolean(YesNo yesNo) {
if (yesNo == null) {
return null;
}
switch (yesNo) {
case YES:
return true;
case NO:
return false;
default:
return null;
}
}
}
@@ -27,6 +27,7 @@
import org.openkilda.grpc.speaker.model.LogMessagesDto;
import org.openkilda.grpc.speaker.model.LogOferrorsDto;
import org.openkilda.grpc.speaker.model.LogicalPortDto;
import org.openkilda.grpc.speaker.model.PacketInOutStatsResponse;
import org.openkilda.grpc.speaker.model.PortConfigDto;
import org.openkilda.grpc.speaker.model.PortConfigSetupResponse;
import org.openkilda.grpc.speaker.model.RemoteLogServerDto;
@@ -278,4 +279,22 @@ public GrpcSenderService(@Autowired NoviflowResponseMapper mapper) {
new GrpcException(format("Could not setup license for switch %s", switchAddress))))
.whenComplete((e, ex) -> sender.shutdown());
}

/**
* Gets packet in out stats.
*
* @param switchAddress switch address.
* @return {@link CompletableFuture} with the execution result.
*/
public CompletableFuture<PacketInOutStatsResponse> getPacketInOutStats(String switchAddress) {
GrpcSession sender = new GrpcSession(switchAddress);
return sender.login(name, password)
.thenCompose(e -> sender.getPacketInOutStats())
.thenApply(statusOptional -> statusOptional
.map(mapper::toPacketInOutStatsResponse)
.orElseThrow(() ->
new GrpcException(format(
"Couldn't get packet in out stats for switch %s", switchAddress))))
.whenComplete((e, ex) -> sender.shutdown());
}
}
@@ -0,0 +1,83 @@
/* Copyright 2020 Telstra Open Source
*
* 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.openkilda.grpc.speaker.mapper;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import org.openkilda.grpc.speaker.model.PacketInOutStatsResponse;

import io.grpc.noviflow.PacketInOutStats;
import io.grpc.noviflow.YesNo;
import org.junit.Assert;
import org.junit.Test;
import org.mapstruct.factory.Mappers;

public class NoviflowResponseMapperTest {

public static final long PACKET_IN_TOTAL_PACKETS = 1L;
public static final long PACKET_IN_TOTAL_PACKETS_DATAPLANE = 2L;
public static final long PACKET_IN_NO_MATCH_PACKETS = 3L;
public static final long PACKET_IN_APPLY_ACTION_PACKETS = 4L;
public static final long PACKET_IN_INVALID_TTL_PACKETS = 5L;
public static final long PACKET_IN_ACTION_SET_PACKETS = 6L;
public static final long PACKET_IN_GROUP_PACKETS = 7L;
public static final long PACKET_IN_PACKET_OUT_PACKETS = 8L;
public static final long PACKET_OUT_TOTAL_PACKETS_DATAPLANE = 9L;
public static final long PACKET_OUT_TOTAL_PACKETS_HOST = 10L;
public static final int REPLY_STATUS = 11;
private NoviflowResponseMapper mapper = Mappers.getMapper(NoviflowResponseMapper.class);

@Test
public void yesNoTest() {
Assert.assertTrue(mapper.toBoolean(YesNo.YES));
Assert.assertFalse(mapper.toBoolean(YesNo.NO));
assertNull(mapper.toBoolean(null));
assertNull(mapper.toBoolean(YesNo.UNRECOGNIZED));
assertNull(mapper.toBoolean(YesNo.YESNO_RESERVED));
}

@Test
public void packetInOutTest() {
PacketInOutStats stats = PacketInOutStats.newBuilder()
.setPacketInTotalPackets(PACKET_IN_TOTAL_PACKETS)
.setPacketInTotalPacketsDataplane(PACKET_IN_TOTAL_PACKETS_DATAPLANE)
.setPacketInNoMatchPackets(PACKET_IN_NO_MATCH_PACKETS)
.setPacketInApplyActionPackets(PACKET_IN_APPLY_ACTION_PACKETS)
.setPacketInInvalidTtlPackets(PACKET_IN_INVALID_TTL_PACKETS)
.setPacketInActionSetPackets(PACKET_IN_ACTION_SET_PACKETS)
.setPacketInGroupPackets(PACKET_IN_GROUP_PACKETS)
.setPacketInPacketOutPackets(PACKET_IN_PACKET_OUT_PACKETS)
.setPacketOutTotalPacketsDataplane(PACKET_OUT_TOTAL_PACKETS_DATAPLANE)
.setPacketOutTotalPacketsHost(PACKET_OUT_TOTAL_PACKETS_HOST)
.setPacketOutEth0InterfaceUp(YesNo.YES)
.setReplyStatus(REPLY_STATUS)
.build();
PacketInOutStatsResponse response = mapper.toPacketInOutStatsResponse(stats);
assertEquals(PACKET_IN_TOTAL_PACKETS, response.getPacketInTotalPackets());
assertEquals(PACKET_IN_TOTAL_PACKETS_DATAPLANE, response.getPacketInTotalPacketsDataplane());
assertEquals(PACKET_IN_NO_MATCH_PACKETS, response.getPacketInNoMatchPackets());
assertEquals(PACKET_IN_APPLY_ACTION_PACKETS, response.getPacketInApplyActionPackets());
assertEquals(PACKET_IN_INVALID_TTL_PACKETS, response.getPacketInInvalidTtlPackets());
assertEquals(PACKET_IN_ACTION_SET_PACKETS, response.getPacketInActionSetPackets());
assertEquals(PACKET_IN_GROUP_PACKETS, response.getPacketInGroupPackets());
assertEquals(PACKET_IN_PACKET_OUT_PACKETS, response.getPacketInPacketOutPackets());
assertEquals(PACKET_OUT_TOTAL_PACKETS_DATAPLANE, response.getPacketOutTotalPacketsDataplane());
assertEquals(PACKET_OUT_TOTAL_PACKETS_HOST, response.getPacketOutTotalPacketsHost());
assertEquals(Boolean.TRUE, response.getPacketOutEth0InterfaceUp());
assertEquals(REPLY_STATUS, response.getReplyStatus());
}
}

0 comments on commit 8d2c6e7

Please sign in to comment.
You can’t perform that action at this time.