/*- * ========================LICENSE_START================================= * O-RAN-SC * %% * Copyright (C) 2019 Nordix Foundation * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ========================LICENSE_END=================================== */ package org.oransc.policyagent.controllers; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import java.time.Duration; import java.util.Collection; import java.util.Vector; import org.oransc.policyagent.exceptions.ServiceException; import org.oransc.policyagent.repository.Service; import org.oransc.policyagent.repository.Services; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class ServiceController { private final Services services; private static Gson gson = new GsonBuilder() // .serializeNulls() // .create(); // @Autowired ServiceController(Services services) { this.services = services; } @GetMapping("/service") public ResponseEntity getService( // @RequestParam(name = "name", required = true) String name) { try { Service s = services.getService(name); String res = gson.toJson(toServiceStatus(s)); return new ResponseEntity(res, HttpStatus.OK); } catch (ServiceException e) { return new ResponseEntity(e.getMessage(), HttpStatus.NO_CONTENT); } } private ServiceStatus toServiceStatus(Service s) { return ImmutableServiceStatus.builder() // .name(s.getName()) // .keepAliveInterval(s.getKeepAliveInterval().toSeconds()) // .timeSincePing(s.timeSinceLastPing().toSeconds()) // .build(); } @PutMapping("/service") public ResponseEntity putService( // @RequestBody String jsonBody) { try { ServiceRegistrationInfo s = gson.fromJson(jsonBody, ImmutableServiceRegistrationInfo.class); this.services.put(toService(s)); return new ResponseEntity("OK", HttpStatus.OK); } catch (Exception e) { return new ResponseEntity(e.getMessage(), HttpStatus.NO_CONTENT); } } private Service toService(ServiceRegistrationInfo s) { return new Service(s.name(), Duration.ofSeconds(s.keepAliveInterval()), s.callbackUrl()); } @GetMapping("/services") public ResponseEntity getServices() { Collection result = new Vector<>(); synchronized (this.services) { for (Service s : this.services.getAll()) { result.add(toServiceStatus(s)); } } return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK); } @PutMapping("/service/ping") public ResponseEntity ping( // @RequestBody String name) { try { Service s = services.getService(name); s.ping(); return new ResponseEntity("OK", HttpStatus.OK); } catch (ServiceException e) { return new ResponseEntity(e.getMessage(), HttpStatus.NO_CONTENT); } } }