Merge "Continue work with PolicyControl"
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / controllers / PolicyController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20 package org.oransc.policyagent.controllers;
21
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;
36
37 @RestController
38 public class PolicyController {
39
40     private final ApplicationConfig appConfig;
41     private final PolicyTypes types;
42     private final Policies policies;
43
44     @Autowired
45     PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies) {
46         this.appConfig = config;
47         this.types = types;
48         this.policies = policies;
49     }
50
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) {
56
57         return new ResponseEntity<String>("policy" + typeName + instanceId, HttpStatus.OK);
58     }
59
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) {
67
68         System.out.println("*********************** " + jsonBody);
69
70         try {
71             Policy policy = ImmutablePolicy.builder() //
72                 .id(instanceId) //
73                 .json(jsonBody) //
74                 .type(types.getType(type)) //
75                 .ric(appConfig.getRic(ric)) //
76                 .ownerServiceName(service) //
77                 .build();
78             policies.put(instanceId, policy);
79             return new ResponseEntity<String>(HttpStatus.OK);
80         } catch (ServiceException e) {
81             System.out.println("*********************** " + e.getMessage());
82
83             return new ResponseEntity<String>(e.getMessage(), HttpStatus.NOT_FOUND);
84         }
85
86     }
87
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);
97     }
98
99 }