c54ea607a7056f5c9940d45e436b5a95e1362d9b
[policy/drools-applications.git] / template.demo / src / main / java / org / openecomp / policy / template / demo / EventManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * demo
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.policy.template.demo;
22
23 import java.util.UUID;
24
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;
30
31 public class EventManager {
32         /*
33          * 
34          */
35         public final String closedLoopControlName;
36         public final UUID requestID;
37         public final String target;
38         public String controlLoopResult;
39         
40         private boolean isActivated = false;
41         private VirtualControlLoopEvent onset;
42         private VirtualControlLoopEvent abatement;
43         private Integer numOnsets = 0;
44         private Integer numAbatements = 0;
45         
46         
47         
48         public EventManager(String closedLoopControlName, UUID requestID, String target) {
49                 this.closedLoopControlName = closedLoopControlName;
50                 this.requestID = requestID;
51                 this.target = target;
52         }
53         
54         public Integer getNumOnsets() {
55                 return numOnsets;
56         }
57         
58         public void setNumOnsets(Integer numOnsets) {
59                 this.numOnsets = numOnsets;
60         }
61         
62         public Integer getNumAbatements() {
63                 return numAbatements;
64         }
65         
66         public void setNumAbatements(Integer numAbatements) {
67                 this.numAbatements = numAbatements;
68         }
69         
70         public boolean isActivated() {
71                 return isActivated;
72         }
73         
74         public void setActivated(boolean isActivated) {
75                 this.isActivated = isActivated;
76         }
77         
78         public VirtualControlLoopEvent  getOnsetEvent() {
79                 return this.onset;
80         }
81         
82         public VirtualControlLoopEvent getAbatementEvent() {
83                 return this.abatement;
84         }
85         
86         public void setControlLoopResult(String result) {
87                 this.controlLoopResult = result;                
88         }
89         
90         public VirtualControlLoopNotification activate(VirtualControlLoopEvent event) {
91                 VirtualControlLoopNotification notification = new VirtualControlLoopNotification(event);
92                 try {
93                         //
94                         // This method should ONLY be called ONCE
95                         //
96                         if (this.isActivated) {
97                                 throw new ControlLoopException("ControlLoopEventManager has already been activated.");
98                         }
99                         //
100                         // Syntax check the event
101                         //
102                         checkEventSyntax(event);
103                         //
104                         // At this point we are good to go with this event
105                         //
106                         this.onset = event;
107                         this.numOnsets = 1;     
108                         //
109                         notification.notification = ControlLoopNotificationType.ACTIVE;
110                         //
111                         // Set ourselves as active
112                         //
113                         this.isActivated = true;
114                 } catch (ControlLoopException e) {
115                         notification.notification = ControlLoopNotificationType.REJECTED;
116                         notification.message = e.getMessage();
117                 }
118                 return notification;
119                 
120         }
121         
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");
127                 }
128                 if (event.closedLoopControlName == null || event.closedLoopControlName.length() < 1) {
129                         throw new ControlLoopException("No control loop name");
130                 }
131                 if (event.requestID == null) {
132                         throw new ControlLoopException("No request ID");
133                 }
134                 if (event.AAI == null) {
135                         throw new ControlLoopException("AAI is null");
136                 }
137                 if (event.target == null || event.target.length() < 1) {
138                         throw new ControlLoopException("No target field");
139                 } else {
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");
145                         }
146                 }
147         }
148         
149         public enum NEW_EVENT_STATUS {
150                 FIRST_ONSET,
151                 SUBSEQUENT_ONSET,
152                 FIRST_ABATEMENT,
153                 SUBSEQUENT_ABATEMENT,
154                 SYNTAX_ERROR
155                 ;
156         }
157         
158         public NEW_EVENT_STATUS onNewEvent(VirtualControlLoopEvent event) {
159                 try {
160                         EventManager.checkEventSyntax(event);
161                         if (event.closedLoopEventStatus == ControlLoopEventStatus.ONSET) {
162                                 //
163                                 // Check if this is our original ONSET
164                                 //
165                                 if (event.equals(this.onset)) {
166                                         //
167                                         // DO NOT retract it
168                                         //
169                                         return NEW_EVENT_STATUS.FIRST_ONSET;
170                                 }
171                                 //
172                                 // Log that we got an onset
173                                 //
174                                 this.numOnsets++;
175                                 return NEW_EVENT_STATUS.SUBSEQUENT_ONSET;
176                         } else if (event.closedLoopEventStatus == ControlLoopEventStatus.ABATED) {
177                                 //
178                                 // Have we already got an abatement?
179                                 //
180                                 if (this.abatement == null) {
181                                         //
182                                         // Save this
183                                         //
184                                         this.abatement = event;
185                                         //
186                                         // Keep track that we received another
187                                         //
188                                         this.numAbatements++;
189                                         //
190                                         //
191                                         //
192                                         return NEW_EVENT_STATUS.FIRST_ABATEMENT;
193                                 } else {
194                                         //
195                                         // Keep track that we received another
196                                         //
197                                         this.numAbatements++;
198                                         //
199                                         //
200                                         //
201                                         return NEW_EVENT_STATUS.SUBSEQUENT_ABATEMENT;
202                                 }
203                         } else {
204                                 return NEW_EVENT_STATUS.SYNTAX_ERROR;
205                         }
206                 } catch (ControlLoopException e) {
207                         return NEW_EVENT_STATUS.SYNTAX_ERROR;
208                 }
209         }
210 }