View Javadoc

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   
9   package org.opendaylight.lispflowmapping.southbound;
10  
11  import java.nio.ByteBuffer;
12  import java.util.ArrayList;
13  import java.util.List;
14  import java.util.concurrent.Future;
15  
16  import org.opendaylight.lispflowmapping.lisp.serializer.MapNotifySerializer;
17  import org.opendaylight.lispflowmapping.lisp.serializer.MapRegisterSerializer;
18  import org.opendaylight.lispflowmapping.lisp.serializer.MapReplySerializer;
19  import org.opendaylight.lispflowmapping.lisp.serializer.MapRequestSerializer;
20  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MessageType;
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.SendMapNotifyInput;
25  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRegisterInput;
26  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapReplyInput;
27  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestInput;
28  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.ctrl.msg.stats.ControlMessage;
29  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.ctrl.msg.stats.ControlMessageBuilder;
30  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.get.stats.output.ControlMessageStatsBuilder;
31  import org.opendaylight.yangtools.yang.common.RpcError;
32  import org.opendaylight.yangtools.yang.common.RpcResult;
33  import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  import com.google.common.util.concurrent.Futures;
38  
39  /**
40   * This class holds all RPCs methods for LispSouthbound Plugin.
41   *
42   * <p>
43   * @author Florin Coras (fcoras@cisco.com)
44   * @author Lorand Jakab (lojakab@cisco.com)
45   */
46  
47  public class LispSouthboundRPC implements OdlLispSbService {
48  
49      protected static final Logger LOG = LoggerFactory.getLogger(LispSouthboundRPC.class);
50  
51      private final LispSouthboundPlugin lispSbPlugin;
52  
53      public LispSouthboundRPC(LispSouthboundPlugin lispSbPlugin) {
54          this.lispSbPlugin = lispSbPlugin;
55      }
56  
57  
58      @Override
59      public Future<RpcResult<Void>> sendMapNotify(SendMapNotifyInput mapNotifyInput) {
60          LOG.trace("sendMapNotify called!!");
61          if (mapNotifyInput != null) {
62              ByteBuffer outBuffer = MapNotifySerializer.getInstance().serialize(mapNotifyInput.getMapNotify());
63              lispSbPlugin.handleSerializedLispBuffer(mapNotifyInput.getTransportAddress(), outBuffer, MessageType.MapNotify);
64          } else {
65              LOG.warn("MapNotify was null");
66              return Futures.immediateFuture(RpcResultBuilder.<Void> failed().build());
67          }
68          return Futures.immediateFuture(RpcResultBuilder.<Void> success().build());
69      }
70  
71      @Override
72      public Future<RpcResult<Void>> sendMapReply(SendMapReplyInput mapReplyInput) {
73          LOG.trace("sendMapReply called!!");
74          if (mapReplyInput != null) {
75              ByteBuffer outBuffer = MapReplySerializer.getInstance().serialize(mapReplyInput.getMapReply());
76              lispSbPlugin.handleSerializedLispBuffer(mapReplyInput.getTransportAddress(), outBuffer, MessageType.MapReply);
77          } else {
78              LOG.warn("MapReply was null");
79              return Futures.immediateFuture(RpcResultBuilder.<Void> failed().build());
80          }
81          return Futures.immediateFuture(RpcResultBuilder.<Void> success().build());
82      }
83  
84      @Override
85      public Future<RpcResult<Void>> sendMapRequest(SendMapRequestInput mapRequestInput) {
86          LOG.trace("sendMapRequest called!!");
87          if (mapRequestInput != null) {
88              ByteBuffer outBuffer = MapRequestSerializer.getInstance().serialize(mapRequestInput.getMapRequest());
89              lispSbPlugin.handleSerializedLispBuffer(mapRequestInput.getTransportAddress(), outBuffer, MessageType.MapRequest);
90          } else {
91              LOG.debug("MapRequest was null");
92              return Futures.immediateFuture(RpcResultBuilder.<Void> failed().build());
93          }
94          return Futures.immediateFuture(RpcResultBuilder.<Void> success().build());
95      }
96  
97      @Override
98      public Future<RpcResult<Void>> sendMapRegister(SendMapRegisterInput mapRegisterInput) {
99          LOG.trace("sendMapRegister called!!");
100         if (mapRegisterInput != null) {
101             ByteBuffer outBuffer = MapRegisterSerializer.getInstance().serialize(mapRegisterInput.getMapRegister());
102             lispSbPlugin.handleSerializedLispBuffer(mapRegisterInput.getTransportAddress(), outBuffer, MessageType.MapRegister);
103         } else {
104             LOG.debug("MapRegister was null");
105             return Futures.immediateFuture(RpcResultBuilder.<Void> failed().build());
106         }
107         return Futures.immediateFuture(RpcResultBuilder.<Void> success().build());
108     }
109 
110     @Override
111     public Future<RpcResult<GetStatsOutput>> getStats() {
112         LOG.trace("getStats called!!");
113 
114         RpcResultBuilder<GetStatsOutput> rpcResultBuilder;
115 
116         LispSouthboundStats stats = lispSbPlugin.getStats();
117 
118         if (stats == null) {
119             rpcResultBuilder = RpcResultBuilder.<GetStatsOutput>failed()
120                     .withError(RpcError.ErrorType.APPLICATION, "data-missing", "No stats found");
121         } else {
122             rpcResultBuilder = RpcResultBuilder.success(createGetStatsOutput(stats));
123         }
124         return Futures.immediateFuture(rpcResultBuilder.build());
125     }
126 
127     @Override
128     public Future<RpcResult<Void>> resetStats() {
129         LOG.trace("resetStats called!!");
130 
131         LispSouthboundStats stats = lispSbPlugin.getStats();
132 
133         if (stats == null) {
134             return Futures.immediateFuture(RpcResultBuilder.<Void> failed()
135                     .withError(RpcError.ErrorType.APPLICATION, "data-missing", "No stats found")
136                     .build());
137         } else {
138             stats.resetStats();
139             return Futures.immediateFuture(RpcResultBuilder.<Void> success().build());
140         }
141     }
142 
143     private static GetStatsOutput createGetStatsOutput(LispSouthboundStats stats) {
144         long rxStats[] = stats.getRx();
145         long txStats[] = stats.getTx();
146 
147         ControlMessageStatsBuilder cmsb = new ControlMessageStatsBuilder();
148         cmsb.setRxUnknown(stats.getRxUnknown());
149         cmsb.setTxErrors(stats.getTxErrors());
150 
151         List<ControlMessage> messages = new ArrayList<ControlMessage>();
152         for (int i = 0; i <= LispSouthboundStats.MAX_LISP_TYPES; i++) {
153             if (MessageType.forValue(i) == null) {
154                 continue;
155             }
156             ControlMessageBuilder cmb = new ControlMessageBuilder();
157             cmb.setMsgType(MessageType.forValue(i));
158             cmb.setRxCount(rxStats[i]);
159             cmb.setTxCount(txStats[i]);
160             messages.add(cmb.build());
161         }
162 
163         cmsb.setControlMessage(messages);
164         return new GetStatsOutputBuilder().setControlMessageStats(cmsb.build()).build();
165     }
166 }