0345a48171f937601f759e0f3114cb3b5ca6858b
[nemo.git] / nemo-impl / src / main / java / org / opendaylight / nemo / intent / condition / ConditionMonitor.java
1 /*
2  * Copyright (c) 2015 Huawei, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.nemo.intent.condition;
10
11 import org.opendaylight.nemo.intent.IntentResolver;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.UserId;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.operation.rev151010.condition.instance.ConditionSegment;
14
15 import java.util.List;
16 import java.util.TimerTask;
17
18 /**
19  * Created by hj on 11/19/15.
20  * TimerTask for one condition.
21  */
22 public class ConditionMonitor extends TimerTask {
23     private List<ConditionSegment> conditionSegments;
24     //Handle operation when condition state changed
25     private IntentResolver intentResolver;
26     private boolean preState;
27     private UserId userId;
28
29     public ConditionMonitor(IntentResolver intentResolver, UserId userId, List<ConditionSegment> conditionSegments) {
30         this.conditionSegments = conditionSegments;
31         this.intentResolver = intentResolver;
32         this.userId = userId;
33         preState = ConditionDeterminer.isConditionMet(conditionSegments);
34     }
35
36     @Override
37     public void run() {
38         boolean currentState = ConditionDeterminer.isConditionMet(conditionSegments);
39         if (currentState != preState) {
40             try {
41                 intentResolver.resolveIntent(userId);
42             } catch (Exception e) {
43                 e.printStackTrace();
44             }
45             preState = currentState;
46         }
47     }
48 }