import org.oransc.policyagent.configuration.ApplicationConfig;
import org.oransc.policyagent.repository.Policies;
import org.oransc.policyagent.repository.PolicyTypes;
+import org.oransc.policyagent.repository.Rics;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Beans {
+ private Rics rics = new Rics();
@Bean
- Policies getPolicies() {
+ public Policies getPolicies() {
return new Policies();
}
@Bean
- PolicyTypes getPolicyTypes() {
+ public PolicyTypes getPolicyTypes() {
return new PolicyTypes();
}
@Bean
- ApplicationConfig getApplicationConfig() {
+ public Rics getRics() {
+ return rics;
+ }
+ @Bean
+ public ApplicationConfig getApplicationConfig() {
return new ApplicationConfig();
}
package org.oransc.policyagent.controllers;
import com.google.gson.Gson;
+import org.oransc.policyagent.Beans;
import com.google.gson.GsonBuilder;
import java.util.Collection;
import org.oransc.policyagent.repository.Policies;
import org.oransc.policyagent.repository.Policy;
import org.oransc.policyagent.repository.PolicyTypes;
+import org.oransc.policyagent.repository.Ric;
+import org.oransc.policyagent.repository.Rics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@RestController
public class PolicyController {
+ private final Beans beans;
private final ApplicationConfig appConfig;
+ private final Rics rics;
private final PolicyTypes types;
private final Policies policies;
private static Gson gson = new GsonBuilder() //
.create(); //
@Autowired
- PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies) {
- this.appConfig = config;
- this.types = types;
- this.policies = policies;
+ PolicyController(Beans beans) {
+ this.beans = beans;
+ this.appConfig = beans.getApplicationConfig();
+ this.rics = beans.getRics();
+ this.types = beans.getPolicyTypes();
+ this.policies = beans.getPolicies();
}
@GetMapping("/policy")
@RequestBody String jsonBody) {
try {
+ Ric ricObj = rics.getRic(ric);
Policy policy = ImmutablePolicy.builder() //
.id(instanceId) //
.json(jsonBody) //
.type(types.getType(type)) //
- .ric(appConfig.getRic(ric)) //
+ .ric(ricObj) //
.ownerServiceName(service) //
.build();
policies.put(policy);
--- /dev/null
+/*-
+ * ========================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 java.util.Vector;
+import org.oransc.policyagent.configuration.ApplicationConfig;
+import org.oransc.policyagent.configuration.RicConfig;
+import org.oransc.policyagent.repository.Ric;
+import org.oransc.policyagent.repository.Rics;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+/**
+ * Loads information about RealTime-RICs at startup.
+ */
+@Service("startupService")
+public class StartupService {
+
+ private static final Logger logger = LoggerFactory.getLogger(StartupService.class);
+
+ @Autowired
+ ApplicationConfig applicationConfig;
+
+ @Autowired
+ private Rics rics;
+
+ StartupService(ApplicationConfig appConfig, Rics rics) {
+ this.applicationConfig = appConfig;
+ this.rics = rics;
+ }
+
+ public void startup() {
+ applicationConfig.initialize();
+ Vector<RicConfig> ricConfigs = applicationConfig.getRicConfigs();
+ for (RicConfig ricConfig : ricConfigs) {
+ logger.error("Ric: {}", ricConfig);
+ Ric ric = new Ric(ricConfig);
+ rics.put(ric);
+ }
+
+ }
+
+}
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
-
import org.oransc.policyagent.exceptions.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.immutables.gson.Gson;
import org.immutables.value.Value;
-import org.oransc.policyagent.configuration.RicConfig;
@Value.Immutable
@Gson.TypeAdapters
public String ownerServiceName();
- public RicConfig ric();
+ public Ric ric();
public PolicyType type();
}
--- /dev/null
+/*-
+ * ========================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.repository;
+
+import org.oransc.policyagent.configuration.RicConfig;
+import org.oransc.policyagent.repository.Ric.RicState;
+
+/**
+ * Represents the dynamic information about a NearRealtime-RIC.
+ */
+public class Ric {
+ private final RicConfig ricConfig;
+ private RicState state = RicState.NOT_INITIATED;
+
+ /**
+ * Creates the Ric. Initial state is {@link RicState.NOT_INITIATED}.
+ *
+ * @param ricConfig The {@link RicConfig} for this Ric.
+ */
+ public Ric(RicConfig ricConfig) {
+ this.ricConfig = ricConfig;
+ }
+
+ public String name() {
+ return ricConfig.name();
+ }
+
+ public RicState state() {
+ return state;
+ }
+
+ public void setState(RicState newState) {
+ state = newState;
+ }
+
+ /**
+ * Determines if the given node is managed by this Ric.
+ *
+ * @param nodeName the node name to check.
+ * @return true if the given node is managed by this Ric.
+ */
+ public boolean isManaging(String nodeName) {
+ return ricConfig.managedElementIds().contains(nodeName);
+ }
+
+ /**
+ * Adds the given node as managed by this Ric.
+ *
+ * @param nodeName the node to add.
+ */
+ public void addManagedNode(String nodeName) {
+ if (!ricConfig.managedElementIds().contains(nodeName)) {
+ ricConfig.managedElementIds().add(nodeName);
+ }
+ }
+
+ /**
+ * Removes the given node as managed by this Ric.
+ *
+ * @param nodeName the node to remove.
+ */
+ public void removeManagedNode(String nodeName) {
+ ricConfig.managedElementIds().remove(nodeName);
+ }
+
+ /**
+ * Represents the states possible for a Ric.
+ */
+ public static enum RicState {
+ /**
+ * The Ric has not been initiated yet.
+ */
+ NOT_INITIATED,
+ /**
+ * The Ric is working fine.
+ */
+ ACTIVE,
+ /**
+ * Something is wrong with the Ric.
+ */
+ FAULTY,
+ /**
+ * The node is unreachable at the moment.
+ */
+ UNREACHABLE
+ }
+}
--- /dev/null
+/*-
+ * ========================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.repository;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Dynamic representation of all Rics in the system.
+ */
+public class Rics {
+ Map<String, Ric> rics = new HashMap<>();
+
+ public void put(Ric ric) {
+ rics.put(ric.name(), ric);
+ }
+
+ public Collection<Ric> getRics() {
+ return rics.values();
+ }
+
+ public Ric getRic(String name) {
+ return rics.get(name);
+ }
+
+ public int size() {
+ return rics.size();
+ }
+}
import static org.junit.Assert.assertFalse;
import java.net.URL;
-
+import java.util.Vector;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.oransc.policyagent.configuration.ApplicationConfig;
+import org.oransc.policyagent.configuration.ImmutableRicConfig;
+import org.oransc.policyagent.configuration.RicConfig;
import org.oransc.policyagent.exceptions.ServiceException;
import org.oransc.policyagent.repository.ImmutablePolicy;
import org.oransc.policyagent.repository.ImmutablePolicyType;
import org.oransc.policyagent.repository.Policy;
import org.oransc.policyagent.repository.PolicyType;
import org.oransc.policyagent.repository.PolicyTypes;
+import org.oransc.policyagent.repository.Ric;
+import org.oransc.policyagent.repository.Rics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
public class ApplicationTest {
@Autowired
- private Policies policies;
-
- @Autowired
- private PolicyTypes policyTypes;
-
- @Autowired
- ApplicationConfig appConfig;
+ private Beans beans;
static class MockApplicationConfig extends ApplicationConfig {
+ @Override
public void initialize() {
URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
loadConfigurationFromFile(url.getFile());
@TestConfiguration
static class Beans {
@Bean
- Policies getPolicies() {
+ public Rics getRics() {
+ return new Rics();
+ }
+
+ @Bean
+ public Policies getPolicies() {
return new Policies();
}
@Bean
- PolicyTypes getPolicyTypes() {
+ public PolicyTypes getPolicyTypes() {
return new PolicyTypes();
}
@Bean
- ApplicationConfig getApplicationConfig() {
+ public ApplicationConfig getApplicationConfig() {
return new MockApplicationConfig();
}
}
String url = "http://localhost:" + port + "/policy?type=type1&instance=instance1&ric=ric1&service=service1";
String json = "{}";
addPolicyType("type1");
+ addRic(beans.getRics(), "ric1", url);
this.restTemplate.put(url, json);
- Policy policy = this.policies.get("instance1");
+
+ Policy policy = beans.getPolicies().get("instance1");
assertThat(policy).isNotNull();
assertThat(policy.id()).isEqualTo("instance1");
assertThat(policy.ownerServiceName()).isEqualTo("service1");
}
+ private void addRic(Rics rics, String ric, String url) {
+ Vector<String> nodeNames = new Vector<>(1);
+ nodeNames.add("node1");
+ RicConfig ricConfig = ImmutableRicConfig.builder() //
+ .name(ric) //
+ .baseUrl(url) //
+ .managedElementIds(nodeNames) //
+ .build();
+ Ric ricObj = new Ric(ricConfig);
+
+ rics.put(ricObj);
+ }
+
private PolicyType addPolicyType(String name) {
PolicyType type = ImmutablePolicyType.builder() //
.jsonSchema("") //
.name(name) //
.build();
- policyTypes.put(type);
+ beans.getPolicyTypes().put(type);
return type;
}
Policy p = ImmutablePolicy.builder().id(id) //
.json("{}") //
.ownerServiceName(service) //
- .ric(appConfig.getRic("ric1")) //
+ .ric(beans.getRics().getRic("ric1")) //
.type(addPolicyType(typeName)) //
.build();
- this.policies.put(p);
+ beans.getPolicies().put(p);
return p;
}
assertThat(rsp).isEqualTo(policy.json());
}
{
- this.policies.remove(policy);
+ beans.getPolicies().remove(policy);
ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class);
assertThat(rsp.getStatusCodeValue()).isEqualTo(HttpStatus.NO_CONTENT.value());
}
@Test
public void getPolicies() throws Exception {
String url = "http://localhost:" + port + "/policies";
+ addRic(beans.getRics(), "ric1", url);
addPolicy("id1", "type1", "service1");
addPolicy("id2", "type2", "service2");
--- /dev/null
+/*-
+ * ========================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 static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.Vector;
+import org.junit.Test;
+import org.oransc.policyagent.configuration.ApplicationConfig;
+import org.oransc.policyagent.configuration.ImmutableRicConfig;
+import org.oransc.policyagent.configuration.RicConfig;
+import org.oransc.policyagent.repository.Rics;
+
+public class StartupServiceTest {
+ ApplicationConfig appConfigMock;
+
+ @Test
+ public void startup_allOk() {
+ appConfigMock = mock(ApplicationConfig.class);
+ Vector<RicConfig> ricConfigs = new Vector<>(2);
+ Vector<String> firstNodes = new Vector<String>(1);
+ firstNodes.add("nodeA");
+ ricConfigs.add(ImmutableRicConfig.builder().name("first").managedElementIds(firstNodes).baseUrl("url").build());
+ when(appConfigMock.getRicConfigs()).thenReturn(ricConfigs);
+
+ Rics rics = new Rics();
+
+ StartupService serviceUnderTest = new StartupService(appConfigMock, rics);
+
+ serviceUnderTest.startup();
+
+ assertEquals("Correct nymber of Rics not added to Rics", 1, rics.size());
+ assertEquals("Not correct Ric added to Rics", "first", rics.getRic("first").name());
+ }
+}