2 * ============LICENSE_START==============================================
3 * Copyright (c) 2019 AT&T Intellectual Property.
4 * =======================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License. You may obtain a
7 * copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 * ============LICENSE_END=================================================
19 package org.onap.optf.cmso.optimizer.clients.optimizer.models;
21 import com.google.common.base.CaseFormat;
23 import java.io.FileInputStream;
24 import java.io.InputStream;
25 import java.util.ArrayList;
26 import java.util.List;
27 import org.onap.observations.Observation;
28 import org.onap.optf.cmso.optimizer.common.LogMessages;
29 import org.yaml.snakeyaml.Yaml;
30 import org.yaml.snakeyaml.constructor.Constructor;
31 import org.yaml.snakeyaml.introspector.Property;
32 import org.yaml.snakeyaml.introspector.PropertyUtils;
34 public class OptimizerResponseUtility extends PropertyUtils {
35 public Results parseOptimizerResult(File resultsFile) {
36 Results results = null;
37 try (InputStream input = new FileInputStream(resultsFile)) {
38 Constructor constructor = new Constructor(OptimizerOutResults.class);
39 constructor.setPropertyUtils(this);
40 Yaml yaml = new Yaml(constructor);
41 OptimizerOutResults optimizerOut = yaml.load(input);
42 results = marshall(optimizerOut);
43 } catch (Exception e) {
44 Observation.report(LogMessages.UNEXPECTED_EXCEPTION, e, e.getMessage());
49 private Results marshall(OptimizerOutResults optimizerOut) {
50 Results results = new Results();
51 results.setElapsedMillis(optimizerOut.getElapsedMillis());
52 List<OptimizerSchedule> schedules = new ArrayList<>();
53 results.setSchedules(schedules);
54 for (OptimizerOutSchedule sch : optimizerOut.getResults()) {
55 schedules.add(marshall(sch));
60 private OptimizerSchedule marshall(OptimizerOutSchedule sch) {
61 OptimizerSchedule optimizerSchedule = new OptimizerSchedule();
62 optimizerSchedule.setNumScheduled(sch.getNumScheduled());
63 optimizerSchedule.setTotalCompletionTime(sch.getTotalCompletionTime());
64 String[] rows = sch.getElementSlotLoader().split("\n");
65 List<ElementSlot> slots = new ArrayList<>();
66 optimizerSchedule.setElementSlotLoader(slots);
67 for (String row : rows) {
68 String[] cols = row.split(",");
69 ElementSlot slot = new ElementSlot(cols);
72 return optimizerSchedule;
76 public Property getProperty(Class<? extends Object> type, String name) {
77 name = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, name);
78 return super.getProperty(type, name);