1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2022 Huawei Canada Limited.
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
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 *******************************************************************************/
21 package org.onap.slice.analysis.ms.service.ccvpn;
23 import lombok.NonNull;
24 import org.onap.slice.analysis.ms.aai.AaiService;
26 import org.onap.slice.analysis.ms.models.Configuration;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.stereotype.Component;
32 import javax.annotation.PostConstruct;
33 import javax.annotation.PreDestroy;
35 import java.util.concurrent.BlockingQueue;
36 import java.util.concurrent.ExecutorService;
37 import java.util.concurrent.Executors;
38 import java.util.concurrent.Future;
39 import java.util.concurrent.LinkedBlockingQueue;
40 import java.util.concurrent.ScheduledExecutorService;
41 import java.util.concurrent.TimeUnit;
44 * This class implements the CCVPN PM Closed-loop logical function.
45 * A simple actor model design is implemented here.
48 public class BandwidthEvaluator {
49 private static Logger log = LoggerFactory.getLogger(BandwidthEvaluator.class);
50 private Configuration configuration;
53 AaiService aaiService;
56 CCVPNPmDatastore ccvpnPmDatastore;
59 StrategyFactory strategyFactory;
61 private Loop evaluationEventLoop;
62 private Loop aaiEventLoop;
64 private static final Event KILL_PILL = new SimpleEvent(null, 0);
65 private static final int DEFAULT_EVAL_INTERVAL = 5;
66 private static final String DEFAULT_STRATEGY_NAME = "FixedUpperBoundStrategy";
69 * Interval of each round of evaluation, defined in config_all.json
71 private static int evaluationInterval;
74 * Bandwidth Evaluation and adjustment strategy.
76 private static String strategyName;
78 private final ScheduledExecutorService executorPool = Executors.newScheduledThreadPool(1);
81 * Initialize and start the bandwidth evaluator process, schedule a periodic service bandwidth usage check
86 strategyName = (strategyName != null)? strategyName : DEFAULT_STRATEGY_NAME;
87 evaluationInterval = (evaluationInterval == 0)? DEFAULT_EVAL_INTERVAL : evaluationInterval;
88 EvaluationStrategy strategy = strategyFactory.getStrategy(strategyName);
93 evaluationEventLoop = new Loop("EvaluationLoop"){
95 public void process(Event event) {
96 strategy.execute(event);
101 * AAI data consumer loop
103 aaiEventLoop = new Loop("AAIEventLoop"){
105 public void process(Event event) {
106 if (event.type() == SimpleEvent.Type.AAI_BW_REQ){
107 log.debug("=== Processing new AAI network policy query at: {} ===", event.time());
108 String serviceId = (String) event.subject();
109 Map<String, Integer> maxBandwidthData = aaiService.fetchMaxBandwidthOfService(serviceId);
110 if (maxBandwidthData.get("maxBandwidth") != null){
111 log.info("Successfully retrieved bandwidth info from AAI; service: {}, bandwidth: {}",
112 serviceId, maxBandwidthData.get("maxBandwidth"));
113 int bwValue = maxBandwidthData.get("maxBandwidth").intValue();
114 if (ccvpnPmDatastore.getProvBwOfSvc(serviceId) == 0){
115 ccvpnPmDatastore.updateProvBw(serviceId, bwValue, true);
116 } else if (ccvpnPmDatastore.getProvBwOfSvc(serviceId) != bwValue) {
117 log.info("Service modification complete; serviceId: {} with new bandwidth: {}", serviceId, bwValue);
118 ccvpnPmDatastore.updateProvBw(serviceId, bwValue, true);
119 ccvpnPmDatastore.updateSvcState(serviceId, ServiceState.RUNNING);
122 log.debug("=== Processing AAI network policy query complete ===");
126 scheduleEvaluation();
130 * Stop the bandwidth evaluator process including two actors and periodic usage check
134 stopScheduleEvaluation();
136 evaluationEventLoop.stop();
140 * Start to schedule periodic usage check at fixed rate
142 private void scheduleEvaluation(){
143 executorPool.scheduleAtFixedRate(new Runnable() {
146 post(new SimpleEvent(SimpleEvent.Type.PERIODIC_CHECK, 1));
148 }, 0, (evaluationInterval == 0? DEFAULT_EVAL_INTERVAL : evaluationInterval), TimeUnit.SECONDS);
152 * Stop periodic bandwidth usage check
154 private void stopScheduleEvaluation(){
155 executorPool.shutdownNow();
159 * Post/broadcast event between Loops
160 * @param event event object
162 public void post(@NonNull Event event){
163 log.debug("A new event triggered, type: {}, subject: {}, at time: {}",
164 event.type(), event.subject(), event.time());
165 if (event.type() == SimpleEvent.Type.AAI_BW_REQ) {
166 aaiEventLoop.add(event);
167 } else if (event.type() == SimpleEvent.Type.PERIODIC_CHECK) {
168 evaluationEventLoop.add(event);
169 } else if (event.type() == SimpleEvent.Type.ONDEMAND_CHECK) {
170 evaluationEventLoop.add(event);
174 private void loadConfig() {
175 configuration = Configuration.getInstance();
176 evaluationInterval = configuration.getCcvpnEvalInterval();
177 strategyName = configuration.getCcvpnEvalStrategy();
181 * Inner loop implementation. Each loop acts like an actor.
183 private abstract class Loop implements Runnable {
184 private final String name;
185 private volatile boolean running;
186 private final BlockingQueue<Event> eventsQueue;
187 private final ExecutorService executor;
188 private volatile Future<?> dispatchFuture;
191 * Constructor that accepts a loop name
192 * @param name name of this loop
196 executor = Executors.newSingleThreadExecutor();
197 eventsQueue = new LinkedBlockingQueue<>();
198 dispatchFuture = executor.submit(this);
202 * Add new event to this loop
206 public boolean add(Event evt) {
207 return eventsQueue.add(evt);
211 * Running loop that process event accordingly
216 log.info("BandwidthEvaluator -- {} initiated", this.name);
219 Event event = eventsQueue.take();
220 if (event == KILL_PILL){
224 } catch (InterruptedException e){
225 log.warn("Process loop interrupted");
226 } catch (Exception | Error e){
227 log.warn("Process loop hit an error {}", e.getMessage());
233 * Operation defined by subclass for different event processing
234 * @param event incoming event
236 abstract public void process(Event event);