Merge "Renamed things to fit with namechange of RicSynchronizationTask"
authorJohn Keeney <John.Keeney@est.tech>
Fri, 20 Mar 2020 08:46:52 +0000 (08:46 +0000)
committerGerrit Code Review <gerrit@o-ran-sc.org>
Fri, 20 Mar 2020 08:46:52 +0000 (08:46 +0000)
1  2 
policy-agent/docs/api.yaml
policy-agent/src/main/java/org/oransc/policyagent/clients/A1ClientFactory.java
policy-agent/src/main/java/org/oransc/policyagent/controllers/PolicyController.java
policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java

@@@ -484,46 -484,6 +484,46 @@@ paths
            schema:
              type: string
        deprecated: false
 +  /policy_ids:
 +    get:
 +      tags:
 +        - A1 Policy Management
 +      summary: 'Query policies, only IDs returned'
 +      operationId: getPolicyIdsUsingGET
 +      produces:
 +        - '*/*'
 +      parameters:
 +        - name: ric
 +          in: query
 +          description: ric
 +          required: false
 +          type: string
 +        - name: service
 +          in: query
 +          description: service
 +          required: false
 +          type: string
 +        - name: type
 +          in: query
 +          description: type
 +          required: false
 +          type: string
 +      responses:
 +        '200':
 +          description: Policy ids
 +          schema:
 +            type: array
 +            items:
 +              type: string
 +        '401':
 +          description: Unauthorized
 +        '403':
 +          description: Forbidden
 +        '404':
 +          description: RIC or type not found
 +          schema:
 +            type: string
 +      deprecated: false
    /policy_schema:
      get:
        tags:
@@@ -910,7 -870,7 +910,7 @@@ definitions
      properties:
        callbackUrl:
          type: string
-         description: callback for notifying of RIC recovery
+         description: callback for notifying of RIC synchronization
        keepAliveIntervalSeconds:
          type: integer
          format: int64
      properties:
        callbackUrl:
          type: string
-         description: callback for notifying of RIC recovery
+         description: callback for notifying of RIC synchronization
        keepAliveIntervalSeconds:
          type: integer
          format: int64
          format: int64
          description: time since last invocation by the service
      title: ServiceStatus
