a6da0553625b6fb0765adb2f456e2b306c3ec7e2
[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.RpcError;
44 import org.opendaylight.yangtools.yang.common.RpcResult;
45 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50  * This class holds all RPCs methods for LispSouthbound Plugin.
51  *
52  * <p>
53  * @author Florin Coras (fcoras@cisco.com)
54  * @author Lorand Jakab (lojakab@cisco.com)
55  */
56 public class LispSouthboundRPC implements OdlLispSbService {
57
58     protected static final Logger LOG = LoggerFactory.getLogger(LispSouthboundRPC.class);
59
60     private final LispSouthboundPlugin lispSbPlugin;
61
62     public LispSouthboundRPC(LispSouthboundPlugin lispSbPlugin) {
63         this.lispSbPlugin = lispSbPlugin;
64     }
65
66
67     @Override
68     public ListenableFuture<RpcResult<SendMapNotifyOutput>> sendMapNotify(SendMapNotifyInput mapNotifyInput) {
69         LOG.trace("sendMapNotify called!!");
70         if (mapNotifyInput != null) {
71             ByteBuffer outBuffer = MapNotifySerializer.getInstance().serialize(mapNotifyInput.getMapNotify());
72             lispSbPlugin.handleSerializedLispBuffer(mapNotifyInput.getTransportAddress(), outBuffer,
73                     MessageType.MapNotify);
74         } else {
75             LOG.warn("MapNotify was null");
76             return Futures.immediateFuture(RpcResultBuilder.<SendMapNotifyOutput>failed().build());
77         }
78         return Futures.immediateFuture(RpcResultBuilder.<SendMapNotifyOutput>success(
79                 new SendMapNotifyOutputBuilder().build()).build());
80     }
81
82     @Override
83     public ListenableFuture<RpcResult<SendMapReplyOutput>> sendMapReply(SendMapReplyInput mapReplyInput) {
84         LOG.trace("sendMapReply called!!");
85         if (mapReplyInput != null) {
86             ByteBuffer outBuffer = MapReplySerializer.getInstance().serialize(mapReplyInput.getMapReply());
87             lispSbPlugin.handleSerializedLispBuffer(mapReplyInput.getTransportAddress(), outBuffer,
88                     MessageType.MapReply);
89         } else {
90             LOG.warn("MapReply was null");
91             return Futures.immediateFuture(RpcResultBuilder.<SendMapReplyOutput>failed().build());
92         }
93         return Futures.immediateFuture(RpcResultBuilder.<SendMapReplyOutput>success(
94                 new SendMapReplyOutputBuilder().build()).build());
95     }
96
97     @Override
98     public ListenableFuture<RpcResult<SendMapRequestOutput>> sendMapRequest(SendMapRequestInput mapRequestInput) {
99         LOG.trace("sendMapRequest called!!");
100         if (mapRequestInput != null) {
101             ByteBuffer outBuffer = MapRequestSerializer.getInstance().serialize(mapRequestInput.getMapRequest());
102             lispSbPlugin.handleSerializedLispBuffer(mapRequestInput.getTransportAddress(), outBuffer,
103                     MessageType.MapRequest);
104         } else {
105             LOG.debug("MapRequest was null");
106             return Futures.immediateFuture(RpcResultBuilder.<SendMapRequestOutput>failed().build());
107         }
108         return Futures.immediateFuture(RpcResultBuilder.<SendMapRequestOutput>success(
109                 new SendMapRequestOutputBuilder().build()).build());
110     }
111
112     @Override
113     public ListenableFuture<RpcResult<SendMapRegisterOutput>> sendMapRegister(SendMapRegisterInput mapRegisterInput) {
114         LOG.trace("sendMapRegister called!!");
115         if (mapRegisterInput != null) {
116             ByteBuffer outBuffer = MapRegisterSerializer.getInstance().serialize(mapRegisterInput.getMapRegister());
117             lispSbPlugin.handleSerializedLispBuffer(mapRegisterInput.getTransportAddress(), outBuffer,
118                     MessageType.MapRegister);
119         } else {
120             LOG.debug("MapRegister was null");
121             return Futures.immediateFuture(RpcResultBuilder.<SendMapRegisterOutput>failed().build());
122         }
123         return Futures.immediateFuture(RpcResultBuilder.<SendMapRegisterOutput>success(
124                 new SendMapRegisterOutputBuilder().build()).build());
125     }
126
127     @Override
128     public ListenableFuture<RpcResult<GetStatsOutput>> getStats(GetStatsInput input) {
129         LOG.trace("getStats called!!");
130
131         RpcResultBuilder<GetStatsOutput> rpcResultBuilder;
132
133         ConcurrentLispSouthboundStats stats = lispSbPlugin.getStats();
134
135         if (stats == null) {
136             rpcResultBuilder = RpcResultBuilder.<GetStatsOutput>failed()
137                     .withError(RpcError.ErrorType.APPLICATION, "data-missing", "No stats found");
138         } else {
139             rpcResultBuilder = RpcResultBuilder.success(createGetStatsOutput(stats));
140         }
141         return Futures.immediateFuture(rpcResultBuilder.build());
142     }
143
144     @Override
145     public ListenableFuture<RpcResult<ResetStatsOutput>> resetStats(ResetStatsInput input) {
146         LOG.trace("resetStats called!!");
147
148         ConcurrentLispSouthboundStats stats = lispSbPlugin.getStats();
149
150         if (stats == null) {
151             return Futures.immediateFuture(RpcResultBuilder.<ResetStatsOutput>failed()
152                     .withError(RpcError.ErrorType.APPLICATION, "data-missing", "No stats found")
153                     .build());
154         } else {
155             stats.resetStats();
156             return Futures.immediateFuture(RpcResultBuilder.<ResetStatsOutput>success(
157                     new ResetStatsOutputBuilder().build()).build());
158         }
159     }
160
161     private static GetStatsOutput createGetStatsOutput(ConcurrentLispSouthboundStats stats) {
162         long[] rxStats = stats.getRx();
163         long[] txStats = stats.getTx();
164
165         ControlMessageStatsBuilder cmsb = new ControlMessageStatsBuilder();
166         cmsb.setRxUnknown(stats.getRxUnknown());
167         cmsb.setTxErrors(stats.getTxErrors());
168
169         List<ControlMessage> messages = new ArrayList<ControlMessage>();
170         for (int i = 0; i <= ConcurrentLispSouthboundStats.MAX_LISP_TYPES; i++) {
171             if (MessageType.forValue(i) == null) {
172                 continue;
173             }
174             ControlMessageBuilder cmb = new ControlMessageBuilder();
175             cmb.setMsgType(MessageType.forValue(i));
176             cmb.setRxCount(rxStats[i]);
177             cmb.setTxCount(txStats[i]);
178             messages.add(cmb.build());
179         }
180
181         cmsb.setControlMessage(messages);
182
183         MapRegisterCacheStatsBuilder mrcsb = new MapRegisterCacheStatsBuilder();
184         mrcsb.setHits(stats.getCacheHits());
185         mrcsb.setMisses(stats.getCacheMisses());
186
187         return new GetStatsOutputBuilder().setControlMessageStats(cmsb.build())
188                 .setMapRegisterCacheStats(mrcsb.build()).build();
189     }
190 }