Bump MRI upstreams
[lispflowmapping.git] / mappingservice / southbound / src / main / java / org / opendaylight / lispflowmapping / southbound / LispSouthboundRPC.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.lispflowmapping.southbound;
9
10 import com.google.common.util.concurrent.Futures;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.nio.ByteBuffer;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.opendaylight.lispflowmapping.lisp.serializer.MapNotifySerializer;
16 import org.opendaylight.lispflowmapping.lisp.serializer.MapRegisterSerializer;
17 import org.opendaylight.lispflowmapping.lisp.serializer.MapReplySerializer;
18 import org.opendaylight.lispflowmapping.lisp.serializer.MapRequestSerializer;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MessageType;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.GetStatsInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.GetStatsOutput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.GetStatsOutputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLispSbService;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.ResetStatsInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.ResetStatsOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.ResetStatsOutputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapNotifyInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapNotifyOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapNotifyOutputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRegisterInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRegisterOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRegisterOutputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapReplyInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapReplyOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapReplyOutputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestOutput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestOutputBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.ctrl.msg.stats.ControlMessage;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.ctrl.msg.stats.ControlMessageBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.get.stats.output.ControlMessageStatsBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.get.stats.output.MapRegisterCacheStatsBuilder;
43 import org.opendaylight.yangtools.yang.common.ErrorTag;
44 import org.opendaylight.yangtools.yang.common.ErrorType;
45 import org.opendaylight.yangtools.yang.common.RpcResult;
46 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 /**
51  * This class holds all RPCs methods for LispSouthbound Plugin.
52  *
53  * <p>
54  * @author Florin Coras (fcoras@cisco.com)
55  * @author Lorand Jakab (lojakab@cisco.com)
56  */
57 public class LispSouthboundRPC implements OdlLispSbService {
58
59     protected static final Logger LOG = LoggerFactory.getLogger(LispSouthboundRPC.class);
60
61     private final LispSouthboundPlugin lispSbPlugin;
62
63     public LispSouthboundRPC(LispSouthboundPlugin lispSbPlugin) {
64         this.lispSbPlugin = lispSbPlugin;
65     }
66
67
68     @Override
69     public ListenableFuture<RpcResult<SendMapNotifyOutput>> sendMapNotify(SendMapNotifyInput mapNotifyInput) {
70         LOG.trace("sendMapNotify called!!");
71         if (mapNotifyInput != null) {
72             ByteBuffer outBuffer = MapNotifySerializer.getInstance().serialize(mapNotifyInput.getMapNotify());
73             lispSbPlugin.handleSerializedLispBuffer(mapNotifyInput.getTransportAddress(), outBuffer,
74                     MessageType.MapNotify);
75         } else {
76             LOG.warn("MapNotify was null");
77             return Futures.immediateFuture(RpcResultBuilder.<SendMapNotifyOutput>failed().build());
78         }
79         return Futures.immediateFuture(RpcResultBuilder.<SendMapNotifyOutput>success(
80                 new SendMapNotifyOutputBuilder().build()).build());
81     }
82
83     @Override
84     public ListenableFuture<RpcResult<SendMapReplyOutput>> sendMapReply(SendMapReplyInput mapReplyInput) {
85         LOG.trace("sendMapReply called!!");
86         if (mapReplyInput != null) {
87             ByteBuffer outBuffer = MapReplySerializer.getInstance().serialize(mapReplyInput.getMapReply());
88             lispSbPlugin.handleSerializedLispBuffer(mapReplyInput.getTransportAddress(), outBuffer,
89                     MessageType.MapReply);
90         } else {
91             LOG.warn("MapReply was null");
92             return Futures.immediateFuture(RpcResultBuilder.<SendMapReplyOutput>failed().build());
93         }
94         return Futures.immediateFuture(RpcResultBuilder.<SendMapReplyOutput>success(
95                 new SendMapReplyOutputBuilder().build()).build());
96     }
97
98     @Override
99     public ListenableFuture<RpcResult<SendMapRequestOutput>> sendMapRequest(SendMapRequestInput mapRequestInput) {
100         LOG.trace("sendMapRequest called!!");
101         if (mapRequestInput != null) {
102             ByteBuffer outBuffer = MapRequestSerializer.getInstance().serialize(mapRequestInput.getMapRequest());
103             lispSbPlugin.handleSerializedLispBuffer(mapRequestInput.getTransportAddress(), outBuffer,
104                     MessageType.MapRequest);
105         } else {
106             LOG.debug("MapRequest was null");
107             return Futures.immediateFuture(RpcResultBuilder.<SendMapRequestOutput>failed().build());
108         }
109         return Futures.immediateFuture(RpcResultBuilder.<SendMapRequestOutput>success(
110                 new SendMapRequestOutputBuilder().build()).build());
111     }
112
113     @Override
114     public ListenableFuture<RpcResult<SendMapRegisterOutput>> sendMapRegister(SendMapRegisterInput mapRegisterInput) {
115         LOG.trace("sendMapRegister called!!");
116         if (mapRegisterInput != null) {
117             ByteBuffer outBuffer = MapRegisterSerializer.getInstance().serialize(mapRegisterInput.getMapRegister());
118             lispSbPlugin.handleSerializedLispBuffer(mapRegisterInput.getTransportAddress(), outBuffer,
119                     MessageType.MapRegister);
120         } else {
121             LOG.debug("MapRegister was null");
122             return Futures.immediateFuture(RpcResultBuilder.<SendMapRegisterOutput>failed().build());
123         }
124         return Futures.immediateFuture(RpcResultBuilder.<SendMapRegisterOutput>success(
125                 new SendMapRegisterOutputBuilder().build()).build());
126     }
127
128     @Override
129     public ListenableFuture<RpcResult<GetStatsOutput>> getStats(GetStatsInput input) {
130         LOG.trace("getStats called!!");
131
132         RpcResultBuilder<GetStatsOutput> rpcResultBuilder;
133
134         ConcurrentLispSouthboundStats stats = lispSbPlugin.getStats();
135
136         if (stats == null) {
137             rpcResultBuilder = RpcResultBuilder.<GetStatsOutput>failed()
138                     .withError(ErrorType.APPLICATION, ErrorTag.DATA_MISSING, "No stats found");
139         } else {
140             rpcResultBuilder = RpcResultBuilder.success(createGetStatsOutput(stats));
141         }
142         return Futures.immediateFuture(rpcResultBuilder.build());
143     }
144
145     @Override
146     public ListenableFuture<RpcResult<ResetStatsOutput>> resetStats(ResetStatsInput input) {
147         LOG.trace("resetStats called!!");
148
149         ConcurrentLispSouthboundStats stats = lispSbPlugin.getStats();
150
151         if (stats == null) {
152             return Futures.immediateFuture(RpcResultBuilder.<ResetStatsOutput>failed()
153                     .withError(ErrorType.APPLICATION, ErrorTag.DATA_MISSING, "No stats found")
154                     .build());
155         } else {
156             stats.resetStats();
157             return Futures.immediateFuture(RpcResultBuilder.<ResetStatsOutput>success(
158                     new ResetStatsOutputBuilder().build()).build());
159         }
160     }
161
162     private static GetStatsOutput createGetStatsOutput(ConcurrentLispSouthboundStats stats) {
163         long[] rxStats = stats.getRx();
164         long[] txStats = stats.getTx();
165
166         ControlMessageStatsBuilder cmsb = new ControlMessageStatsBuilder();
167         cmsb.setRxUnknown(stats.getRxUnknown());
168         cmsb.setTxErrors(stats.getTxErrors());
169
170         List<ControlMessage> messages = new ArrayList<ControlMessage>();
171         for (int i = 0; i <= ConcurrentLispSouthboundStats.MAX_LISP_TYPES; i++) {
172             if (MessageType.forValue(i) == null) {
173                 continue;
174             }
175             ControlMessageBuilder cmb = new ControlMessageBuilder();
176             cmb.setMsgType(MessageType.forValue(i));
177             cmb.setRxCount(rxStats[i]);
178             cmb.setTxCount(txStats[i]);
179             messages.add(cmb.build());
180         }
181
182         cmsb.setControlMessage(messages);
183
184         MapRegisterCacheStatsBuilder mrcsb = new MapRegisterCacheStatsBuilder();
185         mrcsb.setHits(stats.getCacheHits());
186         mrcsb.setMisses(stats.getCacheMisses());
187
188         return new GetStatsOutputBuilder().setControlMessageStats(cmsb.build())
189                 .setMapRegisterCacheStats(mrcsb.build()).build();
190     }
191 }