blob: 37630cfa0a1604a44ad83f817fb8e93499789209 [file] [log] [blame]
Himal Kumarb43724d2016-04-29 14:15:57 +10001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Himal Kumarb43724d2016-04-29 14:15:57 +10003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.castor;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onlab.packet.IpAddress;
21import org.onosproject.rest.AbstractWebResource;
22
Ray Milkeyba547f92018-02-01 15:22:31 -080023import java.io.IOException;
Himal Kumarb43724d2016-04-29 14:15:57 +100024import javax.ws.rs.Consumes;
25import javax.ws.rs.GET;
26import javax.ws.rs.POST;
27import javax.ws.rs.Path;
28import javax.ws.rs.core.MediaType;
29import javax.ws.rs.core.Response;
30
31/**
32 * The Web Resource for REST API calls to the Castor application.
33 */
34@Path("castor")
35public class CastorWebResource extends AbstractWebResource {
36
37
38 /**
39 * Get the present ARP Mapping.
40 * Use this to get the present ARP map stored by Castor
41 *
42 * @return 200 OK
43 */
44 @GET
45 @Path("mac-map")
46 public Response getMac() {
47 String result = get(CastorStore.class).getAddressMap().toString();
48 ObjectNode node = mapper().createObjectNode().put("response", result);
49 return ok(node).build();
50 }
51
52 /**
53 * Get list of added peers.
54 * List of peers added.
55 *
56 * @return 200 OK
57 */
58 @GET
59 @Path("get-peers")
60 public Response getPeers() {
61 String result = get(CastorStore.class).getCustomersMap().toString();
62 ObjectNode node = mapper().createObjectNode().put("response", result);
63 return ok(node).build();
64 }
65
66 /**
67 * Add a Peer.
68 * Use this to add a Customer or a BGP Peer
69 *
70 * @onos.rsModel PeerModel
71 * @param incomingData json Data
72 * @return 200 OK
73 */
74 @POST
75 @Path("add-peer")
76 @Consumes(MediaType.APPLICATION_JSON)
77 public Response addPeer(String incomingData) {
78
79 String arpResult = ", Mac was known";
80
81 try {
82 ObjectMapper mapper = new ObjectMapper();
83 Peer peer = mapper.readValue(incomingData, Peer.class);
84 get(ConnectivityManagerService.class).setUpConnectivity(peer);
85 if ((get(CastorStore.class)).getAddressMap()
86 .get(IpAddress.valueOf(peer.getIpAddress())) != null) {
87 get(ConnectivityManagerService.class).setUpL2(peer);
88 } else {
89 get(ArpService.class).createArp(peer);
90 arpResult = ", ARP packet sent, MAC was not known";
91 }
Ray Milkeyba547f92018-02-01 15:22:31 -080092 } catch (IOException e) {
Himal Kumarb43724d2016-04-29 14:15:57 +100093 String result = "Unable to process due to some reason, Try again";
94 ObjectNode node = mapper().createObjectNode().put("response", result);
95 return ok(node).build();
96 }
97
98 String result = "Success: Peer Entered" + arpResult;
99 ObjectNode node = mapper().createObjectNode().put("response", result);
100 return ok(node).build();
101 }
102
103 /**
104 * Delete a Peer.
105 * Use this to delete a Peer. IpAddress should match as entered while adding.
106 *
107 * @onos.rsModel PeerModel
108 * @param incomingData json Data
109 * @return 200 OK
110 */
111 @POST
112 @Path("delete-peer")
113 @Consumes(MediaType.APPLICATION_JSON)
114 public Response deletePeer(String incomingData) {
115
116 try {
117 ObjectMapper mapper = new ObjectMapper();
118 Peer peer = mapper.readValue(incomingData, Peer.class);
119 get(ConnectivityManagerService.class).deletePeer(peer);
Ray Milkeyba547f92018-02-01 15:22:31 -0800120 } catch (IOException e) {
Himal Kumarb43724d2016-04-29 14:15:57 +1000121 return Response.status(500).entity("Unable to delete the peer").build();
122 }
123 String result = "Peer Deleted";
124 ObjectNode node = mapper().createObjectNode().put("response", result);
125 return ok(node).build();
126 }
127
128 /**
129 * Add router server.
130 * Use this to add to add Route Servers for initializing
131 *
132 * @onos.rsModel PeerModel
133 * @param incomingData json Data
134 * @return 200 OK
135 */
136 @POST
137 @Path("route-server")
138 @Consumes(MediaType.APPLICATION_JSON)
139 public Response addRouteServer(String incomingData) {
140
141 try {
142 ObjectMapper mapper = new ObjectMapper();
143 Peer peer = mapper.readValue(incomingData, Peer.class);
144 get(ConnectivityManagerService.class).start(peer);
Ray Milkeyba547f92018-02-01 15:22:31 -0800145 } catch (IOException e) {
Himal Kumarb43724d2016-04-29 14:15:57 +1000146 return Response.status(500).entity("Unable to add the route server").build();
147 }
148 String result = "Server Entered";
149 ObjectNode node = mapper().createObjectNode().put("response", result);
150 return ok(node).build();
151 }
152}