aboutsummaryrefslogtreecommitdiffstats
path: root/cmso-optimizer/src/main/java/org/onap/optf/cmso/optimizer/clients/optimizer/ElementWindowMapping.java
blob: d2565f01232a21b3ecd864c8816a8e07a5412e1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 *  ============LICENSE_START==============================================
 *  Copyright (c) 2019 AT&T Intellectual Property.
 *  =======================================================================
 *  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.onap.optf.cmso.optimizer.clients.optimizer;

import com.google.ical.compat.jodatime.DateTimeIterator;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
import org.joda.time.DateTime;
import org.onap.optf.cmso.optimizer.availability.timewindows.RecurringWindows;
import org.onap.optf.cmso.optimizer.clients.optimizer.models.ElementSlot;
import org.onap.optf.cmso.optimizer.clients.optimizer.models.OptimizerSchedule;
import org.onap.optf.cmso.optimizer.clients.topology.models.TopologyElementInfo;
import org.onap.optf.cmso.optimizer.clients.topology.models.TopologyResponse;
import org.onap.optf.cmso.optimizer.service.rs.models.ChangeWindow;
import org.onap.optf.cmso.optimizer.service.rs.models.ElementInfo;
import org.onap.optf.cmso.optimizer.service.rs.models.OptimizerRequest;
import org.onap.optf.cmso.optimizer.service.rs.models.OptimizerScheduleInfo;
import org.onap.optf.cmso.optimizer.service.rs.models.ScheduledElement;
import org.onap.optf.cmso.optimizer.service.rs.models.ScheduledElement.ScheduleType;
import org.onap.optf.cmso.optimizer.service.rs.models.UnScheduledElement;
import org.onap.optf.cmso.optimizer.service.rs.models.UnScheduledElement.NotScheduledReason;

// This class ensures that the node indices nodes and the time slots are the
/**
 * The Class ElementWindowMapping.
 */
// same when processing the optimizer engine response as when initiating.
public class ElementWindowMapping {

    protected OptimizerRequest optimizerRequest;
    protected TopologyResponse topologyResponse;

    protected Map<String, TopologyElementInfo> nodeInfo = new TreeMap<>();
    private List<TopologyElementInfo> nodeArray = null;

    /**
     * Instantiates a new element window mapping.
     *
     * @param optimizerRequest the optimizer request
     * @param topologyResponse the topology response
     * @throws ParseException the parse exception
     */
    public ElementWindowMapping(OptimizerRequest optimizerRequest, TopologyResponse topologyResponse)
                    throws ParseException {
        this.optimizerRequest = optimizerRequest;
        this.topologyResponse = topologyResponse;
        initialize();

    }

    private void initialize() throws ParseException {
        List<TopologyElementInfo> elements = topologyResponse.getElements();
        for (TopologyElementInfo info : elements) {
            nodeInfo.put(info.getElementId(), info);
        }
    }

    protected DateTimeIterator getRecurringIterator() throws ParseException {
        // Only support 1 change window for now
        ChangeWindow window = optimizerRequest.getChangeWindows().get(0);
        Long duration = new Long(optimizerRequest.getNormalDuration());
        if (optimizerRequest.getAdditionalDuration() != null) {
            duration += optimizerRequest.getAdditionalDuration();
        }
        DateTimeIterator recur = RecurringWindows.getRecurringListForChangeWindow(window, duration);
        return recur;
    }

    /**
     * Initialize for process result.
     */
    public void initializeForProcessResult() {
        // we need nodeInfo to be an array to speed up the result processing.
        // but we need it sorted by elementId as when we created it....
        nodeArray = nodeInfo.values().stream().collect(Collectors.toList());
        nodeInfo.clear();

    }

    /**
     * Process result.
     *
     * @param result the result
     * @return the optimizer schedule info
     * @throws ParseException the parse exception
     */
    public OptimizerScheduleInfo processResult(OptimizerSchedule result) throws ParseException {
        // When considering the memory vs performance
        // 5 minute duration for a month long change window is 8928 slots
        // The assumption is that there were be fewer allocated slots
        // than potential slots.
        List<ElementSlot> elements = result.getElementSlotLoader();
        Map<Integer, List<ElementSlot>> mapSlotToElement =
                        elements.stream().collect(Collectors.groupingBy(ElementSlot::getSlot));
        DateTimeIterator iter = getRecurringIterator();
        // TODO - supporting only 1 change window at the moment.....
        Long endWindow = optimizerRequest.getChangeWindows().get(0).getEndTime().getTime();
        Integer slotIndex = 1;
        while (iter.hasNext()) {
            DateTime dateTime = iter.next();
            if (dateTime.isAfter(endWindow)) {
                break;
            }
            List<ElementSlot> list = mapSlotToElement.get(slotIndex);
            if (list != null) {
                list.stream().forEach(x -> x.setTime(dateTime.getMillis()));
            }
            slotIndex++;
        }
        //
        // All assigned ElementSlots now have corresponding UTC time
        //
        OptimizerScheduleInfo info = new OptimizerScheduleInfo();
        for (ElementSlot slot : elements) {
            updateInfo(slot, info);
        }
        return info;
    }

    private void updateInfo(ElementSlot slot, OptimizerScheduleInfo info) {
        TopologyElementInfo element = nodeArray.get(slot.getElementIndex() - 1);
        if (slot.getSlot() > 0) {
            ScheduledElement scheduled = new ScheduledElement();
            Integer durationInSeconds = optimizerRequest.getNormalDuration();
            if (optimizerRequest.getAdditionalDuration() != null) {
                durationInSeconds += optimizerRequest.getAdditionalDuration();
            }
            scheduled.setDurationSeconds(durationInSeconds.longValue());
            scheduled.setElementId(element.getElementId());
            scheduled.setStartTime(new Date(slot.getTime()));
            scheduled.setEndTime(new Date(slot.getTime() + (durationInSeconds * 1000)));
            scheduled.setScheduleType(ScheduleType.INDIVIDUAL);
            scheduled.setGroupId(getGroupId(scheduled.getElementId()));
            info.getScheduledElements().add(scheduled);
        } else {
            UnScheduledElement unscheduled = new UnScheduledElement();
            unscheduled.setElementId(element.getElementId());
            unscheduled.setGroupId("unknown");
            unscheduled.getNotScheduledReaons().add(NotScheduledReason.Other);
            unscheduled.getNotScheduledMessages().add("Unknown");
            info.getUnScheduledElements().add(unscheduled);
        }
    }

    private String getGroupId(String elementId) {
        ElementInfo info = getElementInfo(elementId);
        if (info != null) {
            return info.getGroupId();
        }
        return "";
    }

    private ElementInfo getElementInfo(String elementId) {
        List<ElementInfo> elements = optimizerRequest.getElements();
        ElementInfo info = elements.stream().filter(x -> x.getElementId().equals(elementId)).findAny().orElse(null);
        return info;
    }

}