2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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=========================================================
21 package org.openecomp.policy.template.demo;
23 import java.util.UUID;
25 import org.openecomp.policy.controlloop.VirtualControlLoopEvent;
26 import org.openecomp.policy.controlloop.VirtualControlLoopNotification;
27 import org.openecomp.policy.template.demo.ControlLoopException;
28 import org.openecomp.policy.controlloop.ControlLoopNotificationType;
29 import org.openecomp.policy.controlloop.ControlLoopEventStatus;
31 public class EventManager {
35 public final String closedLoopControlName;
36 public final UUID requestID;
37 public final String target;
38 public String controlLoopResult;
40 private boolean isActivated = false;
41 private VirtualControlLoopEvent onset;
42 private VirtualControlLoopEvent abatement;
43 private Integer numOnsets = 0;
44 private Integer numAbatements = 0;
48 public EventManager(String closedLoopControlName, UUID requestID, String target) {
49 this.closedLoopControlName = closedLoopControlName;
50 this.requestID = requestID;
54 public Integer getNumOnsets() {
58 public void setNumOnsets(Integer numOnsets) {
59 this.numOnsets = numOnsets;
62 public Integer getNumAbatements() {
66 public void setNumAbatements(Integer numAbatements) {
67 this.numAbatements = numAbatements;
70 public boolean isActivated() {
74 public void setActivated(boolean isActivated) {
75 this.isActivated = isActivated;
78 public VirtualControlLoopEvent getOnsetEvent() {
82 public VirtualControlLoopEvent getAbatementEvent() {
83 return this.abatement;
86 public void setControlLoopResult(String result) {
87 this.controlLoopResult = result;
90 public VirtualControlLoopNotification activate(VirtualControlLoopEvent event) {
91 VirtualControlLoopNotification notification = new VirtualControlLoopNotification(event);
94 // This method should ONLY be called ONCE
96 if (this.isActivated) {
97 throw new ControlLoopException("ControlLoopEventManager has already been activated.");
100 // Syntax check the event
102 checkEventSyntax(event);
104 // At this point we are good to go with this event
109 notification.notification = ControlLoopNotificationType.ACTIVE;
111 // Set ourselves as active
113 this.isActivated = true;
114 } catch (ControlLoopException e) {
115 notification.notification = ControlLoopNotificationType.REJECTED;
116 notification.message = e.getMessage();
122 public static void checkEventSyntax(VirtualControlLoopEvent event) throws ControlLoopException {
123 if (event.closedLoopEventStatus == null ||
124 (event.closedLoopEventStatus != ControlLoopEventStatus.ONSET &&
125 event.closedLoopEventStatus != ControlLoopEventStatus.ABATED)) {
126 throw new ControlLoopException("Invalid value in closedLoopEventStatus");
128 if (event.closedLoopControlName == null || event.closedLoopControlName.length() < 1) {
129 throw new ControlLoopException("No control loop name");
131 if (event.requestID == null) {
132 throw new ControlLoopException("No request ID");
134 if (event.AAI == null) {
135 throw new ControlLoopException("AAI is null");
137 if (event.target == null || event.target.length() < 1) {
138 throw new ControlLoopException("No target field");
140 if (! event.target.equalsIgnoreCase("VM_NAME") &&
141 ! event.target.equalsIgnoreCase("VNF_NAME") &&
142 ! event.target.equalsIgnoreCase("vserver.vserver-name") &&
143 ! event.target.equalsIgnoreCase("generic-vnf.vnf-id") ) {
144 throw new ControlLoopException("target field invalid");
149 public enum NEW_EVENT_STATUS {
153 SUBSEQUENT_ABATEMENT,
158 public NEW_EVENT_STATUS onNewEvent(VirtualControlLoopEvent event) {
160 EventManager.checkEventSyntax(event);
161 if (event.closedLoopEventStatus == ControlLoopEventStatus.ONSET) {
163 // Check if this is our original ONSET
165 if (event.equals(this.onset)) {
169 return NEW_EVENT_STATUS.FIRST_ONSET;
172 // Log that we got an onset
175 return NEW_EVENT_STATUS.SUBSEQUENT_ONSET;
176 } else if (event.closedLoopEventStatus == ControlLoopEventStatus.ABATED) {
178 // Have we already got an abatement?
180 if (this.abatement == null) {
184 this.abatement = event;
186 // Keep track that we received another
188 this.numAbatements++;
192 return NEW_EVENT_STATUS.FIRST_ABATEMENT;
195 // Keep track that we received another
197 this.numAbatements++;
201 return NEW_EVENT_STATUS.SUBSEQUENT_ABATEMENT;
204 return NEW_EVENT_STATUS.SYNTAX_ERROR;
206 } catch (ControlLoopException e) {
207 return NEW_EVENT_STATUS.SYNTAX_ERROR;