Sho SHIMIZU | 5cb438e | 2015-02-04 13:46:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.impl.phase; |
Sho SHIMIZU | 5cb438e | 2015-02-04 13:46:00 -0800 | [diff] [blame] | 17 | |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 18 | import org.onosproject.net.intent.Intent; |
Brian O'Connor | 0e271dc | 2015-02-04 18:20:25 -0800 | [diff] [blame] | 19 | import org.onosproject.net.intent.IntentData; |
Sho SHIMIZU | 5cb438e | 2015-02-04 13:46:00 -0800 | [diff] [blame] | 20 | import org.onosproject.net.intent.IntentException; |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 21 | import org.onosproject.net.intent.impl.IntentProcessor; |
Sho SHIMIZU | 5cb438e | 2015-02-04 13:46:00 -0800 | [diff] [blame] | 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; |
| 24 | |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 25 | import java.util.List; |
Sho SHIMIZU | 5cb438e | 2015-02-04 13:46:00 -0800 | [diff] [blame] | 26 | import java.util.Optional; |
| 27 | |
| 28 | import static com.google.common.base.Preconditions.checkNotNull; |
| 29 | |
Sho SHIMIZU | d3dcaa1 | 2015-02-13 13:48:51 -0800 | [diff] [blame] | 30 | /** |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 31 | * Represents a phase where an intent is being compiled or recompiled. |
Sho SHIMIZU | d3dcaa1 | 2015-02-13 13:48:51 -0800 | [diff] [blame] | 32 | */ |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 33 | class Compiling implements IntentProcessPhase { |
Sho SHIMIZU | 5cb438e | 2015-02-04 13:46:00 -0800 | [diff] [blame] | 34 | |
| 35 | private static final Logger log = LoggerFactory.getLogger(Compiling.class); |
| 36 | |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 37 | private final IntentProcessor processor; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 38 | private final IntentData data; |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 39 | private final Optional<IntentData> stored; |
Sho SHIMIZU | 5cb438e | 2015-02-04 13:46:00 -0800 | [diff] [blame] | 40 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 41 | /** |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 42 | * Creates a intent recompiling phase. |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 43 | * |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 44 | * @param processor intent processor that does work for recompiling |
| 45 | * @param data intent data containing an intent to be recompiled |
| 46 | * @param stored intent data stored in the store |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 47 | */ |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 48 | Compiling(IntentProcessor processor, IntentData data, Optional<IntentData> stored) { |
Sho SHIMIZU | b0a47d4 | 2015-02-19 13:26:30 -0800 | [diff] [blame] | 49 | this.processor = checkNotNull(processor); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 50 | this.data = checkNotNull(data); |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 51 | this.stored = checkNotNull(stored); |
Sho SHIMIZU | 5cb438e | 2015-02-04 13:46:00 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Override |
Sho SHIMIZU | 36a8a6e | 2015-02-13 15:38:45 -0800 | [diff] [blame] | 55 | public Optional<IntentProcessPhase> execute() { |
Sho SHIMIZU | 5cb438e | 2015-02-04 13:46:00 -0800 | [diff] [blame] | 56 | try { |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 57 | List<Intent> compiled = processor.compile(data.intent(), |
| 58 | //TODO consider passing an optional here in the future |
| 59 | stored.isPresent() ? stored.get().installables() : null); |
| 60 | data.setInstallables(compiled); |
| 61 | return Optional.of(new Installing(processor, data, stored)); |
Sho SHIMIZU | 5cb438e | 2015-02-04 13:46:00 -0800 | [diff] [blame] | 62 | } catch (IntentException e) { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 63 | log.debug("Unable to compile intent {} due to: {}", data.intent(), e); |
Brian O'Connor | f0c5a05 | 2015-04-27 00:34:53 -0700 | [diff] [blame] | 64 | if (stored.isPresent() && !stored.get().installables().isEmpty()) { |
| 65 | // removing orphaned flows and deallocating resources |
| 66 | data.setInstallables(stored.get().installables()); |
| 67 | return Optional.of(new Withdrawing(processor, data)); |
| 68 | } else { |
| 69 | return Optional.of(new Failed(data)); |
| 70 | } |
Sho SHIMIZU | 5cb438e | 2015-02-04 13:46:00 -0800 | [diff] [blame] | 71 | } |
| 72 | } |
Sho SHIMIZU | 5cb438e | 2015-02-04 13:46:00 -0800 | [diff] [blame] | 73 | } |