2dae903dcd9c173ece58e26175349eaeb2d5495f
[govpp.git] / binapigen / validate.go
1 //  Copyright (c) 2020 Cisco and/or its affiliates.
2 //
3 //  Licensed under the Apache License, Version 2.0 (the "License");
4 //  you may not use this file except in compliance with the License.
5 //  You may obtain a copy of the License at:
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 //  Unless required by applicable law or agreed to in writing, software
10 //  distributed under the License is distributed on an "AS IS" BASIS,
11 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 //  See the License for the specific language governing permissions and
13 //  limitations under the License.
14
15 package binapigen
16
17 import (
18         "strings"
19
20         "git.fd.io/govpp.git/binapigen/vppapi"
21         "github.com/sirupsen/logrus"
22 )
23
24 const (
25         serviceEventPrefix   = "want_"
26         serviceDumpSuffix    = "_dump"
27         serviceDetailsSuffix = "_details"
28         serviceReplySuffix   = "_reply"
29 )
30
31 func validateService(svc vppapi.Service) {
32         for _, rpc := range svc.RPCs {
33                 validateRPC(rpc)
34         }
35 }
36
37 func validateRPC(rpc vppapi.RPC) {
38         if len(rpc.Events) > 0 {
39                 // EVENT service
40                 if !strings.HasPrefix(rpc.RequestMsg, serviceEventPrefix) {
41                         logrus.Warnf("unusual EVENTS service: %+v\n"+
42                                 "- events service %q does not have %q prefix in request.",
43                                 rpc, rpc.Name, serviceEventPrefix)
44                 }
45         } else if rpc.Stream {
46                 // STREAM service
47                 if !strings.HasSuffix(rpc.RequestMsg, serviceDumpSuffix) {
48                         logrus.Warnf("unusual STREAM service: %+v\n"+
49                                 "- stream service %q does not have %q suffix in request.",
50                                 rpc, rpc.Name, serviceDumpSuffix)
51                 }
52                 if !strings.HasSuffix(rpc.ReplyMsg, serviceDetailsSuffix) && !strings.HasSuffix(rpc.StreamMsg, serviceDetailsSuffix) {
53                         logrus.Warnf("unusual STREAM service: %+v\n"+
54                                 "- stream service %q does not have %q suffix in reply or stream msg.",
55                                 rpc, rpc.Name, serviceDetailsSuffix)
56                 }
57         } else if rpc.ReplyMsg != "" {
58                 // REQUEST service
59                 // some messages might have `null` reply (for example: memclnt)
60                 if !strings.HasSuffix(rpc.ReplyMsg, serviceReplySuffix) {
61                         logrus.Warnf("unusual REQUEST service: %+v\n"+
62                                 "- service %q does not have %q suffix in reply.",
63                                 rpc, rpc.Name, serviceReplySuffix)
64                 }
65         }
66 }