2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
20 package org.oransc.policyagent.controllers;
22 import org.oransc.policyagent.configuration.ApplicationConfig;
23 import org.oransc.policyagent.exceptions.ServiceException;
24 import org.oransc.policyagent.repository.ImmutablePolicy;
25 import org.oransc.policyagent.repository.Policies;
26 import org.oransc.policyagent.repository.Policy;
27 import org.oransc.policyagent.repository.PolicyTypes;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.http.HttpStatus;
30 import org.springframework.http.ResponseEntity;
31 import org.springframework.web.bind.annotation.GetMapping;
32 import org.springframework.web.bind.annotation.PutMapping;
33 import org.springframework.web.bind.annotation.RequestBody;
34 import org.springframework.web.bind.annotation.RequestParam;
35 import org.springframework.web.bind.annotation.RestController;
38 public class PolicyController {
40 private final ApplicationConfig appConfig;
41 private final PolicyTypes types;
42 private final Policies policies;
45 PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies) {
46 this.appConfig = config;
48 this.policies = policies;
51 // http://localhost:8080/policy?type=type3&instance=xxx
52 @GetMapping("/policy")
53 public ResponseEntity<String> getPolicy( //
54 @RequestParam(name = "type", required = false, defaultValue = "type1") String typeName, //
55 @RequestParam(name = "instance", required = false, defaultValue = "new") String instanceId) {
57 return new ResponseEntity<String>("policy" + typeName + instanceId, HttpStatus.OK);
60 @PutMapping(path = "/policy")
61 public ResponseEntity<String> putPolicy( //
62 @RequestParam(name = "type", required = true) String type, //
63 @RequestParam(name = "instance", required = true) String instanceId, //
64 @RequestParam(name = "ric", required = true) String ric, //
65 @RequestParam(name = "service", required = true) String service, //
66 @RequestBody String jsonBody) {
68 System.out.println("*********************** " + jsonBody);
71 Policy policy = ImmutablePolicy.builder() //
74 .type(types.getType(type)) //
75 .ric(appConfig.getRic(ric)) //
76 .ownerServiceName(service) //
78 policies.put(instanceId, policy);
79 return new ResponseEntity<String>(HttpStatus.OK);
80 } catch (ServiceException e) {
81 System.out.println("*********************** " + e.getMessage());
83 return new ResponseEntity<String>(e.getMessage(), HttpStatus.NOT_FOUND);
88 @PutMapping(path = "/policyX")
89 public ResponseEntity<String> putPolicyX( //
90 // @RequestParam(name = "type", required = false) String type, //
91 // @RequestParam(name = "instance", required = true) String instance, //
92 // @RequestParam(name = "ric", required = true) String ric, //
93 @RequestBody String jsonBody) {
94 System.out.println("*********************** " + jsonBody);
95 // System.out.println("policy" + type + instance);
96 return new ResponseEntity<String>(HttpStatus.OK);