@@@ -66,8 -66,10 +66,8 @@@ public class A1ClientFactory 
      }
  
      A1Client createClient(Ric ric, A1ProtocolType version) {
 -        if (version == A1ProtocolType.STD_V1) {
 +        if (version == A1ProtocolType.STD_V1_1) {
              return new StdA1ClientVersion1(ric.getConfig());
 -        } else if (version == A1ProtocolType.STD_V1_1) {
 -            return new StdA1ClientVersion2(ric.getConfig());
          } else if (version == A1ProtocolType.OSC_V1) {
              return new OscA1Client(ric.getConfig());
          } else if (version == A1ProtocolType.SDNC_OSC) {
  
      private Mono<A1Client.A1ProtocolType> getProtocolVersion(Ric ric) {
          if (ric.getProtocolVersion() == A1ProtocolType.UNKNOWN) {
 -            return fetchVersion(createClient(ric, A1ProtocolType.STD_V1)) //
 -                .onErrorResume(notUsed -> fetchVersion(createClient(ric, A1ProtocolType.STD_V1_1))) //
 +            return fetchVersion(createClient(ric, A1ProtocolType.STD_V1_1)) //
                  .onErrorResume(notUsed -> fetchVersion(createClient(ric, A1ProtocolType.OSC_V1))) //
                  .onErrorResume(notUsed -> fetchVersion(createClient(ric, A1ProtocolType.SDNC_OSC))) //
                  .onErrorResume(notUsed -> fetchVersion(createClient(ric, A1ProtocolType.SDNC_ONAP))) //
                  .doOnNext(ric::setProtocolVersion)
-                 .doOnNext(version -> logger.debug("Recover ric: {}, protocol version:{}", ric.name(), version)) //
+                 .doOnNext(version -> logger.debug("Established protocol version:{} for Ric: {}", version, ric.name())) //
                  .doOnError(notUsed -> logger.warn("Could not get protocol version from RIC: {}", ric.name())); //
          } else {
              return Mono.just(ric.getProtocolVersion());
@@@ -182,7 -182,7 +182,7 @@@ public class PolicyController 
              policy = policies.getPolicy(id);
              keepServiceAlive(policy.ownerServiceName());
              if (policy.ric().getState() != Ric.RicState.IDLE) {
-                 return Mono.just(new ResponseEntity<>("Busy, recovering", HttpStatus.LOCKED));
+                 return Mono.just(new ResponseEntity<>("Busy, synchronizing", HttpStatus.LOCKED));
              }
              Ric ric = policy.ric();
              return ric.getLock().lock(LockType.SHARED) // //
          }
  
          return ric == null || type == null ? Mono.just(new ResponseEntity<>(HttpStatus.NOT_FOUND))
-             : Mono.just(new ResponseEntity<>(HttpStatus.LOCKED)); // Recovering
+             : Mono.just(new ResponseEntity<>(HttpStatus.LOCKED)); // Synchronizing
      }
  
      @SuppressWarnings({"unchecked"})
              return new ResponseEntity<>("RIC not found", HttpStatus.NOT_FOUND);
          }
          synchronized (policies) {
 -            Collection<Policy> result = null;
 -
 -            if (type != null) {
 -                result = policies.getForType(type);
 -                result = filter(result, null, ric, service);
 -            } else if (service != null) {
 -                result = policies.getForService(service);
 -                result = filter(result, type, ric, null);
 -            } else if (ric != null) {
 -                result = filter(policies.getForRic(ric), type, null, service);
 -            } else {
 -                result = policies.getAll();
 -            }
 +            String filteredPolicies = policiesToJson(filter(type, ric, service));
 +            return new ResponseEntity<>(filteredPolicies, HttpStatus.OK);
 +        }
 +    }
  
 -            String policiesJson;
 -            try {
 -                policiesJson = policiesToJson(result);
 -            } catch (ServiceException e) {
 -                return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
 -            }
 -            return new ResponseEntity<>(policiesJson, HttpStatus.OK);
 +    @GetMapping("/policy_ids")
 +    @ApiOperation(value = "Query policies, only IDs returned")
 +    @ApiResponses(
 +        value = {@ApiResponse(code = 200, message = "Policy ids", response = String.class, responseContainer = "List"),
 +            @ApiResponse(code = 404, message = "RIC or type not found", response = String.class)})
 +    public ResponseEntity<String> getPolicyIds( //
 +        @RequestParam(name = "type", required = false) String type, //
 +        @RequestParam(name = "ric", required = false) String ric, //
 +        @RequestParam(name = "service", required = false) String service) //
 +    {
 +        if ((type != null && this.policyTypes.get(type) == null)) {
 +            return new ResponseEntity<>("Policy type not found", HttpStatus.NOT_FOUND);
 +        }
 +        if ((ric != null && this.rics.get(ric) == null)) {
 +            return new ResponseEntity<>("RIC not found", HttpStatus.NOT_FOUND);
 +        }
 +        synchronized (policies) {
 +            String policyIdsJson = toPolicyIdsJson(filter(type, ric, service));
 +            return new ResponseEntity<>(policyIdsJson, HttpStatus.OK);
          }
      }
  
          return filtered;
      }
  
 -    private String policiesToJson(Collection<Policy> policies) throws ServiceException {
 +    private Collection<Policy> filter(String type, String ric, String service) {
 +        synchronized (policies) {
 +            if (type != null) {
 +                return filter(policies.getForType(type), null, ric, service);
 +            } else if (service != null) {
 +                return filter(policies.getForService(service), type, ric, null);
 +            } else if (ric != null) {
 +                return filter(policies.getForRic(ric), type, null, service);
 +            } else {
 +                return policies.getAll();
 +            }
 +        }
 +    }
 +
 +    private String policiesToJson(Collection<Policy> policies) {
          List<PolicyInfo> v = new ArrayList<>(policies.size());
          for (Policy p : policies) {
              PolicyInfo policyInfo = new PolicyInfo();
              policyInfo.service = p.ownerServiceName();
              policyInfo.lastModified = p.lastModified();
              if (!policyInfo.validate()) {
 -                throw new ServiceException("BUG, all fields must be set");
 +                throw new NullPointerException("BUG, all fields must be set");
              }
              v.add(policyInfo);
          }
          return gson.toJson(v);
      }
  
 +    private String toPolicyIdsJson(Collection<Policy> policies) {
 +        List<String> v = new ArrayList<>(policies.size());
 +        for (Policy p : policies) {
 +            v.add(p.id());
 +        }
 +        return gson.toJson(v);
 +    }
 +
      private String getTimeStampUtc() {
          return java.time.Instant.now().toString();
      }
@@@ -22,7 -22,6 +22,7 @@@ package org.oransc.policyagent
  
  import static org.assertj.core.api.Assertions.assertThat;
  import static org.awaitility.Awaitility.await;
 +import static org.junit.jupiter.api.Assertions.assertEquals;
  import static org.junit.jupiter.api.Assertions.assertTrue;
  import static org.mockito.ArgumentMatchers.any;
  import static org.mockito.Mockito.doReturn;
@@@ -87,7 -86,6 +87,7 @@@ import org.springframework.web.reactive
  
  import reactor.core.publisher.Mono;
  import reactor.test.StepVerifier;
 +import reactor.util.annotation.Nullable;
  
  @ExtendWith(SpringExtension.class)
  @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@@@ -214,7 -212,7 +214,7 @@@ public class ApplicationTest 
      }
  
      @Test
-     public void testRecovery() throws Exception {
+     public void testSynchronization() throws Exception {
          addRic("ric").setState(Ric.RicState.UNDEFINED);
          String ricName = "ric";
          Policy policy2 = addPolicy("policyId2", "typeName", "service", ricName);
          testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
      }
  
 +    @Test
 +    public void testGetPolicyIdsFilter() throws Exception {
 +        addPolicy("id1", "type1", "service1", "ric1");
 +        addPolicy("id2", "type1", "service2", "ric1");
 +        addPolicy("id3", "type2", "service1", "ric1");
 +
 +        String url = "/policy_ids?type=type1";
 +        String rsp = restClient().get(url).block();
 +        logger.info(rsp);
 +        assertThat(rsp).contains("id1");
 +        assertThat(rsp).contains("id2");
 +        assertThat(rsp.contains("id3")).isFalse();
 +
 +        url = "/policy_ids?type=type1&service=service1&ric=ric1";
 +        rsp = restClient().get(url).block();
 +        assertThat(rsp).isEqualTo("[\"id1\"]");
 +
 +        // Test get policy ids for non existing type
 +        url = "/policy_ids?type=type1XXX";
 +        testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
 +
 +        // Test get policy ids for non existing RIC
 +        url = "/policy_ids?ric=XXX";
 +        testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
 +    }
 +
      @Test
      public void testPutAndGetService() throws Exception {
          // PUT
 -        putService("name", 0);
 +        putService("name", 0, HttpStatus.CREATED);
 +        putService("name", 0, HttpStatus.OK);
  
          // GET one service
          String url = "/services?name=name";
  
      @Test
      public void testServiceSupervision() throws Exception {
 -        putService("service1", 1);
 +        putService("service1", 1, HttpStatus.CREATED);
          addPolicyType("type1", "ric1");
  
          String url = putPolicyUrl("service1", "ric1", "type1", "instance1");
      }
  
      private void putService(String name) {
 -        putService(name, 0);
 +        putService(name, 0, null);
      }
  
 -    private void putService(String name, long keepAliveIntervalSeconds) {
 +    private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
          String url = "/service";
          String body = createServiceJson(name, keepAliveIntervalSeconds);
 -        restClient().put(url, body).block();
 +        ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
 +        if (expectedStatus != null) {
 +            assertEquals(expectedStatus, resp.getStatusCode(), "");
 +        }
      }
  
      private String baseUrl() {